-
Notifications
You must be signed in to change notification settings - Fork 210
/
Copy pathGoogleTTS.java
58 lines (50 loc) · 1.68 KB
/
GoogleTTS.java
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
package com.swjtu.tts.impl;
import com.swjtu.lang.LANG;
import com.swjtu.tts.AbstractTTS;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.io.FileReader;
public final class GoogleTTS extends AbstractTTS{
private final static String url = "https://translate.google.cn/translate_tts";
public GoogleTTS() {
super(url);
setLangSupport();
}
@Override
public void setLangSupport() {
langMap.put(LANG.ZH, "zh-CN");
langMap.put(LANG.EN, "en");
langMap.put(LANG.FRA, "fr");
langMap.put(LANG.RU, "ru");
langMap.put(LANG.DE, "de");
langMap.put(LANG.KOR, "ko");
langMap.put(LANG.JP, "ja");
}
@Override
public void setFormData(LANG source, String text) {
formData.put("ie", "UTF-8");
formData.put("q", text);
formData.put("tl", langMap.get(source));
formData.put("total", String.valueOf(1));
formData.put("idx", String.valueOf(0));
formData.put("textlen", String.valueOf(11));
formData.put("tk", token(text));
formData.put("client", "t");
}
private String token(String text) {
String tk = "";
ScriptEngine engine = new ScriptEngineManager().getEngineByName("js");
try {
FileReader reader = new FileReader("./tk/Google.js");
engine.eval(reader);
if (engine instanceof Invocable) {
Invocable invoke = (Invocable)engine;
tk = String.valueOf(invoke.invokeFunction("token", text));
}
} catch (Exception e) {
e.printStackTrace();
}
return tk;
}
}