CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-nlp-compromise

Natural language processing library that analyzes, transforms, and extracts meaning from English text in browsers and Node.js

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

text-analysis.mddocs/

Text Analysis

Multi-sentence text processing with comprehensive entity extraction, pattern matching, and document-level transformations. The Text class is designed for analyzing documents, paragraphs, or any text containing multiple sentences.

Capabilities

Text Class

Container for multiple sentences with document-level operations and entity extraction.

/**
 * Text class for multi-sentence analysis and transformation
 */
class Text {
  /** Array of parsed sentences */
  sentences: Sentence[];
  /** Original input text */
  raw_text: string;
  /** Contraction expansion/contraction methods */
  contractions: Contractions;
}

Text Output Methods

Methods for retrieving processed text in different formats.

/**
 * Get formatted text with original spacing and punctuation
 * @returns {string} Reconstructed text
 */
text(): string;

/**
 * Get normalized text with cleaned whitespace
 * @returns {string} Normalized text
 */
normal(): string;

/**
 * Get root/lemmatized form of all words
 * @returns {string} Root form text
 */
root(): string;

Usage Examples:

const nlp = require('nlp_compromise');

const doc = nlp.text('She walks quickly. He ran slowly.');

console.log(doc.text());   // 'She walks quickly. He ran slowly.'
console.log(doc.normal()); // 'she walks quickly he ran slowly'
console.log(doc.root());   // 'she walk quickly he run slowly'

Term and Tag Access

Methods for accessing individual terms and their grammatical tags.

/**
 * Get all terms from all sentences
 * @returns {Term[]} Array of all terms
 */
terms(): Term[];

/**
 * Get part-of-speech tags for all sentences
 * @returns {string[][]} Array of tag arrays for each sentence
 */
tags(): string[][];

Usage Examples:

const doc = nlp.text('Dogs bark loudly.');
const terms = doc.terms();
console.log(terms[0].text); // 'Dogs'
console.log(terms[0].tag);  // 'Plural'

const tags = doc.tags();
console.log(tags[0]); // ['Plural', 'Verb', 'Adverb']

Pattern Matching

Advanced pattern matching across the entire text with support for grammatical patterns.

/**
 * Find patterns across all sentences in the text
 * @param {string} pattern - Search pattern (supports grammatical patterns)
 * @param {object} [options] - Matching options
 * @returns {Result[]} Array of matching results
 */
match(pattern, options): Result[];

/**
 * Replace patterns across all sentences
 * @param {string} pattern - Pattern to replace
 * @param {string} replacement - Replacement text
 * @param {object} [options] - Replacement options
 * @returns {Text} Modified text object
 */
replace(pattern, replacement, options): Text;

Usage Examples:

const doc = nlp.text('The big dog barked. A small cat meowed.');

// Find adjective-noun patterns
const matches = doc.match('[Adjective] [Noun]');
console.log(matches[0].text()); // 'big dog'
console.log(matches[1].text()); // 'small cat'

// Replace patterns
doc.replace('[Adjective] dog', 'tiny puppy');
console.log(doc.text()); // 'The tiny puppy barked. A small cat meowed.'

Grammatical Transformations

Document-level transformations that apply to all verbs in the text.

/**
 * Convert all verbs to past tense
 * @returns {Text} Text with past tense verbs
 */
to_past(): Text;

/**
 * Convert all verbs to present tense
 * @returns {Text} Text with present tense verbs
 */
to_present(): Text;

/**
 * Convert all verbs to future tense
 * @returns {Text} Text with future tense verbs
 */
to_future(): Text;

/**
 * Negate all sentences in the text
 * @returns {Text} Text with negated sentences
 */
negate(): Text;

Usage Examples:

const doc = nlp.text('She walks to school. He runs home.');

console.log(doc.to_past().text());    // 'She walked to school. He ran home.'
console.log(doc.to_future().text());  // 'She will walk to school. He will run home.'
console.log(doc.negate().text());     // 'She doesn\'t walk to school. He doesn\'t run home.'

Entity Extraction

Comprehensive entity recognition and extraction across the entire document.

/**
 * Extract all person entities from text
 * @returns {Person[]} Array of Person objects
 */
people(): Person[];

/**
 * Extract all place entities from text
 * @returns {Place[]} Array of Place objects
 */
places(): Place[];

/**
 * Extract all organization entities from text
 * @returns {Organization[]} Array of Organization objects
 */
organizations(): Organization[];

/**
 * Extract all date entities from text
 * @returns {Date[]} Array of Date objects
 */
dates(): Date[];

/**
 * Extract all numeric values and measurements
 * @returns {Value[]} Array of Value objects
 */
values(): Value[];

/**
 * Extract topics and keywords with frequency counts
 * @returns {Topic[]} Array of Topic objects with counts
 */
topics(): Topic[];

Usage Examples:

const doc = nlp.text(`
  Tony Hawk visited Apple Inc. in Cupertino on March 15th.
  He spent $500 on new skateboard equipment.
`);

const people = doc.people();
console.log(people[0].text);        // 'Tony Hawk'
console.log(people[0].firstName);   // 'Tony'

const places = doc.places();
console.log(places[0].text);        // 'Cupertino'

const orgs = doc.organizations();
console.log(orgs[0].text);          // 'Apple Inc.'

const dates = doc.dates();  
console.log(dates[0].text);         // 'March 15th'

const values = doc.values();
console.log(values[0].number);      // 500
console.log(values[0].unit);        // 'dollar'

Part-of-Speech Extraction

Extract all words of specific grammatical categories from the text.

/**
 * Extract all nouns from text
 * @returns {Noun[]} Array of Noun objects
 */
nouns(): Noun[];

/**
 * Extract all adjectives from text  
 * @returns {Adjective[]} Array of Adjective objects
 */
adjectives(): Adjective[];

/**
 * Extract all verbs from text
 * @returns {Verb[]} Array of Verb objects
 */
verbs(): Verb[];

/**
 * Extract all adverbs from text
 * @returns {Adverb[]} Array of Adverb objects
 */
adverbs(): Adverb[];

Usage Examples:

const doc = nlp.text('The quick brown fox jumps quickly over the lazy dog.');

const nouns = doc.nouns();
console.log(nouns.map(n => n.text)); // ['fox', 'dog']

const adjectives = doc.adjectives();
console.log(adjectives.map(a => a.text)); // ['quick', 'brown', 'lazy']

const verbs = doc.verbs();
console.log(verbs.map(v => v.text)); // ['jumps']

const adverbs = doc.adverbs();
console.log(adverbs.map(a => a.text)); // ['quickly']

Contractions

Methods for expanding and contracting text-wide contractions.

/**
 * Contractions object for expansion/contraction operations
 */
interface Contractions {
  /**
   * Expand all contractions in text (he'd → he would)
   * @returns {Text} Text with expanded contractions
   */
  expand(): Text;
  
  /**
   * Contract appropriate phrases (he would → he'd)
   * @returns {Text} Text with contracted phrases
   */
  contract(): Text;
}

Usage Examples:

const doc = nlp.text("He'd like to go. She's very smart.");

console.log(doc.contractions.expand().text());
// "He would like to go. She is very smart."

const expanded = nlp.text("He would like to go. She is very smart.");
console.log(expanded.contractions.contract().text());
// "He'd like to go. She's very smart."

docs

core-functions.md

entity-recognition.md

index.md

plugin-system.md

sentence-processing.md

text-analysis.md

word-operations.md

tile.json