npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@alordash/parse-word-to-number

v3.0.7

Published

Extracts numbers written as words from string.

Downloads

49

Readme

@alordash/parse-word-to-number

$ npm i @alordash/parse-word-to-number

Description

Parses string and returns numbers written as words inside it.
It uses my realization of Damerau-Levenshtein algorithm to properly parse words even if they are written with mistakes.
Supports Russian and English language.

Usage

Function parseWord(string, errorLimit):{Array.<Number>}

Arguments

  1. string {String} — source string.
  2. errorLimit {Number} — From 0.0 to 1.0, the less — the less results. Used for recognizing words with mistakes.
    Parses all words in that string into numbers.
    Returns all found numbers.

Usage example:

const { parseWord } = require('@alordash/parse-word-to-number');

//Parse single word
let parsedWord = parseWord("twonty-one");
console.log(parsedWord[0].value);
//=> 20
console.log(parsedWord[1].value);
//=> 1

parsedWord = parseWord("читырэ");
console.log(parsedWord[0].value);
//=> 4

//You can specify mistakes multiplication from 0.0 and on with second argument, where
//0 — do not accept words with mistakes,
//1 — accept words if error < error limit for that word
//List of limits for all words is located in /lib/expressions/*.csv files
parsedWord = parseWord("hundrid", 1);
console.log(parserWord[0].value);
//=> 100

parsedWord = parseWord("hundrid", 0);
console.log(parserWord[0]);
//=> undefined

Function parseString(string, errorLimit):{String}

Arguments

  1. string {String} — source string.
  2. errorLimit {Number} — From 0.0 to 1.0, the less — the less results. Used for recognizing words with mistakes.
    Parses all words in that string into numbers and combines them.
    Returns string with parsed numbers.

Usage example:

const { parseString } = require('@alordash/parse-word-to-number');

console.log(parseString("four-huntred-sevinty-six balloons"));
//=> 476 balloons

console.log(parseString("двести дивяносто пять тысоч ложек сто восмьдесят три тарелки"));
//=> 295000 ложек 183 тарелки

//Mistakes multiplication
console.log(parseString("four-huntred-sevinty-six balloons", 0));
//=> 4 balloons

console.log(parseString("двести дивяносто пять тысоч ложек сто восмьдесят три тарелки", 0));
//=> 200 дивяносто 5 тысоч ложек 100 восмьдесят 3 тарелки

Getting array of ConvertedWords

Class ConvertedWord

class ConvertedWord {
   //@type {String}
   text;                    //Text of word
   //@type {Array.<Number>}
   indexes;                 //Indexes of used words from original string
}

Function arrayParseString(string, errorLimit):{Array.}

Works the same as parseString function and accepts same arguments, except it returns array of converted words.

Usage example:

const { parseString } = require('@alordash/parse-word-to-number');

let result = arrayParseString("four huntred sevinty-six balloons");
console.log(JSON.stringify(result));
//=> [{"text":"476","indexes":[0,1,2]},{"text":"balloons","indexes":[3]}]


result = arrayParseString("двести дивяносто пять тысоч ложек сто восмьдесят три тарелки");
console.log(JSON.stringify(result));
//=> [{"text":"295000","indexes":[0,1,2,3]},{"text":"ложек","indexes":[4]},{"text":"183","indexes":[5,6,7]},{"text":"тарелки","indexes":[8]}]

Adding custom expressions

You can add new expressions for parsing more cases by creating .csv file inside lib/expressions folder. Fill new .csv file with following format:

META;;;;
separators;;;;
%separators_symbols% (for example I'm using "-" as separator for English);;;;
text;value;multiply level;errors limit;rank
String;Number;Number;Number;Number

For better understanding see example .csv files located in lib/expressions folder.