numwords

package module
v0.0.0-...-405f4a4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 10, 2020 License: MIT Imports: 6 Imported by: 1

README

numwords
Build Status GoDoc

numwords is a utility package for Go (golang) that converts natural language numbers to their actual numeric values. The numbers can be parsed out as strings, integers, or floats as desired.

func Example() {
  s := "I've got three apples and two and a half bananas"
  fmt.Println(ParseString(s))

  s = "My chili won second place at the county fair"
  fmt.Println(ParseString(s))

  i, _ := ParseInt("fourteen ninety two")
  fmt.Println(i)

  f, _ := ParseFloat("eight and three quarters")
  fmt.Println(f)

  // Output:
  // I've got 3 apples and 2.5 bananas
  // My chili won 2nd place at the county fair
  // 1492
  // 8.75
}

This package is largely inspired by the excellent Numerizer Ruby gem.

Some Valid Conversions

String Input Output Value
fifteen 15
twenty five 25
twenty-five 25
eleven hundred 1100
three hundred twenty five 325
three hundred thousand 300000
one hundred twenty one 121
fourteen hundred sixty seven 1467
nineteen eighty-eight 1988
nine hundred and ninety nine 999
a half 0.5
three halves 1.5
a quarter 0.25
three quarters 0.75
one ninth 0.111111
two thirds 0.666667
two and three eighths 2.375
zeroth 0th
twenty second 22nd
5 hundred 500
one million two hundred fifty thousand and seven 1250007

License

This package is released under the MIT License.

Documentation

Overview

Package numwords is a utility that converts textual numbers to their actual numeric values. The converted numbers can be parsed out as strings, integers, or floats as desired.

Source: https://github.com/rodaine/numwords

Example
s := "I've got three apples and two and a half bananas"
fmt.Println(ParseString(s))

s = "My chili won second place at the county fair"
fmt.Println(ParseString(s))

i, _ := ParseInt("fourteen ninety two")
fmt.Println(i)

f, _ := ParseFloat("eight and three quarters")
fmt.Println(f)
Output:

I've got 3 apples and 2.5 bananas
My chili won 2nd place at the county fair
1492
8.75

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoNumbers is returned if the a value for numbers is requested but there are
	// no number values inside of it
	ErrNoNumbers = errors.New("the input contains no number values")

	// ErrManyNumbers is returned if the value for numbers is requested but there are
	// more than one number values inside of it
	ErrManyNumbers = errors.New("the input contains more than one number")

	// ErrNonNumber is returned if ParseInt or ParseFloat encounters a non-number in
	// the input string.
	ErrNonNumber = errors.New("the string contains a non-number")
)

Functions

func IncludeSecond

func IncludeSecond(include bool)

IncludeSecond toggles whether or not "second" should be included in the interpreted words. If true "second" will be read as "2nd", otherwise the word will be ignored. The default is set to true.

Example
s := "My chili won second place at the county fair"
fmt.Println(ParseString(s))

s = "One second ago"
IncludeSecond(false)
fmt.Println(ParseString(s))
Output:

My chili won 2nd place at the county fair
1 second ago

func ParseFloat

func ParseFloat(s string) (float64, error)

ParseFloat reads a text string and converts it to its float value. An error is returned if the if the string cannot be resolved to a single float value.

Example
f, _ := ParseFloat("eight and three quarters")
fmt.Println(f)
Output:

8.75

func ParseInt

func ParseInt(s string) (int, error)

ParseInt reads a text string and converts it to its integer value. An error is returned if the if the string cannot be resolved to a single integer value. Fractional portions of the number will be truncated.

Example
i, _ := ParseInt("fourteen ninety two")
fmt.Println(i)
Output:

1492

func ParseString

func ParseString(s string) string

ParseString reads a text string and converts all numbers contained within to their appropriate values. Integers are preserved exactly while floating point numbers are limited to six decimal places. The rest of the string is preserved.

Example
s := "I've got three apples and two and a half bananas"
fmt.Println(ParseString(s))
Output:

I've got 3 apples and 2.5 bananas

func ParseStrings

func ParseStrings(in []string) []string

ParseStrings performs the same actions as ParseString but operates on a pre- sanitized and split string. This method is exposed for convenience if further processing of the string is required.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL