Java Code Examples for edu.stanford.nlp.ling.IndexedWord#setTag()

The following examples show how to use edu.stanford.nlp.ling.IndexedWord#setTag() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImplicitExtractions.java    From minie with GNU General Public License v3.0 5 votes vote down vote up
/** Set the the relation to a is-a relation **/
public void setIsARelation() {
    this.rel = new AnnotatedPhrase();
    IndexedWord beWord = new IndexedWord();
    beWord.setWord("is");
    beWord.setOriginalText("is");
    beWord.setTag(POS_TAG.VBZ);
    beWord.setNER(NE_TYPE.NO_NER);
    beWord.setLemma("be");
    beWord.setValue("is");
    beWord.setIndex(-2);
    this.rel.addWordToList(beWord);
    this.rel.setRoot(beWord);
}
 
Example 2
Source File: MinIE.java    From minie with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Process possessives in the object.
 * If we have ("SUBJ", "REL", "NP_1 POS NP_2"), then: ("SUBJ", "REL + NP_1 + of", "NP_2")
 * @param prop: proposition (list of annotated phrases)
 */
public void processPoss(ObjectArrayList<AnnotatedPhrase> prop){
    // If there's no object (clause type SV), return
    if (prop.size() < 3)
        return;
    
    AnnotatedPhrase object = prop.get(2);
    AnnotatedPhrase rel = prop.get(1);
    TokenSequencePattern tPattern = TokenSequencePattern.compile(REGEX.T_NP_POS_NP);
    TokenSequenceMatcher tMatcher = tPattern.getMatcher(object.getWordCoreLabelList());
    
    int posIndex = -1;
    
    while (tMatcher.find()){         
        List<CoreMap> match = tMatcher.groupNodes();
        
        // Check if the first/last word of the match is the first/last word of the object
        CoreLabel firstWord = new CoreLabel(match.get(0));
        CoreLabel lastWord = new CoreLabel(match.get(match.size() - 1));
        boolean check = false;
        if (firstWord.index() == object.getWordList().get(0).index()){
            if (lastWord.index() == object.getWordList().get(object.getWordList().size() - 1).index()){
                check = true;
            }
        }
        if (!check) break;
        
        for (CoreMap cm: match){
            CoreLabel cl = new CoreLabel(cm);
            if (cl.tag().equals(POS_TAG.POS) && (cl.ner().equals(NE_TYPE.NO_NER))){
                posIndex = object.getWordCoreLabelList().indexOf(cl);
                break;
            }
        }
    }
    
    if (posIndex > -1){
        IndexedWord of = new IndexedWord();
        of.setOriginalText("of");
        of.setLemma("of");
        of.setWord("of");
        of.setTag("IN");
        of.setNER("O");
        of.setIndex(-1);
        
        ObjectArrayList<IndexedWord> pushedWords = new ObjectArrayList<>();
        object.removeWordFromList(posIndex);
        for (int i = posIndex; i < object.getWordList().size(); i++){
            pushedWords.add(object.getWordList().get(i));
        }
        rel.addWordsToList(pushedWords);
        rel.addWordToList(of);
        object.removeWordsFromList(pushedWords);
    }
}
 
Example 3
Source File: ImplicitExtractions.java    From minie with GNU General Public License v3.0 4 votes vote down vote up
/** If   ORG+ POS? NP PERSON+ => "PERSON" "is NP of" "ORG" (if there are , and or -> make multiple extractions) **/
public void extractPersonIsNPOfOrg() {
    // Reusable variables
    ObjectArrayList<AnnotatedPhrase> tempProp = new ObjectArrayList<>();
    ObjectArrayList<AnnotatedPhrase> subjects = new ObjectArrayList<>();
    IndexedWord subjRoot;
    IndexedWord objRoot;
    
    this.tPattern = TokenSequencePattern.compile(REGEX.T_ORG_NP_PERSON);
    this.tMatcher = this.tPattern.getMatcher(CoreNLPUtils.getCoreLabelListFromIndexedWordList(this.sentence));
    while (this.tMatcher.find()){    
        // Set the relation to be "is-a" relation
        this.setIsARelation();
        
        for (IndexedWord w: CoreNLPUtils.listOfCoreMapWordsToIndexedWordList(this.tMatcher.groupNodes())) {
            if (w.ner().equals(NE_TYPE.PERSON))
                this.subj.addWordToList(w);
            else if (w.ner().equals(NE_TYPE.ORGANIZATION))
                this.obj.addWordToList(w);
            else if (w.tag().equals(POS_TAG.POS))
                continue;
            else if (w.lemma().equals(CHARACTER.COMMA) || w.lemma().equals("and") || w.lemma().equals("or")) {
                subjRoot = CoreNLPUtils.getRootFromWordList(this.sentenceSemGraph, this.subj.getWordList());
                subjects.add(new AnnotatedPhrase(this.subj.getWordList().clone(), subjRoot));
                this.subj.clear();
            }
            else this.rel.addWordToList(w);
        }
        subjRoot = CoreNLPUtils.getRootFromWordList(this.sentenceSemGraph, this.subj.getWordList());
        subjects.add(new AnnotatedPhrase(this.subj.getWordList().clone(), subjRoot));
        objRoot = CoreNLPUtils.getRootFromWordList(this.sentenceSemGraph, this.obj.getWordList());
        
        IndexedWord ofWord = new IndexedWord();
        ofWord.setWord("of");
        ofWord.setOriginalText("of");
        ofWord.setTag(POS_TAG.IN);
        ofWord.setNER(NE_TYPE.NO_NER);
        ofWord.setLemma("of");
        ofWord.setValue("of");
        ofWord.setIndex(-2);
        this.rel.addWordToList(ofWord);
        
        for (AnnotatedPhrase subject: subjects) {
            // Add the subj/rel/obj to the temporary proposition and then to the real propositions
            subjRoot = CoreNLPUtils.getRootFromWordList(this.sentenceSemGraph, subject.getWordList());
            tempProp.add(new AnnotatedPhrase(subject.getWordList(), subjRoot));
            tempProp.add(new AnnotatedPhrase(this.rel.getWordList().clone(), this.rel.getRoot()));
            tempProp.add(new AnnotatedPhrase(this.obj.getWordList().clone(), objRoot));
            this.propositions.add(new AnnotatedProposition(tempProp.clone(), new Attribution()));
            tempProp.clear();
        }
        
        // Clean the variables
        this.subj.clear();
        this.obj.clear();
        this.rel.clear();
    }
}
 
Example 4
Source File: ImplicitExtractions.java    From minie with GNU General Public License v3.0 4 votes vote down vote up
/** If (NP+ PERSON) => "PERSON" "is" "NP" **/
public void extractNounPerson() {
    // Reusable variables
    ObjectArrayList<AnnotatedPhrase> tempProp = new ObjectArrayList<>();
    IndexedWord subjRoot;
    IndexedWord objRoot;
    
    // Set the relation to be "is-a" relation
    this.setIsARelation();
    
    this.tPattern = TokenSequencePattern.compile(REGEX.T_NP_PERSON);
    this.tMatcher = this.tPattern.getMatcher(CoreNLPUtils.getCoreLabelListFromIndexedWordList(this.sentence));
    while (this.tMatcher.find()){         
        for (IndexedWord w: CoreNLPUtils.listOfCoreMapWordsToIndexedWordList(this.tMatcher.groupNodes())) {
            if (w.ner().equals(NE_TYPE.PERSON)) {
                this.subj.addWordToList(w);
            }
            else {
                if (w.lemma().toLowerCase().equals("mrs.") || w.lemma().toLowerCase().equals("ms.") || 
                    w.lemma().toLowerCase().equals("mrs") || w.lemma().toLowerCase().equals("ms")) {
                    IndexedWord female = new IndexedWord();
                    female.setWord("female");
                    female.setOriginalText("female");
                    female.setTag(POS_TAG.NN);
                    female.setNER(NE_TYPE.NO_NER);
                    female.setLemma("female");
                    female.setValue("female");
                    female.setIndex(-2);
                    this.obj.addWordToList(female);
                }
                else if (w.lemma().toLowerCase().equals("mr.") || w.lemma().toLowerCase().equals("mr")) {
                    IndexedWord male = new IndexedWord();
                    male.setWord("male");
                    male.setOriginalText("male");
                    male.setTag(POS_TAG.NN);
                    male.setNER(NE_TYPE.NO_NER);
                    male.setLemma("male");
                    male.setValue("male");
                    male.setIndex(-2);
                    this.obj.addWordToList(male);
                }
                else if (Polarity.NEG_WORDS.contains(w.lemma().toLowerCase())) {
                    continue;
                }
                else {
                    this.obj.addWordToList(w);
                }
            }
        }
            
        // Add the subj/rel/obj to the temporary proposition and then to the real propositions
        subjRoot = CoreNLPUtils.getRootFromWordList(this.sentenceSemGraph, this.subj.getWordList());
        objRoot = CoreNLPUtils.getRootFromWordList(this.sentenceSemGraph, this.obj.getWordList());
        tempProp.add(new AnnotatedPhrase(this.subj.getWordList().clone(), subjRoot));
        tempProp.add(new AnnotatedPhrase(this.rel.getWordList().clone(), this.rel.getRoot()));
        tempProp.add(new AnnotatedPhrase(this.obj.getWordList().clone(), objRoot));
        this.propositions.add(new AnnotatedProposition(tempProp.clone(), new Attribution()));
            
        // Clean the variables
        tempProp.clear();
        this.subj.clear();
        this.obj.clear();
    }

    // Clear the relation
    this.rel.clear();
}
 
Example 5
Source File: ClauseDetector.java    From minie with GNU General Public License v3.0 4 votes vote down vote up
/** Generates a clause from an apposition 
 * @param subject The subject of the clause (first argument of the appos relation)
 * @param object  The object of the clause (second argument of the appos relation)
 */
private static void addApposClause(ClausIE clausIE, IndexedWord subject, IndexedWord object) {
    Clause clause = new Clause();
    clause.setSubject(0);
    clause.verb = 1;
    clause.complement = 2;
    Clause.Type clauseType = Clause.Type.SVC;
    Constituent.Type argumentConstType = Constituent.Type.COMPLEMENT;
    
    // Create a relation phrase with the possessive verb 'is' 
    Phrase apposPhrase = new Phrase();
    IndexedWord appositionVerb = new IndexedWord();
    appositionVerb.setWord("is");
    appositionVerb.setTag(POS_TAG.VBZ);
    appositionVerb.setNER(NE_TYPE.NO_NER);
    appositionVerb.setLemma("be");
    appositionVerb.setValue("be");
    appositionVerb.setIndex(-2);
    apposPhrase.addWordToList(appositionVerb);
    apposPhrase.setRoot(appositionVerb);
    
    PhraseConstituent verbConstit = new PhraseConstituent(apposPhrase, Constituent.Type.VERB);
    
    if (subject.ner().equals(NE_TYPE.DATE) || object.ner().equals(NE_TYPE.DATE))
        return;
    if (subject.ner().equals(NE_TYPE.TIME) || object.ner().equals(NE_TYPE.TIME))
        return;
    
    // If both the subject and the objects are LOCATION-ners, then clause type is SVA and the appos. verb "is in" 
    if ((subject.ner().equals(NE_TYPE.ORGANIZATION) || 
            subject.ner().equals(NE_TYPE.LOCATION)) && object.ner().equals(NE_TYPE.LOCATION)){
        clauseType = Clause.Type.SVA;
        
        // Create a relation phrase with the verb 'is' and the preposition 'in'
        IndexedWord prepWord = new IndexedWord();
        prepWord.setWord("in");
        prepWord.setTag(POS_TAG.IN);
        prepWord.setNER(NE_TYPE.NO_NER);
        prepWord.setLemma("in");
        prepWord.setValue("in");
        prepWord.setIndex(-2);
        apposPhrase.addWordToList(prepWord);
        
        verbConstit = new PhraseConstituent(apposPhrase, Constituent.Type.VERB);
        argumentConstType = Constituent.Type.ADVERBIAL;
        clause.adverbials.add(2);
        clause.complement = -1;
    }
    
    clause.constituents.add(new IndexedConstituent(clausIE.getSemanticGraph(), subject, Constituent.Type.SUBJECT));
    clause.constituents.add(verbConstit);
    clause.constituents.add(new IndexedConstituent(clausIE.getSemanticGraph(), object, argumentConstType));
    clause.setType(clauseType);
    clausIE.getClauses().add(clause);
}
 
Example 6
Source File: ClauseDetector.java    From minie with GNU General Public License v3.0 4 votes vote down vote up
/** Generates a clause from a possessive relation
* @param subject The subject of the clause
* @param object  The object of the clause 
*/
private static void addPossessiveClause(ClausIE clausIE, IndexedWord subject, IndexedWord object) {
    Clause clause = new Clause();
    SemanticGraph newSemanticGraph = new SemanticGraph(clausIE.getSemanticGraph());
    clause.setSubject(0);
    clause.verb = 1;
    clause.dobjects.add(2);
    Set<IndexedWord> excludesub = new TreeSet<IndexedWord>();
    Set<IndexedWord> excludeobj = new TreeSet<IndexedWord>();

    excludeobj.add(subject);
    List<SemanticGraphEdge> outedobj = newSemanticGraph.getOutEdgesSorted(object);
    excludeVertexPoss(outedobj, excludeobj, clausIE);

    SemanticGraphEdge rcmod = null;
    if (subject.tag().charAt(0) == 'W') {
        IndexedWord root = newSemanticGraph.getParent(object); 
        if (root != null){
            if (root.tag().equals(POS_TAG.IN)){
                root = newSemanticGraph.getParent(root); // "I saw the man in whose wife I trust"
            }
            List<SemanticGraphEdge> inedges = newSemanticGraph.getIncomingEdgesSorted(root);
            rcmod = DpUtils.findFirstOfRelation(inedges, EnglishGrammaticalRelations.RELATIVE_CLAUSE_MODIFIER);
        }
    } else {
        List<SemanticGraphEdge> outedges = newSemanticGraph.getOutEdgesSorted(subject);
        SemanticGraphEdge ps = DpUtils.findFirstOfRelation(outedges, EnglishGrammaticalRelations.POSSESSIVE_MODIFIER);
        if (ps != null){
            excludesub.add(ps.getDependent());
        }
    }

    if (rcmod != null) {
        clause.constituents.add(createRelConstituent(newSemanticGraph, rcmod.getGovernor(), Type.SUBJECT));
        // To avoid the s in  "Bill's clothes are great".
        ((IndexedConstituent) clause.constituents.get(0)).getExcludedVertexes().addAll(excludesub);
    } else {
        clause.constituents.add(new IndexedConstituent(newSemanticGraph, subject, Collections.<IndexedWord> emptySet(), 
                                                        excludesub, Type.SUBJECT));
    }
    
    // Create a relation phrase with the possessive verb 'has'
    Phrase possPhrase = new Phrase();
    IndexedWord possessiveVerb = new IndexedWord();
    possessiveVerb.setWord("has");
    possessiveVerb.setTag(POS_TAG.VBZ);
    possessiveVerb.setNER(NE_TYPE.NO_NER);
    possessiveVerb.setLemma("have");
    possessiveVerb.setValue("has");
    possessiveVerb.setIndex(-2);
    possPhrase.addWordToList(possessiveVerb);
    possPhrase.setRoot(possessiveVerb);
    
    clause.constituents.add(new PhraseConstituent(possPhrase, Constituent.Type.VERB));
    clause.constituents.add(new IndexedConstituent(newSemanticGraph, object, Collections.<IndexedWord> emptySet(), 
                                                    excludeobj, Constituent.Type.DOBJ));
    clause.setType(Clause.Type.SVO);
    clausIE.getClauses().add(clause);
}