Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Calculating "similarity"

by parv (Parson)
on Mar 02, 2003 at 04:50 UTC ( [id://239821]=note: print w/replies, xml ) Need Help??


in reply to Calculating "similarity"

Could you please clarify...

  • what are the differences/similarities among @words & @basewords and %stopwords & %filterwords as each pair seem to have duplicates?
  • why is the $total set to twice the size of @D1 (near the end of OP)?

...only if you had commented the code... :(

Replies are listed 'Best First'.
Re: Re: Calculating "similarity"
by Anonymous Monk on Mar 02, 2003 at 12:37 UTC
    Basewords are the words shown in the file 1 if i am comparing five files.. and words are the rest of the words in the other 4 files. stopwords are the words shown in a stop list. indeed, i think i did it wrong and thats why i can't finish it.. can you be able to solve it?? i need to find the similarity of a file comparing with the rest of the four files...(if there are 5 files in total) . and the similarity formula are the formula used to find similiarity.. its a fixed formula.. thanks

      OK, you may need to adjust the definition of $total as indicated somewhere; otherwise following program works as i understood your problem...

      #! /usr/local/bin/perl -w use strict ; my $stopfile = 'stopwords'; my %stoplist; # fill stop word list assuming each word is on one line open STOP, "<$stopfile" or die "cannot open $stopfile: $!\n"; while ( defined (my $stop = <STOP>) ) { chomp $stop; $stoplist{$stop} = 1; } close STOP or die "cannot close $stopfile: $!\n"; # FIRST file contains the words to compare against, # get the target word list # my @target = @{ filter( \%stoplist , [ shift @ARGV ] ) }; # rest of the files contain words which we want # to compare against the target list # my @words = @{ filter( \%stoplist , \@ARGV ) }; # adjust as desired as i fail to see what is @D1 (in OP) and # why $total needs to be the twice the size of @D1 # # BELOW IS MY NOTION OF $total # my $total = scalar @target + scalar @words; my $similarity = 2 * ( scalar @{ intersect( \@target , \@words ) } / $total ); # display similarity upto 4 decimal places printf "\nsimilarity is: %0.4g\n\n", $similarity; # find intersection of two arrays: 1st contains all the interesting v +alues, # 2d both interesting & uninteresting sub intersect { my ($ref , $misc) = @_; my %intersection; foreach my $misc ( @{$misc} ) { foreach my $ref ( @{$ref} ) { next if $misc ne $ref; $intersection{$ref} = 1; } } return [ keys %intersection ]; } # given a stop word hash & file name array (consisting of input word +list), # return the word list that are not stop words sub filter { my ($stop , $files) = @_; my %filtered; foreach my $file ( @{$files} ) { open FH , "<$file" or die "cannot open $file to read: $!\n"; while ( defined (my $line = <FH>) ) { foreach my $word (@{ line2words( $line ) }) { next if $stop->{$word}; $filtered{$word} = 1; } } close FH or die "cannot close $file: $!\n"; } return [ keys %filtered ]; } # return words, lower cased, from a given line sub line2words { my $line = $_[0]; return [ map { lc $_ } grep { $_ ne '' } split /\W+/ , $line ]; }


      Update: Add missing die if cannot close STOP.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://239821]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-16 05:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found