Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinguistic_Example.java
More file actions
Latest commit
110 lines (88 loc) · 3.41 KB
/
Copy pathLinguistic_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
importjava.io.BufferedReader;
importjava.io.DataOutputStream;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.io.OutputStreamWriter;
importjava.io.Writer;
importjava.net.HttpURLConnection;
importjava.net.URL;
importjava.net.URLEncoder;
importjavax.xml.parsers.DocumentBuilder;
importjavax.xml.parsers.DocumentBuilderFactory;
importjavax.xml.parsers.ParserConfigurationException;
importorg.w3c.dom.Document;
importorg.w3c.dom.Element;
importorg.w3c.dom.Node;
importorg.w3c.dom.NodeList;
importorg.xml.sax.SAXException;
publicclassTest {
publicstaticvoidmain(String[] args) throwsIOException, ParserConfigurationException, SAXException {
HttpURLConnectionconnection;
DataOutputStreamoutStream;
Stringsentence;
sentence = "Bruce Springsteen is an American singer-songwriter and multi-instrumentalist.";
for(inti=0; i<args.length; i++){
sentence = sentence.concat(args[i]+" ");
}
URLurl = newURL("http://sandbox-xlike.isoco.com/services/analysis_en/analyze");
Stringbody = 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 = newDataOutputStream(connection.getOutputStream());
outStream.writeBytes(body);
outStream.flush();
outStream.close();
System.out.println("Receiving response...");
//Receiving response
if (connection.getResponseCode() != 200) {
thrownewIOException(connection.getResponseMessage());
}
InputStreamis = connection.getInputStream();
BufferedReaderrd = newBufferedReader(newInputStreamReader(is, "UTF-8"));
StringBufferresponse = newStringBuffer();
Stringstr;
while ((str = rd.readLine()) != null) {
response.append(str);
}
rd.close();
System.out.println(response);
Filefile = newFile("test.xml");
Writerwriter = newOutputStreamWriter(newFileOutputStream(file), "UTF-8");
writer.write(response.toString());
writer.flush();
writer.close();
DocumentBuilderFactorydbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilderdBuilder = dbFactory.newDocumentBuilder();
Documentdoc = dBuilder.parse(newFile("test.xml"));
doc.getDocumentElement().normalize();
NodeListnodes = doc.getElementsByTagName("sentence");
for (inti = 0; i < nodes.getLength(); i++) {
Nodenode = nodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Elementelement = (Element) node;
NodeListsents = element.getElementsByTagName("text").item(0).getChildNodes();
System.out.println(sents.item(0).getNodeValue());
//Tokens
Elementtokens = (Element) element.getElementsByTagName("tokens").item(0);
NodeListlist = tokens.getElementsByTagName("token");
for (intj = 0; j < list.getLength(); j++) {
Nodetoken = list.item(j);
System.out.println(token.getTextContent());
}
}
}
}
}