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";
}
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.