Natural language processing library that analyzes, transforms, and extracts meaning from English text in browsers and Node.js
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Extensible plugin architecture for adding custom functionality and extending existing classes. The plugin system allows developers to add new methods to any nlp_compromise class or modify the internal lexicon.
Core function for registering plugins that extend nlp_compromise functionality.
/**
* Register a plugin to extend nlp_compromise functionality
* @param {object|function} obj - Plugin object or function
*/
function plugin(obj): void;Plugins can extend any of the core classes by providing method objects:
// Plugin object structure
const pluginObject = {
Term: {
// Methods to add to Term class
customMethod(): any;
},
Text: {
// Methods to add to Text class
customTextMethod(): any;
},
Sentence: {
// Methods to add to Sentence class
customSentenceMethod(): any;
},
Noun: {
// Methods to add to Noun class
customNounMethod(): any;
},
Verb: {
// Methods to add to Verb class
customVerbMethod(): any;
},
// ... other classes
};Plugins can also be functions that receive the nlp instance and return method objects:
/**
* Function-based plugin that receives nlp instance
* @param {object} nlp - The nlp_compromise instance
* @returns {object} Plugin methods object
*/
function pluginFunction(nlp) {
return {
Term: {
customMethod() {
// Access to nlp instance and this (Term instance)
return this.text + ' custom';
}
}
};
}Usage Examples:
const nlp = require('nlp_compromise');
// Object-based plugin
const customPlugin = {
Term: {
reverse() {
return this.text.split('').reverse().join('');
},
wordCount() {
return this.text.split(' ').length;
}
},
Text: {
wordFrequency() {
const words = {};
this.terms().forEach(term => {
const word = term.normal;
words[word] = (words[word] || 0) + 1;
});
return words;
}
}
};
// Register the plugin
nlp.plugin(customPlugin);
// Use the new methods
const term = nlp.term('hello');
console.log(term.reverse()); // 'olleh'
const text = nlp.text('hello world hello');
console.log(text.wordFrequency()); // { hello: 2, world: 1 }// Function-based plugin with access to nlp instance
nlp.plugin(function(nlp) {
return {
Text: {
analyze() {
return {
sentences: this.sentences.length,
words: this.terms().length,
people: this.people().length,
places: this.places().length
};
}
},
Verb: {
allTenses() {
return {
present: this.to_present(),
past: this.to_past(),
future: this.to_future()
};
}
}
};
});
// Use the function-based plugin methods
const text = nlp.text('John went to New York yesterday.');
console.log(text.analyze());
// { sentences: 1, words: 6, people: 1, places: 1 }
const verb = nlp.verb('run');
console.log(verb.allTenses());
// { present: 'runs', past: 'ran', future: 'will run' }Access and modify the internal lexicon used for part-of-speech tagging.
/**
* Access or modify the internal lexicon
* @param {object} [obj] - Object with word->tag mappings to add
* @returns {object} The lexicon object
*/
function lexicon(obj): object;Usage Examples:
// View current lexicon (partial)
const lex = nlp.lexicon();
console.log(lex.dog); // 'Noun'
console.log(lex.run); // 'Verb'
// Add custom words to lexicon
nlp.lexicon({
'pokemon': 'Noun',
'googling': 'Verb',
'supercalifragilisticexpialidocious': 'Adjective'
});
// Test custom lexicon entries
const customTerm = nlp.term('pokemon');
console.log(customTerm.tag); // 'Noun'
const customVerb = nlp.verb('googling');
console.log(customVerb.tense()); // 'present'Plugins can extend any of these classes:
// Available classes for plugin extension
const extendableClasses = [
'Term', // Base term class
'Text', // Multi-sentence text
'Sentence', // Single sentence
'Statement', // Declarative sentence
'Question', // Interrogative sentence
'Noun', // Noun terms
'Verb', // Verb terms
'Adjective', // Adjective terms
'Adverb', // Adverb terms
'Value', // Numeric values
'Person', // Person entities
'Place', // Place entities
'Date', // Date entities
'Organization' // Organization entities
];Here are examples of useful plugins that extend nlp_compromise:
Syllable Counter Plugin:
nlp.plugin({
Term: {
syllables() {
// Simple syllable counting algorithm
const word = this.text.toLowerCase();
let count = word.match(/[aeiouy]+/g);
if (count) {
count = count.length;
if (word.endsWith('e')) count--;
if (count === 0) count = 1;
} else {
count = 1;
}
return count;
}
},
Text: {
readabilityScore() {
const words = this.terms().length;
const sentences = this.sentences.length;
const syllables = this.terms().reduce((sum, term) => {
return sum + (term.syllables ? term.syllables() : 1);
}, 0);
// Flesch Reading Ease formula
return 206.835 - (1.015 * (words / sentences)) - (84.6 * (syllables / words));
}
}
});
// Usage
const text = nlp.text('The quick brown fox jumps over the lazy dog.');
console.log(text.readabilityScore()); // ~83 (easy to read)Sentiment Analysis Plugin:
nlp.plugin({
Term: {
sentiment() {
const positive = ['good', 'great', 'excellent', 'amazing', 'wonderful'];
const negative = ['bad', 'terrible', 'awful', 'horrible', 'sad'];
const word = this.normal;
if (positive.includes(word)) return 1;
if (negative.includes(word)) return -1;
return 0;
}
},
Text: {
overallSentiment() {
const scores = this.terms().map(term => term.sentiment ? term.sentiment() : 0);
const sum = scores.reduce((a, b) => a + b, 0);
return sum / scores.length;
}
}
});
// Usage
const text = nlp.text('This is a great day but terrible weather.');
console.log(text.overallSentiment()); // Slightly negative due to "terrible"Best practices for developing nlp_compromise plugins:
this refers to the instance of the class being extendedthis for methods that should be chainablePlugin Template:
const myPlugin = {
// Extend Term class
Term: {
myTermMethod() {
// Access term properties: this.text, this.normal, this.tag, etc.
return this; // Return this for chaining
}
},
// Extend Text class
Text: {
myTextMethod() {
// Access text properties: this.sentences, this.raw_text, etc.
return this; // Return this for chaining
}
}
// Add other class extensions as needed
};
// Register the plugin
nlp.plugin(myPlugin);