Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Re: Re: Calculating "similarity"

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


in reply to Re: Re: Calculating "similarity"
in thread Calculating "similarity"

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://239907]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-25 23:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found