-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinguistic_Example.java
More file actions
110 lines (88 loc) · 3.41 KB
/
Linguistic_Example.java
File metadata and controls
110 lines (88 loc) · 3.41 KB
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
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Test {
public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException {
HttpURLConnection connection;
DataOutputStream outStream;
String sentence;
sentence = "Bruce Springsteen is an American singer-songwriter and multi-instrumentalist.";
for(int i=0; i<args.length; i++){
sentence = sentence.concat(args[i]+" ");
}
URL url = new URL("http://sandbox-xlike.isoco.com/services/analysis_en/analyze");
String body = URLEncoder.encode(sentence, "UTF-8");
body = body.replace("+", "%20");
body = "text=" + body + "&target=entities&conll=false";
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Content-Length",
"" + Integer.toString(body.getBytes().length));
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
System.out.println("Sending POST...");
outStream = new DataOutputStream(connection.getOutputStream());
outStream.writeBytes(body);
outStream.flush();
outStream.close();
System.out.println("Receiving response...");
//Receiving response
if (connection.getResponseCode() != 200) {
throw new IOException(connection.getResponseMessage());
}
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuffer response = new StringBuffer();
String str;
while ((str = rd.readLine()) != null) {
response.append(str);
}
rd.close();
System.out.println(response);
File file = new File("test.xml");
Writer writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
writer.write(response.toString());
writer.flush();
writer.close();
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new File("test.xml"));
doc.getDocumentElement().normalize();
NodeList nodes = doc.getElementsByTagName("sentence");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
NodeList sents = element.getElementsByTagName("text").item(0).getChildNodes();
System.out.println(sents.item(0).getNodeValue());
//Tokens
Element tokens = (Element) element.getElementsByTagName("tokens").item(0);
NodeList list = tokens.getElementsByTagName("token");
for (int j = 0; j < list.getLength(); j++) {
Node token = list.item(j);
System.out.println(token.getTextContent());
}
}
}
}
}