Hi, I'm trying to create a tagcloud from the collection of texts. That is, given a set of text (e.g., sentences), I'll need to count frequencies for the phrases. It's pretty easy to count frequencies of individual words, but I'm wondering whether there is a good algorithm for counting phrases. Here's a naiive way to count them:
my %words = ();
my %phrases = ();
while(<DATA>){
chomp;
my @words = split/\s+/;
#count words
++$words{lc $_} foreach @words;
# count phrases
for(my $i = 0; $i < @words; ++$i){
for(my $j = $i; $j < @words; ++$j){
++$phrases{lc join(" ", @words[$i..$j])};
}
}
}
print "Words:\n";
foreach my $wd (sort {$words{$b}<=>$words{$a}}keys %words) {
print "$wd => $words{$wd}\n";
}
print "\n\nPhrases:\n";
foreach my $p (sort {$phrases{$b}<=>$phrases{$a}}keys %phrases) {
print "$p => $phrases{$p}\n";
}
__DATA__
Mary had a little lamb
little lamb
John had a lamb
Mary and John both had a lamb
Mary and John had two little lambs
For my particular case, each sentence is about 50 words long, and there can be up to a few thousand such sentences.
Related to this, is there a good way to rule out the common words (e.g., "a", "the", etc.)?
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.