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
//! Generated file, do not edit by hand, see `/build.rs`
use super::Translator;
use crate::{Translate, Translation};
#[test]
fn cn_word() {
let trans = Translator;
let r = trans.translate("你好");
assert!(r.is_ok());
let r = r.unwrap();
assert!(r.is_some());
let r = r.unwrap();
let expected: Translation = serde_json::from_str(
r#"{
"query": "你好",
"url": "http://dict.youdao.com/w/%E4%BD%A0%E5%A5%BD",
"prons": [
{
"tag": "CN",
"value": "nǐ hǎo",
"audio": null
}
],
"exps": [
{
"tag": "Phrase",
"items": [
"hello",
"hi",
"how do you do"
]
},
{
"tag": "Web",
"items": [
"Hello",
"How do you do",
"hi",
"What's up"
]
}
],
"phrases": [
[
"你好吗",
[
"How are you",
"How Do You Do",
"Are You OK"
]
],
[
"你好小娜",
[
"Hey Cortana"
]
],
[
"你好啊",
[
"Hello",
"Hey"
]
],
[
"先生你好",
[
"Hello Sir"
]
]
]
}"#,
)
.unwrap();
let msg = format!(
"\n=== Json dump:\n{}\n=== ",
serde_json::to_string(&r).unwrap()
);
assert_eq!(r.query, expected.query, "{}", msg);
assert_eq!(r.url, expected.url, "{}", msg);
assert_eq!(r.exps, expected.exps, "{}", msg);
assert_eq!(r.phrases, expected.phrases, "{}", msg);
}
#[test]
fn cn_phrase() {
let trans = Translator;
let r = trans.translate("你好,世界");
assert!(r.is_ok());
let r = r.unwrap();
assert!(r.is_some());
let r = r.unwrap();
let expected: Translation = serde_json::from_str(
r#"{
"query": "你好,世界",
"url": "http://dict.youdao.com/w/%E4%BD%A0%E5%A5%BD%EF%BC%8C%E4%B8%96%E7%95%8C",
"prons": [],
"exps": [
{
"tag": "Machine",
"items": [
"Hello, world"
]
},
{
"tag": "Web",
"items": []
}
],
"phrases": [
[
"你好世界",
[
"hello world"
]
],
[
"你好世界新闻和密钥",
[
"hello world press and key"
]
],
[
"你好世界新闻和关键",
[
"hello world press and key"
]
],
[
"你好世界命令行参数",
[
"hello world command line args"
]
],
[
"世界你好",
[
"Hello World",
"World how are"
]
]
]
}"#,
)
.unwrap();
let msg = format!(
"\n=== Json dump:\n{}\n=== ",
serde_json::to_string(&r).unwrap()
);
assert_eq!(r.query, expected.query, "{}", msg);
assert_eq!(r.url, expected.url, "{}", msg);
assert_eq!(r.exps, expected.exps, "{}", msg);
assert_eq!(r.phrases, expected.phrases, "{}", msg);
}
#[test]
fn en_word() {
let trans = Translator;
let r = trans.translate("hello");
assert!(r.is_ok());
let r = r.unwrap();
assert!(r.is_some());
let r = r.unwrap();
let expected: Translation = serde_json::from_str(
r#"{
"query": "hello",
"url": "http://dict.youdao.com/w/hello",
"prons": [
{
"tag": "UK",
"value": "həˈləʊ",
"audio": "https://dict.youdao.com/dictvoice?audio=hello&type=1"
},
{
"tag": "US",
"value": "həˈloʊ",
"audio": "https://dict.youdao.com/dictvoice?audio=hello&type=2"
}
],
"exps": [
{
"tag": {
"Pos": "int."
},
"items": [
"喂,你好(用于问候或打招呼)",
"喂,你好(打电话时的招呼语)",
"喂,你好(引起别人注意的招呼语)",
"<非正式>喂,嘿 (认为别人说了蠢话或分心)",
"<英,旧>嘿(表示惊讶)"
]
},
{
"tag": {
"Pos": "n."
},
"items": [
"招呼,问候",
"(Hello)(法、印、美、俄)埃洛(人名)"
]
},
{
"tag": {
"Pos": "v."
},
"items": [
"说(或大声说)“喂”",
"打招呼"
]
},
{
"tag": "Web",
"items": [
"您好",
"哈啰",
"喂"
]
}
],
"phrases": [
[
"Hello Kitty",
[
"凯蒂猫",
"吉蒂猫"
]
],
[
"Hello Neighbor",
[
"你好邻居"
]
]
]
}"#,
)
.unwrap();
let msg = format!(
"\n=== Json dump:\n{}\n=== ",
serde_json::to_string(&r).unwrap()
);
assert_eq!(r.query, expected.query, "{}", msg);
assert_eq!(r.url, expected.url, "{}", msg);
assert_eq!(r.exps, expected.exps, "{}", msg);
assert_eq!(r.phrases, expected.phrases, "{}", msg);
}
#[test]
fn en_phrase() {
let trans = Translator;
let r = trans.translate("Hello, world");
assert!(r.is_ok());
let r = r.unwrap();
assert!(r.is_some());
let r = r.unwrap();
let expected: Translation = serde_json::from_str(
r#"{
"query": "Hello, world",
"url": "http://dict.youdao.com/w/Hello,%20world",
"prons": [],
"exps": [
{
"tag": "Machine",
"items": [
"你好,世界"
]
},
{
"tag": "Web",
"items": []
}
],
"phrases": [
[
"hello world",
[
"你好世界",
"开始",
"别来无恙",
"举个例子"
]
],
[
"Hello world UI",
[
"实现方法"
]
],
[
"hello world press and key",
[
"您好世界新闻和关键",
"你好世界新闻和密钥"
]
],
[
"Hello world only",
[
"世间只有你好"
]
],
[
"Hello world always",
[
"世间始终你好"
]
],
[
"air hello world",
[
"调税计算器"
]
],
[
"Hello Kitty Cutie World",
[
"凯蒂猫顽皮世界"
]
],
[
"HELLO WORLD&",
[
"第一行代码"
]
]
]
}"#,
)
.unwrap();
let msg = format!(
"\n=== Json dump:\n{}\n=== ",
serde_json::to_string(&r).unwrap()
);
assert_eq!(r.query, expected.query, "{}", msg);
assert_eq!(r.url, expected.url, "{}", msg);
assert_eq!(r.exps, expected.exps, "{}", msg);
assert_eq!(r.phrases, expected.phrases, "{}", msg);
}
// vim: ro