Search in sources :

Example 91 with TextAnnotation

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;
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) TextAnnotation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation) URL(java.net.URL)

Example 92 with TextAnnotation

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());
}
Also used : FileNotFoundException(java.io.FileNotFoundException) AnnotatorException(edu.illinois.cs.cogcomp.annotation.AnnotatorException) TextAnnotation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation)

Example 93 with TextAnnotation

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);
    }
}
Also used : POSAnnotator(edu.illinois.cs.cogcomp.pos.POSAnnotator) AnnotatorException(edu.illinois.cs.cogcomp.annotation.AnnotatorException) TokenLabelView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TokenLabelView) TextAnnotation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation) Test(org.junit.Test)

Example 94 with TextAnnotation

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;
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) TokenLabelView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TokenLabelView) TextAnnotation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation)

Example 95 with TextAnnotation

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());
}
Also used : FileNotFoundException(java.io.FileNotFoundException) AnnotatorException(edu.illinois.cs.cogcomp.annotation.AnnotatorException) TextAnnotation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation) Constituent(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent) Test(org.junit.Test)

Aggregations

TextAnnotation (edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation)218 Constituent (edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent)95 Test (org.junit.Test)65 View (edu.illinois.cs.cogcomp.core.datastructures.textannotation.View)49 Feature (edu.illinois.cs.cogcomp.edison.features.Feature)48 AnnotatorException (edu.illinois.cs.cogcomp.annotation.AnnotatorException)29 DiscreteFeature (edu.illinois.cs.cogcomp.edison.features.DiscreteFeature)28 TreeView (edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView)25 ArrayList (java.util.ArrayList)23 EdisonException (edu.illinois.cs.cogcomp.edison.utilities.EdisonException)22 LinkedHashSet (java.util.LinkedHashSet)21 IntPair (edu.illinois.cs.cogcomp.core.datastructures.IntPair)16 Relation (edu.illinois.cs.cogcomp.core.datastructures.textannotation.Relation)16 FeatureExtractor (edu.illinois.cs.cogcomp.edison.features.FeatureExtractor)16 ProjectedPath (edu.illinois.cs.cogcomp.edison.features.lrec.ProjectedPath)16 FeatureManifest (edu.illinois.cs.cogcomp.edison.features.manifest.FeatureManifest)16 FileInputStream (java.io.FileInputStream)16 TokenLabelView (edu.illinois.cs.cogcomp.core.datastructures.textannotation.TokenLabelView)14 SpanLabelView (edu.illinois.cs.cogcomp.core.datastructures.textannotation.SpanLabelView)12 PredicateArgumentView (edu.illinois.cs.cogcomp.core.datastructures.textannotation.PredicateArgumentView)11