/word2number

Library to convert words (three hundred thousand) to numbers (300,000) in Go

Primary LanguageGo

word2number

This is a library to convert words (three hundred-thousand) to numbers (300,000) in Go

Installation

Go get the package:

go get github.com/donna-legal/word2number

and add it as an import:

import "github.com/donna-legal/word2number"

Usage

package main

import (
    "fmt"
    "github.com/donna-legal/word2number"
)

func main() {
    converter, err := word2number.NewConverter("en")
    if err != nil {
        panic(err)
    }
    var f float64
    f = converter.Words2Number("two-thousand seventy-five")
    fmt.Println(f) // should return 2075
    f = converter.Words2Number("one-million two hundred thousand")
    fmt.Println(f) // should return 1200000
}

Now and the future

Look in the test cases what works and what doesn't. Most things to the left of the decimal point should work.

Needs improvement:

  • Decimal numbers. The simpler cases work just fine (eg. one point three hundredths = 1.03), but there are quite a few failing test cases
  • Things like One point two billion doesn't check out at the moment either.

Extensions:

  • More languages. It would be somewhat easy to extend with other languages that are constructed the same way, like Swedish or Spanish.
  • Misspellings. One could extend the resources with common misspellings.
  • Concatenations. Some people might write seventyfive instead of seventy-five. This is definitely necessary for Spanish and Swedish.

Contribute

If you use this program and find short-comings. Please start a fork or add issues. We appreciate it deeply.