CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-natural

Comprehensive natural language processing library with tokenization, stemming, classification, sentiment analysis, phonetics, distance algorithms, and WordNet integration.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

classification.mddocs/

Text Classification

Machine learning classifiers for categorizing text into predefined classes. Natural provides Naive Bayes, Logistic Regression, and Maximum Entropy classifiers with full training, persistence, and evaluation capabilities.

Capabilities

Bayes Classifier

Naive Bayes classifier implementation for text classification using probabilistic learning.

/**
 * Naive Bayes text classifier
 * @param stemmer - Optional stemmer for text preprocessing
 * @param smoothing - Laplace smoothing parameter (default: 1)
 */
class BayesClassifier {
  constructor(stemmer?: object, smoothing?: number);
  
  /** Add a training document with its classification */
  addDocument(text: string, classification: string): void;
  
  /** Remove a training document */
  removeDocument(text: string, classification: string): void;
  
  /** Train the classifier on added documents */
  train(): void;
  
  /** Retrain from scratch */
  retrain(): void;
  
  /** Classify a text observation */
  classify(observation: string): string;
  
  /** Get all classification scores for an observation */
  getClassifications(observation: string): ClassificationResult[];
  
  /** Save classifier to file */
  save(filename: string, callback: (err?: Error) => void): void;
  
  /** Set classifier options */
  setOptions(options: object): void;
}

interface ClassificationResult {
  label: string;
  value: number;
}

Static Methods:

/** Restore classifier from serialized data */
static BayesClassifier.restore(classifier: object, stemmer?: object): BayesClassifier;

/** Load classifier from file */
static BayesClassifier.load(filename: string, stemmer?: object, callback: (err?: Error, classifier?: BayesClassifier) => void): void;

/** Load from storage backend */
static BayesClassifier.loadFrom(key: string, stemmer?: object, storageBackend: object): Promise<BayesClassifier>;

Usage Examples:

const natural = require('natural');

// Create and train classifier
const classifier = new natural.BayesClassifier();

// Add training data
classifier.addDocument('I love this sandwich', 'positive');
classifier.addDocument('This is an amazing book', 'positive');
classifier.addDocument('The book is terrible', 'negative');
classifier.addDocument('I hate this movie', 'negative');

// Train the model
classifier.train();

// Classify new text
console.log(classifier.classify('I love this book')); // 'positive'
console.log(classifier.classify('This is terrible')); // 'negative'

// Get detailed scores
const scores = classifier.getClassifications('This book is amazing');
console.log(scores);
// [{ label: 'positive', value: 0.87 }, { label: 'negative', value: 0.13 }]

// Save classifier
classifier.save('my-classifier.json', (err) => {
  if (err) console.error(err);
  else console.log('Classifier saved');
});

// Load classifier
natural.BayesClassifier.load('my-classifier.json', null, (err, classifier) => {
  if (!err) {
    console.log(classifier.classify('This is great!'));
  }
});

Logistic Regression Classifier

Logistic regression implementation for text classification using maximum likelihood estimation.

/**
 * Logistic regression text classifier
 * @param stemmer - Optional stemmer for text preprocessing
 */
class LogisticRegressionClassifier {
  constructor(stemmer?: object);
  
  /** Add a training document with its classification */
  addDocument(text: string, classification: string): void;
  
  /** Remove a training document */
  removeDocument(text: string, classification: string): void;
  
  /** Train the classifier on added documents */
  train(): void;
  
  /** Classify a text observation */
  classify(observation: string): string;
  
  /** Get all classification scores for an observation */
  getClassifications(observation: string): ClassificationResult[];
  
  /** Save classifier to file */
  save(filename: string, callback: (err?: Error) => void): void;
}

Static Methods:

/** Restore classifier from serialized data */
static LogisticRegressionClassifier.restore(classifier: object, stemmer?: object): LogisticRegressionClassifier;

/** Load classifier from file */
static LogisticRegressionClassifier.load(filename: string, stemmer?: object, callback: (err?: Error, classifier?: LogisticRegressionClassifier) => void): void;

Usage Examples:

const natural = require('natural');

// Create logistic regression classifier
const classifier = new natural.LogisticRegressionClassifier();

// Add training documents
classifier.addDocument('The movie was fantastic', 'positive');
classifier.addDocument('I really enjoyed it', 'positive');
classifier.addDocument('It was boring and slow', 'negative');
classifier.addDocument('Waste of time', 'negative');

// Train
classifier.train();

// Classify
console.log(classifier.classify('Great movie!')); // 'positive'

Maximum Entropy Classifier

Maximum entropy classifier for complex feature-based classification tasks.

/**
 * Maximum entropy classifier
 * @param features - FeatureSet for defining features (optional, creates new if not provided)
 * @param sample - Sample for training data (optional, creates new if not provided)
 */
class MaxEntClassifier {
  constructor(features?: FeatureSet, sample?: Sample);
  
  /** Add feature set with classification */
  addFeatureSet(features: FeatureSet, classification: string): void;
  
  /** Train the classifier */
  train(): void;
  
  /** Classify feature set */
  classify(features: FeatureSet): string;
}

/**
 * Feature set for MaxEnt classifier
 */
class FeatureSet {
  constructor();
  
  /** Add feature with value */
  addFeature(name: string, value: any): void;
}

/**
 * Individual feature
 */
class Feature {
  constructor(name: string, value: any);
}

/**
 * Training sample for MaxEnt
 */
class Sample {
  constructor(features: FeatureSet, classification: string);
}

Usage Examples:

const natural = require('natural');

// Create MaxEnt classifier
const classifier = new natural.MaxEntClassifier();

// Create feature sets
const posFeatures = new natural.FeatureSet();
posFeatures.addFeature('contains_great', true);
posFeatures.addFeature('contains_amazing', true);
posFeatures.addFeature('word_count', 5);

const negFeatures = new natural.FeatureSet();
negFeatures.addFeature('contains_terrible', true);
negFeatures.addFeature('contains_awful', true);
negFeatures.addFeature('word_count', 4);

// Add training data
classifier.addFeatureSet(posFeatures, 'positive');
classifier.addFeatureSet(negFeatures, 'negative');

// Train
classifier.train();

// Classify new feature set
const testFeatures = new natural.FeatureSet();
testFeatures.addFeature('contains_great', true);
testFeatures.addFeature('word_count', 3);

console.log(classifier.classify(testFeatures)); // 'positive'

Supporting Classes

Advanced MaxEnt Supporting Classes:

/**
 * Context for MaxEnt elements containing feature information
 */
class Context {
  constructor(data?: object);
}

/**
 * Base element class for MaxEnt training samples
 */
class Element {
  constructor(classification: string, context: Context);
  
  /** Generate features for this element */
  generateFeatures(featureSet: FeatureSet): void;
}

/**
 * Simple example element for basic MaxEnt usage
 */
class SEElement extends Element {
  constructor(classification: string, context: Context);
  
  /** Generate features for simple examples */
  generateFeatures(featureSet: FeatureSet): void;
}

/**
 * POS (Part-of-Speech) specific element for tagging applications
 */
class POSElement extends Element {
  constructor(classification: string, context: Context);
  
  /** Generate POS-specific features including word and tag windows */
  generateFeatures(featureSet: FeatureSet): void;
}

/**
 * GIS (Generalized Iterative Scaling) scaler for MaxEnt parameter estimation
 */
class GISScaler {
  constructor(featureSet: FeatureSet, sample: Sample);
  
  /** Calculate maximum sum of features for normalization */
  calculateMaxSumOfFeatures(): boolean;
}

/**
 * MaxEnt sentence class for POS tagging
 */
class MESentence {
  constructor(taggedWords: Array<{token: string, tag: string}>);
  
  /** Generate sample elements from sentence for MaxEnt training */
  generateSampleElements(sample: Sample): void;
}

/**
 * MaxEnt corpus for POS tagging applications
 */
class MECorpus {
  constructor();
  
  /** Add tagged sentence to corpus */
  addSentence(sentence: MESentence): void;
  
  /** Generate training sample from corpus */
  generateSample(): Sample;
}

docs

classification.md

distance.md

index.md

ngrams-tfidf.md

phonetics.md

pos-tagging.md

sentiment.md

text-processing.md

transliterators.md

utilities.md

wordnet.md

tile.json