-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnnotation_example.java
More file actions
149 lines (122 loc) · 4.71 KB
/
Annotation_example.java
File metadata and controls
149 lines (122 loc) · 4.71 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
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
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_Example {
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());
}
}
}
///////////////////////////////////////////
//Semantic annotations
///////////////////////////////////////////
URL curl2 = new URL("http://km.aifb.kit.edu/services/annotation-en/");
HttpURLConnection urlConnection2;
DataOutputStream outStream2;
InputStream iscA;
//Opening connection
urlConnection2 = (HttpURLConnection) curl2.openConnection();
urlConnection2.setRequestMethod("POST");
urlConnection2.setDoInput(true);
urlConnection2.setDoOutput(true);
urlConnection2.setUseCaches(false);
String full = response.toString();
urlConnection2.setRequestProperty("Content-Length",
"" + Integer.toString(full.getBytes().length));
urlConnection2.setRequestProperty("Content-Type", "text/xml");
// Create I/O streams
outStream2 = new DataOutputStream(urlConnection2.getOutputStream());
outStream2.write(full.getBytes("UTF-8"));
outStream2.flush();
outStream2.close();
StringBuffer response2= new StringBuffer();
String str2;
iscA = urlConnection2.getInputStream();
BufferedReader rd2 = new BufferedReader(new InputStreamReader(iscA, "UTF-8"));
response2 = new StringBuffer();
str="";
// Printing response
while ((str2 = rd2.readLine()) != null) {
response2.append(str2);
}
String result= response2.toString();
System.out.println(result);
}
}