Git Product home page Git Product logo

words-to-numbers's Introduction

Words To Numbers

Convert words to numbers. Optionally fuzzy match the words to numbers.

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'); //100
wordsToNumbers('one hundred and five'); //105
wordsToNumbers('one hundred and twenty five'); //125
wordsToNumbers('four thousand and thirty'); //4030
wordsToNumbers('six million five thousand and two'); //6005002
wordsToNumbers('a thousand one hundred and eleven'); //1111
wordsToNumbers('twenty thousand five hundred and sixty nine'); //20569
wordsToNumbers('five quintillion'); //5000000000000000000
wordsToNumbers('one-hundred'); //100
wordsToNumbers('one-hundred and five'); //105
wordsToNumbers('one-hundred and twenty-five'); //125
wordsToNumbers('four-thousand and thirty'); //4030
wordsToNumbers('six-million five-thousand and two'); //6005002
wordsToNumbers('a thousand, one-hundred and eleven'); //1111
wordsToNumbers('twenty-thousand, five-hundred and sixty-nine'); //20569

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')) // 'there were 20569 X in the 5000000000000000000 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}); //100
wordsToNumbers('too thousant and fiev', {fuzzy: true}); //2005
wordsToNumbers('tree millyon sefen hunderd and twinty sex', {fuzzy: true}); //3000726

Decimal Points

import wordsToNumbers from 'words-to-numbers';
wordsToNumbers('ten point five'); //10.5
wordsToNumbers('three point one four one five nine two six'); //3.1415926

Ordinal Numbers

import wordsToNumbers from 'words-to-numbers';
wordsToNumbers('first'); //1
wordsToNumbers('second'); //2
wordsToNumbers('third'); //3
wordsToNumbers('fourteenth'); //14
wordsToNumbers('twenty fifth'); //25
wordsToNumbers('thirty fourth'); //34
wordsToNumbers('forty seventh'); //47
wordsToNumbers('fifty third'); //53
wordsToNumbers('sixtieth'); //60
wordsToNumbers('seventy second'); //72
wordsToNumbers('eighty ninth'); //89
wordsToNumbers('ninety sixth'); //96
wordsToNumbers('one hundred and eighth'); //108
wordsToNumbers('one hundred and tenth'); //110
wordsToNumbers('one hundred and ninety ninth'); //199

Commonjs

const { wordsToNumbers } = require('words-to-numbers');
wordsToNumbers('one hundred'); //100;

Implied Hundreds

wordsToNumbers('nineteen eighty four', { impliedHundreds: true }); //1984
wordsToNumbers('one thirty', { impliedHundreds: true }); //130
wordsToNumbers('six sixty two', { impliedHundreds: true }); //662
wordsToNumbers('ten twelve', { impliedHundreds: true }); //1012
wordsToNumbers('nineteen ten', { impliedHundreds: true }); //1910
wordsToNumbers('twenty ten', { impliedHundreds: true }); //2010
wordsToNumbers('twenty seventeen', { impliedHundreds: true }); //2017
wordsToNumbers('twenty twenty', { impliedHundreds: true }); //2020
wordsToNumbers('twenty twenty one', { impliedHundreds: true }); //2021
wordsToNumbers('fifty sixty three', { impliedHundreds: true }); //5063
wordsToNumbers('fifty sixty', { impliedHundreds: true }); //5060
wordsToNumbers('fifty sixty three thousand', { impliedHundreds: true }); //5063000
wordsToNumbers('one hundred thousand', { impliedHundreds: true }); //100000

words-to-numbers's People

Contributors

finnfiddle avatar james-frowen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

words-to-numbers's Issues

Replacement in strings is inconsistent and/or throws error

Looks like the replacement of numbers in an arbitrary string isn't working properly:

> wordsToNumbers('Fifty People, One Question: Brooklyn')
Line 1, col 1:
> 1 | Fifty
      ^
Expected end of input
Line 1, col 1:
> 1 | One
      ^
Expected end of input
'Fifty People, One Question: Brooklyn'

I would like to use this library to replace number-ish words to numerals in movie titles. These titles fail as well:

  • Model Fifty-One Fifty-Six
  • Fifty Million Frenchmen
  • A Thousand and One Wives
  • Ten Thousand Pictures of You

Seems to be an issue with case as well. e.g. the line below doesn't produce an error, but it doesn't replace the number as promised in the docs.

wordsToNumbers('Ten Thousand Pictures of You'.toLowerCase())
10000

Does not work with Uglify

as the title says, i tried using it with ionic framework and it compiles with uglify where it doesnt work!

uses a plugin that breaks node

sadly words-to-numbers uses "clj-fuzzy" a module that attaches a new function, called "s" to the Date prototype

this explains some of the other issues here, and it makes this module unusable, it breaks modules like 'later' and any other module that attaches a property called "s" on a Date (also a bad idea, but that doesn't interfere with anyone else)

the offending code in clj-fuzzy

Date.prototype.s = function (a, b) { return b instanceof Date && this.valueOf() === b.valueOf() }

thirty five thousand is coming back as 5030

With impliedHundreds set to false, i am getting the below translated wrong:

thirty five thousand is coming back as 5030
forty three thousand as 3040

This is pretty standard expression all over the world. Not working!

I am getting this error using it . clj_fuzzy is depricated , is there any other alternative?

Uncaught TypeError: Cannot use 'in' operator to search for 'clj_fuzzy' in undefined
at ea (clj-fuzzy.js:2)
at clj-fuzzy.js:5
at Object../node_modules/words-to-numbers/node_modules/clj-fuzzy/src-js/clj-fuzzy.js (clj-fuzzy.js:6)
at webpack_require (bootstrap:79)
at Object../node_modules/words-to-numbers/dist/fuzzy.js (fuzzy.js:7)
at webpack_require (bootstrap:79)
at Object../node_modules/words-to-numbers/dist/parser.js (parser.js:13)
at webpack_require (bootstrap:79)
at Object../node_modules/words-to-numbers/dist/index.js (index.js:8)
at webpack_require (bootstrap:79)

Parser has issues with converting spelled out phrases containing

If the target string for conversion contains a spelled out number flanked by the word dot, the parser is confusing 'dot' as a decimal and expects a numeral following the last dot. I'm guessing that is the reason :-)

To Reproduce:

$ node
> const words2num = require('words-to-numbers').wordsToNumbers;
> words2num('Dot two Dot')
TypeError: Cannot set property 'end' of undefined
    at matchRegions (/Users/gstone/hacking/node/testing_ground/node_modules/words-to-numbers/dist/parser.js:169:29)
    at exports.default (/Users/gstone/hacking/node/testing_ground/node_modules/words-to-numbers/dist/parser.js:229:17)
    at wordsToNumbers (/Users/gstone/hacking/node/testing_ground/node_modules/words-to-numbers/dist/index.js:21:38)
    at repl:1:1
    at ContextifyScript.Script.runInThisContext (vm.js:44:33)
    at REPLServer.defaultEval (repl.js:239:29)
    at bound (domain.js:301:14)
    at REPLServer.runBound [as eval] (domain.js:314:12)
    at REPLServer.onLine (repl.js:433:10)
    at emitOne (events.js:120:20)

Expected Result:
Dot 2 Dot

Note, replacing the last 'dot' with another word works fine.

> words2num('Dot two hello')
'Dot two hello'

IP Addresses
There seems to be other situations. For example, imagine ip addresses that previously have been converted to words using another library that parses numbers to words. So, 17.24.12.5 becomes seventeen dot twenty-four dot twelve dot five. Words-to-numbers does:

$ node
> const words2num = require('words-to-numbers').wordsToNumbers;
> words2num('seventeen dot twenty-four dot twelve dot five')
53.5

Looks like it ignored the first parsed 'dots' and added them. I get that in most cases this makes sense, but IP addresses should be special-cased. Perhaps an options flag to avoid the performance hit for the 99% of cases where it wont' be in the data set?

Needed something like this library

I recently needed something like this library. My use-case is converting numeric sentences from speech-recognition libraries (like PocketSphinx.js). Unfortunately, after surveying this library, I had several concerns and unaddressed wants:

  • Several open/unaddressed bugs reported and not triaged for quite awhile -- is this project still maintained?
  • A few known-unsupported conversions (listed in the tests)
  • Supporting a few looser forms of english number sentences
  • Exact outputs (via string) instead of as numbers (with floating point quirks)
  • Operate in a mode that errors if contents in the string aren't recognized as valid (such as "four thousand six million")
  • Ability to collect individual digits as a single number (like "one two three four" -> "1234")
  • Able to specify an optional formatter option: unit-place separator (,)
  • Zero-dependency lib that could run in the browser with no build-steps/etc
  • Wanted to expose an AST result from parsing such that other number formatting/localization would be possible as custom serialization from the AST
  • I did not need/want the fuzzy matching or the multiple numbers per string features -- wasn't clear if I could disable those?

In the end, I decided to implement a parser/compiler to suit my needs, rather than adapting this library from its current form. Just wanted to share my efforts in case they're useful to anyone (including this project):

https://gist.github.com/getify/83e6fae8f54c1d38f89132a130493f98

Implied hundreds, better support for hundred-thousands

Hi @finnfiddle! I'm considering using words-to-numbers instead of porting some code of mine from another language to JS. I thought you might be interested in these additional test cases I supported in my old code that maybe words-to-numbers could support as well.

One feature is "implied hundreds," because it's very common to omit the explicit "hundred" when speaking naturally in English. For example, in "nineteen eighty four", it's implied that the number is actually "nineteen hundred eighty four". Or when counting high numbers, it's often skipped for brevity: "one thirty one, one thirty two, one thirty three". More examples, with current behavior shown:

> wordsToNumbers('nineteen eighty four')
'19 84' // Expected: 1984 (number), instead got string with two numbers.

> wordsToNumbers('one thirty')
'1 30' // Expected: 130 (number), instead got string with two numbers.

> wordsToNumbers('six sixty two')
'6 62' // Expected: 662 (number), instead got string with two numbers.

> wordsToNumbers('ten twelve')
'10 12' // Expected: 1012 (number), instead got string with two numbers.

> wordsToNumbers('nineteen ten')
'19 10' // Expected: 1910 (number), instead got string with two numbers.

> wordsToNumbers('twenty ten')
30 // Expected: 2010 (number), instead got 20 + 10.

> wordsToNumbers('twenty seventeen')
37 // Expected: 2017 (number), instead got 20 + 17.

> wordsToNumbers('twenty twenty')
'20 20' // Expected: 2020 (number), instead got string with two numbers.

> wordsToNumbers('twenty twenty one')
'20 21' // Expected: 2021 (number), instead got string with two numbers.

> wordsToNumbers('fifty sixty three')
'50 63' // Expected: 5063 (number), instead got string with two numbers.

> wordsToNumbers('one thirty thousand')
'1 30000' // OK. Other magnitudes may not follow implied hundreds. Nobody talks this way.

> wordsToNumbers('nineteen eighty thousand')
'19 80000' // OK. Other magnitudes may not follow implied hundreds. Nobody talks this way.

The other feature is better support for hundred-thousands. Current behavior:

> wordsToNumbers('one hundred thousand')
'100 1000' // Expected: 100000 (number), instead got string with two numbers.

> wordsToNumbers('one hundred two thousand')
2100 // Expected: 102000 (number), instead got 100 + 2000.

> wordsToNumbers('one hundred and two thousand')
2100 // Same result with "and".

Happy to work on this in a PR if you're interested.

ohm-js missing from package.json

Looks like there is a required package ohm-js that is used inside your lib that is not inside your package.json. This causes it to fail when npm installed.

'four point six two' => 4.619999999999999

Love this project. Just ran into a few issues with decimals:

'four point six two' => 4.619999999999999

'forty point six two' => 40.620000000000005

'forty point sixty two' => 46.02

No matching version found for [email protected] when installing

getting an error when installing the package:

npm install --save words-to-numbers
npm WARN deprecated [email protected]: Custom ALPHABET bug fixed in v7.0.2
npm WARN deprecated [email protected]: This version is no longer maintained. Please upgrade to the latest version.
npm WARN deprecated [email protected]: All projects should upgrade to our universal JS notifier: "@bugsnag/js". See https://github.com/bugsnag/bugsnag-js/blob/master/UPGRADING.md for more details.
npm WARN deprecated [email protected]: This version is no longer maintained. Please upgrade to the latest version.
npm ERR! code ETARGET
npm ERR! notarget No matching version found for [email protected]
npm ERR! notarget In most cases you or one of your dependencies are requesting
npm ERR! notarget a package version that doesn't exist.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/h/.npm/_logs/2019-01-07T15_38_50_829Z-debug.log

Time to mark this repository as public archive?

No updates in 6 years and when running npm install you see:

75 vulnerabilities (2 low, 15 moderate, 39 high, 19 critical)

Also, the manifest file is not valid (no main or exports field) and as a result, it fails to import in ESM code.

wordsToNumbers('xxxxxxx one hundred') = null

Hi...

This is great package for word to number conversion.But is there possible that if there is word like xxxxx which is not related to any number is return same as it passed like....

Expected
wordsToNumbers('xxxxxxx one hundred') = xxxxxxx 100

Current output
wordsToNumbers('xxxxxxx one hundred') = null

Can any one help?

Word "and" and "a" maps to zero.

I don't know if this is intended but it seems to parse "and" and "a" when alone as numbers instead of seeing them as words.

When those words are alone they should just map to null as they are not numbers. I am using this in a project and it's throwing errors as these words are getting mapped to numbers.

I have a pull request which adds the tests as I work on getting it to pass them.

Windows 10 installation

Initially, I could not install this using npm on a windows 10 machine as it apparently requires 'bash'. Installing the windows linux subsystem and free Ubuntu distribution allowed the installation to succeed.

Any plans for a version that runs on windows without having to install the linux subsystem?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.