Before I start, please note this is not strictly a Perl question. It is more of a general algorithms questions, and that's why I posted to Meditations.

A friend of mine has come up with a neat algorithm for creating a hierarchy from a messy bag of keywords. I was wondering if this was something novel or not, so I thought I would ask the Monks if anyone recognizes it.

Another reason I'm posting is simply because I like this algorithm. Whether it's new or not, it's simple and intuitive. It creates hierarchies in a way that seems natural and "right." I would like to hear other people's thoughts about it. Maybe there are some things I am overlooking.

The idea is to start with a list of items that have keywords assigned to them. The keywords are not necessarily chosen with a hierarchy in mind, yet by grouping commonly used keywords, a hierarchy can be synthesized. It goes something like this:

for each item in items: for each keyword in item.keywords: frequency{keyword}++ for each item in items: sort keywords by frequency push hierarchy{sorted keywords}, item

Thus, given the following items:

item = 1, keywords = a b c item = 2, keywords = b c item = 3, keywords = d b

The frequencies would be:

a = 1, b = 3, c = 2, d = 1

And the hierarchy would end up looking like:

b/c/2 b/c/a/1 b/d/3

Here's my quick and dirty Perl implementation, so there's something to actually play with:

my @items; push @items, { name => 1, keywords => [ qw( b ) ] }, { name => 2, keywords => [ qw( b c ) ] }, { name => 3, keywords => [ qw( b c ) ] }, { name => 4, keywords => [ qw( b d ) ] }, { name => 5, keywords => [ qw( e ) ] }, { name => 6, keywords => [ qw( e f ) ] }; my %freq; for (@items) { for (@{ $_->{keywords} }) { $freq{$_}++; } } my @nodes; for (@items) { my @keywords = sort { $freq{$b} <=> $freq{$a} } @{ $_->{keywords} }; my $path = join "/", @keywords, $_->{name}; push @nodes, $path; } print "$_\n" for sort @nodes

Update: funny that The Mad Hatter should mention tags. This was originally made to parse the tags from a http://del.icio.us dump and create a hierarchy of links from it. I used "keywords" instead of "tags" because I thought it would be a more generic term for essentially the same thing. :-)


In reply to Create hierarchies from list of keywords -- well known algorithm? by revdiablo

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.