As for algorithm, it seems like you are just looking for large correlations among keywords. Somtimes people look for large coorelations among keywords as a marker for eliminating redundant variables. If A and B always appear together, you don't need both. Such redunandcy reduction is often used in statisitcal modeling to arrive at an independent set of variables upon which to base a model.
As for code to implement this measure, it is simple to use HoHs:
my %items = ( z => [ qw/one six/ ],
'y' => [ qw/two three five/ ],
x => [ qw/one two five/ ],
);
my %corr;
foreach my $item (keys %items) {
my $max = scalar @{$items{$item}} - 1;
for my $first (0..$max) {
for my $second ($first+1..$max) {
$corr{ $items{$item}[$first] }{ $items{$item}[$second] }++;
}
}
}
for my $first (keys %corr) {
for my $second (keys %{$corr{$first}}) {
print "$first $second: $corr{$first}{$second}\n";
}
}
Update: Note that this isn't a full correlation calculation, but implements the OPs desired numbers for a 2-way measure. As
tall_man pointed out, I blew it :) Here is the corrected code:
foreach my $item (keys %items) {
my @set = sort @{$items{$item}};
for my $first (0..$#set) {
for my $second ($first+1..$#set) {
$corr{ $set[$first] }{ $set[$second] }++;
}
}
}
for my $first (sort keys %corr) {
for my $second (sort keys %{$corr{$first}}) {
print "$first $second: $corr{$first}{$second}\n";
}
}
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.