1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*!
This crate provides a library for recognizing, parsing and transcribing into digits (base 10) numbers expressed in natural language.
This top level documentation describes the usage of the library with the builtin languages and provides some simple examples.
For more specific details on how to add support for new natural languages (and contributing to the builtin set!), please see the documentation of the [`lang`] module.
# Usage
This crate is [on crates.io](https://crates.io/crates/text2num) and can be
used by adding `text2num` to your dependencies in your project's `Cargo.toml`.
```toml
[dependencies]
text2num = "1"
```
# Example: check some string is a valid number in a given language.
For convenience, the builtin languages are encapsulated into the [`Language`] type so
you can easily switch languages at runtime.
Each builtin language support regional varieties automatically, so you don't need to specify a region.
The language interpreters are stateless so you can reuse and share them.
```rust
use text2num::{Language, text2digits};
let en = Language::english();
assert!(
text2digits("one hundred fifty-seven", &en).is_ok()
);
assert!(text2digits("twenty twelve", &en).is_err());
```
Of course, you can get the base 10 digit representation too:
```rust
use text2num::{Language, text2digits};
let es = Language::spanish();
let utterance = "ochenta y cinco";
match text2digits(utterance, &es) {
Ok(repr) => println!("'{}' means {} in Spanish", utterance, repr),
Err(_) => println!("'{}' is not a number in Spanish", utterance)
}
```
When run, the above code should print `'ochenta y cinco' means 85 in Spanish` on the standard output.
If you don't need to dynamically switch languages, you can directly use the appropriate interpreter instead of
the `Language` type:
```
use text2num::lang::English;
use text2num::text2digits;
let en = English::new();
assert!(text2digits("fifty-five", &en).is_ok());
```
# Example: find and replace numbers in a natural speech string.
Most often, you just want to rewrite a string containing natural speech so that the numbers it contains (cardinals,
ordinals, decimal numbers) appear in digit (base 10) form instead.
As isolated smaller numbers may be easier to read in plain text, you can specify a threshold under which isolated simple cardinals and ordinals are
not replaced.
```rust
use text2num::{Language, replace_numbers_in_text};
let en = Language::english();
let sentence = "Let me show you two things: first, isolated numbers are treated differently than groups like one, two, three. And then, that decimal numbers like three point one four one five are well understood.";
assert_eq!(
replace_numbers_in_text(sentence, &en, 10.0),
"Let me show you two things: first, isolated numbers are treated differently than groups like 1, 2, 3. And then, that decimal numbers like 3.1415 are well understood."
);
assert_eq!(
replace_numbers_in_text(sentence, &en, 0.0),
"Let me show you 2 things: 1st, isolated numbers are treated differently than groups like 1, 2, 3. And then, that decimal numbers like 3.1415 are well understood."
);
```
# More advanced usage: operations on token streams.
Among the real life applications of this library are the post-processing of Automatic Speech Recognition (ASR)
output or taking part in a Natural Language Processing (NLP) pipeline.
In those cases, you'll probably get a stream of tokens of a certain type instead of a string.
The `text2num` library can process those streams as long as the token type implements the [`Token trait`](word_to_digit::Token).
# Example: substitutions in a token list.
We can show a simple example with `String` streams:
```rust
use text2num::{replace_numbers_in_stream, Language, Token, Replace};
let en = Language::english();
struct BareToken(String);
impl Token for &BareToken {
fn text(&self) -> &str {
self.0.as_ref()
}
fn text_lowercase(&self) -> &str {
self.0.as_ref()
}
}
impl Replace for BareToken {
fn replace<I: Iterator<Item = Self>>(_replaced: I, data: String) -> Self {
BareToken(data)
}
}
// Poor man's tokenizer
let token_list = "I have two hundreds and twenty dollars in my pocket".split_whitespace().map(|s| BareToken(s.to_string())).collect();
let processed_stream = replace_numbers_in_stream(token_list, &en, 10.0);
assert_eq!(
processed_stream.into_iter().map(|t| t.0).collect::<Vec<_>>(),
vec!["I", "have", "220", "dollars", "in", "my", "pocket"]
);
```
# Example: find numbers in a token stream.
In this more elaborate example, we show how to implement the `Token` trait on a typical ASR token type and
how to locate numbers (and their values) in a stream of those tokens.
```rust
use text2num::{find_numbers, Language, Token};
struct DecodedWord<'a> {
text: &'a str,
start: u64, // in milliseconds
end: u64
}
impl Token for DecodedWord<'_> {
fn text(&self) -> &str {
self.text
}
fn text_lowercase(&self) -> &str {
self.text
}
fn nt_separated(&self, previous: &Self) -> bool {
// if there is a voice pause of more than 100ms between words, it is worth a punctuation
self.start - previous.end > 100
}
fn not_a_number_part(&self) -> bool {
false
}
}
// Simulate real time (online) ASR output
let stream = [
DecodedWord{ text: "i", start: 0, end: 100},
DecodedWord{ text: "have", start: 100, end: 200},
DecodedWord{ text: "twenty", start: 200, end: 300},
DecodedWord{ text: "four", start: 300, end: 400},
DecodedWord{ text: "dollars", start: 410, end: 800},
DecodedWord{ text: "in", start: 800, end: 900},
DecodedWord{ text: "my", start: 900, end: 1000},
DecodedWord{ text: "pocket", start: 1010, end: 1410},
].into_iter();
// Process
let en = Language::english();
let occurences = find_numbers(stream, &en, 10.0);
assert_eq!(occurences.len(), 1);
let found = &occurences[0];
// Match position in the stream
assert_eq!(found.start, 2);
assert_eq!(found.end, 4);
// Match values
assert_eq!(found.text, "24");
assert_eq!(found.value, 24.0);
assert!(!found.is_ordinal);
```
*/
pub use ;
pub use ;
/// Get an interpreter for the language represented by the `language_code` ISO code.