Example usage for edu.stanford.nlp.semgraph SemanticGraph getChildren

List of usage examples for edu.stanford.nlp.semgraph SemanticGraph getChildren

Introduction

In this page you can find the example usage for edu.stanford.nlp.semgraph SemanticGraph getChildren.

Prototype

public Set<IndexedWord> getChildren(IndexedWord vertex) 

Source Link

Usage

From source file:count_dep.Count_dep.java

public LinkedList<Event> GetEvents(SemanticGraph dependencies, CoreMap sentence) {
    LinkedList<Event> res = new LinkedList<>();
    LinkedList<IndexedWord> roots = new LinkedList<>();
    List<CoreLabel> words = sentence.get(TokensAnnotation.class);
    List<GrammaticalRelation> senserel = new LinkedList<>();
    senserel.add(GrammaticalRelation.valueOf("nsubj"));
    senserel.add(GrammaticalRelation.valueOf("dobj"));
    for (CoreLabel word : words) {
        if (word.tag().length() >= 2
                && ("VB".equals(word.tag().substring(0, 2)) || "NN".equals(word.tag().substring(0, 2)))) {
            IndexedWord iword = new IndexedWord(word);
            roots.add(iword);/* w w  w .  j ava2s . c  o  m*/
        }
    }
    for (IndexedWord word : roots) {
        Event e = new Event();
        e.trigger = word.word();
        try {
            Set<IndexedWord> children = dependencies.getChildren(word);
            children.stream().forEach((iw) -> {
                e.arguments.add(new EventArgument(iw.word(), ""));
            });
            if (dependencies.inDegree(word) > 0) {
                IndexedWord parent = dependencies.getParent(word);
                if (parent.tag().length() >= 2 && "VB".equals(parent.tag().substring(0, 2))) {
                    Set<IndexedWord> children1 = dependencies.getChildrenWithRelns(parent, senserel);
                    children1.remove(word);
                    children1.stream().forEach((iw) -> {
                        e.arguments.add(new EventArgument(iw.word(), ""));
                    });
                } else {
                    e.arguments.add(new EventArgument(dependencies.getParent(word).word(), ""));
                }
            }
        } catch (java.lang.IllegalArgumentException error) {
            continue;
        }
        res.add(e);
    }
    return res;
}

From source file:edu.illinois.cs.cogcomp.pipeline.handlers.StanfordDepHandler.java

License:Open Source License

private void populateChildren(SemanticGraph depGraph, IndexedWord root, Tree<Pair<String, Integer>> tree,
        TextAnnotation ta, int sentId) {
    if (depGraph.getChildren(root).size() == 0)
        return;//from w w  w .j a v  a 2 s  .c o  m
    for (IndexedWord child : depGraph.getChildren(root)) {
        int childPosition = getNodePosition(ta, child, sentId);
        Pair<String, Integer> nodePair = new Pair<>(child.originalText(), childPosition);
        Tree<Pair<String, Integer>> childTree = new Tree<>(nodePair);
        tree.addSubtree(childTree, new Pair<>(depGraph.getEdge(root, child).toString(), childPosition));
        populateChildren(depGraph, child, childTree, ta, sentId);
    }
}

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 {/*www  . j a v  a2  s  .  co  m*/
        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:sleventextraction.SLEventExtraction.java

public static List<SLEntity> BPGetNPs(SemanticGraph dependencies) {
    List<SLEntity> res = new LinkedList<>();
    Set<IndexedWord> done_tokens = new HashSet<>();
    Queue<IndexedWord> roots = new LinkedList(dependencies.getRoots());
    Map<IndexedWord, Pair<String, IndexedWord>> modified_dep = new HashMap<>();
    while (!roots.isEmpty()) {
        IndexedWord curr = roots.poll();
        roots.addAll(dependencies.getChildren(curr));
        if ((curr.tag().contains("NN") || curr.tag().contains("CD") || curr.tag().contains("PRP")
                || curr.tag().contains("JJ") || curr.tag().contains("RB") || curr.tag().contains("FW")
                || curr.tag().contains("DT")
                || dependencies.getParent(curr) != null && "csubj".equals(
                        dependencies.getEdge(dependencies.getParent(curr), curr).getRelation().getShortName())
                || dependencies.getParent(curr) != null && "nsubj".equals(
                        dependencies.getEdge(dependencies.getParent(curr), curr).getRelation().getShortName())
                || dependencies.getParent(curr) != null && "nsubjpass".equals(
                        dependencies.getEdge(dependencies.getParent(curr), curr).getRelation().getShortName()))
                && !done_tokens.contains(curr)) {
            SLEntity e = new SLEntity();
            e.head = curr;/*from  w  ww.  jav a2 s  .c  om*/
            e.predicate = dependencies.getParent(curr);
            if (e.predicate == null) { // e is root
                e.predicate = e.head;
                e.content = GetNPstring(curr, dependencies, done_tokens);
                e.dep = "self";
            } else {
                e.content = GetNPstring(curr, dependencies, done_tokens);
                try {
                    e.dep = dependencies.getEdge(e.predicate, curr).getRelation().getShortName();
                    if (dependencies.getEdge(e.predicate, curr).getRelation().getSpecific() != null) {
                        e.dep += "_" + dependencies.getEdge(e.predicate, curr).getRelation().getSpecific();
                    }
                    if ("appos".equals(e.dep) || "conj_and".equals(e.dep)) {
                        if (modified_dep.containsKey(e.predicate)) {
                            e.dep = modified_dep.get(e.predicate).getKey();
                            e.predicate = modified_dep.get(e.predicate).getValue();
                            modified_dep.put(curr, new Pair<>(e.dep, e.predicate));
                        } else {
                            e.dep = dependencies.getEdge(dependencies.getParent(e.predicate), e.predicate)
                                    .getRelation().getShortName();
                            if (dependencies.getEdge(dependencies.getParent(e.predicate), e.predicate)
                                    .getRelation().getSpecific() != null) {
                                e.dep += "_"
                                        + dependencies.getEdge(dependencies.getParent(e.predicate), e.predicate)
                                                .getRelation().getSpecific();
                            }
                            e.predicate = dependencies.getParent(e.predicate);
                            modified_dep.put(curr, new Pair<>(e.dep, e.predicate));
                        }
                    } else if (e.predicate.tag().contains("NN") || e.predicate.tag().contains("PRP")
                            || e.predicate.tag().contains("CD") || e.predicate.tag().contains("RB")) {
                        if (modified_dep.containsKey(e.predicate)) {
                            e.predicate = modified_dep.get(e.predicate).getValue();
                            modified_dep.put(curr, new Pair<>(e.dep, e.predicate));
                        } else {
                            if (dependencies.getParent(e.predicate) != null) {
                                e.predicate = dependencies.getParent(e.predicate);
                                modified_dep.put(curr, new Pair<>(e.dep, e.predicate));
                            }
                        }
                    }
                } catch (java.lang.NullPointerException err) {
                    continue;
                }
            }
            if (modified_dep.containsKey(e.predicate)) {
                e.Grand_predicate = modified_dep.get(e.predicate).getValue();
            } else {
                e.Grand_predicate = dependencies.getParent(e.predicate);
                if (e.Grand_predicate != null) {
                    if (e.Grand_predicate.tag().contains("NN") || e.Grand_predicate.tag().contains("PRP")
                            || e.Grand_predicate.tag().contains("CD")
                            || e.Grand_predicate.tag().contains("RB")) {
                        if (modified_dep.containsKey(e.Grand_predicate)) {
                            e.Grand_predicate = modified_dep.get(e.Grand_predicate).getValue();
                        } else {
                            if (dependencies.getParent(e.Grand_predicate) != null) {
                                e.Grand_predicate = dependencies.getParent(e.Grand_predicate);
                            }
                        }
                    }
                }
            }
            res.add(e);
        }
    }
    return res;
}