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

sentence-processing.mddocs/

Sentence Processing

Single sentence analysis including grammatical transformations, pattern matching, and entity extraction. The Sentence class provides detailed analysis and manipulation of individual sentences.

Capabilities

Sentence Class

Core sentence object with term-level operations and grammatical analysis.

/**
 * Sentence class for single sentence analysis and transformation
 */
class Sentence {
  /** Array of terms in the sentence */
  terms: Term[];
  /** Original sentence string */
  str: string;
  /** Contraction expansion/contraction methods */
  contractions: Contractions;
}

Sentence Output Methods

Methods for retrieving processed sentence text in different formats.

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

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

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

/**
 * Get part-of-speech tags for all terms in sentence
 * @returns {string[]} Array of POS tags
 */
tags(): string[];

/**
 * Get the primary tag for the sentence (from main verb or subject)
 * @returns {Term[]} Array of primary terms
 */
tag(): Term[];

Usage Examples:

const nlp = require('nlp_compromise');

const sent = nlp.sentence('She walks quickly to school.');

console.log(sent.text());   // 'She walks quickly to school.'
console.log(sent.normal()); // 'she walks quickly to school'
console.log(sent.root());   // 'she walk quickly to school'
console.log(sent.tags());   // ['Pronoun', 'Verb', 'Adverb', 'Preposition', 'Noun']

Sentence Analysis

Methods for analyzing sentence structure and properties.

/**
 * Determine the type of sentence
 * @returns {string} Sentence type: 'declarative', 'interrogative', or 'exclamative'
 */
sentence_type(): string;

/**
 * Get the sentence terminator punctuation
 * @returns {string} Terminator: '.', '?', or '!'
 */
terminator(): string;

/**
 * Check if sentence is in passive voice
 * @returns {boolean} True if passive voice
 */
is_passive(): boolean;

Usage Examples:

const stmt = nlp.sentence('She goes to school.');
console.log(stmt.sentence_type()); // 'declarative'
console.log(stmt.terminator());    // '.'

const quest = nlp.sentence('Where does she go?');
console.log(quest.sentence_type()); // 'interrogative'
console.log(quest.terminator());    // '?'

const passive = nlp.sentence('The ball was thrown by him.');
console.log(passive.is_passive()); // true

Pattern Matching

Advanced pattern matching within the sentence with grammatical pattern support.

/**
 * Find patterns within the sentence
 * @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 within the sentence
 * @param {string} pattern - Pattern to replace
 * @param {string} replacement - Replacement text
 * @param {object} [options] - Replacement options
 * @returns {Sentence} Modified sentence object
 */
replace(pattern, replacement, options): Sentence;

Usage Examples:

const sent = nlp.sentence('The big red car drove fast.');

// Find adjective patterns
const adjectives = sent.match('[Adjective]');
console.log(adjectives.map(a => a.text())); // ['big', 'red']

// Find adjective-noun patterns
const adjNoun = sent.match('[Adjective] [Noun]');
console.log(adjNoun[0].text()); // 'red car'

// Replace patterns
sent.replace('[Adjective] car', 'blue truck');
console.log(sent.text()); // 'The big blue truck drove fast.'

Sentence Editing

Methods for programmatically modifying sentence structure.

/**
 * Insert a new term before the specified index
 * @param {number} index - Position to insert before
 * @param {string} str - Text to insert
 */
addBefore(index, str): void;

/**
 * Insert a new term after the specified index
 * @param {number} index - Position to insert after
 * @param {string} str - Text to insert
 */
addAfter(index, str): void;

Usage Examples:

const sent = nlp.sentence('She walks to school.');

// Add word before index 1 (before 'walks')
sent.addBefore(1, 'always');
console.log(sent.text()); // 'She always walks to school.'

// Add word after index 2 (after 'walks')
sent.addAfter(2, 'slowly');
console.log(sent.text()); // 'She always walks slowly to school.'

Grammatical Transformations

Sentence-level transformations for tense and negation.

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

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

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

/**
 * Negate the sentence
 * @returns {Sentence} Negated sentence
 */
negate(): Sentence;

Usage Examples:

const sent = nlp.sentence('She walks to school.');

console.log(sent.to_past().text());    // 'She walked to school.'
console.log(sent.to_future().text());  // 'She will walk to school.'
console.log(sent.negate().text());     // 'She doesn\'t walk to school.'

// Chain transformations
console.log(sent.to_past().negate().text()); // 'She didn\'t walk to school.'

Entity Extraction

Extract entities from the sentence with the same methods as Text class.

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

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

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

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

/**
 * Extract numeric values from sentence
 * @returns {Value[]} Array of Value objects
 */
values(): Value[];

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

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

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

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

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

Usage Examples:

const sent = nlp.sentence('Tony Hawk visited Apple Inc. in Cupertino yesterday.');

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

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

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

const nouns = sent.nouns();
console.log(nouns.map(n => n.text)); // ['Tony Hawk', 'Apple Inc.', 'Cupertino']

const topics = sent.topics();
console.log(topics.map(t => t.text)); // ['Tony Hawk', 'Apple Inc.', 'Cupertino']

Conditional Processing

Methods for removing conditional clauses and extracting core sentence meaning.

/**
 * Remove conditional clauses from sentence
 * @returns {Sentence} Sentence with conditions stripped
 */
strip_conditions(): Sentence;

Usage Examples:

const sent = nlp.sentence('If it rains, she will stay home.');
console.log(sent.strip_conditions().text()); // 'she will stay home'

Question Class

Extended sentence class specifically for interrogative sentences.

/**
 * Question class extending Sentence for interrogative analysis
 */
class Question extends Sentence {
  /**
   * Determine the type of question
   * @returns {string} Question type: 'how', 'when', 'where', 'who', 'why', 'what', 'which', 'number', 'yesNo'
   */
  from(): string;
}

Usage Examples:

const question = nlp.question('Where did she go yesterday?');
console.log(question.from()); // 'where'

const yesNo = nlp.question('Did she go to school?');
console.log(yesNo.from()); // 'yesNo'

const what = nlp.question('What time is it?');
console.log(what.from()); // 'what'

Statement Class

Extended sentence class specifically for declarative sentences.

/**
 * Statement class extending Sentence for declarative analysis
 */
class Statement extends Sentence {
  // Inherits all Sentence methods with declarative-specific behavior
}

docs

core-functions.md

entity-recognition.md

index.md

plugin-system.md

sentence-processing.md

text-analysis.md

word-operations.md

tile.json