AI::Categorize - Automatically categorize documents based on content


NAME

AI::Categorize - Automatically categorize documents based on content


SYNOPSIS

  ### This is one of the categorizers available (see below for more)
  use AI::Categorize::NaiveBayes;
  my $c = new AI::Categorize::NaiveBayes();
  ### Supply some training documents so it can learn how to categorize
  $c->stopwords('the','a','and','but','I');  # Ignore these words
  $c->add_document($name, \@categories, $content);
  ... repeat for many documents, then:
  $c->crunch();
  $c->save_state('filename'); # Save machine for later use
  ### Categorize a new unknown document
  my $c = new AI::Categorize::NaiveBayes();
  $c->restore_state('filename');
  my $results = $c->categorize($content);
  if ($results->in_category('sports')) { ... }
  my @cats = $results->categories;
  my @scores = $results->scores(@cats);


DESCRIPTION

This module implements several algorithms for automatically guessing category information of documents based on the category information of existing documents. For example, one might categorize incoming email messages in order to place them into existing mailboxes, or one might categorize newspaper articles by general topic (business, sports, etc.). All of the categorizers learn their categorization rules from a body of existing pre-categorized documents.

Disclaimer: the results of any of these algorithms are far from infallible (close to fallible?). Categorization of documents is often a difficult task even for humans well-trained in the particular domain of knowledge, and there are many things a human would consider that none of these algorithms consider. These are only statistical tests - at best they are neat tricks or helpful assistants, and at worst they are totally unreliable. If you plan to use this module for anything important, human supervision is essential.

But this voodoo can be quite fun. =)


ALGORITHMS

Currently two different algorithms are implemented in this bundle:

  AI::Categorize::NaiveBayes
  AI::Categorize::kNN

These are all subclasses of AI::Categorize. Please see the documentation of these individual modules for more details on their guts and quirks. The common interface for all the algorithms is described here.

All these classes are designed to be subclassible so you can modify their behavior to suit your needs.


AI::Categorize Methods


AI::Categorize::Result Methods

An AI::Categorize::Result object (hereafter abbreviated as $r) is returned by the $c->categorize method, described above.


AI::Categorize::Map Methods

The AI::Categorize::Map class manages the relationships between documents and categories. It is designed to support fast lookups either by category or by document. An AI::Categorize::Map object is returned by the $c->cat_map method (described above) and will be called $m for convenience in this documentation.

In general, feel free to make any queries of the map, but don't make any changes to its data. Changes should usually only be made through the categorizer object.


CAVEATS

Don't depend on the specific scores given by $r->scores. They may change in future releases.

The entire categorizer is currently created in memory, which can get pretty demanding if you have a lot of data. If this turns out to be a problem, future versions may try to cache large chunks on disk. This would come with a speed penalty.

Finally, I am not an expert in document categorization. I have thought about it some, and I have written these modules largely as a way to concretize my thinking and learn more about the processes. If you know of ways to improve accuracy, please let me know.


TO DO

Idea from obvy: try tying into infobot (purl) to identify IRC moods: @moods = qw(indifferent flame_mode pissed inebriated happy sad)


AUTHOR

Ken Williams, ken@forum.swarthmore.edu


COPYRIGHT

Copyright 2000-2001 Ken Williams. All rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


SEE ALSO

perl(1), DBI(3).

``A re-examination of text categorization methods'' by Yiming Yang http://www.cs.cmu.edu/~yiming/publications.html

Other links from Na'im Tyson:

www.ruf.rice.edu/~barlow/corpus.html (corp. lx.) ciir.cs.umass.edu (info. ret) www.georgetown.edu/wilson/IR/IR.html (class in IR @ Georgetown University) www.research.att.com/~lewis (professional homepage of David Lews, one of the leaders in document categorization. you may want to visit his site sooner than the others since he has left AT&T research.)

 AI::Categorize - Automatically categorize documents based on content