Search in sources :

Example 6 with Sentence

use of edu.stanford.nlp.simple.Sentence in project Info-Evaluation by TechnionYP5777.

the class AnalyzeParagraph method AnalyzeArrestsQuery.

public void AnalyzeArrestsQuery() {
    /*
		 * First step is initiating the Stanford CoreNLP pipeline (the pipeline
		 * will be later used to evaluate the text and annotate it) Pipeline is
		 * initiated using a Properties object which is used for setting all
		 * needed entities, annotations, training data and so on, in order to
		 * customized the pipeline initialization to contains only the models
		 * you need
		 */
    final Properties props = new Properties();
    /*
		 * The "annotators" property key tells the pipeline which entities
		 * should be initiated with our pipeline object, See
		 * http://nlp.stanford.edu/software/corenlp.shtml for a complete
		 * reference to the "annotators" values you can set here and what they
		 * will contribute to the analyzing process
		 */
    props.put("annotators", "tokenize,ssplit, pos, regexner, parse,lemma,natlog,openie");
    final StanfordCoreNLP pipeLine = new StanfordCoreNLP(props);
    // inputText will be the text to evaluate in this example
    int index = 0;
    for (final Element paragraph : this.Paragraphs) {
        final String inputText = paragraph.text() + "";
        final Annotation document = new Annotation(inputText);
        System.out.println(document);
        String reason = "";
        // more details about the reason. e.g - where it
        String details = "";
        // happened.
        String aux = "";
        String prefixDetails = "";
        // this string tells us what is the penalty for the arrest.
        String penalty = "";
        // Finally we use the pipeline to annotate the document we created
        pipeLine.annotate(document);
        for (final CoreMap sentence : document.get(SentencesAnnotation.class)) {
            Sentence sent = new Sentence(sentence);
            if (sent.text().contains("sentenced") || sent.text().contains("juried") || sent.text().contains("sent to jail") || sent.text().contains("charged")) {
                penalty = ArrestPenalty(sent);
                System.out.println("Sentenced for:" + penalty);
            }
            final SemanticGraph dependencies = sentence.get(CollapsedDependenciesAnnotation.class);
            for (final IndexedWord root : dependencies.getRoots()) for (final SemanticGraphEdge edge : dependencies.getOutEdgesSorted(root)) {
                final IndexedWord dep = edge.getDependent();
                final String rel = edge.getRelation() + "";
                if (!"arrested".equals(edge.getGovernor().word()))
                    switch(rel) {
                        case "nmod:in":
                            details += "in" + " " + dep.word() + " ";
                            break;
                        case "nmod:during":
                            details += "during" + " " + dep.word() + " ";
                            break;
                        case "nmod:at":
                            details += "at" + " " + dep.word() + " ";
                            break;
                    }
                else {
                    //Finding the reason in the paragraph
                    if ("advcl".equals(rel) || "advcl:for".equals(rel) || "nmod:for".equals(rel)) {
                        for (final SemanticGraphEdge keshet : dependencies.getOutEdgesSorted(dep)) {
                            final String rel2 = keshet.getRelation() + "";
                            final IndexedWord dep2 = keshet.getDependent();
                            if ("amod".equals(rel2) || "dobj".equals(rel2)) {
                                reason += dep2.word() + " ";
                                try {
                                    prefixDetails = ((sentence + "").substring(dep.beginPosition(), dep2.endPosition()));
                                } catch (IndexOutOfBoundsException e) {
                                    prefixDetails = sentence + "";
                                }
                            }
                            if ("xcomp".equals(rel2))
                                aux += " " + dep2.word();
                            switch(rel2) {
                                case "nmod:in":
                                    final String longLocation = dep2.word();
                                    details += "in ";
                                    for (final SemanticGraphEdge keshet2 : dependencies.getOutEdgesSorted(dep2)) if ("compound".equals(keshet2.getRelation() + ""))
                                        details += keshet2.getDependent().word() + " ";
                                    details += longLocation;
                                    break;
                                case "nmod:during":
                                    details += "during" + " " + dep2.word() + " ";
                                    break;
                                case "nmod:under":
                                    details += "under " + dep2.word() + " ";
                                    break;
                                case "nmod:of":
                                    details += "of " + dep2.word();
                                    break;
                                case "nmod:at":
                                    details += "at" + " " + dep2.word() + " ";
                                    break;
                            }
                            if ("suspicion".equals(keshet.getSource().word()) && "acl:of".equals(rel2))
                                details += dep2.word();
                        }
                        reason += dep.word();
                        reason += aux;
                    }
                }
            }
            if (!"".equals(prefixDetails.trim())) {
                this.Information.add(prefixDetails.trim());
                System.out.println((this.Information.get(index) + ""));
                ++index;
            }
            this.Information.add((reason + " " + details).trim());
            System.out.println((this.Information.get(index) + ""));
            ++index;
        }
    }
}
Also used : Element(org.jsoup.nodes.Element) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) Properties(java.util.Properties) IndexedWord(edu.stanford.nlp.ling.IndexedWord) CoreMap(edu.stanford.nlp.util.CoreMap) Sentence(edu.stanford.nlp.simple.Sentence) StanfordCoreNLP(edu.stanford.nlp.pipeline.StanfordCoreNLP) SentencesAnnotation(edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation) Annotation(edu.stanford.nlp.pipeline.Annotation) CollapsedDependenciesAnnotation(edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation) SemanticGraphEdge(edu.stanford.nlp.semgraph.SemanticGraphEdge)

Example 7 with Sentence

use of edu.stanford.nlp.simple.Sentence in project Info-Evaluation by TechnionYP5777.

the class InteractiveReasonFindingTest method test1.

@Test
public void test1() {
    for (ReasonPair ¢ : new AnalyzeParagragh(new Sentence("Axl Rose was arrested for consuming drugs on October 5th 2012 in NY during rock concert ."), "2015").InteractiveReasonFinding()) {
        System.out.print(¢.getProbability() + " ");
        System.out.println(¢.getReason());
    }
}
Also used : AnalyzeParagragh(main.Analyze.AnalyzeParagragh) ReasonPair(main.database.ReasonPair) Sentence(edu.stanford.nlp.simple.Sentence) Test(org.junit.Test)

Example 8 with Sentence

use of edu.stanford.nlp.simple.Sentence in project Info-Evaluation by TechnionYP5777.

the class AnalyzeParagraphTest method test7.

@Test
public void test7() {
    final Sentence sent = new Sentence("Tito Ortiz was arrested for driving under the influence after he drove his Porsche Panamera into the concrete median on Jan 6 on the 405 freeway in L.A. at 4am.\n");
    final TableTuple tt = new AnalyzeParagragh(sent, "2015").Analyze();
    assertEquals("Tito Ortiz", tt.getName());
    assertEquals("driving under influence", tt.getReason());
    assertEquals("01/06/2015", tt.getDate());
}
Also used : TableTuple(main.database.TableTuple) AnalyzeParagragh(main.Analyze.AnalyzeParagragh) Sentence(edu.stanford.nlp.simple.Sentence) Test(org.junit.Test)

Example 9 with Sentence

use of edu.stanford.nlp.simple.Sentence in project Info-Evaluation by TechnionYP5777.

the class AnalyzeParagraphTest method test3.

@Test
public void test3() {
    final Sentence sent = new Sentence("Vin Diesel was arrested for furious driving on August 14, 2002 in California");
    final TableTuple tt = new AnalyzeParagragh(sent, "2002").Analyze();
    assertEquals("Vin Diesel", tt.getName());
    assertEquals("driving furious in California", tt.getReason());
    assertEquals("08/14/2002", tt.getDate());
}
Also used : TableTuple(main.database.TableTuple) AnalyzeParagragh(main.Analyze.AnalyzeParagragh) Sentence(edu.stanford.nlp.simple.Sentence) Test(org.junit.Test)

Example 10 with Sentence

use of edu.stanford.nlp.simple.Sentence in project Info-Evaluation by TechnionYP5777.

the class AnalyzeParagraphTest method test1.

@Test
public void test1() {
    final Sentence sent = new Sentence("Justin Bieber is in the sky with diamonds on Jan. 26 1970");
    final AnalyzeParagragh anal = new AnalyzeParagragh(sent, "1970");
    assertEquals("Justin Bieber ", anal.AnalyzeSimple().getName());
    final LocalDate date = anal.AnalyzeSimple().getRegularDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    assertEquals(1, date.getMonthValue());
    assertEquals(26, date.getDayOfMonth());
    assertEquals(anal.AnalyzeSimple().getDate(), "01/26/1970");
}
Also used : AnalyzeParagragh(main.Analyze.AnalyzeParagragh) Sentence(edu.stanford.nlp.simple.Sentence) LocalDate(java.time.LocalDate) Test(org.junit.Test)

Aggregations

Sentence (edu.stanford.nlp.simple.Sentence)29 Test (org.junit.Test)18 AnalyzeParagragh (main.Analyze.AnalyzeParagragh)16 TableTuple (main.database.TableTuple)12 CoreLabel (edu.stanford.nlp.ling.CoreLabel)5 Span (edu.stanford.nlp.ie.machinereading.structure.Span)3 CoreMap (edu.stanford.nlp.util.CoreMap)3 SentenceField (edu.stanford.nlp.util.TSVSentenceIterator.SentenceField)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 CoreLabelTokenFactory (edu.stanford.nlp.process.CoreLabelTokenFactory)2 ScoredPassage (io.anserini.qa.passage.ScoredPassage)2 StringReader (java.io.StringReader)2 InteractiveTableTuple (main.database.InteractiveTableTuple)2 ReasonPair (main.database.ReasonPair)2 edu.stanford.nlp.classify (edu.stanford.nlp.classify)1 NERTag (edu.stanford.nlp.ie.KBPRelationExtractor.NERTag)1 IOUtils (edu.stanford.nlp.io.IOUtils)1 RuntimeIOException (edu.stanford.nlp.io.RuntimeIOException)1 CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)1