-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathAnnotateText.java
145 lines (127 loc) · 5.75 KB
/
AnnotateText.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
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
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class AnnotateText {
static final String REST_URL = "http://data.bioontology.org";
static final String API_KEY = "";
static final ObjectMapper mapper = new ObjectMapper();
public static void main(String[] args) throws Exception {
String urlParameters;
JsonNode annotations;
String textToAnnotate = URLEncoder.encode("Melanoma is a malignant tumor of melanocytes which are found predominantly in skin but also in the bowel and the eye.", "ISO-8859-1");
// Get just annotations
urlParameters = "text=" + textToAnnotate;
annotations = jsonToNode(get(REST_URL + "/annotator?" + urlParameters));
printAnnotations(annotations);
// Annotations with hierarchy
urlParameters = "max_level=3&text=" + textToAnnotate;
annotations = jsonToNode(get(REST_URL + "/annotator?" + urlParameters));
printAnnotations(annotations);
// Annotations using POST (necessary for long text)
urlParameters = "text=" + textToAnnotate;
annotations = jsonToNode(post(REST_URL + "/annotator", urlParameters));
printAnnotations(annotations);
// Get labels, synonyms, and definitions with returned annotations
urlParameters = "include=prefLabel,synonym,definition&text=" + textToAnnotate;
annotations = jsonToNode(get(REST_URL + "/annotator?" + urlParameters));
for (JsonNode annotation : annotations) {
System.out.println(annotation.get("annotatedClass").get("prefLabel").asText());
}
}
private static void printAnnotations(JsonNode annotations) {
for (JsonNode annotation : annotations) {
// Get the details for the class that was found in the annotation and print
JsonNode classDetails = jsonToNode(get(annotation.get("annotatedClass").get("links").get("self").asText()));
System.out.println("Class details");
System.out.println("\tid: " + classDetails.get("@id").asText());
System.out.println("\tprefLabel: " + classDetails.get("prefLabel").asText());
System.out.println("\tontology: " + classDetails.get("links").get("ontology").asText());
System.out.println("\n");
JsonNode hierarchy = annotation.get("hierarchy");
// If we have hierarchy annotations, print the related class information as well
if (hierarchy.isArray() && hierarchy.elements().hasNext()) {
System.out.println("\tHierarchy annotations");
for (JsonNode hierarchyAnnotation : hierarchy) {
classDetails = jsonToNode(get(hierarchyAnnotation.get("annotatedClass").get("links").get("self").asText()));
System.out.println("\t\tClass details");
System.out.println("\t\t\tid: " + classDetails.get("@id").asText());
System.out.println("\t\t\tprefLabel: " + classDetails.get("prefLabel").asText());
System.out.println("\t\t\tontology: " + classDetails.get("links").get("ontology").asText());
}
}
}
}
private static JsonNode jsonToNode(String json) {
JsonNode root = null;
try {
root = mapper.readTree(json);
} catch (JsonProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return root;
}
private static String get(String urlToGet) {
URL url;
HttpURLConnection conn;
BufferedReader rd;
String line;
String result = "";
try {
url = new URL(urlToGet);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Authorization", "apikey token=" + API_KEY);
conn.setRequestProperty("Accept", "application/json");
rd = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null) {
result += line;
}
rd.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
private static String post(String urlToGet, String urlParameters) {
URL url;
HttpURLConnection conn;
String line;
String result = "";
try {
url = new URL(urlToGet);
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setInstanceFollowRedirects(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "apikey token=" + API_KEY);
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("charset", "utf-8");
conn.setUseCaches(false);
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
conn.disconnect();
BufferedReader rd = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null) {
result += line;
}
rd.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}