-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathTestTextRazor.java
More file actions
264 lines (192 loc) · 8.96 KB
/
Copy pathTestTextRazor.java
File metadata and controls
264 lines (192 loc) · 8.96 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package com.textrazor;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.textrazor.account.AccountManager;
import com.textrazor.account.model.Account;
import com.textrazor.annotations.Custom;
import com.textrazor.annotations.Entity;
import com.textrazor.annotations.AnalyzedText;
import com.textrazor.annotations.ScoredCategory;
import com.textrazor.annotations.Sentence;
import com.textrazor.annotations.Topic;
import com.textrazor.annotations.Word;
import com.textrazor.annotations.Word.Sense;
import com.textrazor.annotations.Word.Suggestion;
import com.textrazor.classifier.ClassifierManager;
import com.textrazor.classifier.model.Category;
import com.textrazor.dictionary.DictionaryManager;
import com.textrazor.dictionary.model.Dictionary;
import com.textrazor.dictionary.model.DictionaryEntry;
import com.textrazor.dictionary.model.PagedAllEntries;
public class TestTextRazor {
public static void testAccount(String apiKey) throws NetworkException, AnalysisException {
AccountManager manager = new AccountManager(apiKey);
Account account = manager.getAccount();
System.out.println("Your current account plan is " + account.getPlan() + ", which includes " + account.getPlanDailyRequestsIncluded() + " daily requests, " + account.getRequestsUsedToday() + " used today");
}
public static void testClassifier(String apiKey) throws NetworkException, AnalysisException {
ClassifierManager manager = new ClassifierManager(apiKey);
String testClassifierId = "java_test_classifier";
try {
for (Category cat : manager.allCategories(testClassifierId).getCategories()) {
System.out.println("Current category: " + cat.getCategoryId());
}
manager.deleteClassifier(testClassifierId);
}
catch (AnalysisException ex) {}
System.out.println("Creating classifier..");
try {
manager.createClassifier(testClassifierId,
Arrays.asList(Category.builder()
.setCategoryId("ID1")
.setQuery("or(concept('sport>soccer'),concept('sport>association football'))")
.build(),
Category.builder()
.setCategoryId("ID2")
.setQuery("or(concept('sport>super bowl'),concept('sport>NFL'),concept('sport>american football'))")
.build()));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Finished Creating classifier");
System.out.println(manager.getCategory(testClassifierId, "ID1").getCategoryId());
{
for (Category cat : manager.allCategories(testClassifierId).getCategories()) {
System.out.println("Final Classifier: " + cat.getCategoryId() + " " + cat.getQuery());
}
}
TextRazor client = new TextRazor(apiKey);
String url = "http://us.cnn.com/2016/02/01/us/carolina-panthers-haters/index.html";
client.setCleanupMode("cleanHTML");
client.setClassifiers(Arrays.asList(testClassifierId));
AnalyzedText response = client.analyzeurl(/sitelet?url=https%3A%2F%2Fgithub.com%2FTextRazor%2Ftextrazor-java%2Fblob%2Fmaster%2Ftest%2Fcom%2Ftextrazor%2Furl);
for (ScoredCategory category : response.getResponse().getCategories()) {
System.out.println("Category:" + category.getCategoryId() + " " + category.getLabel() + " " + category.getScore());
}
}
public static void testDictionary(String apiKey) throws NetworkException, AnalysisException {
DictionaryManager dictionaryManager = new DictionaryManager(apiKey);
// Delete all dictionaries in our account.
{
for (Dictionary dict : dictionaryManager.allDictionaries()) {
System.out.println("Deleting current dictionary: " + dict.getId());
dictionaryManager.deleteDictionary(dict);
}
}
// Create a new dictionary
Dictionary newDict = Dictionary.builder().setId("developers").build();
dictionaryManager.createDictionary(newDict);
Dictionary teamsDictionary = dictionaryManager.getDictionary("developers");
System.out.println("Got dict:");
System.out.println(teamsDictionary.getId());
{
List<DictionaryEntry> newEntries = new ArrayList<DictionaryEntry>();
List<String> types = Arrays.asList("cpp_developer", "writer");
newEntries.add(DictionaryEntry.builder().setText("Bjarne Stroustrup").setId("DEV2").addData("types", types).build());
dictionaryManager.addEntries(newDict.getId(), newEntries);
}
// Loop over all entries in a specific dictionary
PagedAllEntries allEntries = dictionaryManager.allEntries(newDict.getId());
System.out.println("Dictionary contains : " + allEntries.getTotal() + " total entries.");
for (DictionaryEntry entry : allEntries.getEntries()) {
System.out.println("Dictionary entry in 'developers':");
System.out.println(entry.getId() + " " + entry.getText());
}
// Retrieve them directly by ID
{
DictionaryEntry entry = dictionaryManager.getEntry(newDict.getId(), "DEV2");
System.out.println("Entry text: " + entry.getText());
}
// Try extracting the new entry from some sample text.
TextRazor client = new TextRazor(apiKey);
client.setEntityDictionaries(Arrays.asList(newDict.getId()));
client.addExtractor("entities");
AnalyzedText response = client.analyze("Although it is very early in the process, higher-level parallelism is slated to be a key theme of the next version of C++, says Bjarne Stroustrup");
for (Entity entity : response.getResponse().getEntities()) {
String customEntityId = entity.getCustomEntityId();
if (null != customEntityId) {
System.out.println("Found custom entity: " + customEntityId);
for (String type : entity.getData().get("types")) {
System.out.println("Type: " + type);
}
}
}
}
public static void testAnalysis(String apiKey) throws NetworkException, AnalysisException {
// Sample request, showcasing a couple of TextRazor features
TextRazor client = new TextRazor(apiKey);
client.addExtractor("words");
client.addExtractor("entities");
client.addExtractor("topics");
client.addExtractor("spelling");
client.setClassifiers(Arrays.asList("textrazor_newscodes"));
//client.addExtractor("entailments");
client.addExtractor("senses");
//client.addExtractor("phrases");
//client.addExtractor("dependency-trees");
//client.addExtractor("relations");
client.addExtractor("entity_companies");
client.setEnrichmentQueries(Arrays.asList("fbase:/location/location/geolocation>/location/geocode/latitude", "fbase:/location/location/geolocation>/location/geocode/longitude"));
String rules = "entity_companies(CompanyEntity) :- entity_type(CompanyEntity, 'Company').";
client.setRules(rules);
AnalyzedText response = client.analyze("LONDON - Barclays misled shareholders and the public RBS about one of the biggest investments in the bank's history, a BBC Panrama investigation has found.");
System.out.println(response.isOk());
for (ScoredCategory category : response.getResponse().getCategories()) {
System.out.println("Category:" + category.getCategoryId() + " " + category.getLabel() + " " + category.getScore());
}
for (Topic topic : response.getResponse().getTopics()) {
System.out.println(topic.getId() + " " + topic.getWikidataId());
}
for (Sentence sentence : response.getResponse().getSentences()) {
for (Word word : sentence.getWords()) {
System.out.println("----------------");
System.out.println("Word: " + word.getToken());
for (Word child : word.getChildren()) {
System.out.println("Child: " + child.getToken());
}
for (Entity entity : word.getEntities()) {
System.out.println("Matched Entity: " + entity.getEntityEnglishId());
Map<String, List<String>> entityData = entity.getData();
List<String> latitudeValues = entityData.get("fbase:/location/location/geolocation>/location/geocode/latitude");
List<String> longitudeValues = entityData.get("fbase:/location/location/geolocation>/location/geocode/longitude");
if (null != latitudeValues) {
System.out.println("Entity latitude: " + latitudeValues.get(0));
}
if (null != longitudeValues) {
System.out.println("Entity longitude: " + longitudeValues.get(0));
}
}
for (Sense sense: word.getSenses()) {
System.out.println("Word sense: " + sense.getSynset() + " has score: " + sense.getScore());
}
for (Suggestion suggestion : word.getSpellingSuggestions()) {
System.out.println("Word suggestion: " + suggestion.getSuggestion() + " has score: " + suggestion.getScore());
}
}
}
// Use a custom rule to match 'Company' type entities
for (Custom custom : response.getResponse().getCustomAnnotations()) {
for (Custom.BoundVariable variable : custom.getContents()) {
if (null != variable.getEntityValue()) {
for (Entity entity : variable.getEntityValue()) {
System.out.println("Variable: " + variable.getKey() + " Value:" + entity.getEntityId());
}
}
}
}
}
/**
* @param args
* @throws NetworkException
*/
public static void main(String[] args) throws NetworkException, AnalysisException {
String API_KEY = "YOUR_API_KEY_HERE";
testAccount(API_KEY);
testClassifier(API_KEY);
testDictionary(API_KEY);
testAnalysis(API_KEY);
}
}