Example usage for edu.stanford.nlp.ling IndexedWord ner

List of usage examples for edu.stanford.nlp.ling IndexedWord ner

Introduction

In this page you can find the example usage for edu.stanford.nlp.ling IndexedWord ner.

Prototype

@Override
    public String ner() 

Source Link

Usage

From source file:ca.ualberta.exemplar.core.RelationExtraction.java

License:Open Source License

private static boolean hasNerChild(IndexedWord word, SemanticGraph dependencies) {
    for (IndexedWord child : dependencies.getChildren(word)) {
        if (child.ner() != null) {
            return true;
        }//from  w  w w . ja va 2s  .  co  m
    }
    return false;
}

From source file:ca.ualberta.exemplar.core.RelationExtraction.java

License:Open Source License

private static void addModifiers(List<IndexedWord> words, IndexedWord word, SemanticGraph dependencies) {
    List<IndexedWord> adjs = dependencies.getChildrenWithReln(word, GrammaticalRelation.valueOf("amod"));
    List<IndexedWord> nns = dependencies.getChildrenWithReln(word, GrammaticalRelation.valueOf("nn"));
    List<IndexedWord> negs = dependencies.getChildrenWithReln(word, GrammaticalRelation.valueOf("neg"));
    List<IndexedWord> pvts = dependencies.getChildrenWithReln(word, GrammaticalRelation.valueOf("pvt")); // phrasal verb particle -- shut down

    List<IndexedWord> newWords = new ArrayList<IndexedWord>();
    if (adjs != null)
        newWords.addAll(adjs);/*from   w w w  .j  a  v a2 s  .  c om*/
    if (nns != null)
        newWords.addAll(nns);
    if (negs != null)
        newWords.addAll(negs);
    if (pvts != null)
        newWords.addAll(pvts);

    for (IndexedWord newWord : newWords) {

        if (Math.abs(word.index() - newWord.index()) > 5) {
            // If a modifier is too far way from trigger (> 5 tokens), ignore this modifier since it is probably a mistake
            continue;
        }

        if (!newWord.ner().equals("PERSON") && !newWord.ner().equals("ORGANIZATION")
                && !newWord.ner().equals("LOCATION") && !newWord.ner().equals("MISC")) {
            words.add(newWord);
        }
    }

}

From source file:featureExtractor.NLPFeatures.java

static void processLine(String text, int lineId) throws IOException {
    bw_root.write(Integer.toString(lineId));
    bw_subj.write(Integer.toString(lineId));
    bw_underRoot.write(Integer.toString(lineId));
    bw_nerType.write(Integer.toString(lineId));

    //text = "A gigantic Hong Kong set was constructed in downtown Detroit. The set was so big that the Detroit People Mover track ended up becoming part of the set and shooting had to be adjusted to allow the track to move through the set.  ";//"One of three new television series scheduled for release in 2014 based on DC Comics characters. The others being Constantine (2014) and The Flash (2014).  ";
    HashMap<String, Integer> nerCount = new HashMap<>();
    int superlativePOS = 0;

    try {//from  ww  w.  j a va  2 s .com
        Annotation document = new Annotation(text);
        pipeline.annotate(document);

        List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);

        for (CoreMap sentence : sentences) {
            SemanticGraph dependencies = sentence
                    .get(SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation.class);
            // getting root words
            for (IndexedWord rword : dependencies.getRoots()) {
                //System.out.println(rword.lemma());
                //System.out.println(rword.ner());
                if (rword.ner().equals("O"))
                    bw_root.write("\t" + rword.ner() + ":" + rword.lemma());
                //else if(rword.ner().equals("PERSON"))
                else
                    bw_root.write("\t" + rword.ner() + ":" + rword.originalText());
                /*
                else
                bw_root.write(" entity_" + rword.ner());
                */
                // under root
                for (IndexedWord child : dependencies.getChildren(rword)) {
                    //System.out.println("here: " + child.originalText());
                    /*
                    if(child.ner().equals("PERSON"))
                    bw_underRoot.write(" " + child.originalText());
                    else*/
                    if (!child.ner().equals("O"))
                        bw_underRoot.write("\t" + child.ner() + ":" + child.originalText());
                }

                // nsubj | nsubpass words
                GrammaticalRelation[] subjects = { EnglishGrammaticalRelations.NOMINAL_SUBJECT,
                        EnglishGrammaticalRelations.NOMINAL_PASSIVE_SUBJECT };
                for (IndexedWord current : dependencies.descendants(rword))
                    for (IndexedWord nsubWord : dependencies.getChildrenWithRelns(current,
                            Arrays.asList(subjects))) {
                        //System.out.println("wow: " + nsubWord.originalText());
                        if (!nsubWord.ner().equals("O"))
                            bw_subj.write("\t" + nsubWord.ner() + ":" + nsubWord.originalText());
                        else {
                            //System.out.println(nsubWord.lemma());
                            bw_subj.write("\t" + nsubWord.ner() + ":" + nsubWord.lemma());
                        } /*
                          else
                          bw_subj.write(" entity_"+nsubWord.ner());
                          */
                    }
            }

            // NER Types frequency
            for (CoreLabel token : sentence.get(CoreAnnotations.TokensAnnotation.class)) {
                String pos = token.get(CoreAnnotations.PartOfSpeechAnnotation.class);
                String ne = token.get(CoreAnnotations.NamedEntityTagAnnotation.class);

                if (pos.equals("JJS") || pos.equals("RBS"))
                    superlativePOS++;

                nerCount.putIfAbsent(ne, 0);
                nerCount.put(ne, nerCount.get(ne) + 1);
            }

            //System.out.println("dependency graph:\n" + dependencies);
        }
    } catch (Exception e) {
        System.out.println("IGNORED:");
    }

    bw_nerType.write("\t" + Integer.toString(superlativePOS));

    for (String ne : ners) {
        if (nerCount.containsKey(ne))
            bw_nerType.write("\t" + nerCount.get(ne).toString());
        else
            bw_nerType.write("\t0");
    }
    bw_root.write("\n");
    bw_underRoot.write("\n");
    bw_nerType.write("\n");
    bw_subj.write("\n");
    if (lineId % 25 == 0) {
        bw_root.flush();
        bw_underRoot.flush();
        bw_nerType.flush();
        bw_subj.flush();
    }
}

From source file:semRewrite.datesandnumber.DatesAndDuration.java

License:Open Source License

/** ***************************************************************
   *//*  w w  w.  j ava  2s.c o  m*/
public void generateDurationSumoTerms(IndexedWord tempParent, Utilities utilities,
        semRewrite.datesandnumber.DateInfo startDateInfo, DateInfo endDateInfo) {

    if (tempParent != null) {
        if (Utilities.VerbTags.contains(tempParent.tag())) {
            utilities.sumoTerms.add("StartTime(" + tempParent.value() + "-" + tempParent.index() + "," + "time-"
                    + startDateInfo.getTimeCount() + ")");
            utilities.sumoTerms.add("EndTime(" + tempParent.value() + "-" + tempParent.index() + "," + "time-"
                    + endDateInfo.getTimeCount() + ")");

        }
        if (Utilities.nounTags.contains(tempParent.tag())) {
            if (tempParent.ner().equals("PERSON")) {
                utilities.sumoTerms.add("BirthDate(" + tempParent.value() + "-" + tempParent.index() + ","
                        + "time-" + startDateInfo.getTimeCount() + ")");
                utilities.sumoTerms.add("DeathDate(" + tempParent.value() + "-" + tempParent.index() + ","
                        + "time-" + endDateInfo.getTimeCount() + ")");
            } else {
                utilities.sumoTerms.add("StartTime(" + tempParent.value() + "-" + tempParent.index() + ","
                        + "time-" + startDateInfo.getTimeCount() + ")");
                utilities.sumoTerms.add("EndTime(" + tempParent.value() + "-" + tempParent.index() + ","
                        + "time-" + endDateInfo.getTimeCount() + ")");
            }
        }
        startDateInfo.setDurationFlag(true);
        endDateInfo.setDurationFlag(true);
    }
}