English Inflectors Library
For noun (plural to singular and singular to plural), verb (gerund, present & past) and adjective (comparative, superlative) transformations.
Demo
Here's a quick demo: RunKit
Installation
npm install en-inflectors --save
Usage
- Import the library
// javascriptconst Inflectors = Inflectors;
// typescript;
- Instantiate the class
let instance = "book";
- Adjective Inflection
let instance = "big";instance; // biggerinstance; // biggest
- Verb Conjugation
"rallied"; // rally"fly"; // flew"throw"; // thrown"rally"; // rallies"die"; // dying // or you can use the aliases"rallied"; // rally"fly"; // flew"throw"; // thrown"rally"; // rallies"die"; // dying
- Noun Inflection
const instanceA = "bus";const instanceB = "ellipses";const instanceC = "money"; instanceA; // trueinstanceB; // trueinstanceC; // false instanceA; // falseinstanceB; // falseinstanceC; // true instanceA; // trueinstanceB; // falseinstanceC; // true instanceA; // falseinstanceB; // trueinstanceC; // true // note that uncountable words return true// on both plural and singular checks instanceA; // bus (no change)instanceB; // ellipsisinstanceC; // money (no change) instanceA; // busesinstanceB; // ellipses (no change)instanceC; // money (no change)
How does it work
-
Adjective inflection
- Checks against a dictionary of known irregularities (e.g. little/less/least)
- Applies inflection based on:
- Number of syllables
- word ending
-
Noun inflection
- Dictionary lookup (known irregularities e.g. octopus/octopi & uncountable words)
- Identifies whether the word is plural or singular based on:
- Dictionary
- Machine learned regular expressions
- Applies transformation based on ending and word pattern (vowels, consonants and word endings)
-
Verb conjugation
- Dictionary lookup (known irregularities + 4000 common verbs)
- If the passed verb is identified as infinitive, it then applies regular expression transformations that are based on word endings, vowels and consonant phonetics.
- Tries to trim character from the beginning of the verb, thus solving prefixes (e.g. undergoes, overthrown)
- Tries to stem the word and get the infinitive form, then apply regular expression transformations.
- Applies regular expressions.
How accurate is it?
First of all, unless you have a dictionary of all the words and verbs that exist in English, you can't really write a regular expression or an algorithm and expect to have a 100% success rate. English has been adopting words from a lot of different languages (French, Greek and Latin for example), and each one of these languages has its own rules of pluralization and singularization, let alone verb conjugation.
Even with dictionaries you'll have the problem of complex and made up words like maskedlocation
, and you might have to add dictionaries for specialties (like medicine which does actually have its own dictionary).
However, I think what you'll find in this library is what can be achieved with the least amount of compromise.
I've used a set of rules (for detection/transformation) in combination with an exceptions list.
However, testing the library was more challenging than anticipated. If you have any case inaccuracy or false positives please submit an issue.
And of course, You can clone this repository, install mocha
and test it for yourself, and you'll see how it passes the 9900 tests successfully.
License
License: The MIT License (MIT) - Copyright (c) 2017 Alex Corvi