AI::Categorizer::KnowledgeSet - Encapsulates set of documents
use AI::Categorizer::KnowledgeSet; my $k = new AI::Categorizer::KnowledgeSet(...parameters...); my $nb = new AI::Categorizer::Learner::NaiveBayes(...parameters...); $nb->train(knowledge_set => $k);
The KnowledgeSet class that provides an interface to a set of documents, a set of categories, and a mapping between the two. Many parameters for controlling the processing of documents are managed by the KnowledgeSet class.
Creates a new KnowledgeSet and returns it. Accepts the following parameters:
loadparameter is present, the
load()method will be invoked immediately. If the
loadparameter is a string, it will be passed as the
load(). If the
loadparameter is a hash reference, it will represent all the parameters to pass to
An optional reference to an array of Category objects representing the complete set of categories in a KnowledgeSet. If used, the
documentsparameter should also be specified.
An optional reference to an array of Document objects representing the complete set of documents in a KnowledgeSet. If used, the
categoriesparameter should also be specified.
A number indicating how many features (words) should be considered when training the Learner or categorizing new documents. May be specified as a positive integer (e.g. 2000) indicating the absolute number of features to be kept, or as a decimal between 0 and 1 (e.g. 0.2) indicating the fraction of the total number of features to be kept, or as 0 to indicate that no feature selection should be done and that the entire set of features should be used. The default is 0.2.
A string indicating the type of feature selection that should be performed. Currently the only option is also the default option:
Specifies how document word counts should be converted to vector values. Uses the three-character specification strings from Salton & Buckley's paper "Term-weighting approaches in automatic text retrieval". The three characters indicate the three factors that will be multiplied for each feature to find the final vector value for that feature. The default weighting is
The first character specifies the "term frequency" component, which can take the following values:
Binary weighting - 1 for terms present in a document, 0 for terms absent.
Raw term frequency - equal to the number of times a feature occurs in the document.
A synonym for 't'.
Normalized term frequency - 0.5 + 0.5 * t/max(t). This is the same as the 't' specification, but with term frequency normalized to lie between 0.5 and 1.
The second character specifies the "collection frequency" component, which can take the following values:
Inverse document frequency - multiply term
t's value by
Nis the total number of documents in the collection, and
nis the number of documents in which term
Probabilistic inverse document frequency - multiply term
t's value by
log((N-n)/n)(same variable meanings as above).
No change - multiply by 1.
The third character specifies the "normalization" component, which can take the following values:
Apply cosine normalization - multiply by 1/length(document_vector).
No change - multiply by 1.
The three components may alternatively be specified by the
If set to a true value, some status/debugging information will be output on
In a list context returns a list of all Category objects in this KnowledgeSet. In a scalar context returns the number of such objects.
In a list context returns a list of all Document objects in this KnowledgeSet. In a scalar context returns the number of such objects.
Given a document name, returns the Document object with that name, or
undefif no such Document object exists in this KnowledgeSet.
Returns a FeatureSet object which represents the features of all the documents in this KnowledgeSet.
verboseparameter of this KnowledgeSet, or sets it with an optional argument.
Scans all the documents of a Collection and returns a hash reference containing several statistics about the Collection. (XXX need to describe stats)
This method scans through a Collection object and determines the "best" features (words) to use when loading the documents and training the Learner. This process is known as "feature selection", and it's a very important part of categorization.
The Collection object should be specified as a
collectionparameter, or by giving the arguments to pass to the Collection's
The process of feature selection is governed by the
features_keptparameters given to the KnowledgeSet's
This method returns the features as a FeatureVector whose values are the "quality" of each feature, by whatever measure the
feature_selectionparameter specifies. Normally you won't need to use the return value, because this FeatureVector will become the
use_featuresparameter of any Document objects created by this KnowledgeSet.
Given the name of a file, this method writes the features (as determined by the
scan_featuresmethod) to the file.
Given the name of a file written by
save_features, loads the features from that file and passes them as the
use_featuresparameter for any Document objects created in the future by this KnowledgeSet.
Iterates through a Collection of documents and adds them to the KnowledgeSet. The Collection can be specified using a
collectionparameter - otherwise, specify the arguments to pass to the
new()method of the Collection class.
This method can do feature selection and load a Collection in one step (though it currently uses two steps internally).
Given a Document object as an argument, this method will add it and any categories it belongs to to the KnowledgeSet.
This method will create a Document object with the given data and then call
add_document()to add it to the KnowledgeSet. A
categoriesparameter should specify an array reference containing a list of categories by name. These are the categories that the document belongs to. Any other parameters will be passed to the Document class's
This method will be called prior to training the Learner. Its purpose is to perform any operations (such as feature vector weighting) that may require examination of the entire KnowledgeSet.
This method will be called during
finish()to adjust the weights of the features according to the
Given a single feature (word) as an argument, this method will return the number of documents in the KnowledgeSet that contain that feature.
Divides the KnowledgeSet into several subsets. This may be useful for performing cross-validation. The relative sizes of the subsets should be passed as arguments. For example, to split the KnowledgeSet into four KnowledgeSets of equal size, pass the arguments .25, .25, .25 (the final size is 1 minus the sum of the other sizes). The partitions will be returned as a list.
Ken Williams, email@example.com
Copyright 2000-2003 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.