Text::AI::CRM114 - Perl interface for CRM114
use Text::AI::CRM114; my $db = Text::AI::CRM114->new( Text::AI::CRM114::OSBF_BAYES, 8*1024*1024, ["Alice", "Macbeth"]); $db->learn("Alice", "Alice was beginning to ..."); $db->learn("Macbeth", "When shall we three meet again ..."); my @ret = $db->classify_text("The Mole had been working very hard all the morning ..."); say "Best classification is $ret[1]" unless ($ret[0] != Text::AI::CRM114::OK);
This module provides a simple Perl interface to libcrm114, a library that implements several text classification algorithms.
libcrm114
libcrm114 uses several constants as status return values and to set the classification algorithm of a new datablock. -- These constants are accessible in this module's namespace, for example Text::AI::CRM114::OK and Text::AI::CRM114::OSB_WINNOW.
Text::AI::CRM114::OK
Text::AI::CRM114::OSB_WINNOW
Creates a new instance.
sets the classification algorithm, recommended values are
Text::AI::CRM114::OSB_BAYES (default), Text::AI::CRM114::OSB_WINNOW, or Text::AI::CRM114::HYPERSPACE. libcrm114 includes some more algorithms (SVM, PCA, FSCM) which may or may not be production ready.
Text::AI::CRM114::OSB_BAYES
Text::AI::CRM114::HYPERSPACE
the memory size of learned data (default is 4 Mb). Note that some algorithms have to grow the datasize when learning.
a list of classes passed by reference (default: ['A', 'B']).
['A', 'B']
Creates a new instance by reading a previously saved CRM114 DB from $filename.
$filename
Returns a hash reference to the DB's classes. This hash associates the class names (keys) with the internal integer index (values).
Writes the DB into a (binary) file.
Learn some text of a given class.
Classify the text.
The normal working mode without the optional $verbatim flag adjusts the return values to be more useful with two classes (e.g. spam/ham). If the flag is given then the values are passed unchanged as they come from libcrm114. In practice this is only relevant if you use more than two classes. (Then you have to consider the success/non-success classes and probably want to add a method to retrieve the single per-class results.)
$verbatim
Returns a list of five scalar values:
A numeric error code, should be Text::AI::libcrm114::OK
Text::AI::libcrm114::OK
The name of the best matching class.
The success probability. Normally the probability of the matching class (with 0.5 <= $prob <= 1)
With $verbatim this is the success probability, i.e. with two classes the probability of the first class and with multiple classes the sum of probabilities for all successful classes (with 0 <= $prob <= 1).
The logarithmic probability ratio i.e. log10($prob) - log10(1-$prob) (theorethic range is 0 <= $pR <= 340, limited by floating point precision; but in practice a p = .99 yields a pR = 2, so high values are rather unusual).
log10($prob) - log10(1-$prob)
With $verbatim this is the ratio between all success and all non-success probabilities, so for a non-successful result the value can also be negative (range -340 <= $pR <= 340).
This is my first attempt to write a Perl module, so all hints and improvements are appreciated.
I wonder if we should ensure Text::AI::CRM114::OK maps to 0, as this makes the caller's return value checking easier. Currently this is trivial because it already is 0 in libcrm114. If that should change we would have to insert a rewrite into every XS call to a C function (ugly, but maybe worth it).
I am still not sure if the C memory management works correctly.
Another issue is Unicode support, which is missing in libcrm114, so it might be a good thing to convert unicode strings into some 8-bit encoding. As long as no string contains \0-values nothing bad[tm] will happen, but I assume that Unicode strings will internally cause wrong tokenization (this should be checked in libtre).
libtre
CRM114 homepage: http://crm114.sourceforge.net/
AI::CRM114, a module using the crm language interpreter: https://metacpan.org/module/AI::CRM114
v0.04 remove crm114_strerror, which is not in libcrm114 tarball
v0.03 initial CPAN release
v0.02 initial push to github
Martin Schuette, <info@mschuette.name>
Perl module: Copyright (C) 2012 by Martin Schuette
libcrm114: Copyright (C) 2009-2010 by William S. Yerazunis
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 3.
To install Text::AI::libcrm114, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Text::AI::libcrm114
CPAN shell
perl -MCPAN -e shell install Text::AI::libcrm114
For more information on module installation, please visit the detailed CPAN module installation guide.