Repository navigation
Expand file tree
/
Copy pathPythonPRTest.java
More file actions
150 lines (113 loc) · 4.97 KB
/
Copy pathPythonPRTest.java
File metadata and controls
150 lines (113 loc) · 4.97 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
package gate.python;
import gate.*;
import gate.creole.ResourceInstantiationException;
import gate.creole.SerialAnalyserController;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.File;
import java.net.URL;
import static org.junit.Assert.*;
/**
* Created by "Dominic Rout" on 04/07/2017.
*/
public class PythonPRTest {
private static final String TEST_TEXT = "this is a test document";
private SerialAnalyserController controller;
private PythonPR pythonPR;
private Corpus corpus;
private Document document;
@BeforeClass
public static void setupClass() throws Exception {
Gate.init();
Gate.getCreoleRegister().registerDirectories(new File(".").toURI().toURL()); // Add this directory as a plugin.
}
@Before
public void setUp() throws Exception {
controller = (SerialAnalyserController) Factory.createResource("gate.creole.SerialAnalyserController");
pythonPR = (PythonPR) Factory.createResource("gate.python.PythonPR");
controller.add(pythonPR);
loadDocument(TEST_TEXT);
}
public void setDocument(Document document) throws ResourceInstantiationException {
if (corpus != null) {
Factory.deleteResource(corpus);
}
if (this.document != null) {
Factory.deleteResource(this.document);
}
corpus = Factory.newCorpus("test_corpus");
this.document = document;
corpus.add(this.document);
controller.setCorpus(corpus);
controller.setDocument(this.document);
}
public void loadDocument(String documentText) throws ResourceInstantiationException {
setDocument(Factory.newDocument(documentText));
}
public void loadDocumentXML(URL url) throws ResourceInstantiationException {
setDocument(Factory.newDocument(url));
}
@Test
public void addDocumentSpan() throws Exception {
pythonPR.setScript(new File("examples/add_document_span.py").toURI().toURL());
controller.controllerExecutionStarted(controller);
controller.execute();
controller.controllerExecutionFinished(controller);
// Check that we got a new annotation in the document.
assertTrue(document.getAnnotationSetNames().contains("python"));
assertEquals(document.getAnnotations("python").size(), 1);
assertEquals(document.getAnnotations("python").get(0).getStartNode().getOffset().longValue(), 0l);
assertEquals(document.getAnnotations("python").get(0).getEndNode().getOffset().intValue(),
TEST_TEXT.length());
assertEquals(document.getAnnotations("python").get(0).getFeatures().get("text"),
TEST_TEXT);
}
@Test
public void testFunctionPR() throws Exception {
pythonPR.setScript(new File("examples/add_document_span_function.py").toURI().toURL());
pythonPR.setOutputAS("python");
controller.controllerExecutionStarted(controller);
controller.execute();
controller.controllerExecutionFinished(controller);
// Check that we got a new annotation in the document.
assertTrue(document.getAnnotationSetNames().contains("python"));
assertEquals(document.getAnnotations("python").size(), 1);
assertEquals(document.getAnnotations("python").get(0).getStartNode().getOffset().longValue(), 0l);
assertEquals(document.getAnnotations("python").get(0).getEndNode().getOffset().intValue(),
TEST_TEXT.length());
assertEquals(document.getAnnotations("python").get(0).getFeatures().get("text"),
TEST_TEXT);
}
@Test
public void testNullKeyInMap() throws Exception {
pythonPR.setScript(new File("examples/tokenise_badly.py").toURI().toURL());
loadDocumentXML(this.getClass().getResource("/bad_document.xml"));
controller.controllerExecutionStarted(controller);
controller.execute();
controller.controllerExecutionFinished(controller);
// Check that we got a new annotation in the document.
assertEquals(document.getAnnotations().size(), 13);
}
@Test
public void testAnnotationInDefaultSet() throws Exception {
pythonPR.setScript(new File("examples/tokenise_badly.py").toURI().toURL());
Document document = Factory.newDocument("anything goes");
document.getAnnotations().add(0l, 4l, "test", Factory.newFeatureMap());
controller.controllerExecutionStarted(controller);
controller.execute();
controller.controllerExecutionFinished(controller);
// Check that we got a new annotation in the document.
assertEquals(document.getAnnotations().size(), 1);
}
@After
public void tearDown() throws Exception {
Factory.deleteResource(pythonPR);
Factory.deleteResource(controller);
Factory.deleteResource(corpus);
Factory.deleteResource(document);
corpus = null;
document = null;
}
}