-
Notifications
You must be signed in to change notification settings - Fork 210
/
TrycanTranslator.java
56 lines (44 loc) · 1.65 KB
/
TrycanTranslator.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
package com.swjtu.trans.impl;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.swjtu.lang.LANG;
import com.swjtu.trans.AbstractTranslator;
import com.swjtu.util.Util;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
public final class TrycanTranslator extends AbstractTranslator {
private static final String url = "http://fanyi.trycan.com/Transfer.do";
public TrycanTranslator(){
super(url);
}
@Override
public void setLangSupport() {
langMap.put(LANG.ZH, "zh");
langMap.put(LANG.EN, "en");
}
@Override
public void setFormData(LANG from, LANG to, String text) {
formData.put("word_from", langMap.get(from));
formData.put("word_to", langMap.get(to));
formData.put("word_src", text);
}
@Override
public String query() throws Exception {
HttpPost request = new HttpPost(Util.getUrlWithQueryString(url, formData));
CloseableHttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "utf-8");
EntityUtils.consume(entity);
response.getEntity().getContent().close();
response.close();
return result;
}
@Override
public String parses(String text) throws IOException {
ObjectMapper mapper = new ObjectMapper();
return mapper.readTree(text).path("data").findPath("dst").toString();
}
}