Skip to content

Commit

Permalink
Bitesize formatting (#6)
Browse files Browse the repository at this point in the history
* feat: format numbers to bitesize representations (1000 –> 1k)
* refactor: split monolith approximation result into separate functions
* refactor: change interface, abandon functoin which does it all
* refactor: remove unused spec and functions
* refactor: simplify numeric relations calculation
* refactor: add specs to all interface functions
* test: update tests
* refactor: move spec to 'domain' ns; do not use sets expression config
* feat: add number conversion to a desired precision
* refactor: functions accept all config parameters at the end of the list
* feat: have one entry function with defaults
* refactor: update java interface
* doc: update documentation
* feat: add deploy task
  • Loading branch information
Žygimantas Medelis authored Nov 23, 2020
1 parent 450b993 commit 5acb5b0
Show file tree
Hide file tree
Showing 15 changed files with 344 additions and 276 deletions.
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ lint:

.PHONY: test
test:
clojure -A:test:runner
clojure -M:test:runner

.PHONY: uberjar
uberjar:
clj -A:uberjar
clj -M:uberjar

.PHONY: deploy
deploy:
clj -M:deploy
56 changes: 29 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,15 @@ The result of *actual value* approximation to a given scale provides:
* *Favorite Number* expresses some common language names for certain numbers. A `0.25` is a favorite number in that that it has the name - `a quarter`.

A full approximation result returns three such approximation data structures for a *given value* which is:
* **smaller** than the *actual value* on the scaled number range.
* **greater** than the *actual value* on the scaled number range.
* **around** the *actual value* on the scaled number range. For this a is chosen from the above two which is closer to the *actual value*.

* **less** than the *actual value* on the scaled number range.
* **more** than the *actual value* on the scaled number range.
* **around** the *actual value* on the scaled number range. For this 'less' or 'more' value closer to the *actual value* is chosen.

Lastly the number formatting can be specified:
* **words** - spell out the number in words (110 -> hundred and ten).
* **bites** - spell out the number using bite size style shortening (1022 -> 1k).
* **numbers** - report number as is.

## Languages

Numeric approximation has two functionality points which are language dependent
Expand All @@ -63,10 +68,12 @@ Currently supported languages:

## Usage

Number Words exposes approximation functionality through `approximations` function which takes on the following parameters:
* `language` - `:de` or `:en`
Number Words exposes approximation functionality through `numeric-expression` function which takes on the following parameters:
* `actual-value` - the number to approximate
* `scale` - at which the approximation is to be performed.
* `language` - use two letter language code (like :pt), default is :en
* `relation` - what kind of relation to between actual and given value to use (valid values specified in `:numberwords.domain/relation`)
* `formatting` - which number formatting should be used (valid values specified in `:numberwords.domain/formatting`)

### Installation

Expand All @@ -76,36 +83,32 @@ Number Words exposes approximation functionality through `approximations` functi

_Leiningen_
```
[ai.tokenmill.numberwords/numberwords "1.0.2"]
[ai.tokenmill.numberwords/numberwords "1.1.0"]
```

_deps.edn_
```
ai.tokenmill.numberwords/numberwords {:mvn/version "1.0.2"}
ai.tokenmill.numberwords/numberwords {:mvn/version "1.1.0"}
```

Usage example:

```
(require '[numberwords.core :as nw])
(nw/approximations :en 0.258 1/4)
(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)
=>
#:numwords{:around
#:numwords{:hedges #{"approximately" "about" "around"},
:text "zero point two five",
:given-value 1/4,
:favorite-number #{"a quarter"}},
:more-than
#:numwords{:hedges #{"over" "more than"},
:text "zero point two five",
:given-value 1/4,
:favorite-number #{"a quarter"}},
:less-than
#:numwords{:hedges #{"nearly" "under" "less than"},
:text "zero point five",
:given-value 1/2,
:favorite-number #{"a half"}}}
"weniger als 150"
;; with defaults
(numeric-expression 144 10)
=>
"around 140"
```

### Java
Expand All @@ -126,7 +129,7 @@ Or as a _Maven_ dependency
<dependency>
<groupId>ai.tokenmill.numberwords</groupId>
<artifactId>numberwords</artifactId>
<version>1.0.2-SNAPSHOT</version>
<version>1.1.0</version>
</dependency>
```

Expand All @@ -136,10 +139,9 @@ Usage example:
import ai.tokenmill.numberwords.NumberWords;
NumberWords nw = new NumberWords();
nw.approximations("en", 1.22, 0.1);
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`
Expand Down
6 changes: 3 additions & 3 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"-m" "ai.tokenmill.numberwords.NumberWords"
"--app-group-id" "ai.tokenmill.numberwords"
"--app-artifact-id" "numberwords"
"--app-version" "1.0.2"]}
:deploy {:extra-deps {deps-deploy {:mvn/version "RELEASE"}}
"--app-version" "1.1.0"]}
:deploy {:extra-deps {deps-deploy/deps-deploy {:mvn/version "RELEASE"}}
:main-opts ["-m" "deps-deploy.deps-deploy" "deploy"
"target/numberwords-1.0.2.jar"]}
"target/numberwords-1.1.0.jar"]}
:test {:extra-paths ["test"]
:extra-deps {org.clojure/test.check {:mvn/version "RELEASE"}}}
:runner {:extra-deps {com.cognitect/test-runner
Expand Down
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<artifactId>numberwords</artifactId>

<version>1.0.2</version>
<version>1.1.0</version>

<name>numberwords</name>

Expand Down Expand Up @@ -94,13 +94,19 @@
</repository>

</repositories>

<distributionManagement>

<repository>

<id>clojars</id>

<name>Clojars repository</name>

<url>https://clojars.org/repo</url>

</repository>

</distributionManagement>

<scm>
Expand Down
84 changes: 42 additions & 42 deletions resources/numwords.edn
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
{:en {:hedges {:equal #{"exactly"}
:around #{"around" "approximately" "about"}
:more #{"more than" "over"}
:less #{"less than" "under" "nearly"}}
:favorite-numbers {1/4 #{"a quarter"}
1/3 #{"a third"}
1/2 #{"a half"}}}
{:en {:hedges {:equal ["exactly"]
:around ["around" "approximately" "about"]
:more ["more than" "over"]
:less ["less than" "under" "nearly"]}
:favorite-numbers {1/4 ["a quarter"]
1/3 ["a third"]
1/2 ["a half"]}}

:de {:hedges {:equal #{"genau"}
:around #{"ungefähr" "etwa"}
:more #{"mehr als" "über"}
:less #{"weniger als" "unter"}}
:favorite-numbers {1/4 #{"Viertel"}
1/3 #{"Drittel"}
1/2 #{"Hälfte"}}}
:de {:hedges {:equal ["genau"]
:around ["ungefähr" "etwa"]
:more ["mehr als" "über"]
:less ["weniger als" "unter"]}
:favorite-numbers {1/4 ["Viertel"]
1/3 ["Drittel"]
1/2 ["Hälfte"]}}

:pt {:hedges {:equal #{"exatamente"}
:around #{"cerca de" "aproximadamente"}
:more #{"mais de" "acima de"}
:less #{"menos de" "abaixo de"}}
:favorite-numbers {1/4 #{"um quarto"}
1/3 #{"um terço"}
1/2 #{"metade"}}}
:pt {:hedges {:equal ["exatamente"]
:around ["cerca de" "aproximadamente"]
:more ["mais de" "acima de"]
:less ["menos de" "abaixo de"]}
:favorite-numbers {1/4 ["um quarto"]
1/3 ["um terço"]
1/2 ["metade"]}}

:lt {:hedges {:equal #{"lygiai"}
:around #{"apie" "apytiksliai"}
:more #{"daugiau nei" "virš"}
:less #{"mažiau nei" "mažiau"}}
:favorite-numbers {1/4 #{"ketvirtis"}
1/3 #{"trečdalis"}
1/2 #{"pusė"}}}
:lt {:hedges {:equal ["lygiai"]
:around ["apie" "apytiksliai"]
:more ["daugiau nei" "virš"]
:less ["mažiau nei" "mažiau"]}
:favorite-numbers {1/4 ["ketvirtis"]
1/3 ["trečdalis"]
1/2 ["pusė"]}}

:fr {:hedges {:equal #{"exactement"}
:around #{"environ"}
:more #{"plus de"}
:less #{"moins de"}}
:favorite-numbers {1/4 #{"un quart"}
1/3 #{"un tiers"}
1/2 #{"un demi"}}}
:fr {:hedges {:equal ["exactement"]
:around ["environ"]
:more ["plus de"]
:less ["moins de"]}
:favorite-numbers {1/4 ["un quart"]
1/3 ["un tiers"]
1/2 ["un demi"]}}

:ru {:hedges {:equal #{"ровно"}
:around #{"около" "примерно"}
:more #{"больше чем" "более"}
:less #{"меньше чем" "менее"}}
:favorite-numbers {1/4 #{"четверть"}
1/3 #{"треть"}
1/2 #{"пол" "половина"}}}
:ru {:hedges {:equal ["ровно"]
:around ["около" "примерно"]
:more ["больше чем" "более"]
:less ["меньше чем" "менее"]}
:favorite-numbers {1/4 ["четверть"]
1/3 ["треть"]
1/2 ["пол" "половина"]}}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns numberwords.number-ops)
(ns numberwords.approx-math)

(defn nat-num? [x] (not (neg? x)))

Expand All @@ -25,3 +25,17 @@
[(rationalize (- rational-av m))
(rationalize (- (+ rational-av scale) m))]))

(defn distances-from-edges
[actual-value scale]
(let [[start end] (bounding-box actual-value scale)]
[[start (delta start actual-value)]
[end (delta end actual-value)]]))

(defn unreliable?
"Actual value and scale values which will generate unreliable results:
- actual-value much bigger that the scale
- scale is bigger than the actual value"
[actual-value scale]
;; 1000x difference between the scale and value is a random choice
(or (< 1000 (/ actual-value scale))
(and (< 1 scale) (< actual-value scale))))
4 changes: 4 additions & 0 deletions src/numberwords/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
:favorite-numbers (fn [given-value]
(get-in config [:favorite-numbers given-value]))})))

(defn numwords-config []
(with-open [r (io/reader (io/resource "numwords.edn"))]
(edn/read (PushbackReader. r))))

(defn supported-langauges []
(with-open [r (io/reader (io/resource "numwords.edn"))]
(-> (PushbackReader. r)
Expand Down
Loading

0 comments on commit 5acb5b0

Please sign in to comment.