NlpTools

Natural language processing in php

Documents

Documents wrap the actual document that gets classified (whatever that may be). A document can be anything from a single word to a word with context or maybe a whole email with metadata about previous conversations.

The Document interface

  1. interface DocumentInterface
  2. {
  3. /*
  4. * Return the data of what is being represented. If it were a word
  5. * we could return a word. If it were a blog post we could return
  6. * an array(Title,Body,array(Comments)).
  7. *
  8. * @return mixed
  9. */
  10. public function getDocumentData();
  11. }

Document is very generic and it can return anything as its data. It is, though, useful to design generic document classes that can be used by purpose-specific feature factories.

With the mindset expressed above, NlpTools contains three generic document types that could be used for many applications.

  1. TokensDocument
  2. WordDocument
  3. RawDocument

Documents also get to decide how to apply a transformation. For instance to apply a stemmer to a document one need not know how is the data structured in the document but can simply delegate the that to the document instance.

TokensDocument

TokensDocument is useful for feature factories and algorithms that use the "Bag of words" model for representing a large group of text. It wraps an array of tokens and simply returns this array as the data.

When applying a transformation it applies it to every single token removing the ones which are transformed to null.

To continue the tokenizers' example.

  1. use \NlpTools\Tokenizers\WhitespaceTokenizer;
  2. use \NlpTools\Documents\TokensDocument;
  3. $s1 = "Please allow me to introduce myself
  4. I'm a man of wealth and taste";
  5. $s2 = "Hello, I love you, won't you tell me your name
  6. Hello, I love you, let me jump in your game";
  7. $tok = new WhitespaceTokenizer();
  8. $stones = new TokensDocument($tok->tokenize($s1)); // $stones now represents the "Sympathy for the devil" song
  9. $doors = new TokensDocument($tok->tokenize($s2)); // $doors now represents the "Hello, I love you" song

WordDocument

WordDocument represents a single token (usually a word) surrounded by a token context of some length. It is useful for representing certain elements, like person names, places, etc, in the context of a larger piece of text.

Also as the TokensDocument this document applies a transformation to each token separately and removes the nulls.

To illustrate the difference from above.

  1. use \NlpTools\Tokenizers\WhitespaceTokenizer;
  2. use \NlpTools\Documents\WordDocument;
  3. $s1 = "Please allow me to introduce myself
  4. I'm a man of wealth and taste";
  5. $s2 = "Hello, I love you, won't you tell me your name
  6. Hello, I love you, let me jump in your game";
  7. $tok = new WhitespaceTokenizer();
  8. $stones = new WordDocument(
  9. $tok->tokenize($s1),
  10. 2,
  11. 4
  12. ); // $stones now represents the word 'me' in the context of 4
  13. // surrounding words in any direction
  14. $doors = new WordDocument(
  15. $tok->tokenize($s2),
  16. 7,
  17. 4
  18. ); // $doors now also represents the word 'me' in the context of 4
  19. // surrounding words in any direction

Because we have also captured the context of those words, above, we can distinguish them and produce different features.

RawDocument

RawDocument wraps a single php variable. The transformations are, as is expected, applied to the variable.

Training document and Training set

In the Documents namespace there exist two more classes created to make working with documents a lot easier.

TrainingDocument is a simple wrapper for any Document class that also contains the label (or class) information regarding that specific Document instance. Thus, TrainingDocument is useful for supervised learning where the model is trained on documents whose labels are known beforehand.

  1. // $d is a Document instance
  2. $td = new TrainingDocument('CLASS 1',$d);
  3. var_dump($td->getClass()); // CLASS 1
  4. var_dump($td->getDocumentData()); // $d->getDocumentData()

TrainingSet is a TrainingDocument container. Besides adding an easier way of instanciating TrainingDocuments it also provides the useful \Iterator, \ArrayAccess and \Countable interfaces for manipulating many TrainingDocuments and it allows for an easy way to apply a transformation to every single document.

  1. $tset = new TrainingSet();
  2. $tset->addDocument('CLASS 1',$a);
  3. foreach ($tset as $class=>$d) {
  4. ...
  5. }
  6. $tset->applyTransformations(array($transform1, $transform2, ...));

« Tokenizers / Feature Factories »