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

The following examples show how to use edu.stanford.nlp.ling.IndexedWord#setValue() . 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: PropositionGenerator.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
/** Generates a textual representation of a given constituent in a given clause*/
private void generate(Clause clause, int constituentIndex, int factPosition, Collection<GrammaticalRelation> excludeRelations,
    Collection<GrammaticalRelation> excludeRelationsTop, Proposition proposition) {
  Constituent constituent = clause.getConstituents().get(constituentIndex);
  if (constituent instanceof TextConstituent) {
    IndexedWord iw = new IndexedWord();
    iw.setWord(((TextConstituent) constituent).text());
    iw.setValue(((TextConstituent) constituent).text());
    proposition.constituents.put(factPosition, ((TextConstituent) constituent).text());
    proposition.addTokens(factPosition, iw);
    proposition.addHead(factPosition, iw);
  } else if (constituent instanceof IndexedConstituent) {
    IndexedConstituent iconstituent = (IndexedConstituent) constituent;
    proposition.addHead(factPosition, iconstituent.getRoot());
    SemanticGraph subgraph = clause.createSemanticGraph(false);
    DpUtils.removeEdges(subgraph, iconstituent.getRoot(), excludeRelations, excludeRelationsTop);
    Set<IndexedWord> words = new TreeSet<IndexedWord>(subgraph.descendants(iconstituent.getRoot()));
    for (IndexedWord v : iconstituent.getAdditionalVertexes()) {
      words.addAll(subgraph.descendants(v));
    }
    if (iconstituent.isPrepositionalPhrase()) words.remove(iconstituent.getRoot());
    proposition.constituents.put(factPosition, generatePhrase(iconstituent, words));
    proposition.addTokens(factPosition, words);
  } else {
    throw new IllegalArgumentException();
  }
}
 
Example 2
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 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);
}