use of edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation in project cogcomp-nlp by CogComp.
the class ServerClientAnnotator method annotate.
public TextAnnotation annotate(String str) throws Exception {
String viewsConnected = Arrays.toString(viewsToAdd);
String views = viewsConnected.substring(1, viewsConnected.length() - 1).replace(" ", "");
ConcurrentMap<String, byte[]> concurrentMap = (db != null) ? db.hashMap(viewName, Serializer.STRING, Serializer.BYTE_ARRAY).createOrOpen() : null;
String key = DigestUtils.sha1Hex(str + views);
if (concurrentMap != null && concurrentMap.containsKey(key)) {
byte[] taByte = concurrentMap.get(key);
return SerializationHelper.deserializeTextAnnotationFromBytes(taByte);
} else {
URL obj = new URL(url + ":" + port + "/annotate");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("charset", "utf-8");
con.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
con.setDoOutput(true);
con.setUseCaches(false);
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
wr.write("text=" + URLEncoder.encode(str, "UTF-8") + "&views=" + views);
wr.flush();
int responseCode = con.getResponseCode();
logger.debug("\nSending '" + con.getRequestMethod() + "' request to URL : " + url);
logger.debug("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
TextAnnotation ta = SerializationHelper.deserializeFromJson(response.toString());
if (concurrentMap != null) {
concurrentMap.put(key, SerializationHelper.serializeTextAnnotationToBytes(ta));
this.db.commit();
}
return ta;
}
}
use of edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation in project cogcomp-nlp by CogComp.
the class CachingPipelineTest method stanfordFailTest.
@Test
public void stanfordFailTest() {
String inputFile = "src/test/resources/stanfordFailExample.txt";
String text = null;
try {
text = LineIO.slurp(inputFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
fail(e.getMessage());
}
TextAnnotation basicTextAnnotation = null;
try {
basicTextAnnotation = processor.createBasicTextAnnotation("test", "test", text);
} catch (AnnotatorException e) {
e.printStackTrace();
fail(e.getMessage());
}
try {
processor.addView(basicTextAnnotation, ViewNames.DEPENDENCY_STANFORD);
} catch (RuntimeException | AnnotatorException e) {
e.printStackTrace();
System.out.println("Expected exception from stanford.");
}
System.out.println(basicTextAnnotation.toString());
}
use of edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation in project cogcomp-nlp by CogComp.
the class TestDiff method testAnnotatorDiff.
@Test
public void testAnnotatorDiff() {
POSAnnotator annotator = new POSAnnotator();
TextAnnotation record = BasicTextAnnotationBuilder.createTextAnnotationFromTokens(refTokens);
try {
annotator.getView(record);
} catch (AnnotatorException e) {
fail("AnnotatorException thrown!\n" + e.getMessage());
}
TokenLabelView view = (TokenLabelView) record.getView(ViewNames.POS);
if (refTags.size() != view.getNumberOfConstituents()) {
fail("Number of tokens tagged in annotator does not match actual number of tokens!");
}
int correctCounter = 0;
for (int i = 0; i < refTags.size(); i++) {
if (view.getLabel(i).equals(refTags.get(i))) {
correctCounter++;
}
}
double result = ((double) correctCounter) / refTags.size();
if (result < thresholdAcc) {
fail("Tagger performance is insufficient: " + "\nProduced: " + result + "\nExpected: " + thresholdAcc);
}
}
use of edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation in project cogcomp-nlp by CogComp.
the class PrepSRLDataReader method makeNewTextAnnotation.
private TextAnnotation makeNewTextAnnotation(Element item) {
String id = item.getAttribute("id");
NodeList nl = item.getElementsByTagName("context");
NodeList children = nl.item(0).getChildNodes();
String rawSentenceString = nl.item(0).getTextContent().replaceAll("[\\t\\n]", "").trim();
String preposition = "";
int prepositionPosition = -1;
for (int i = 0; i < children.getLength(); i++) {
Node currentNode = children.item(i);
if (currentNode.getNodeName().equals("head")) {
preposition = currentNode.getTextContent().toLowerCase();
int previousLength = 0;
if (i > 0)
previousLength = tokenize(children.item(i - 1).getTextContent()).size();
prepositionPosition = previousLength;
}
}
String label;
if (corpusName.equals("test")) {
if (keys.containsKey(id))
label = keys.get(id);
else
return null;
} else {
label = ((Element) (item.getElementsByTagName("answer").item(0))).getAttribute("senseid");
}
// Take only the first label for the 500 or so instances which are given multiple labels.
if (label.contains(" "))
label = label.substring(0, label.indexOf(" ")).trim();
if (label.length() == 0) {
log.info("No label for id {}, ignoring sentence", id);
return null;
}
rawSentenceString = rawSentenceString.replaceAll("`", "``");
rawSentenceString = rawSentenceString.replaceAll("\"", "''");
// XXX Assume text is pre-tokenized
String[] tokens = rawSentenceString.split("\\s+");
TextAnnotation ta = BasicTextAnnotationBuilder.createTextAnnotationFromTokens("Semeval2007Prepositions", id, Collections.singletonList(tokens));
if (!ta.getTokens()[prepositionPosition].toLowerCase().equals(preposition)) {
assert false;
}
TokenLabelView prepositionLabelView = new TokenLabelView(viewName, ta);
String role = senseToRole.get(preposition + ":" + label);
prepositionLabelView.addTokenLabel(prepositionPosition, role, 1.0);
ta.addView(viewName, prepositionLabelView);
return ta;
}
use of edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation in project cogcomp-nlp by CogComp.
the class SentencePipelineTest method testFailingPosFile.
@Test
public void testFailingPosFile() {
String text = null;
try {
text = LineIO.slurp(POS_FILE);
} catch (FileNotFoundException e) {
e.printStackTrace();
fail(e.getMessage());
}
TextAnnotation ta = null;
try {
ta = sentenceProcessor.createAnnotatedTextAnnotation("testPos", "tesPos", text);
} catch (AnnotatorException e) {
e.printStackTrace();
fail(e.getMessage());
}
Constituent s = ta.getView(ViewNames.SENTENCE).getConstituents().get(3);
List<Constituent> posConstituentsInThirdSent = ta.getView(ViewNames.POS).getConstituentsOverlappingCharSpan(s.getStartCharOffset(), s.getEndCharOffset());
List<Constituent> toksInThirdSent = ta.getView(ViewNames.TOKENS).getConstituentsOverlappingCharSpan(s.getStartCharOffset(), s.getEndCharOffset());
assertTrue(posConstituentsInThirdSent.size() > 0);
assertEquals(toksInThirdSent.size(), posConstituentsInThirdSent.size());
}
Aggregations