chinese_dictionary 2.1.6

A searchable Chinese / English dictionary with helpful utilities.
Documentation
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! ### About
//! A searchable Chinese / English dictionary with helpful utilities.
//!
//! ### Features
//! - Search with Traditional Chinese characters, Simplified Chinese characters, pinyin with tone marks, pinyin with tone numbers, pinyin with no tones, and English.
//! - Classify a string of text as either English, pinyin, or Chinese characters.
//! - Convert between Traditional and Simplified Chinese characters.
//! - Segment strings of Chinese characters into tokens using a dictionary-driven segmentation approach.
//!
//! ### Usage
//! Querying the dictionary
//! ```rust
//! extern crate chinese_dictionary;
//!
//! use chinese_dictionary::query;
//!
//! // Querying the dictionary returns an `Option<Vec<&WordEntry>>`
//! // Read more about the WordEntry struct below
//! let text = "to run";
//! let results = query(text).unwrap();
//! assert_eq!("执行", results[0].simplified);
//! ```
//!
//! Classifying a string of text
//! ```rust
//! extern crate chinese_dictionary;
//!
//! use chinese_dictionary::{classify, ClassificationResult};
//!
//! // Read more about the ClassificationResult enum below
//! assert_eq!(ClassificationResult::PY, classify("nihao"));
//! ```
//!
//! Convert between Traditional and Simplified Chinese characters
//! ```rust
//! extern crate chinese_dictionary;
//!
//! use chinese_dictionary::{traditional_to_simplified, simplified_to_traditional};
//!
//! assert_eq!("简体字", traditional_to_simplified("簡體字"));
//! assert_eq!("繁體字", simplified_to_traditional("繁体字"));
//! ```
//!
//! Segment a string of characters
//! ```rust
//! extern crate chinese_dictionary;
//!
//! use chinese_dictionary::{tokenize};
//!
//! assert_eq!(vec!["今天", "天气", "不错"], tokenize("今天天气不错"));
//! ```
//!
//! #### `WordEntry` struct
//! ```rust
//! extern crate chinese_dictionary;
//!
//! use chinese_dictionary::{MeasureWord, WordEntry};
//!
//! let example_measure_word = MeasureWord {
//!     traditional: "example_traditional".to_string(),
//!     simplified: "example_simplified".to_string(),
//!     pinyin_marks: "example_pinyin_marks".to_string(),
//!     pinyin_numbers: "example_pinyin_numbers".to_string(),
//! };
//!
//! let example = WordEntry {
//!     traditional: "繁體字".to_string(),
//!     simplified: "繁体字".to_string(),
//!     pinyin_marks: "fán tǐ zì".to_string(),
//!     pinyin_numbers: "fan2 ti3 zi4".to_string(),
//!     english: vec!["traditional Chinese character".to_string()],
//!     tone_marks: vec![2 as u8, 3 as u8, 4 as u8],
//!     hash: 000000 as u64,
//!     measure_words: vec![example_measure_word],
//!     hsk: 6 as u8,
//!     word_id: 11111111 as u32,
//! };
//! ```
//!
//! #### `ClassificationResult` enum
//! The possible values for the `ClassificationResult` enum are:
//! - `PY`: Represents Pinyin
//! - `EN`: Represents English
//! - `ZH`: Represents Chinese
//! - `UN`: Represents an uncertain classification result

extern crate bincode;
extern crate character_converter;
extern crate chinese_detection;
extern crate once_cell;

mod chinese_dictionary;
pub use self::chinese_dictionary::{
    classify, init, is_simplified, is_traditional, query, query_by_chinese, query_by_english,
    query_by_pinyin, query_by_simplified, query_by_traditional, simplified_to_traditional,
    tokenize, traditional_to_simplified, ClassificationResult, MeasureWord, WordEntry,
};

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_search_by_english_1() {
        let text = "watermelon";
        let result = query(text);
        let actual = &result.unwrap().first().unwrap().traditional;
        let expected = "西瓜";
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_search_by_english_2() {
        let text = "to run";
        let result = query(text);
        let actual = &result.unwrap().first().unwrap().traditional;
        let expected = "執行";
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_search_by_english_3() {
        let text = "people around the world";
        let result = query(text);
        let actual = &result.unwrap().first().unwrap().traditional;
        let expected = "人們";
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_search_by_traditional() {
        let text = "繁體字";
        let result = query(text);
        let actual = result.unwrap().first().unwrap().english.first().unwrap();
        let expected = "traditional Chinese character";
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_search_by_simplified() {
        let text = "龙纹";
        let result = query(text);
        let actual = result.unwrap().first().unwrap().english.first().unwrap();
        let expected = "dragon (as a decorative design)";
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_search_by_simplified_exact() {
        let text = "龙纹";
        let result = query_by_simplified(text);
        let actual = result.first().unwrap().english.first().unwrap();
        let expected = "dragon (as a decorative design)";
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_search_by_traditional_exact() {
        let text = "繁體字";
        let result = query_by_traditional(text);
        let actual = result.first().unwrap().english.first().unwrap();
        let expected = "traditional Chinese character";
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_search_sentence() {
        let text = "你好今天的天气还好。";
        let result = query(text);
        let actual = result.unwrap().first().unwrap().english.first().unwrap();
        let expected = "hello";
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_search_by_pinyin_1() {
        let text = "hánlěng";
        let result = query(text);
        let actual = &result.unwrap().first().unwrap().traditional;
        let expected = "寒冷";
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_search_by_pinyin_2() {
        let text = "dian4nao3";
        let result = query(text);
        let actual = &result.unwrap().first().unwrap().traditional;
        let expected = "電腦";
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_search_by_pinyin_3() {
        let text = "nihao";
        let result = query(text);
        let actual = &result.unwrap().first().unwrap().traditional;
        let expected = "你好";
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_tokenize_traditional() {
        let sentence = "今天的天氣挺爽";
        let actual = tokenize(sentence);
        let expected = vec![
            "今天".to_string(),
            "".to_string(),
            "天氣".to_string(),
            "".to_string(),
            "".to_string(),
        ];
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_tokenize_simplified() {
        let sentence = "今天的天气挺爽";
        let actual = tokenize(sentence);
        let expected = vec![
            "今天".to_string(),
            "".to_string(),
            "天气".to_string(),
            "".to_string(),
            "".to_string(),
        ];
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_tokenize_complex() {
        let sentence = "红色是我favorite颜色。";
        let actual = tokenize(sentence);
        let expected = vec![
            "红色".to_string(),
            "".to_string(),
            "".to_string(),
            "颜色".to_string(),
        ];
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_classify_english() {
        let text = "boat";
        let actual = classify(text);
        let expected = ClassificationResult::EN;
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_classify_pinyin_1() {
        let text = "fán tǐ zì";
        let actual = classify(text);
        let expected = ClassificationResult::PY;
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_classify_pinyin_2() {
        let text = "fan2ti3zi4";
        let actual = classify(text);
        let expected = ClassificationResult::PY;
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_classify_pinyin_3() {
        let text = "jiantizi";
        let actual = classify(text);
        let expected = ClassificationResult::PY;
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_classify_simplified() {
        let text = "简体字";
        let actual = classify(text);
        let expected = ClassificationResult::ZH;
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_classify_traditional() {
        let text = "繁體字";
        let actual = classify(text);
        let expected = ClassificationResult::ZH;
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_simplified_to_traditional() {
        let text = "繁体字";
        let actual = simplified_to_traditional(text);
        let expected = "繁體字";
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_traditional_to_simplified() {
        let text = "簡體字";
        let actual = traditional_to_simplified(text);
        let expected = "简体字";
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_is_simplified() {
        let text = "简体字";
        let actual = is_simplified(text);
        let expected = true;
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_is_not_simplified() {
        let text = "簡體字";
        let actual = is_simplified(text);
        let expected = false;
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_is_traditional() {
        let text = "繁體字";
        let actual = is_traditional(text);
        let expected = true;
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_is_not_traditional() {
        let text = "繁体字";
        let actual = is_traditional(text);
        let expected = false;
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_capitalization() {
        let english_text = "Watermelon";
        let english_result = query(english_text);
        let english_actual = &english_result.unwrap().first().unwrap().traditional;
        let english_expected = "西瓜";
        assert_eq!(english_expected, english_actual);

        let pinyin_text = "Beijing";
        let pinyin_result = query(pinyin_text);
        let pinyin_actual = &pinyin_result.unwrap().first().unwrap().traditional;
        let pinyin_expected = "北京";
        assert_eq!(pinyin_expected, pinyin_actual);
    }

    #[test]
    fn test_empty_search_chinese() {
        let text = "";
        let result = query_by_chinese(text);
        let length = result.len();
        assert_eq!(length, 0 as usize);
    }

    #[test]
    fn test_space_search_chinese() {
        let text = " ";
        let result = query_by_chinese(text);
        let length = result.len();
        assert_eq!(length, 0 as usize);
    }

    #[test]
    fn test_empty_search_pinyin() {
        let text = "";
        let result = query_by_pinyin(text);
        let length = result.len();
        assert_eq!(length, 0 as usize);
    }

    #[test]
    fn test_space_search_pinyin() {
        let text = " ";
        let result = query_by_pinyin(text);
        let length = result.len();
        assert_eq!(length, 0 as usize);
    }

    #[test]
    fn test_empty_search_english() {
        let text = "";
        let result = query_by_english(text);
        let length = result.len();
        assert_eq!(length, 0 as usize);
    }

    #[test]
    fn test_space_search_english() {
        let text = " ";
        let result = query_by_english(text);
        let length = result.len();
        assert_eq!(length, 0 as usize);
    }

    #[test]
    fn test_no_duplicates() {
        let text = "test";
        let results = query(text).unwrap();
        let mut seen = Vec::new();
        for entry in results {
            assert!(!seen.contains(&entry.word_id));
            seen.push(entry.word_id);
        }
    }
}