I am beginning perl user, so I'm sorry in advance if I miss some easy things, but I cannot get it to work and I am not sure whether every step is necessary(I have problems in particular with the while loops and 'saving' its outcome). I would like to do the following:
I have two text files. File 1 with a word list (one word per line) and File 2 that is a regular text document with text and numbers. I would like to count the number of words in file 2 that I can also find in file 1 and the total number of words in the document (so that I can calculate the percentage of corresponding words)
this is how far I have come:
use strict; use warnings; #Part one of the code, the wordlist is a file with one word per line a +nd I transform this into a hash my $filename = "wordlist.txt"; open(INPUT, $filename) or die "Cannot open $filename"; my $line = <INPUT>; while($line = <INPUT>){ chomp($line); my @words = split(/\s+/, $line); my %unique = map {$_ => 1 } @words; my @unique = %unique; #part two of code, open the text file and extract words only (because +the file also includes many numbers), and count the number of occuren +ce stored in a hash open (DATA, "4.txt") or die; my @UnNum; my $x; my %dict; while (<DATA>) { chomp; $_ = lc; s/ -- / /g; s/ - / /g; s/ +/ /g; s/[.,:;?"!_()\[\]]//g; my @UnNum = split(/\s+/); foreach $x(@UnNum){ if ($x =~ /(([a-zA-Z']+-)*[a-zA-Z']+)/ ){ ++$dict{$x}; }}} #part 3 of the code, I try to compare the two different hashes and add + the total number of occurrences while ((my $words,my $number) = each (%dict)) {my $total+= $number; if (exists($unique{$words})){ my $corresponding +=$number; print "There are $corresponding corresponding words of in total $total + words";}} }
thank you in advance for the help!
In reply to count number of overlapping words in a document by dmarcel
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |