├── doc └── intro.md ├── .gitignore ├── project.clj ├── deps.edn ├── CHANGELOG.md ├── src └── word_conversion │ ├── dictionaries.clj │ ├── core.clj │ └── journal.clj ├── README.md ├── test └── word_conversion │ └── core_test.clj └── LICENSE /doc/intro.md: -------------------------------------------------------------------------------- 1 | # Introduction to word-conversion 2 | 3 | TODO: write [great documentation](http://jacobian.org/writing/what-to-write/) 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | profiles.clj 5 | pom.xml 6 | pom.xml.asc 7 | *.jar 8 | *.class 9 | /.lein-* 10 | /.nrepl-port 11 | .hgignore 12 | .hg/ 13 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject word-conversion "0.1.0" 2 | :description "Convert numberical representation of numbers to British English" 3 | :url "https://github.com/practicalli/numbers-to-words" 4 | :license {:name "Creative Commons Attribution Share-Alike 4.0 International" 5 | :url "https://creativecommons.org"} 6 | :dependencies [[org.clojure/clojure "1.10.1"]] 7 | :repl-options {:init-ns word-conversion.core}) 8 | -------------------------------------------------------------------------------- /deps.edn: -------------------------------------------------------------------------------- 1 | {:paths 2 | ["src" "resources"] 3 | 4 | :deps 5 | {org.clojure/clojure {:mvn/version "1.10.1"}} 6 | 7 | :aliases 8 | {:test 9 | {:extra-paths ["test"] 10 | :extra-deps {org.clojure/test.check {:mvn/version "1.1.0"}}} 11 | 12 | :runner 13 | {:extra-deps {com.cognitect/test-runner 14 | {:git/url "https://github.com/cognitect-labs/test-runner" 15 | :sha "6ec7f8eef509cd14d831e8cc16e856b31327a862"}} 16 | :main-opts ["-m" "cognitect.test-runner" 17 | "-d" "test"]} 18 | 19 | :jar 20 | {:extra-deps {seancorfield/depstar {:mvn/version "1.1.104"}} 21 | :main-opts ["-m" "hf.depstar.jar" "esrever.jar"]}}} 22 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/). 3 | 4 | ## [Unreleased] 5 | ### Changed 6 | - Add a new arity to `make-widget-async` to provide a different widget shape. 7 | 8 | ## [0.1.1] - 2019-04-01 9 | ### Changed 10 | - Documentation on how to make the widgets. 11 | 12 | ### Removed 13 | - `make-widget-sync` - we're all async, all the time. 14 | 15 | ### Fixed 16 | - Fixed widget maker to keep working when daylight savings switches over. 17 | 18 | ## 0.1.0 - 2019-04-01 19 | ### Added 20 | - Files from the new template. 21 | - Widget maker public API - `make-widget-sync`. 22 | 23 | [Unreleased]: https://github.com/your-name/word-conversion/compare/0.1.1...HEAD 24 | [0.1.1]: https://github.com/your-name/word-conversion/compare/0.1.0...0.1.1 25 | -------------------------------------------------------------------------------- /src/word_conversion/dictionaries.clj: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; Dictionaries for Number to Word Conversion 3 | ;; 4 | ;; Dictionaries used as lookup tables to convert numbers into their 5 | ;; respective word representations 6 | ;; 7 | ;; Author(s): John Stevenson 8 | ;; Date created: 7th April 2019 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | (ns word-conversion.dictionaries) 12 | 13 | 14 | ;; Specific number lookups 15 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 16 | 17 | (def digit->word 18 | "Lookup for single digit words and number levels hundred, thousand, etc. 19 | Not used for numbers between 10 and 99." 20 | {\0 "zero" 21 | \1 "one" 22 | \2 "two" 23 | \3 "three" 24 | \4 "four" 25 | \5 "five" 26 | \6 "six" 27 | \7 "seven" 28 | \8 "eight" 29 | \9 "nine"}) 30 | 31 | (def teens->word 32 | {"10" "ten" 33 | "11" "eleven" 34 | "12" "twelve" 35 | "13" "thirteen" 36 | "14" "fourteen" 37 | "15" "fifteen" 38 | "16" "sixteen" 39 | "17" "seventeen" 40 | "18" "eighteen" 41 | "19" "nineteen"}) 42 | 43 | (def tens->word 44 | {\2 "twenty" 45 | \3 "thirty" 46 | \4 "forty" 47 | \5 "fifty" 48 | \6 "sixty" 49 | \7 "seventy" 50 | \8 "eighty" 51 | \9 "ninety"}) 52 | 53 | 54 | 55 | ;; previous approach using strings 56 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 57 | 58 | 59 | ;; zero to nine 60 | #_(def single-digit 61 | {"0" "zero" 62 | "1" "one" 63 | "2" "two" 64 | "3" "three" 65 | "4" "four" 66 | "5" "five" 67 | "6" "six" 68 | "7" "seven" 69 | "8" "eight" 70 | "9" "nine"}) 71 | 72 | #_(def teens 73 | {"10" "ten" 74 | "11" "eleven" 75 | "12" "twelve" 76 | "13" "thirteen" 77 | "14" "fourteen" 78 | "15" "fifteen" 79 | "16" "sixteen" 80 | "17" "seventeen" 81 | "18" "eighteen" 82 | "19" "nineteen"}) 83 | 84 | ;; tens 85 | #_(def tens 86 | {"10" "ten" 87 | "20" "twenty" 88 | "30" "thirty" 89 | "40" "forty" 90 | "50" "fifty" 91 | "60" "sixty" 92 | "70" "seventy" 93 | "80" "eighty" 94 | "90" "ninety"}) 95 | 96 | ;; hundreds (all the same, but a bit of an edge case) 97 | #_(def hundreds 98 | {"100" "one hundred" 99 | "200" "two hundred" 100 | "300" "three hundred" 101 | "400" "four hundred" 102 | "500" "five hundred" 103 | "600" "six hundred" 104 | "700" "seven hundred" 105 | "800" "eight hundred" 106 | "900" "nine hundred"}) 107 | 108 | 109 | 110 | 111 | ;; Generic Number levels 112 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 113 | ;; A dictionary of number levels 114 | ;; Indexed by the size of the number as a string 115 | ;; 100 has 3 characters, 1000000 has 7 characters, etc. 116 | 117 | ;; hundreds, thousands and millions all the same term regardless 118 | ;; of prefix number (one thousand, two thousand, etc.) 119 | ;; simply add number level as post-fix 120 | 121 | #_(def number-levels 122 | "List of number levels." 123 | {3 "hundred" 4 "thousand" 6 "hundred thousand" 7 "million" 10 "billion" 13 "trillion"}) 124 | 125 | 126 | #_(def generic-number-levels 127 | {"x00" "hundred" 128 | "x000" "thousand" 129 | "x000000" "million" 130 | "x000000000" "billion" 131 | "x000000000000" "trillion"}) 132 | 133 | 134 | ;; Basic error warnings 135 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 136 | 137 | #_(def invalid-number 138 | {"-1" "Computer says no!"}) 139 | 140 | 141 | #_(def british-english-dictionary 142 | "Combination of dictionaries that include British English lookup 143 | values" 144 | (merge single-digit teens tens hundreds generic-number-levels)) 145 | -------------------------------------------------------------------------------- /src/word_conversion/core.clj: -------------------------------------------------------------------------------- 1 | (ns word-conversion.core 2 | (:require [word-conversion.dictionaries :as dictionary])) 3 | 4 | 5 | 6 | (defn- hyphenated-pound-words 7 | "Convert two word numbers to a hyphenated word. 8 | 9 | If the last two numbers of each three groups of numbers in pounds 10 | are not zero, then hyphenate those two words. 11 | 12 | Example: 79.00 => seventy-nine 13 | 14 | Arguments: Vector of strings (representing numbers grouped in threes) 15 | Return: Vector of strings" 16 | 17 | ^:grammar-rule 18 | 19 | [number-triple] 20 | 21 | number-triple) 22 | 23 | 24 | (defn- andify-word-group 25 | " " 26 | ^:grammar-rule 27 | [] 28 | ) 29 | 30 | 31 | 32 | (defn partition-number-string 33 | "Partition a string representing a whole number into groups 34 | (group-size of 3 to represent how we speak numbers). Grouping 35 | is from the right hand side. 36 | 37 | If the string number is divisible by group size without remainder, then we 38 | can just partition. 39 | 40 | Otherwise calculate the remainder of dividing the number by group-size, 41 | take the remainder number of elements from the head of the number and 42 | use partion-all, and partition the rest. Then combine the results of the two. 43 | 44 | Precondition: number must be 0 or greater 45 | Arguments: String representing a number 46 | Return: Sequence of one or more sequences of characters" 47 | 48 | [number-string] 49 | 50 | (let [group-size 3 51 | number-size (count number-string) 52 | quotient (quot number-size group-size) 53 | remainder (rem number-size group-size)] 54 | 55 | (if (= 0 remainder) 56 | (partition group-size number-string) 57 | 58 | ;; If the partition is smaller than 3, then pad with \0 to make 59 | ;; processing conversion rules consistent for each partition 60 | (concat 61 | (map #(concat (repeat (- group-size remainder) \0) %) 62 | (partition-all group-size (take remainder number-string))) 63 | (partition group-size (drop remainder number-string)))))) 64 | 65 | 66 | (defn character->number-word 67 | "Convert a sequence of numbers to their word equivalents in a given 68 | dictionary 69 | 70 | Arguments: hash-map dictionary, vector of strings (representing numbers) 71 | Return: Sequence of strings (each string is a word representing a number)" 72 | 73 | [dictionary character] 74 | 75 | (get dictionary character)) 76 | 77 | 78 | (defn character-sequence->word-sequence 79 | "Rules: apply specific look-up for tens and ones combination 80 | - ten = \0 then lookup digit in digits dictionary (a digit is whole numbers 0-9), dont return anything for tens 81 | - if ten = \1 then combine ten and digit and lookup in tens dictionary (could refine this around teens) 82 | - if ten >= \2 then lookup ten in tens dictionary and digit in digits dictionary 83 | - lookup hundred in digits dictionary and post-fix hundred " 84 | [dictionary character-sequence] 85 | (let [[hundred ten digit] character-sequence] 86 | 87 | (cond 88 | (= \0 ten) (map #(character->number-word dictionary/digit->word %) character-sequence) 89 | (= \1 ten) (list (character->number-word dictionary/digit->word hundred) 90 | (character->number-word dictionary/teens->word (str ten digit))) 91 | :else (list (character->number-word dictionary/digit->word hundred) 92 | (character->number-word dictionary/tens->word ten) 93 | (character->number-word dictionary/digit->word digit))))) 94 | 95 | 96 | 97 | (for [x-y [[4.6 48.9] [3.5 2.2]] 98 | :let [co-ord (map str (flatten [4.6 48.9]))]] 99 | (clojure.string/join ", " co-ord)) 100 | 101 | 102 | 103 | 104 | (reduce str (clojure.string/join ", " (flatten [4.6 48.9]))) 105 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # word-conversion 2 | 3 | A Clojure library to convert numerical representation of whole numbers to British English representation, using correct basic grammar. 4 | 5 | 6 | ## Usage 7 | 8 | As this is a library and does not include a user interface. Open a REPL and load the namespace then evaluate the examples and experiment with the design journal section. 9 | 10 | ### Command Line / Terminal 11 | In a terminal window, change to the root of the `word-conversion` project and run the command `lein repl`. Once the repl starts either include the project namespace in the current `user` namespace, using `(require 'word-conversion.core)` 12 | 13 | ### Clojure aware editors 14 | Or open this project in your favourite editor, run a repl and switch to the `word-conversion.core` namespace. 15 | 16 | ### main function 17 | 18 | Use the `speak-number-as-words` to convert a number to a sentence that describes that word. For example: 19 | 20 | ```clojure 21 | (speak-number-as-words british-english-dictionary 1105) 22 | 23 | ``` 24 | 25 | ## License 26 | 27 | Creative Commons Attribution Share-Alike 4.0 International 28 | 29 | Copyright © 2019 John Stevenson 30 | 31 | 32 | ## Number to Words Conversion 33 | 34 | ### Problem Description 35 | Create a Clojure library, suitable for use in a server-side application,that can take a Java int in the range 0 to 999,999,999 inclusive andreturns the equivalent number, as a String, in British English words. 36 | 37 | ### Sample Data 38 | 39 | | Input | Output | 40 | |-----------|----------------------------------------------------------------------------------------------------------| 41 | | 0 | zero | 42 | | 1 | one | 43 | | 21 | twenty one | 44 | | 105 | one hundred and five | 45 | | 123 | one hundred and twenty three | 46 | | 1005 | one thousand and five | 47 | | 1042 | one thousand and forty two | 48 | | 1105 | one thousand one hundred and five | 49 | | 56945781 | fifty six million nine hundred and forty five thousand seven hundred and eighty one | 50 | | 999999999 | nine hundred and ninety nine million nine hundred and ninety nine thousand nine hundred and ninety nine | 51 | 52 | 53 | ### Guidelines 54 | 55 | ●The solution must be correct. 56 | Please pay attention to the specific conventions of British English, particularly concerning the use of ‘and’between certain phrases. 57 | 58 | ●The solution is not expected to involve a command line or GUI application 59 | – we’re looking for a Clojure library that could be packaged as a jar andused in a larger application. 60 | 61 | 62 | 63 | ## Refactored algorithm 64 | - take a dictionary and a number 65 | - convert number to string 66 | - split into individual digits (characters) of that number 67 | - partition from right into groups of threes (partition-all where there are less than 3 digits) 68 | - process each partition of three (hundreds, tens, ones) 69 | - convert partitions from digits (characters) to words (keep return values in same groups) 70 | -- apply specific look-up for tens and ones combination 71 | --- if ten = \0 then lookup digit in digits dictionary (a digit is whole numbers 0-9), dont return anything for tens 72 | --- if ten = \1 then combine ten and digit and lookup in tens dictionary (could refine this around teens) 73 | --- if ten >= \2 then lookup ten in tens dictionary and digit in digits dictionary 74 | -- lookup hundred in digits dictionary and post-fix hundred 75 | - apply relevant grammar rules 76 | -- andify 77 | --- any number after one hundred requires an `and` 78 | --- no and after thousand if there is a hundred value or no value at all 79 | -- hyphenate 80 | - add number levels based on groups, each group after the first is a higher number level (thousand, million, billion) 81 | - turn in to a string sentence 82 | 83 | where to apply the dictionary? 84 | 85 | ### andify-sentence 86 | 87 | one hundred 88 | one hundred and one 89 | one hundred and ten 90 | one hundred and eleven 91 | ... 92 | any number after one hundred requires an and 93 | 94 | 95 | one thousand 96 | one thousand and one 97 | one thousand and ten 98 | one thousand one hundred 99 | one thousand one hundred and one 100 | one thousand one hundred and ten 101 | one thousand nine hundred and ninety nine 102 | ... 103 | no and after thousand if there is a hundred value or no value at all 104 | 105 | the same for million, billion, trillion 106 | 107 | one hundred thousand 108 | one hundred and one thousand 109 | one hundred and ten thousand 110 | one hundred thousand and one 111 | one hundred thousand and ten 112 | one hundred thousand one hundred 113 | one hundred thousand one hundred and one 114 | one hundred thousand one hundred and ten 115 | one hundred and one thousand one hundred and ten 116 | 117 | 118 | ## Original Analysis of the problem statement 119 | 120 | Thoughts about the solution before a personal matter interrupted solving the solution. 121 | 122 | ### Notes 123 | * the British English words are all lower case, no capitalisation is required for the start of each sentence. 124 | 125 | 126 | ### Initial thoughts for a solution 127 | 128 | Taking a simplest set first approach: 129 | 130 | #### Convert each word to a string using a simple lookup 131 | 132 | Define a dictionary that maps numbers 0 to 9 to their word equivalents. 133 | 134 | 135 | #### Insert the number scales in the correct places 136 | 137 | Insert `hundred`, `thousand`, and `million` number scale names into the words. 138 | 139 | > Wondering if this needs to be done first or at least at the same time as converting from numbers to strings. 140 | 141 | 142 | #### Insert the correct grammar into the words (and) 143 | 144 | Parse a collection of words inserting `and` at the appropriate point in the sentence. The rules seem to be positional, so we can just process the word strings for the correct places to insert `and`. 145 | 146 | The rules seem to be: 147 | - numbers between 101 and 999 have `and` after the first number (or before the last two numbers). 148 | - the above rule should be applied all at each number scale for numbers larger than 1001 149 | 150 | 151 | ### Additional thoughts ### 152 | 153 | #### Splitting up a number into digits #### 154 | 155 | How can we split up a number into its digits? Unlike string, a number is not seen as a collection. We can convert each number to a string, this would allow us to split the number into individual digits, although if using clojure.core functions then the string will be treated as a collection of characters. Can we convert characters back to numbers (should we need to)? Strings can be converted back to numbers easily with the `java.lang.Integer` class. 156 | 157 | If we use strings or characters in our dictionary lookup, then we don't need to convert back. 158 | 159 | Some experiments in partitioning are in the design journal. 160 | 161 | 162 | #### replace each number with its whole value #### 163 | 164 | If we took each digit in the original number and converted it to its representative number in terms of position, then the dictionary lookup becomes much simpler. 165 | 166 | For example, if the original number is 12345, then we would first generate a sequence of `[10000 2000 300 40 5]`. 167 | 168 | This approach seems to be pretty obvious, although we still need to group along number levels, for numbers such as 124,110 (One hundred and twenty four thousand, one hundred and ten) 169 | 170 | 171 | #### Break down numbers into their smallest parts 172 | 173 | Replacing the whole number with a value to represent each digit is close to how I want to solve this problem. However, for larger numbers they still need to be broken down further and have some notation to represent their number level. Then it is just a simple matter of using a dictionary to map over the individual numbers and levels to convert it to words. 174 | 175 | Some post processing on the converted sequence adds grammar correction to the words and gives a sentence by injecting `and` at the relevant place. This seems to be after any instance of `hundred` which is followed by another number. 176 | 177 | 178 | ## Interesting functions to consider 179 | 180 | `partition` will group consistently and provide an simple way to insert new strings into the right parts of the words. 181 | -------------------------------------------------------------------------------- /test/word_conversion/core_test.clj: -------------------------------------------------------------------------------- 1 | (ns word-conversion.core-test 2 | (:require [clojure.test :refer [deftest is testing]] 3 | [word-conversion.core :as SUT] 4 | [word-conversion.dictionaries :as dictionary])) 5 | 6 | ;; Testing approach 7 | ;; `deftest` each public function in a namespace 8 | ;; `deftest-` each function that will be private (development only) 9 | ;; `testing` different aspects of a specific function 10 | ;; `is` assertion with intent in string 11 | 12 | 13 | (deftest- partition-number-string-test 14 | "Convert a number string into groups of three, from right hand side, 15 | to model the structure of English words that represent numbers" 16 | 17 | (testing "Convert string number to a 3 digit character sequence, 18 | along the number levels for thousand, million, billion, etc." 19 | (is (= '((\0 \0 \0)) 20 | (SUT/partition-number-string "0")) 21 | "Testing number scales - one") 22 | (is (= '((\0 \2 \1)) 23 | (SUT/partition-number-string "21")) 24 | "Testing number scales - ten") 25 | (is (= '((\3 \2 \1)) 26 | (SUT/partition-number-string "321")) 27 | "Testing number scales - hundred") 28 | (is (= '((\0 \0 \4) (\3 \2 \1)) 29 | (SUT/partition-number-string "4321")) 30 | "Testing number scales - thousand") 31 | (is (= '((\0 \5 \4) (\3 \2 \1)) 32 | (SUT/partition-number-string "54321")) 33 | "Testing number scales - ten thousand") 34 | (is (= '((\6 \5 \4) (\3 \2 \1)) 35 | (SUT/partition-number-string "654321")) 36 | "Testing number scales - hundred thousand") 37 | (is (= '((\0 \0 \7) (\6 \5 \4) (\3 \2 \1)) 38 | (SUT/partition-number-string "7654321")) 39 | "Testing number scales - million") 40 | (is (= '((\0 \8 \7) (\6 \5 \4) (\3 \2 \1)) 41 | (SUT/partition-number-string "87654321")) 42 | "Testing number scales - ten million") 43 | (is (= '((\9 \8 \7) (\6 \5 \4) (\3 \2 \1)) 44 | (SUT/partition-number-string "987654321")) 45 | "Testing number scales - hundred million") 46 | (is (= '((\1 \0 \0) (\0 \0 \0) (\0 \0 \0)) 47 | (SUT/partition-number-string "100000000")) 48 | "Testing number scales - billion") 49 | ) 50 | ;; Move defensive programming higher up the function chain 51 | #_(testing "Defensive programming tests" 52 | (is (thrown? java.lang.AssertionError 53 | (SUT/partition-number-string "-1")) 54 | "Defensive: checking for out of bounds handling"))) 55 | 56 | (deftest- character->number-word-test 57 | (testing "Edge cases" 58 | (is (= "zero" 59 | (SUT/character->number-word dictionary/digit->word \0)) 60 | "Edge case - lower bound test for single character") 61 | (is (= "nine" 62 | (SUT/character->number-word dictionary/digit->word \9)) 63 | "Edge case - upper bound test for single character"))) 64 | 65 | 66 | (deftest- character-sequence->word-sequence-test 67 | "Test the conversion of a sequence of characters (\3 \2 \1) to 68 | a sequence of strings of number words (three twenty one)." 69 | 70 | (testing "Digits to number words" 71 | (is (= '("zero" "zero" "zero") 72 | (SUT/character-sequence->word-sequence dictionary/digit->word '(\0 \0 \0)))) 73 | (is (= '("zero" "zero" "one") 74 | (SUT/character-sequence->word-sequence dictionary/digit->word '(\0 \0 \1))))) 75 | 76 | (testing "Tens to number words" 77 | (is (= '("zero" "ten") 78 | (SUT/character-sequence->word-sequence dictionary/digit->word '(\0 \1 \0)))) 79 | (is (= '("zero" "eleven") 80 | (SUT/character-sequence->word-sequence dictionary/digit->word '(\0 \1 \1)))) 81 | (is (= '("zero" "twenty" "zero") 82 | (SUT/character-sequence->word-sequence dictionary/digit->word '(\0 \2 \0)))) 83 | (is (= '("zero" "twenty""one") 84 | (SUT/character-sequence->word-sequence dictionary/digit->word '(\0 \2 \1)))) 85 | (is (= '("zero" "forty" "two") 86 | (SUT/character-sequence->word-sequence dictionary/digit->word '(\0 \4 \2))))) 87 | 88 | (testing "Hundreds to number words" 89 | (is (= '("one" "zero" "five") 90 | (SUT/character-sequence->word-sequence dictionary/digit->word '(\1 \0 \5)))) 91 | (is (= '("one" "twenty" "three") 92 | (SUT/character-sequence->word-sequence dictionary/digit->word '(\1 \2 \3)))))) 93 | 94 | 95 | 96 | 97 | 98 | 99 | (deftest development-unit-tests 100 | "Tests created to develop the solution. 101 | These are testing the actual implementation of the algorithms uses, 102 | so should eventually be replaced by namespace api tests which test 103 | the intent and context of the namespace and not a specific implementation." 104 | 105 | (partition-number-string-test) 106 | (character->number-word-test) 107 | (character-sequence->word-sequence-test)) 108 | 109 | 110 | 111 | #_(testing "Thousands to number words" 112 | (is (= "one thousand and five" 113 | (SUT/character-sequence->word-sequence dictionary/digit->word 1005))) 114 | (is (= "one thousand and forty two" 115 | (SUT/character-sequence->word-sequence dictionary/digit->word 1042))) 116 | (is (= "one thousand one hundred and five" 117 | (SUT/character-sequence->word-sequence dictionary/digit->word 1105)))) 118 | 119 | #_(testing "Millions to number words" 120 | (is (= "fifty six million nine hundred and fourty five thousand seven hundred and eighty one" 121 | (SUT/character-sequence->word-sequence dictionary/digit->word 56945781))) 122 | (is (= "nine hundred and ninety nine million nine hundred and ninety nine thousand 123 | nine hundred and ninety nine" 124 | (SUT/character-sequence->word-sequence dictionary/digit->word 999999999)))) 125 | 126 | 127 | 128 | 129 | #_(deftest cheque-numbers->words-test 130 | "Converting numbers on a cheque to words" 131 | 132 | (testing "convert-cheque simple number conversion" 133 | (is (= "seventy-nine pounds and six pence" 134 | (SUT/convert-cheque 79.06))) )) 135 | 136 | 137 | 138 | #_(deftest number->british-english-test 139 | "Test the conversion of numerical whole numbers to 140 | strings containing British English words as a sentence" 141 | 142 | (testing "Speaking the words as numbers" 143 | (is (= "zero" 144 | (SUT/speak-number-as-words british-english-dictionary 0))) 145 | (is (= "one" 146 | (SUT/speak-number-as-words british-english-dictionary 1))) 147 | (is (= "twenty one" 148 | (SUT/speak-number-as-words british-english-dictionary 21))) 149 | (is (= "one hundred and five" 150 | (SUT/speak-number-as-words british-english-dictionary 105))) 151 | (is (= "one hundred and twenty three" 152 | (SUT/speak-number-as-words british-english-dictionary 123))) 153 | (is (= "one thousand and five" 154 | (SUT/speak-number-as-words british-english-dictionary 1005))) 155 | (is (= "one thousand and forty two" 156 | (SUT/speak-number-as-words british-english-dictionary 1042))) 157 | (is (= "one thousand one hundred and five" 158 | (SUT/speak-number-as-words british-english-dictionary 1105))) 159 | (is (= "fifty six million nine hundred and fourty five thousand seven hundred and eighty one" 160 | (SUT/speak-number-as-words british-english-dictionary 56945781))) 161 | (is (= "nine hundred and ninety nine million nine hundred and ninety nine thousand 162 | nine hundred and ninety nine" 163 | (SUT/speak-number-as-words british-english-dictionary 999999999)))) 164 | ) 165 | 166 | 167 | ;; Sample data test cases 168 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 169 | 170 | ;; | Input | Output | 171 | ;; |-----------|----------------------------------------------------------------------------------------------------------| 172 | ;; | 0 | zero | 173 | ;; | 1 | one | 174 | ;; | 21 | twenty one | 175 | ;; | 105 | one hundred and five | 176 | ;; | 123 | one hundred and twenty three | 177 | ;; | 1005 | one thousand and five | 178 | ;; | 1042 | one thousand and forty two | 179 | ;; | 1105 | one thousand one hundred and five | 180 | ;; | 56945781 | fifty six million nine hundred and forty five thousand seven hundred and eighty one | 181 | ;; | 999999999 | nine hundred and ninety nine million nine hundred and ninety nine thousand nine hundred and ninety nine | 182 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Eclipse Public License - v 2.0 2 | 3 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE 4 | PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION 5 | OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 6 | 7 | 1. DEFINITIONS 8 | 9 | "Contribution" means: 10 | 11 | a) in the case of the initial Contributor, the initial content 12 | Distributed under this Agreement, and 13 | 14 | b) in the case of each subsequent Contributor: 15 | i) changes to the Program, and 16 | ii) additions to the Program; 17 | where such changes and/or additions to the Program originate from 18 | and are Distributed by that particular Contributor. A Contribution 19 | "originates" from a Contributor if it was added to the Program by 20 | such Contributor itself or anyone acting on such Contributor's behalf. 21 | Contributions do not include changes or additions to the Program that 22 | are not Modified Works. 23 | 24 | "Contributor" means any person or entity that Distributes the Program. 25 | 26 | "Licensed Patents" mean patent claims licensable by a Contributor which 27 | are necessarily infringed by the use or sale of its Contribution alone 28 | or when combined with the Program. 29 | 30 | "Program" means the Contributions Distributed in accordance with this 31 | Agreement. 32 | 33 | "Recipient" means anyone who receives the Program under this Agreement 34 | or any Secondary License (as applicable), including Contributors. 35 | 36 | "Derivative Works" shall mean any work, whether in Source Code or other 37 | form, that is based on (or derived from) the Program and for which the 38 | editorial revisions, annotations, elaborations, or other modifications 39 | represent, as a whole, an original work of authorship. 40 | 41 | "Modified Works" shall mean any work in Source Code or other form that 42 | results from an addition to, deletion from, or modification of the 43 | contents of the Program, including, for purposes of clarity any new file 44 | in Source Code form that contains any contents of the Program. Modified 45 | Works shall not include works that contain only declarations, 46 | interfaces, types, classes, structures, or files of the Program solely 47 | in each case in order to link to, bind by name, or subclass the Program 48 | or Modified Works thereof. 49 | 50 | "Distribute" means the acts of a) distributing or b) making available 51 | in any manner that enables the transfer of a copy. 52 | 53 | "Source Code" means the form of a Program preferred for making 54 | modifications, including but not limited to software source code, 55 | documentation source, and configuration files. 56 | 57 | "Secondary License" means either the GNU General Public License, 58 | Version 2.0, or any later versions of that license, including any 59 | exceptions or additional permissions as identified by the initial 60 | Contributor. 61 | 62 | 2. GRANT OF RIGHTS 63 | 64 | a) Subject to the terms of this Agreement, each Contributor hereby 65 | grants Recipient a non-exclusive, worldwide, royalty-free copyright 66 | license to reproduce, prepare Derivative Works of, publicly display, 67 | publicly perform, Distribute and sublicense the Contribution of such 68 | Contributor, if any, and such Derivative Works. 69 | 70 | b) Subject to the terms of this Agreement, each Contributor hereby 71 | grants Recipient a non-exclusive, worldwide, royalty-free patent 72 | license under Licensed Patents to make, use, sell, offer to sell, 73 | import and otherwise transfer the Contribution of such Contributor, 74 | if any, in Source Code or other form. This patent license shall 75 | apply to the combination of the Contribution and the Program if, at 76 | the time the Contribution is added by the Contributor, such addition 77 | of the Contribution causes such combination to be covered by the 78 | Licensed Patents. The patent license shall not apply to any other 79 | combinations which include the Contribution. No hardware per se is 80 | licensed hereunder. 81 | 82 | c) Recipient understands that although each Contributor grants the 83 | licenses to its Contributions set forth herein, no assurances are 84 | provided by any Contributor that the Program does not infringe the 85 | patent or other intellectual property rights of any other entity. 86 | Each Contributor disclaims any liability to Recipient for claims 87 | brought by any other entity based on infringement of intellectual 88 | property rights or otherwise. As a condition to exercising the 89 | rights and licenses granted hereunder, each Recipient hereby 90 | assumes sole responsibility to secure any other intellectual 91 | property rights needed, if any. For example, if a third party 92 | patent license is required to allow Recipient to Distribute the 93 | Program, it is Recipient's responsibility to acquire that license 94 | before distributing the Program. 95 | 96 | d) Each Contributor represents that to its knowledge it has 97 | sufficient copyright rights in its Contribution, if any, to grant 98 | the copyright license set forth in this Agreement. 99 | 100 | e) Notwithstanding the terms of any Secondary License, no 101 | Contributor makes additional grants to any Recipient (other than 102 | those set forth in this Agreement) as a result of such Recipient's 103 | receipt of the Program under the terms of a Secondary License 104 | (if permitted under the terms of Section 3). 105 | 106 | 3. REQUIREMENTS 107 | 108 | 3.1 If a Contributor Distributes the Program in any form, then: 109 | 110 | a) the Program must also be made available as Source Code, in 111 | accordance with section 3.2, and the Contributor must accompany 112 | the Program with a statement that the Source Code for the Program 113 | is available under this Agreement, and informs Recipients how to 114 | obtain it in a reasonable manner on or through a medium customarily 115 | used for software exchange; and 116 | 117 | b) the Contributor may Distribute the Program under a license 118 | different than this Agreement, provided that such license: 119 | i) effectively disclaims on behalf of all other Contributors all 120 | warranties and conditions, express and implied, including 121 | warranties or conditions of title and non-infringement, and 122 | implied warranties or conditions of merchantability and fitness 123 | for a particular purpose; 124 | 125 | ii) effectively excludes on behalf of all other Contributors all 126 | liability for damages, including direct, indirect, special, 127 | incidental and consequential damages, such as lost profits; 128 | 129 | iii) does not attempt to limit or alter the recipients' rights 130 | in the Source Code under section 3.2; and 131 | 132 | iv) requires any subsequent distribution of the Program by any 133 | party to be under a license that satisfies the requirements 134 | of this section 3. 135 | 136 | 3.2 When the Program is Distributed as Source Code: 137 | 138 | a) it must be made available under this Agreement, or if the 139 | Program (i) is combined with other material in a separate file or 140 | files made available under a Secondary License, and (ii) the initial 141 | Contributor attached to the Source Code the notice described in 142 | Exhibit A of this Agreement, then the Program may be made available 143 | under the terms of such Secondary Licenses, and 144 | 145 | b) a copy of this Agreement must be included with each copy of 146 | the Program. 147 | 148 | 3.3 Contributors may not remove or alter any copyright, patent, 149 | trademark, attribution notices, disclaimers of warranty, or limitations 150 | of liability ("notices") contained within the Program from any copy of 151 | the Program which they Distribute, provided that Contributors may add 152 | their own appropriate notices. 153 | 154 | 4. COMMERCIAL DISTRIBUTION 155 | 156 | Commercial distributors of software may accept certain responsibilities 157 | with respect to end users, business partners and the like. While this 158 | license is intended to facilitate the commercial use of the Program, 159 | the Contributor who includes the Program in a commercial product 160 | offering should do so in a manner which does not create potential 161 | liability for other Contributors. Therefore, if a Contributor includes 162 | the Program in a commercial product offering, such Contributor 163 | ("Commercial Contributor") hereby agrees to defend and indemnify every 164 | other Contributor ("Indemnified Contributor") against any losses, 165 | damages and costs (collectively "Losses") arising from claims, lawsuits 166 | and other legal actions brought by a third party against the Indemnified 167 | Contributor to the extent caused by the acts or omissions of such 168 | Commercial Contributor in connection with its distribution of the Program 169 | in a commercial product offering. The obligations in this section do not 170 | apply to any claims or Losses relating to any actual or alleged 171 | intellectual property infringement. In order to qualify, an Indemnified 172 | Contributor must: a) promptly notify the Commercial Contributor in 173 | writing of such claim, and b) allow the Commercial Contributor to control, 174 | and cooperate with the Commercial Contributor in, the defense and any 175 | related settlement negotiations. The Indemnified Contributor may 176 | participate in any such claim at its own expense. 177 | 178 | For example, a Contributor might include the Program in a commercial 179 | product offering, Product X. That Contributor is then a Commercial 180 | Contributor. If that Commercial Contributor then makes performance 181 | claims, or offers warranties related to Product X, those performance 182 | claims and warranties are such Commercial Contributor's responsibility 183 | alone. Under this section, the Commercial Contributor would have to 184 | defend claims against the other Contributors related to those performance 185 | claims and warranties, and if a court requires any other Contributor to 186 | pay any damages as a result, the Commercial Contributor must pay 187 | those damages. 188 | 189 | 5. NO WARRANTY 190 | 191 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, AND TO THE EXTENT 192 | PERMITTED BY APPLICABLE LAW, THE PROGRAM IS PROVIDED ON AN "AS IS" 193 | BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR 194 | IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF 195 | TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR 196 | PURPOSE. Each Recipient is solely responsible for determining the 197 | appropriateness of using and distributing the Program and assumes all 198 | risks associated with its exercise of rights under this Agreement, 199 | including but not limited to the risks and costs of program errors, 200 | compliance with applicable laws, damage to or loss of data, programs 201 | or equipment, and unavailability or interruption of operations. 202 | 203 | 6. DISCLAIMER OF LIABILITY 204 | 205 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, AND TO THE EXTENT 206 | PERMITTED BY APPLICABLE LAW, NEITHER RECIPIENT NOR ANY CONTRIBUTORS 207 | SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 208 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION LOST 209 | PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 210 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 211 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 212 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE 213 | POSSIBILITY OF SUCH DAMAGES. 214 | 215 | 7. GENERAL 216 | 217 | If any provision of this Agreement is invalid or unenforceable under 218 | applicable law, it shall not affect the validity or enforceability of 219 | the remainder of the terms of this Agreement, and without further 220 | action by the parties hereto, such provision shall be reformed to the 221 | minimum extent necessary to make such provision valid and enforceable. 222 | 223 | If Recipient institutes patent litigation against any entity 224 | (including a cross-claim or counterclaim in a lawsuit) alleging that the 225 | Program itself (excluding combinations of the Program with other software 226 | or hardware) infringes such Recipient's patent(s), then such Recipient's 227 | rights granted under Section 2(b) shall terminate as of the date such 228 | litigation is filed. 229 | 230 | All Recipient's rights under this Agreement shall terminate if it 231 | fails to comply with any of the material terms or conditions of this 232 | Agreement and does not cure such failure in a reasonable period of 233 | time after becoming aware of such noncompliance. If all Recipient's 234 | rights under this Agreement terminate, Recipient agrees to cease use 235 | and distribution of the Program as soon as reasonably practicable. 236 | However, Recipient's obligations under this Agreement and any licenses 237 | granted by Recipient relating to the Program shall continue and survive. 238 | 239 | Everyone is permitted to copy and distribute copies of this Agreement, 240 | but in order to avoid inconsistency the Agreement is copyrighted and 241 | may only be modified in the following manner. The Agreement Steward 242 | reserves the right to publish new versions (including revisions) of 243 | this Agreement from time to time. No one other than the Agreement 244 | Steward has the right to modify this Agreement. The Eclipse Foundation 245 | is the initial Agreement Steward. The Eclipse Foundation may assign the 246 | responsibility to serve as the Agreement Steward to a suitable separate 247 | entity. Each new version of the Agreement will be given a distinguishing 248 | version number. The Program (including Contributions) may always be 249 | Distributed subject to the version of the Agreement under which it was 250 | received. In addition, after a new version of the Agreement is published, 251 | Contributor may elect to Distribute the Program (including its 252 | Contributions) under the new version. 253 | 254 | Except as expressly stated in Sections 2(a) and 2(b) above, Recipient 255 | receives no rights or licenses to the intellectual property of any 256 | Contributor under this Agreement, whether expressly, by implication, 257 | estoppel or otherwise. All rights in the Program not expressly granted 258 | under this Agreement are reserved. Nothing in this Agreement is intended 259 | to be enforceable by any entity that is not a Contributor or Recipient. 260 | No third-party beneficiary rights are created under this Agreement. 261 | 262 | Exhibit A - Form of Secondary Licenses Notice 263 | 264 | "This Source Code may also be made available under the following 265 | Secondary Licenses when the conditions for such availability set forth 266 | in the Eclipse Public License, v. 2.0 are satisfied: {name license(s), 267 | version(s), and exceptions or additional permissions here}." 268 | 269 | Simply including a copy of this Agreement, including this Exhibit A 270 | is not sufficient to license the Source Code under Secondary Licenses. 271 | 272 | If it is not possible or desirable to put the notice in a particular 273 | file, then You may include the notice in a location (such as a LICENSE 274 | file in a relevant directory) where a recipient would be likely to 275 | look for such a notice. 276 | 277 | You may add additional accurate notices of copyright ownership. 278 | -------------------------------------------------------------------------------- /src/word_conversion/journal.clj: -------------------------------------------------------------------------------- 1 | (ns word-conversion.journal 2 | (:require [word-conversion.core :refer :all] 3 | [word-conversion.dictionaries :refer :all])) 4 | 5 | 6 | 7 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 8 | ;; REPL design journal 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | ;; Convert numbers to words using a hash-map dictionary 12 | ;; convert word using dictionary 13 | #_(digit->word 0 british-english-numbers) 14 | 15 | 16 | ;; Splitting up a number into individual parts 17 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 18 | ;; We cant partition a number so lets change it to a string. A string is a collection of chars after all. 19 | 20 | ;; convert to a string 21 | #_(str 24) 22 | ;; => "24" 23 | 24 | #_(partition 1 "1234") 25 | ;; => ((\1) (\2) (\3) (\4)) 26 | 27 | 28 | ;; Using for just splits to individual words 29 | #_(defn partition-number 30 | [numbers] 31 | (for [number numbers] 32 | [(Integer. (str number))])) 33 | 34 | #_(partition-number "1234") 35 | ;; => ([1] [2] [3] [4]) 36 | 37 | 38 | ;; create a sequence of strings representing numbers 39 | #_(clojure.string/split "24" #"") 40 | ;; => ["2" "4"] 41 | 42 | ;; We loose some of the sense of number level though. 43 | ;; Is 2 the value two or is it twenty? 44 | ;; We would need to track where we are in the sequence, relative to the end 45 | 46 | ;; recursive function to create a sequence of positional numbers 47 | ;;;;;;;;;;;;;;;;;;;;;;;; 48 | ;; get the first value 49 | ;; map (constantly 0) over the rest 50 | ;; conjoin 51 | ;; recur... 52 | 53 | #_(defn positional-numbers-recursive 54 | [numbers] 55 | (let [string-numbers (str numbers)] 56 | (conj 57 | (map (constantly 0) (rest string-numbers)) 58 | (first string-numbers)))) 59 | 60 | #_(positional-numbers-recursive 21) 61 | ;; => (\2 0) 62 | 63 | ;; still not quite the right format 64 | 65 | #_(conj (first "2345") (map (constantly "0") (rest "2345"))) 66 | 67 | 68 | #_(map (constantly "0") (rest "2345")) 69 | ;; => ("0" "0" "0") 70 | 71 | 72 | 73 | #_(mapcat (constantly "0") (rest "2345")) 74 | ;; => (\0 \0 \0) 75 | 76 | 77 | #_(merge [] (first "2345") (map (constantly "0") (rest "2345"))) 78 | ;; => [\2 ("0" "0" "0")] 79 | 80 | 81 | #_(cons (first "2345") (map (constantly "0") (rest "2345"))) 82 | ;; => (\2 "0" "0" "0") 83 | 84 | #_(apply str 85 | (cons (first "2345") (map (constantly "0") (rest "2345")))) 86 | ;; => "2000" 87 | 88 | #_(defn rounded-number-string 89 | "Round a number down to its number level. 90 | 91 | Examples: 2345 becomes 2000" 92 | [number-string] 93 | (apply str 94 | (cons (first number-string) 95 | (map (constantly "0") (rest number-string))))) 96 | 97 | #_(defn number-sequence 98 | "Convert a number into a sequence of numbers that also represent the 99 | number level" 100 | [number] 101 | {:pre [(<= 0 number)]} 102 | (loop [current-string (str number) 103 | sequence-of-numbers []] 104 | (if (empty? current-string) 105 | sequence-of-numbers 106 | (recur (rest current-string) 107 | (conj sequence-of-numbers 108 | (rounded-number-string current-string)))))) 109 | 110 | #_(number-sequence 23456) 111 | ;; => ["20000" "3000" "400" "50" "6"] 112 | 113 | ;; If called with a negative number, the function should return an error 114 | 115 | #_(number-sequence -1) 116 | ;; java.lang.AssertionError 117 | ;; Assert failed: (<= 0 number) 118 | 119 | ;; Limitation 120 | ;; there is a bit of a gotcha in that if there is a zero value part way through a number, 121 | ;; then we would get 00 000 0000 etc. 122 | 123 | ;; We can just filter out all numbers that start with a zero, 124 | ;; as they would not be pronounced 125 | ;; would need to add a check for a single number in a sequence that is zero 126 | 127 | #_(defn cclean-number-sequencelean-number-sequence 128 | "All number strings should start with something other than zero" 129 | [number-string] 130 | (filter #(not= \0 (first %)) number-string)) 131 | ;; => #'word-conversion.core/clean-number-sequence 132 | 133 | #_(number-sequence 1024) 134 | ;; => ["1000" "000" "20" "4"] 135 | 136 | #_(clean-number-sequence (number-sequence 1024)) 137 | ;; => ("1000" "20" "4") 138 | 139 | 140 | ;; Add some defensive coding for a single zero sequence 141 | #_(defn clean-number-sequence 142 | "All number strings should start with something other than zero" 143 | [number-string] 144 | (if (= ["0"] number-string) 145 | ["0"] 146 | (filter #(not= \0 (first %)) number-string))) 147 | ;; => #'word-conversion.core/clean-number-sequence 148 | 149 | #_(clean-number-sequence ["0"]) 150 | ;; => ["0"] 151 | 152 | 153 | ;; defining dictionaries 154 | ;;;;;;;;;;;;; 155 | 156 | ;; zero to nine 157 | #_(def single-digit 158 | {"0" "zero" 159 | "1" "one" 160 | "2" "two" 161 | "3" "three" 162 | "4" "four" 163 | "5" "five" 164 | "6" "six" 165 | "7" "seven" 166 | "8" "eight" 167 | "9" "nine"}) 168 | 169 | #_(def teens 170 | {"11" "eleven" 171 | "12" "twelve" 172 | "13" "thirteen" 173 | "14" "fourteen" 174 | "15" "fifteen" 175 | "16" "sixteen" 176 | "17" "seventeen" 177 | "18" "eighteen" 178 | "19" "nineteen"}) 179 | 180 | ;; tens 181 | #_(def tens 182 | {"10" "ten" 183 | "20" "twenty" 184 | "30" "thirty" 185 | "40" "forty" 186 | "50" "fifty" 187 | "60" "sixty" 188 | "70" "seventy" 189 | "80" "eighty" 190 | "90" "ninety"}) 191 | 192 | ;; hundreds, thousands and millions - no difference in numbers, 193 | ;; simply add level as postfix 194 | 195 | ;; create a sequential list of number levels 196 | #_(def number-levels 197 | "List of number levels." 198 | ["hundred" "thousand" "hundred thousand" "million" "billion" "trillion"]) 199 | 200 | 201 | ;; Alternative idea: 202 | ;; seems like many more combinations will need to be defined within dictionaries. 203 | #_(def number-levels 204 | {2 ["two" "twenty" "two hundred" "two thousand" "two hundred thousand" "two million"]}) 205 | 206 | #_(def number-word-dictionary 207 | (merge single-digit teens tens)) 208 | 209 | 210 | #_(map #(get number-word-dictionary %) (word-sequence 42)) 211 | ;; => ("forty" "two") 212 | 213 | 214 | #_(map #(get number-word-dictionary %) (word-sequence 2)) 215 | ;; => ("two") 216 | 217 | ;; numbers larger than 100 218 | ;;;;;;;;;;;;;;;;;;;;;;;; 219 | 220 | ;; If a number is larger than a hundred, we can just use the first digit 221 | ;; as a word and post-fix with the relevant number level (hundred, 222 | ;; thousand, etc.) 223 | 224 | #_(word-sequence 101) 225 | ;; => ["100" "00" "1"] 226 | 227 | #_(clean-word-sequence 228 | (word-sequence 101)) 229 | ;; => ("100" "1") 230 | 231 | ;; If we count the number of digits in a stringified number, 232 | ;; then we get a consistent number level. 233 | 234 | #_(count "200") 235 | ;; => 3 236 | 237 | ;; we can define a dictionary that is a lookup for number levels, 238 | ;; based on the string size 239 | 240 | #_(def number-levels 241 | "List of number levels." 242 | {3 "hundred" 4 "thousand" 6 "hundred thousand" 7 "million" 10 "billion" 13 "trillion"}) 243 | 244 | 245 | (map (fn [positional-number] 246 | (if (<= (count positional-number) 2) 247 | (get british-english-dictionary positional-number) 248 | (str (get british-english-dictionary (str (first positional-number))) 249 | " " 250 | (get number-levels (count positional-number))))) 251 | ["100"]) 252 | 253 | 254 | (map (fn [positional-number] 255 | (if (<= (count positional-number) 2) 256 | (get british-english-dictionary positional-number) 257 | (str (get british-english-dictionary (str (first positional-number))) 258 | " " 259 | (get number-levels (count positional-number))))) 260 | ["100000"]) 261 | ;; => ("one hundred thousand") 262 | 263 | 264 | ;; solution so far does not work for 10,000 265 | ;; so we need to add more logic to capture this case 266 | (map (fn [positional-number] 267 | (if (<= (count positional-number) 2) 268 | (get british-english-dictionary positional-number) 269 | (str (get british-english-dictionary (str (first positional-number))) 270 | " " 271 | (get number-levels (count positional-number))))) 272 | ["10000"]) 273 | ;; => ("one ") 274 | 275 | 276 | (map (fn [positional-number] 277 | (if (<= (count positional-number) 2) 278 | (get british-english-dictionary positional-number) 279 | (str (get british-english-dictionary (str (first positional-number))) 280 | " " 281 | (get number-levels (count positional-number))))) 282 | ["10000"]) 283 | 284 | 285 | ;; get the number level 286 | ;; 10000 => thousand 287 | 288 | ;; drop those numbers that represent the number level 289 | ;; 10000 => 10 290 | 291 | (apply str 292 | (take (rem (count "10000") 3) "10000")) 293 | ;; => "10" 294 | 295 | ;; convert number after drop 296 | ;; 10 => "ten" 297 | 298 | (defn get-prefix-number 299 | "Gets the prefix of a number without the number level. 300 | 301 | Number levels go up in groups of three, so " 302 | [number-string] 303 | (apply str 304 | (take (rem (count number-string) 3) number-string))) 305 | 306 | (get-prefix-number "1") 307 | ;; => "1" 308 | (get-prefix-number "10") 309 | ;; => "10" 310 | (get-prefix-number "100") 311 | ;; => "" 312 | (get-prefix-number "1000") 313 | ;; => "1" 314 | (get-prefix-number "10000") 315 | ;; => "10" 316 | (get-prefix-number "100000") 317 | ;; => "" 318 | (get-prefix-number "1000000") 319 | ;; => "1" 320 | (get-prefix-number "10000000") 321 | ;; => "10" 322 | (get-prefix-number "100000000") 323 | ;; => "" 324 | 325 | ;; Partitioning 326 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 327 | 328 | (clojure.string/reverse "1234567") 329 | ;; => "7654321" 330 | 331 | (partition-all 332 | 3 333 | (clojure.string/reverse "1234567")) 334 | ;; => ((\7 \6 \5) (\4 \3 \2) (\1)) 335 | 336 | (defn partition-string-number 337 | [number-string] 338 | (partition-all 339 | 3 340 | (clojure.string/reverse number-string))) 341 | 342 | (partition-string-number "12001") 343 | ;; => ((\1 \0 \0) (\2 \1)) 344 | 345 | 346 | #_(partition-all 3 (word-sequence 1042)) 347 | ;; => (("1000" "000" "40") ("2")) 348 | 349 | 350 | ;; seems like we need to reverse the sequence to partition correctly 351 | 352 | #_(reverse 353 | (word-sequence 1042)) 354 | ;; => ("2" "40" "000" "1000") 355 | 356 | #_(partition-all 3 357 | (reverse 358 | (word-sequence 1042))) 359 | ;; => (("2" "40" "000") ("1000")) 360 | 361 | #_(reverse 362 | (partition-all 3 363 | (reverse 364 | (word-sequence 1042)))) 365 | ;; => (("1000") ("2" "40" "000")) 366 | 367 | ;; a double reverse seems ugly and its also reversing the elements 368 | ;; in each partition 369 | 370 | 371 | 372 | #_(count (word-sequence 1042)) 373 | ;; => 4 374 | 375 | #_(mod 376 | (count (word-sequence 1042)) 377 | 3) 378 | ;; => 1 379 | 380 | 381 | #_(quot 382 | (count (word-sequence 1042)) 383 | 3) 384 | ;; => 1 385 | 386 | #_(rem 387 | (count (word-sequence 1042)) 388 | 3) 389 | ;; => 1 390 | 391 | 392 | 393 | #_(word-sequence 56945781) 394 | ;; => ["50000000" "6000000" "900000" "40000" "5000" "700" "80" "1"] 395 | 396 | #_(partition-all 3 397 | (word-sequence 56945781)) 398 | ;; => (("50000000" "6000000" "900000") ("40000" "5000" "700") ("80" "1")) 399 | 400 | 401 | #_(quot 402 | (count (word-sequence 56945781)) 403 | 3) 404 | ;; => 2 405 | 406 | #_(rem 407 | (count (word-sequence 56945781)) 408 | 3) 409 | ;; => 2 410 | 411 | 412 | 413 | 414 | ;; Abstractions - map-indexed 415 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 416 | ;; alternative approach for positional-number-string 417 | 418 | 419 | (map-indexed (fn [index item] [index item]) "12345") 420 | ;; => ([0 \1] [1 \2] [2 \3] [3 \4] [4 \5]) 421 | 422 | (map-indexed (fn [index item] [(inc index) item]) "12345") 423 | ;; => ([1 \1] [2 \2] [3 \3] [4 \4] [5 \5]) 424 | 425 | 426 | (map-indexed (fn [index item] [item (inc index)]) "12345") 427 | ;; => ([\1 1] [\2 2] [\3 3] [\4 4] [\5 5]) 428 | 429 | ;; now the position is associated, we can use that to pad numbers 430 | ;; if we count the size of the number string, we can subtract 431 | ;; the position from the count and add that many zeros to the end of the number 432 | 433 | 434 | (map (fn [[item index]] [item (- 5 index)]) 435 | '([\1 1] [\2 2] [\3 3] [\4 4] [\5 5])) 436 | ;; => ([\1 4] [\2 3] [\3 2] [\4 1] [\5 0]) 437 | 438 | 439 | ;; expand on this example as the basis of defining a function 440 | (let [number "12345" 441 | size (count number) 442 | index (map-indexed (fn [index item] [item (inc index)]) number)] 443 | (map 444 | (fn [[digit position]] 445 | [digit (- size position)]) 446 | index)) 447 | ;; => ([\1 4] [\2 3] [\3 2] [\4 1] [\5 0]) 448 | 449 | 450 | ;; create the padding using repeat (join the stings after) 451 | (take 4 (repeat "0")) 452 | ;; => ("0" "0" "0" "0") 453 | 454 | 455 | (let [number "12345" 456 | size (count number) 457 | index (map-indexed (fn [index item] [item (inc index)]) number)] 458 | (map 459 | (fn [[digit position]] 460 | 461 | (apply str digit 462 | (repeat (- size position) "0"))) 463 | index)) 464 | 465 | 466 | (defn positional-number-string-map-index 467 | [number-string] 468 | (let [size (count number-string) 469 | index (map-indexed (fn [index item] [item (inc index)]) number-string)] 470 | (map 471 | (fn [[digit position]] 472 | 473 | (apply str digit 474 | (repeat (- size position) "0"))) 475 | index)) ) 476 | 477 | 478 | 479 | ;; cl-format 480 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 481 | 482 | #_(require '[clojure.pprint :refer [cl-format]]) 483 | 484 | #_(defn int-word [n] (cl-format nil "~r" n)) 485 | 486 | #_(int-word 23010) 487 | ;; => "twenty-three thousand, ten" 488 | 489 | 490 | 491 | 492 | 493 | 494 | ;; Depreciated functions 495 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 496 | 497 | (defn digit->word 498 | "Converts a numeric whole number (digit) into a word representation 499 | Returns: java.lang.string" 500 | 501 | ^:depreciated 502 | 503 | [digit dictionary] 504 | 505 | (get dictionary digit)) 506 | 507 | 508 | 509 | (defn split-larger-numbers 510 | "Any number string larger than 99 should be split into its parts. 511 | 512 | For example: 200 should be [2 x00], 2000 should be [2 x000] 513 | 514 | Arguments: string (representing a number) 515 | Return: vector of one or more strings" 516 | 517 | ^:depreciated 518 | 519 | [number-string] 520 | 521 | (let [size (count number-string)] 522 | (cond 523 | (> 3 size) [number-string] 524 | (= 3 size) [(str (first number-string))"x00"] 525 | (= 4 size) [(str (first number-string))"x000"] 526 | (= 5 size) [(apply str (take 2 number-string)) "x000"] 527 | (= 6 size) [(str (first number-string)) "x00" "x000"] 528 | (= 7 size) [(str (first number-string)) "x000000"] 529 | (= 8 size) [(apply str (take 2 number-string)) "x000000"] 530 | (= 9 size) [(str (first number-string)) "x00 x000000"] 531 | (= 10 size) [(str (first number-string)) "x000000000"]))) 532 | 533 | 534 | 535 | 536 | (defn number-sequence 537 | "Convert a number into a sequence of numbers that also represent the 538 | number level 539 | 540 | Arguments: Integer or Long 541 | Return: Vector of strings" 542 | ^:depreciated 543 | 544 | [number] 545 | 546 | {:pre [(<= 0 number)]} ; Exception if function called with negative number 547 | 548 | (loop [current-string (str number) 549 | sequence-of-numbers []] 550 | (if (empty? current-string) 551 | (clean-number-sequence sequence-of-numbers) 552 | (recur (rest current-string) 553 | (conj sequence-of-numbers 554 | (positional-number-string current-string)))))) 555 | 556 | 557 | 558 | 559 | (defn multiple-number-levels 560 | [number] 561 | ^:;; Refactored algorithm 562 | ;; take a number 563 | ;; convert to string 564 | ;; split into individual digits that represent the number positional ("21" => "20 1") 565 | ;; partition from right into groups of threes 566 | ;; process each partition of three (hundreds, tens, ones) 567 | ;; apply relevant rules - andify, hyphenate, etc 568 | ;; process whole sequence (thousands, millions, billions) 569 | ;; turn in to a string sentence 570 | 571 | depreciated 572 | (let [remainder (rem (count number) 3) 573 | levels (quot (count number) 3)] 574 | (if (> 1 levels) 575 | (apply str (take remainder number) (multiple-number-levels (drop remainder))) 576 | number))) 577 | 578 | 579 | (defn get-prefix-number 580 | "Gets the prefix of a number without the number level. 581 | 582 | Number levels go up in groups of three, so 1000 returns 1, 10000 returns 10. 583 | 584 | Arguments: String (representing a number with its positional number level) 585 | Return: String (representing just the prefix value)" 586 | 587 | [number-string] 588 | 589 | ^:depreciated 590 | 591 | (let [prefix (rem (count number-string) 3) 592 | levels (quot (count number-string) 3)] 593 | 594 | (cond 595 | (= prefix 0) (str (first number-string)) 596 | (> 1 levels) (multiple-number-levels number-string) 597 | :else (apply str (take prefix number-string))))) 598 | 599 | (get-prefix-number "1000") 600 | ;; => "1" 601 | (get-prefix-number "10000") 602 | ;; => "10" 603 | (get-prefix-number "100000") 604 | ;; => "1" 605 | 606 | ;; if 607 | ;; 100 = 1 608 | ;; 1000 = 1 609 | ;; 10,000 = 10 610 | ;; 100,000 = 100 611 | ;; 1,000,000 = 1 612 | (defn word-sequence 613 | "Convert numbers in a sequence to their corresponding words, using a dictionary 614 | 615 | Arguments: dictionary lookup table (hash-map), sequence of integer/long numbers 616 | Return: sequence of words (vector of strings)" 617 | 618 | ^:depreciated 619 | 620 | [dictionary number-sequence] 621 | 622 | (map (fn [positional-number] 623 | (if (<= (count positional-number) 3) 624 | (get dictionary positional-number) 625 | (str (get dictionary (get-prefix-number positional-number)) 626 | " " 627 | (get number-levels (count positional-number))))) 628 | number-sequence)) 629 | 630 | 631 | 632 | ;; second approach - far too low level 633 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 634 | 635 | 636 | 637 | 638 | (defn convert-cheque 639 | "Convert cheque and speak as words functions should be top level 640 | functions (API) for this namespace." 641 | 642 | [amount] 643 | 644 | ;; convert to string 645 | ;; split to pounds and pence on . 646 | 647 | (let [amout-string (str amount) 648 | [pounds pence] (clojure.string/split amout-string #"\.")] 649 | 650 | (str ((speak-number-as-words british-english-dictionary pounds)) 651 | "pounds and " 652 | (speak-number-as-words british-english-dictionary pence) 653 | "pence"))) 654 | 655 | ;; the last digit and second last digit not=0 then add hyphen 656 | 657 | 658 | 659 | (defn positional-number-string 660 | "Round a number down to its positional number level. 661 | 662 | Examples: 2345 becomes 2000 663 | 664 | Arguments: String representing a whole number 665 | Return: Vector of strings (representing each digit and its number 666 | level, eg. hundred, thousand, etc.)" 667 | 668 | [number-string] 669 | 670 | (apply str 671 | (cons (first number-string) 672 | (map (constantly "0") (rest number-string))))) 673 | 674 | 675 | (defn clean-number-sequence 676 | "All number strings that are pronounced start with something other than zero. 677 | All zero numbers are removed, except where zero is the only value in the sequence. 678 | 679 | Arguments: Vector of positional numbers as strings 680 | Return: Vector of positional numbers as strings" 681 | 682 | [number-string] 683 | 684 | (if (= ["0"] number-string) 685 | ["0"] 686 | (filter #(not= \0 (first %)) number-string))) 687 | 688 | 689 | (defn rest-as-string 690 | "Simple helper function to convert a sequence of characters to a 691 | string. 692 | 693 | Arguments: A sequence of characters that represent the digits of a number 694 | Return: A string of those characters" 695 | 696 | [number-as-characters] 697 | 698 | (apply str (rest number-as-characters))) 699 | 700 | 701 | 702 | 703 | 704 | (defn andify-sentence 705 | "Correct the grammar of the words as a sentence, by placing `and` in 706 | the sentence. `and` is inserted after every occurance of `hundred` 707 | if it is followed by another number 708 | 709 | Example: [one hundred one] becomes [one hundred and one] 710 | 711 | TODO: refactor to create a generalised function that will update a 712 | number sequence (we should apply this to a grouped set of number 713 | rather than the whole sequence of word numbers). 714 | 715 | Arguments: vector of strings (representing a sequence of words) 716 | Return: vector of strings (as above but with grammar corrections)" 717 | 718 | ^:deprecated 719 | 720 | [word-sequence] 721 | 722 | (loop [sequence word-sequence 723 | and-sequence []] 724 | (if (empty? sequence) 725 | and-sequence 726 | (recur (rest sequence) 727 | (if (and (= (first sequence) "hundred") 728 | (not (nil? (second sequence)))) 729 | "and" 730 | nil) 731 | (if (not= (first sequence))) 732 | (conj and-sequence 733 | (first sequence) 734 | ))))) 735 | 736 | 737 | 738 | (defn parse-number 739 | "A rather brute force approach to parsing the sequence of numbers 740 | create a representative sequence of numbers that can be readily converted 741 | into words using a dictionary 742 | 743 | TODO: should first partition the numbers in to groups of threes, 744 | starting from the right hand side. A common pattern can then be applied 745 | overall by groups of threes. Also specific rules (andify, hyphenate) can be 746 | applied within each group. 747 | 748 | Arguments: string (representing a number) 749 | Return: A vector of strings (representing )" 750 | 751 | ^:abstract-me-please 752 | 753 | [number-string] 754 | 755 | (let [size (count number-string)] 756 | (cond 757 | (= 1 size) [number-string] 758 | (= 2 size) (concat [(str (first number-string) "0")] 759 | (parse-number (rest-as-string number-string))) 760 | (= 3 size) (concat [(str (first number-string))"x00"] 761 | (parse-number (rest-as-string number-string))) 762 | (= 4 size) (concat [(str (first number-string))"x000" ] 763 | (parse-number (rest-as-string number-string))) 764 | (= 5 size) (concat [(apply str (take 2 number-string)) "x000"] 765 | (parse-number (apply str (drop 2 number-string)))) 766 | (= 6 size) (concat [(str (first number-string)) "x00"] 767 | (parse-number (rest-as-string number-string))) 768 | (= 7 size) (concat [(str (first number-string)) "x000000"] 769 | (parse-number (rest-as-string number-string))) 770 | (= 8 size) (concat [(apply str (take 2 number-string)) "x000000"] 771 | (parse-number (apply str (drop 2 number-string)))) 772 | (= 9 size) (concat [(str (first number-string)) "x00"] 773 | (parse-number (rest-as-string number-string))) 774 | (= 10 size) (concat [(str (first number-string)) "x000000000"] 775 | (parse-number (rest-as-string number-string)))))) 776 | 777 | (defn numbers->words 778 | "Convert a sequence of numbers to their word equivalents in a given 779 | dictionary 780 | 781 | Arguments: hash-map dictionary, vector of strings (representing numbers) 782 | Return: Sequence of strings (each string is a word representing a number)" 783 | 784 | ^:refactor 785 | 786 | [dictionary number-sequence] 787 | 788 | (map dictionary number-sequence)) 789 | 790 | 791 | (defn speak-number-as-words 792 | [dictionary number] 793 | (clojure.string/join 794 | " " 795 | (andify-sentence 796 | (numbers->words dictionary (parse-number (str number)))))) 797 | 798 | 799 | #_(defn speak-number-as-words 800 | [dictionary number] 801 | (->> number 802 | str 803 | parse-number 804 | (numbers->words dictionary) 805 | andify-sentence 806 | (clojure.string/join " "))) 807 | 808 | 809 | ;; str makes nil disappear 810 | (str "Hello" nil) 811 | ;; => "Hello" 812 | 813 | 814 | ;; Tests for this approach (and previous approach, a bit of a mess really). 815 | 816 | 817 | (deftest number->british-english-test 818 | "Test the conversion of numerical whole numbers to 819 | strings containing British English words as a sentence" 820 | 821 | #_(testing "Sample data tests - single values" 822 | (is (= "zero" (sut/digit->word 0 sut/british-english-numbers))) 823 | (is (= "one" (sut/digit->word 1 sut/british-english-numbers)))) 824 | 825 | #_(testing "Sample data tests - double figure values" 826 | (is (= "20" (sut/positional-number-string "21")))) 827 | 828 | #_(testing "Generating sequences of numbers with positional number level" 829 | (is (= ["20" "1"] (sut/number-sequence 21))) 830 | (is (= ["2000"] (sut/number-sequence 2000))) 831 | (is (= ["2000" "1"] (sut/number-sequence 2001)))) 832 | 833 | ;; 2001 probably should be "2" "x000" "1" 834 | 835 | 836 | #_(testing "Convert sequences of numbers to words" 837 | (is (= ["zero"] 838 | (sut/word-sequence british-english-dictionary ["0"]))) 839 | (is (= ["one"] 840 | (sut/word-sequence british-english-dictionary ["1"]))) 841 | (is (= ["twenty"] 842 | (sut/word-sequence british-english-dictionary ["20"]))) 843 | (is (= ["twenty" "one"] 844 | (sut/word-sequence british-english-dictionary ["20" "1"]))) 845 | (is (= ["ninety" "nine"] 846 | (sut/word-sequence british-english-dictionary ["90" "9"]))) 847 | (is (= ["one hundred"] 848 | (sut/word-sequence british-english-dictionary ["100"]))) 849 | (is (= ["one hundred" "one"] 850 | (sut/word-sequence british-english-dictionary ["100" "1"]))) 851 | (is (= ["nine hundred" "ninety" "nine"] 852 | (sut/word-sequence british-english-dictionary ["900" "90" "9"]))) 853 | (is (= ["one thousand"] 854 | (sut/word-sequence british-english-dictionary ["1000"]))) 855 | (is (= ["one thousand" "one"] 856 | (sut/word-sequence british-english-dictionary ["1000" "1"]))) 857 | ) 858 | 859 | #_(testing "Parsing a number into a sequence of numbers" 860 | (is (= ["0"], 861 | (sut/parse-number "0"))) 862 | (is (= ["20" "1"], 863 | (sut/parse-number "21"))) 864 | (is (= ["1" "x00"], 865 | (sut/parse-number "100"))) 866 | (is (= ["10" "x000" "5" "x00" "40" "5"] 867 | (sut/parse-number "10545")))) 868 | 869 | (testing "Speaking the words as numbers" 870 | (is (= "zero" 871 | (sut/speak-number-as-words british-english-dictionary 0))) 872 | (is (= "one" 873 | (sut/speak-number-as-words british-english-dictionary 1))) 874 | (is (= "twenty one" 875 | (sut/speak-number-as-words british-english-dictionary 21))) 876 | (is (= "one hundred and five" 877 | (sut/speak-number-as-words british-english-dictionary 105))) 878 | (is (= "one hundred and twenty three" 879 | (sut/speak-number-as-words british-english-dictionary 123))) 880 | (is (= "one thousand and five" 881 | (sut/speak-number-as-words british-english-dictionary 1005))) 882 | (is (= "one thousand and forty two" 883 | (sut/speak-number-as-words british-english-dictionary 1042))) 884 | (is (= "one thousand one hundred and five" 885 | (sut/speak-number-as-words british-english-dictionary 1105))) 886 | (is (= "fifty six million nine hundred and fourty five thousand seven hundred and eighty one" 887 | (sut/speak-number-as-words british-english-dictionary 56945781))) 888 | (is (= "nine hundred and ninety nine million nine hundred and ninety nine thousand 889 | nine hundred and ninety nine" 890 | (sut/speak-number-as-words british-english-dictionary 999999999)))) 891 | ) 892 | 893 | 894 | 895 | 896 | ;; partition approach - revisited 897 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 898 | 899 | ;; I missed something when I first looked at partition and had 900 | ;; identified all the right pieces, except I did not bring them 901 | ;; all together. 902 | 903 | 904 | 905 | ;; partition will drop any digits that do not make up the group size. 906 | ;; so we should use partition all on any group that is smaller. 907 | ;; Or add a check that the number size is divisible exactly by group size. 908 | (partition 3 "0") 909 | ;; => () 910 | 911 | (partition-all 3 "0") 912 | ;; => ((\0)) 913 | 914 | (partition-all 3 "321") 915 | ;; => ((\3 \2 \1)) 916 | 917 | (partition 3 "7654321") 918 | 919 | (reduce str 920 | (partition 3 "321")) 921 | 922 | 923 | ;; Where the number is not in a group of three, 924 | ;; we can use rem and quot to see how it should be grouped. 925 | 926 | ;; if we take the rem number of digits from the start of the number 927 | ;; then we can partition the rest correctly. 928 | 929 | 930 | 931 | (partition-all 3 (take 1 "4321")) 932 | ;; => ((\4)) 933 | 934 | (drop 1 "4321") 935 | ;; => (\3 \2 \1) 936 | 937 | (partition 3 (drop 1 "4321")) 938 | ;; => ((\3 \2 \1)) 939 | 940 | (conj 941 | (partition-all 3 (take 1 "4321")) 942 | (partition 3 (drop 1 "4321")) 943 | ) 944 | ;; => (((\3 \2 \1)) (\4)) 945 | 946 | 947 | (concat 948 | (partition-all 3 (take 1 "4321")) 949 | (partition 3 (drop 1 "4321")) 950 | ) 951 | ;; => ((\4) (\3 \2 \1)) 952 | 953 | 954 | 955 | (partition-number-string "987654321") 956 | ;; => ((\9 \8 \7) (\6 \5 \4) (\3 \2 \1)) 957 | 958 | 959 | 960 | 961 | ;; converting numbers to a sequence of words 962 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 963 | 964 | 965 | (map #(character->number-word dictionary/digit->word %) 966 | (partition-number-string "1")) 967 | ;; => (nil) 968 | 969 | ;; its a sequence of sequences, silly... 970 | 971 | (for [group (partition-number-string "1")] 972 | (map #(character->number-word dictionary/digit->word %) 973 | group)) 974 | ;; => (("one")) 975 | 976 | 977 | (for [group (partition-number-string "10")] 978 | (map #(character->number-word dictionary/digit->word %) 979 | group)) 980 | ;; => (("one" "zero")) 981 | 982 | 983 | (for [group (partition-number-string "100")] 984 | (map #(character->number-word dictionary/digit->word %) 985 | group)) 986 | ;; => (("one" "zero" "zero")) 987 | 988 | ;; leave "zero" in for now so we can easily maintain the position 989 | ;; strip out "zero" when all grammar rules have been applied 990 | ;; and all is left is convert to a string. 991 | 992 | 993 | (for [group (partition-number-string "1000")] 994 | (map #(character->number-word dictionary/digit->word %) 995 | group)) 996 | ;; => (("one") ("zero" "zero" "zero")) 997 | 998 | (for [group (partition-number-string "10000")] 999 | (map #(character->number-word dictionary/digit->word %) 1000 | group)) 1001 | ;; => (("one" "zero") ("zero" "zero" "zero")) 1002 | 1003 | (for [group (partition-number-string "9876543210")] 1004 | (map #(character->number-word dictionary/digit->word %) 1005 | group)) 1006 | ;; => (("nine") ("eight" "seven" "six") ("five" "four" "three") ("two" "one" "zero")) 1007 | 1008 | 1009 | 1010 | ;; partitioning with partition all for the section of the number not divisible by three does work, however, we are left with one partition smaller than the rest. 1011 | 1012 | ;; having uneven partitions when trying to convert to words with specific rules leads to complexity. If the partitions can be padded at the front, then things are simpler. 1013 | 1014 | ;; could pad using the `partition`, but it puts the padding to the right, 1015 | ;; which breaks the number value 1016 | (partition 3 3 [\0] "15") 1017 | ;; => ((\1 \5 \0)) 1018 | 1019 | ;; we can concatonate zero characters on to the front of a character 1020 | (concat (repeat 2 \0) '(\1) ) 1021 | ;; => (\0 \0 \1) 1022 | 1023 | ;; we can do it manually by generating the right number of zeros based on the remainer 1024 | ;; and group size 1025 | (conj (repeat (- group-size remainder) \0)) 1026 | 1027 | 1028 | ;; putting it all together 1029 | #_(concat 1030 | (concat (repeat (- group-size remainder) \0) 1031 | (partition-all group-size (take remainder number-string))) 1032 | (partition group-size (drop remainder number-string))) 1033 | 1034 | 1035 | ;; NPE 1036 | #_(character-sequence->word-sequence 1037 | dictionary/digit->word 1038 | (partition-number-string "321")) 1039 | 1040 | 1041 | 1042 | 1043 | ;; parse-number 1044 | ;; convert char sequence - word sequence 1045 | ;; -- map function that calls char to number based on grammar rules 1046 | 1047 | #_(for [group (partition-number-string "9876543210")] 1048 | (map #(character->number-word dictionary/digit->word %) 1049 | group)) 1050 | ;; => (("nine") ("eight" "seven" "six") ("five" "four" "three") ("two" "one" "zero")) 1051 | 1052 | 1053 | 1054 | 1055 | ;; (reduce str '(\1 \3)) 1056 | 1057 | 1058 | ;; rather than use q pre-condition, which would have to be long and prevent us from using count, defensive programming around the initial input 1059 | 1060 | 1061 | ;; {:pre [(<= 0 (Long. number-string))]} 1062 | --------------------------------------------------------------------------------