Home

Awesome

Clojars Project

Number Words

ZEN

Number Words will build numeric expressions for natural numbers, percentages and fractions. For example:

Supports multiple languages.

The implementation is based on ideas expressed in Generating Numerical Approximations.

Numerical Approximations

Numerical approximations are all over texts based on data:

- Water temperature is below 10C (input data would be 9.53C)
- A third of students failed the exam (34.3%)
- Q2 sales were over 1M$ (1,002,184 $)

Numeric data providing information about some metrics of interest is often a number with the precision we do not need. If we see 9.382%, the information we need is likely - almost 10% - instead of the precise number. Furthermore, different approximation strategies are often used in the report involving the same metrics. At the beginning of the report we might say almost 10% or "below 10%" while later in the text, we might choose a more precise expression - around 9.4%.

Number Words will help you build such numerical approximations. Making them available for the text generation systems.

Features

Number Words uses the following abstractions:

The result of actual value approximation to a given scale provides:

A full approximation result returns three such approximation data structures for a given value which is:

Lastly the number formatting can be specified:

Languages

Numeric approximation has two functionality points which are language dependent

Currently supported languages:

Usage

Number Words exposes approximation functionality through numeric-expression function which takes on the following parameters:

Installation

Number Words is available as a Maven artifact from Clojars.

Clojure

Leiningen

[ai.tokenmill.numberwords/numberwords "1.1.0"]

deps.edn

ai.tokenmill.numberwords/numberwords {:mvn/version "1.1.0"}

Usage example:

(require '[numberwords.core :as nw])

(numeric-expression 144 10 :en :numberwords.domain/around :numberwords.domain/words)
=>
"around one hundred forty"

(numeric-expression 144 10 :de :numberwords.domain/less :numberwords.domain/numbers)
=>
"weniger als 150"

;; with defaults
(numeric-expression 144 10)
=>
"around 140"

Java

Get a jar by building it with

clojure -A:uberjar

Or as a Maven dependency

<repository>
    <id>clojars.org</id>
    <url>http://clojars.org/repo</url>
</repository>
<dependency>
    <groupId>ai.tokenmill.numberwords</groupId>
    <artifactId>numberwords</artifactId>
    <version>1.1.0</version>
</dependency>

Usage example:

import ai.tokenmill.numberwords.NumberWords;

NumberWords nw = new NumberWords();
nw.numericExpression(1.22, 0.1, "en", "more", "numbers");

Configuration

Hedges, favorite numbers can be modified and new languages added via changes to a configuration file - resources/numwords.edn

{;;Configuration is structured by the language 
 :en {
      ;;Hedges section specifies which words are associated with given actual to given value relations
      :hedges {:equal  #{"exactly"}
               :around #{"around" "approximately" "about"}
               :more   #{"more than" "over"}
               :less   #{"less than" "under" "nearly"}}
      
      ;;Favourite numbers map a special number with its textual expressions
      :favorite-numbers {1/4  #{"a quarter" "a fourth"}
                         1/2  #{"a half"}}}}

License

Copyright © 2020 TokenMill UAB.

Distributed under the The Apache License, Version 2.0.


<span>Photo by <a href="https://unsplash.com/@amandagraphc?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Amanda Jones</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></span>