-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
StanfordCoreNLPServerITest.java
181 lines (150 loc) · 7.68 KB
/
StanfordCoreNLPServerITest.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
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
package edu.stanford.nlp.pipeline;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.Assert;
import edu.stanford.nlp.io.IOUtils;
import edu.stanford.nlp.io.RuntimeIOException;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.util.CoreMap;
import edu.stanford.nlp.util.PropertiesUtils;
import edu.stanford.nlp.util.StringUtils;
/**
* Test launching a server and running a few random commands
*/
public class StanfordCoreNLPServerITest {
static StanfordCoreNLPServer server;
static int port;
@BeforeClass
public static void launchServer() throws IOException {
String[] args = { "-port", "0", "-preload", "tokenize,ssplit,pos,parse" };
server = StanfordCoreNLPServer.launchServer(args);
port = server.getServer().get().getAddress().getPort();
System.out.println("Server running on port " + port);
}
@Test
public void testLive() {
String result = IOUtils.slurpURLNoExceptions("http://localhost:" + port + "/live");
Assert.assertNotNull(result);
Assert.assertEquals("live", result.trim());
}
@Test
public void testReady() {
String result = IOUtils.slurpURLNoExceptions("http://localhost:" + port + "/ready");
Assert.assertNotNull(result);
Assert.assertEquals("ready", result.trim());
}
@Test
public void testClient() {
String query = "The dog ate a fish";
Properties props = new Properties();
props.setProperty("annotators", "tokenize,ssplit,pos,parse");
StanfordCoreNLPClient client = new StanfordCoreNLPClient(props, "http://localhost", port);
// if something goes wrong, we don't want the unittest waiting forever for a response
client.setTimeoutMilliseconds(30 * 1000);
Annotation annotation = client.process(query);
Throwable t = annotation.get(CoreAnnotations.ExceptionAnnotation.class);
Assert.assertNull(t);
List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
Assert.assertEquals(1, sentences.size());
}
@Test
public void testClientFailure() {
String query = "The dog ate a fish";
Properties props = new Properties();
props.setProperty("annotators", "tokenize,ssplit,pos,parse");
//StanfordCoreNLPClient client = new StanfordCoreNLPClient(props, "http://localhost", port);
StanfordCoreNLPClient client = new StanfordCoreNLPClient(props, "localhost", port);
client.setTimeoutMilliseconds(1000);
Annotation annotation = client.process(query);
Throwable t = annotation.get(CoreAnnotations.ExceptionAnnotation.class);
Assert.assertNotNull(t);
}
public InputStream postURL(URL serverURL, byte[] message) throws IOException {
URLConnection connection = serverURL.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
connection.setRequestProperty("Accept-Charset", "utf-8");
connection.setRequestProperty("User-Agent", StanfordCoreNLPServerITest.class.getName());
connection.setConnectTimeout(30 * 1000);
connection.setReadTimeout(30 * 1000);
((HttpURLConnection) connection).setRequestMethod("POST");
connection.connect();
connection.getOutputStream().write(message);
connection.getOutputStream().flush();
return connection.getInputStream();
}
public String slurpURL(URL serverURL, byte[] message) throws IOException {
InputStream in = postURL(serverURL, message);
String response = IOUtils.slurpInputStream(in, "utf-8");
return response;
}
@Test
public void testTregexJson() throws IOException {
String expected="{\"sentences\":[{\"0\":{\"sentIndex\":0,\"characterOffsetBegin\":4,\"characterOffsetEnd\":7,\"match\":\"(NNdog)\\n\",\"spanString\":\"dog\",\"namedNodes\":[]},\"1\":{\"sentIndex\":0,\"characterOffsetBegin\":14,\"characterOffsetEnd\":18,\"match\":\"(NNfish)\\n\",\"spanString\":\"fish\",\"namedNodes\":[]}}]}".replaceAll(" ", "");
String query = "The dog ate a fish";
byte[] message = query.getBytes("utf-8");
Properties props = new Properties();
props.setProperty("annotators", "tokenize,ssplit,pos,parse");
String queryParams = String.format("pattern=NN&properties=%s",
URLEncoder.encode(PropertiesUtils.propsAsJsonString(props), "utf-8"));
URL serverURL = new URL("http", "localhost", port, "/tregex?" + queryParams);
String response = slurpURL(serverURL, message);
Assert.assertEquals(expected, response.replaceAll(" ", "").replaceAll("\n", ""));
}
@Test
public void testSemgrexJson() throws IOException {
String expected="{ \"sentences\": [ { \"0\": { \"text\": \"ate\", \"begin\": 2, \"end\": 3, \"$verb\": { \"text\": \"ate\", \"begin\": 2, \"end\": 3 }, \"$obj\": { \"text\": \"fish\", \"begin\": 4, \"end\": 5 } }, \"length\": 1 } ]}".replaceAll(" ", "");
String query = "The dog ate a fish";
byte[] message = query.getBytes("utf-8");
Properties props = new Properties();
props.setProperty("annotators", "tokenize,ssplit,pos,parse");
String queryParams = String.format("pattern=%s&properties=%s",
URLEncoder.encode("{}=verb >obj {}=obj", "utf-8"),
URLEncoder.encode(PropertiesUtils.propsAsJsonString(props), "utf-8"));
URL serverURL = new URL("http", "localhost", port, "/semgrex?" + queryParams);
String response = slurpURL(serverURL, message);
Assert.assertEquals(expected, response.replaceAll(" ", "").replaceAll("\n", ""));
}
@Test
public void testSemgrexAnnotation() throws IOException {
String expected = "result { result { match { matchIndex: 3 node { name: \"verb\" matchIndex: 3 } node { name: \"obj\" matchIndex: 5 } graphIndex:0 semgrexIndex:0 } }}".replaceAll(" ", "");
String query = "The dog ate a fish";
byte[] message = query.getBytes("utf-8");
Properties props = new Properties();
props.setProperty("annotators", "tokenize,ssplit,pos,parse");
String queryParams = String.format("pattern=%s&properties=%s&outputFormat=serialized",
URLEncoder.encode("{}=verb >obj {}=obj", "utf-8"),
URLEncoder.encode(PropertiesUtils.propsAsJsonString(props), "utf-8"));
URL serverURL = new URL("http", "localhost", port, "/semgrex?" + queryParams);
InputStream is = postURL(serverURL, message);
CoreNLPProtos.SemgrexResponse response = CoreNLPProtos.SemgrexResponse.parseFrom(is);
Assert.assertEquals(expected, response.toString().replaceAll(" ", "").replaceAll("\n", ""));
}
/**
* The first sentence should match this query, but the second one should not.
*/
@Test
public void testSemgrexFilter() throws IOException {
String expected="{ \"sentences\": [ true, false ]}".replaceAll(" ", "");
String query = "The dog ate a fish. He went home.";
byte[] message = query.getBytes("utf-8");
Properties props = new Properties();
props.setProperty("annotators", "tokenize,ssplit,pos,parse");
String queryParams = String.format("pattern=%s&properties=%s&filter=true",
URLEncoder.encode("{}=verb >obj {}=obj", "utf-8"),
URLEncoder.encode(PropertiesUtils.propsAsJsonString(props), "utf-8"));
URL serverURL = new URL("http", "localhost", port, "/semgrex?" + queryParams);
String response = slurpURL(serverURL, message);
Assert.assertEquals(expected, response.replaceAll(" ", "").replaceAll("\n", ""));
}
}