Part-of-Speech Tagger with WordNet
wordpos is a set of fast part-of-speech (POS) utilities for both Node.js and browser environments, leveraging the WordNet database for its linguistic data. The current stable version is 2.1.0, which marked a significant refactoring to enable full browser compatibility, overcoming previous limitations regarding the size and un-browserify-ability of the WordNet database. While a specific release cadence isn't published, the project has undergone major version updates (1.x, 2.x) that introduced substantial performance improvements (up to 5x faster than prior versions), promise-based APIs, and a decoupling from the `natural` library's WordNet module. Its key differentiators include its speed, cross-environment support, a flexible API with options for stopword filtering and profiling, and a command-line interface.
Common errors
-
TypeError: wordpos.getAdjectives is not a function
cause The WordPOS class was imported but not instantiated with `new WordPOS()`.fixEnsure you create an instance of the class: `const wordpos = new WordPOS();` -
ReferenceError: require is not defined
cause Attempting to use CommonJS `require()` syntax in an ECMAScript Module (ESM) environment.fixChange your import statement to `import WordPOS from 'wordpos';` and ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`). -
Callback is not a function or missing
cause An asynchronous API method was called without providing a callback function.fixProvide a callback function as the last argument to all asynchronous API calls, e.g., `wordpos.getNouns('text', (result) => { /* handle result */ });`.
Warnings
- breaking Version 1.x removed the direct dependency on the `natural` library's WordNet module, implementing its own fast lookup. Projects relying on `natural`'s specific WordNet behavior might require adjustments.
- breaking Version 2.x involved a total refactor to enable browser compatibility. While the API remains similar, internal changes might affect environments with specific build processes or require different asset loading strategies for the WordNet database.
- gotcha All API methods in wordpos are asynchronous. Failing to provide a callback function (or `await` a Promise in newer versions) will result in uncaught errors or unexpected behavior.
Install
-
npm install wordpos -
yarn add wordpos -
pnpm add wordpos
Imports
- WordPOS
import { WordPOS } from 'wordpos';import WordPOS from 'wordpos';
- WordPOS
import WordPOS from 'wordpos';
const WordPOS = require('wordpos'); - wordpos.getAdjectives
wordpos.getAdjectives('text');wordpos.getAdjectives('text', callback);
Quickstart
const WordPOS = require('wordpos');
const wordpos = new WordPOS();
wordpos.getAdjectives('The angry bear chased the frightened little squirrel.', (result) => {
console.log('Adjectives:', result);
});
wordpos.isAdjective('awesome', (result) => {
console.log('Is awesome an adjective?', result);
});
// Demonstrating getPOS for all parts of speech
wordpos.getPOS('The quick brown fox jumps over the lazy dog.', (result) => {
console.log('All POS breakdown:', result);
});