UNPKG

dbrans-natural

Version:

General natural language (tokenizing, stemming (English, Russian, Spanish), classification, inflection, phonetics, tfidf, WordNet, jaro-winkler, Levenshtein distance, Dice's Coefficient) facilities for node.

128 lines (102 loc) 3.6 kB
/* Copyright (c) 2011, Chris Umbel Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ var PorterStemmer = require('../stemmers/porter_stemmer'), util = require('util'); var Classifier = function(classifier, stemmer) { this.classifier = classifier; this.docs = []; this.features = {}; this.stemmer = stemmer || PorterStemmer; this.lastAdded = 0; }; function addDocument(text, classification) { if(typeof text === 'string') text = this.stemmer.tokenizeAndStem(text); if(text.length === 0) { // ignore empty documents return; } this.docs.push({ label: classification, text: text }); for(var i = 0; i < text.length; i++) { this.features[text[i]] = 1; } } function textToFeatures(observation) { var features = []; if(typeof observation === 'string') observation = this.stemmer.tokenizeAndStem(observation); for(var feature in this.features) { if(observation.indexOf(feature) > -1) features.push(1); else features.push(0); } return features; } function train() { for(var i = this.lastAdded; i < this.docs.length; i++) { var features = this.textToFeatures(this.docs[i].text); this.classifier.addExample(features, this.docs[i].label); this.lastAdded++; } this.classifier.train(); } function getClassifications(observation) { return this.classifier.getClassifications(this.textToFeatures(observation)); } function classify(observation) { return this.classifier.classify(this.textToFeatures(observation)); } function restore(classifier, stemmer) { classifier.stemmer = stemmer || PorterStemmer; return classifier; } function save(filename, callback) { var data = JSON.stringify(this); var fs = require('fs'); var classifier = this; fs.writeFile(filename, data, 'utf8', function(err) { if(callback) { callback(err, err ? null : classifier); } }); } function load(filename, callback) { var fs = require('fs'); fs.readFile(filename, 'utf8', function(err, data) { var classifier; if(!err) { classifier = JSON.parse(data); } if(callback) callback(err, classifier); }); } Classifier.prototype.addDocument = addDocument; Classifier.prototype.train = train; Classifier.prototype.classify = classify; Classifier.prototype.textToFeatures = textToFeatures; Classifier.prototype.save = save; Classifier.prototype.getClassifications = getClassifications; Classifier.restore = restore; Classifier.load = load; module.exports = Classifier;