Hello,
It has been a while, so I hope you all have a little patience.

I have written a short script to do a little text mining. I have one file that contains a list of words/terms (new line separated), and a second file that contains a fairly substantial amount of text. I then want to take each term from the first file in turn and count the number of occurances of that term in the second file.

The output would be something like:

Term Number ---------------------- term1 10 term2 1 my term3 16

I am not bothered about the output as yet. The problem I do have, however, is that I am using a regular expression that does not seem to work for a term that consists of multiple words, i.e.

term1 term2 my term 3

The code I have at the moment is:

#!/usr/bin/perl -w use warnings; use strict; use Getopt::Long; my $terms = ""; my $pathway_abstracts = ""; my %word_counts; my $count = 0; my %term_score; my $term_frequency_score = 0; my $word_number = 0; ##################### GET USER INPUT ################################# +########## GetOptions( "pathway_abstracts=s" => \$pathway_abstracts, "terms_file=s" => \$terms ); ################### STORE IN ARRAYS ################################## +########## # store pathway abstract in arrays open(IN, "$pathway_abstracts" ) || die "$!"; my @array_1 = <IN>; close(IN); # store terms in array open(IN2, "$terms" ) || die "$!"; my @array_2 = <IN2>; close(IN2); #################### CREATE HASHES OF TERMS ########################## +########## foreach my $key (@array_2) # assign a score of 0 to each term { chomp($key); $term_score{$key} = 0; } ###################################################################### +########## print("Term\t| "); print("Number\t| "); print("Frequency\n"); print("----------------------------------------------------\n"); for (my $j = 0; $j < @array_2; $j++) # loop through each search + term { chomp($array_2[$j]); my $phenotype_term = $array_2[$j]; # set the search term for(my $i = 0; $i < @array_1; $i++) # loop through each line +in the document { my @word_array = split(/\s/, $array_1[$i]); # split abstra +cts on each word $word_number = $word_number + scalar(@word_array); # find out + how many words are in abstracts foreach my $word (@word_array) # look through each word i +n current line { if($word =~ /\b\Q$phenotype_term\E\b/) # does line contain fi +lter term { $term_score{$array_2[$j]} = $term_score{$array_2[$j]} + 1; +# increment term count } } } $term_frequency_score = $term_score{$array_2[$j]} / $word_number; +# calculate term frequency print($array_2[$j]."\t "); # print the term print($term_score{$array_2[$j]}."\t "); # print the number o +f term occurances print($term_frequency_score."\n"); # print the frequenc +y }

Could anyone please let me know why the \b or \B do not appear to work here in the regualr expression:
$word =~ /\b\Q$phenotype_term\E\b/

Any help is very much appreciated.
many thanks,
MonkPaul.


In reply to Regex word boundries by MonkPaul

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.