in reply to Re: count number of overlapping words in a document
in thread count number of overlapping words in a document
Dear JohnGG
Thank you very much for your prompt reply! I really appreciate it! I did run into some small problems because my computer did not recognize the code(had many errors). After some research I found out that this is because I using windows (I feel even more beginner now for not figuring that out), but as a result I had some difficulties understanding the code because I am used to a completely different way of writing code(some lines are still difficult for me to understand, but I get what you're doing)
Could you maybe check whether this 'translation' is correct, because when I replace the first line by using strict and warnings it provides me with 2 errors later in the code
$ perl -Mstrict -Mwarnings -E ; my $wordsFH = "woord.txt"; open(INPUT, $wordsFH) or die "Cannot open $filename"; my %words = map {chomp; $_ => 0 } <INPUT>; my $rxWords = do { local $" = q{ | }; qr{(?x) \b ( @{ [ keys %words ] } ) \b }; }; print "Regex is $rxWords"; my $textFH = "4.txt"; open (texting, $textFH) or die; my $text = do { local $/; <texting>; }; $words{$1} ++ while $text =~ m{$rxWords}g; print "$_ => $words{ $_ }}" for sort keys %words;
Furhtermore, I still have 2 problems that I need to tackle and of which I am not sure how to handle them. 1 Is it possible to extract the total number of 'hits'? $total ++ while $text =~ m{$rxWords}g; works, but may I incorporate it so that I do not have to use the regex twice. 2 Is there a simple addition to calculate the total number of words? (is setting up an array with chop the easiest solution?)
Again, thank you so much for your time!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: count number of overlapping words in a document
by johngg (Canon) on Sep 17, 2014 at 11:44 UTC | |
by dmarcel (Initiate) on Sep 18, 2014 at 08:39 UTC |