Words To Numbers
This repository was forked from
words-from-numbers because the
project seems abandoned. This fork:
- Fixes the manifest and makes it a pure ESM package
- Removes old dependencies and Babel transpilation
- Uses Vitest to replace old testing tools
- Upgrades clj-fuzzy to latest version
- Add prettier formatting
- Replace NPM with PNPM
- Add basis for Typescript conversion
I might convert the code to Typescript later. PRs are welcome.
Usage
npm install words-to-numbers
If the whole string passed is a number then it will return a Number
type
otherwise it will return the original string with all instances of numbers
replaced.
TODO: Add functionality for parsing mixed numbers and words. PRs welcome.
Basic Examples
import wordsToNumbers from "words-to-numbers";
wordsToNumbers("one hundred");
wordsToNumbers("one hundred and five");
wordsToNumbers("one hundred and twenty five");
wordsToNumbers("four thousand and thirty");
wordsToNumbers("six million five thousand and two");
wordsToNumbers("a thousand one hundred and eleven");
wordsToNumbers("twenty thousand five hundred and sixty nine");
wordsToNumbers("five quintillion");
wordsToNumbers("one-hundred");
wordsToNumbers("one-hundred and five");
wordsToNumbers("one-hundred and twenty-five");
wordsToNumbers("four-thousand and thirty");
wordsToNumbers("six-million five-thousand and two");
wordsToNumbers("a thousand, one-hundred and eleven");
wordsToNumbers("twenty-thousand, five-hundred and sixty-nine");
Multiple numbers in a string
Returns a string with all instances replaced.
wordsToNumbers('there were twenty-thousand, five-hundred and sixty-nine X in the five quintillion Y'))
With Fuzzy Matching
Uses
Jaro distance
to find the best match for the number words. Don't rely on this being completely
accurate...
import wordsToNumbers from "words-to-numbers";
wordsToNumbers("won huntred", { fuzzy: true });
wordsToNumbers("too thousant and fiev", { fuzzy: true });
wordsToNumbers("tree millyon sefen hunderd and twinty sex", { fuzzy: true });
Decimal Points
import wordsToNumbers from "words-to-numbers";
wordsToNumbers("ten point five");
wordsToNumbers("three point one four one five nine two six");
Ordinal Numbers
import wordsToNumbers from "words-to-numbers";
wordsToNumbers("first");
wordsToNumbers("second");
wordsToNumbers("third");
wordsToNumbers("fourteenth");
wordsToNumbers("twenty fifth");
wordsToNumbers("thirty fourth");
wordsToNumbers("forty seventh");
wordsToNumbers("fifty third");
wordsToNumbers("sixtieth");
wordsToNumbers("seventy second");
wordsToNumbers("eighty ninth");
wordsToNumbers("ninety sixth");
wordsToNumbers("one hundred and eighth");
wordsToNumbers("one hundred and tenth");
wordsToNumbers("one hundred and ninety ninth");
Implied Hundreds
wordsToNumbers("nineteen eighty four", { impliedHundreds: true });
wordsToNumbers("one thirty", { impliedHundreds: true });
wordsToNumbers("six sixty two", { impliedHundreds: true });
wordsToNumbers("ten twelve", { impliedHundreds: true });
wordsToNumbers("nineteen ten", { impliedHundreds: true });
wordsToNumbers("twenty ten", { impliedHundreds: true });
wordsToNumbers("twenty seventeen", { impliedHundreds: true });
wordsToNumbers("twenty twenty", { impliedHundreds: true });
wordsToNumbers("twenty twenty one", { impliedHundreds: true });
wordsToNumbers("fifty sixty three", { impliedHundreds: true });
wordsToNumbers("fifty sixty", { impliedHundreds: true });
wordsToNumbers("fifty sixty three thousand", { impliedHundreds: true });
wordsToNumbers("one hundred thousand", { impliedHundreds: true });