OK, let me rephrase your problem to see if I understood you correctly. I assume that you want to compare several (other) files to one base file. You want to calculate a similarity rating between those files based on the number of words that are same (=intersection) and the total number of words (=total)
in each of the other files - this last point I'm not sure about, it could also be total number of words in base and other file together.
Does this do what you want?
#!/usr/local/bin/perl -w
use strict ;
my $stopfile = 'stopwords';
my $base= shift @ARGV;
# read in stopwords
open STOP, "<$stopfile" or die $!;
chomp( my @stop= <STOP> );
close STOP;
my %stopwords=();
# add the empty string '' to the stopwords as well
@stopwords{@stop,''} = ();
# read in basefile
my %base_filterwords=();
open BASETEXT, "<$base" or die $!;
while ( <BASETEXT> ) {
@base_filterwords{ map { my $l = lc; exists $stopwords{$l}?():$l }
+split /\W+/ } = ();
}
close BASETEXT;
# read in other files
my %other_filterwords=();
while ( <> ) {
@other_filterwords{ map { my $l = lc; exists $stopwords{$l}?():$l }
+ split /\W+/ } = ();
}
continue {
if (eof) {
my $total = scalar keys %other_filterwords;
my $intersect = 0;
for my $key (keys %other_filterwords) {
$intersect++ if exists $base_filterwords{$key};
}
my $sim = 2*$intersect/$total;
print "Similarity between $base and $ARGV = $sim\n";
print "\t@{[keys %other_filterwords]}\n";
print "\t@{[keys %base_filterwords]}\n";
# reset other filter words
%other_filterwords = ();
}
}
Update Fixed some small bugs in the code ...
-- Hofmator
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.