当前位置: 首页>>代码示例>>Java>>正文


Java GrammaticalRelation类代码示例

本文整理汇总了Java中edu.stanford.nlp.trees.GrammaticalRelation的典型用法代码示例。如果您正苦于以下问题:Java GrammaticalRelation类的具体用法?Java GrammaticalRelation怎么用?Java GrammaticalRelation使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


GrammaticalRelation类属于edu.stanford.nlp.trees包,在下文中一共展示了GrammaticalRelation类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getStanfordTypedDependencies

import edu.stanford.nlp.trees.GrammaticalRelation; //导入依赖的package包/类
public List<TypedDependency> getStanfordTypedDependencies(DependencyForm form) {
    List<TypedDependency> dependencies = new ArrayList<TypedDependency>();
    if (this.nodes == null)
    	nodes = getStanfordTreeGraphNodes(form);

    List<AgigaTypedDependency> agigaDeps = getAgigaDeps(form);
    for (AgigaTypedDependency agigaDep : agigaDeps) {
        // Add one, since the tokens are zero-indexed but the TreeGraphNodes are one-indexed
        TreeGraphNode gov = nodes.get(agigaDep.getGovIdx() + 1);
        TreeGraphNode dep = nodes.get(agigaDep.getDepIdx() + 1);
        // Create the typed dependency
        TypedDependency typedDep = new TypedDependency(GrammaticalRelation.valueOf(agigaDep.getType()), gov, dep);
        dependencies.add(typedDep);
    }
    return dependencies;
}
 
开发者ID:mgormley,项目名称:agiga,代码行数:17,代码来源:StanfordAgigaSentence.java

示例2: addModifiers

import edu.stanford.nlp.trees.GrammaticalRelation; //导入依赖的package包/类
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);
	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);
		}
	}

}
 
开发者ID:U-Alberta,项目名称:exemplar,代码行数:27,代码来源:RelationExtraction.java

示例3: findNextParagraphSpeaker

import edu.stanford.nlp.trees.GrammaticalRelation; //导入依赖的package包/类
private String findNextParagraphSpeaker(List<CoreMap> paragraph, int paragraphOffset, Dictionaries dict) {
  CoreMap lastSent = paragraph.get(paragraph.size()-1);
  String speaker = "";
  for(CoreLabel w : lastSent.get(CoreAnnotations.TokensAnnotation.class)) {
    if(w.get(CoreAnnotations.LemmaAnnotation.class).equals("report") || w.get(CoreAnnotations.LemmaAnnotation.class).equals("say")) {
      String word = w.get(CoreAnnotations.TextAnnotation.class);
      SemanticGraph dependency = lastSent.get(SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation.class);
      IndexedWord t = dependency.getNodeByWordPattern(word);

      for(Pair<GrammaticalRelation,IndexedWord> child : dependency.childPairs(t)){
        if(child.first().getShortName().equals("nsubj")) {
          int subjectIndex = child.second().index();  // start from 1
          IntTuple headPosition = new IntTuple(2);
          headPosition.set(0, paragraph.size()-1 + paragraphOffset);
          headPosition.set(1, subjectIndex-1);
          if(mentionheadPositions.containsKey(headPosition)
              && mentionheadPositions.get(headPosition).nerString.startsWith("PER")) {
            speaker = Integer.toString(mentionheadPositions.get(headPosition).mentionID);
          }
        }
      }
    }
  }
  return speaker;
}
 
开发者ID:benblamey,项目名称:stanford-nlp,代码行数:26,代码来源:Document.java

示例4: isSpeaker

import edu.stanford.nlp.trees.GrammaticalRelation; //导入依赖的package包/类
/** Check one mention is the speaker of the other mention */
public static boolean isSpeaker(Mention m, Mention ant, Dictionaries dict) {

  if(!dict.firstPersonPronouns.contains(ant.spanToString().toLowerCase())
      || ant.number==Number.PLURAL || ant.sentNum!=m.sentNum) return false;

  int countQuotationMark = 0;
  for(int i = Math.min(m.headIndex, ant.headIndex)+1 ; i < Math.max(m.headIndex, ant.headIndex) ; i++) {
    String word = m.sentenceWords.get(i).get(CoreAnnotations.TextAnnotation.class);
    if(word.equals("``") || word.equals("''")) countQuotationMark++;
  }
  if(countQuotationMark!=1) return false;

  IndexedWord w = m.dependency.getNodeByWordPattern(m.sentenceWords.get(m.headIndex).get(CoreAnnotations.TextAnnotation.class));
  if(w== null) return false;

  for(Pair<GrammaticalRelation,IndexedWord> parent : m.dependency.parentPairs(w)){
    if(parent.first().getShortName().equals("nsubj")
        && dict.reportVerb.contains(parent.second().get(CoreAnnotations.LemmaAnnotation.class))) {
      return true;
    }
  }
  return false;
}
 
开发者ID:benblamey,项目名称:stanford-nlp,代码行数:25,代码来源:Document.java

示例5: getPremodifiers

import edu.stanford.nlp.trees.GrammaticalRelation; //导入依赖的package包/类
public ArrayList<ArrayList<IndexedWord>> getPremodifiers(){

    ArrayList<ArrayList<IndexedWord>> premod = new ArrayList<ArrayList<IndexedWord>>();

    if(headIndexedWord == null) return premod;
    for(Pair<GrammaticalRelation,IndexedWord> child : dependency.childPairs(headIndexedWord)){
      String function = child.first().getShortName();
      if(child.second().index() < headWord.index()
          && !child.second.tag().equals("DT") && !child.second.tag().equals("WRB")
          && !function.endsWith("det") && !function.equals("num")
          && !function.equals("rcmod") && !function.equals("infmod")
          && !function.equals("partmod") && !function.equals("punct")){
        ArrayList<IndexedWord> phrase = new ArrayList<IndexedWord>(dependency.descendants(child.second()));
        Collections.sort(phrase);
        premod.add(phrase);
      }
    }
    return premod;
  }
 
开发者ID:benblamey,项目名称:stanford-nlp,代码行数:20,代码来源:Mention.java

示例6: getPostmodifiers

import edu.stanford.nlp.trees.GrammaticalRelation; //导入依赖的package包/类
public ArrayList<ArrayList<IndexedWord>> getPostmodifiers(){

    ArrayList<ArrayList<IndexedWord>> postmod = new ArrayList<ArrayList<IndexedWord>>();

    if(headIndexedWord == null) return postmod;
    for(Pair<GrammaticalRelation,IndexedWord> child : dependency.childPairs(headIndexedWord)){
      String function = child.first().getShortName();
      if(child.second().index() > headWord.index() &&
          !function.endsWith("det") && !function.equals("num")
          && !function.equals("rcmod") && !function.equals("infmod")
          && !function.equals("partmod") && !function.equals("punct")
          && !(function.equals("possessive") && dependency.descendants(child.second()).size() == 1)){
        ArrayList<IndexedWord> phrase = new ArrayList<IndexedWord>(dependency.descendants(child.second()));
        Collections.sort(phrase);
        postmod.add(phrase);
      }
    }
    return postmod;
  }
 
开发者ID:benblamey,项目名称:stanford-nlp,代码行数:20,代码来源:Mention.java

示例7: getRelation

import edu.stanford.nlp.trees.GrammaticalRelation; //导入依赖的package包/类
public String getRelation(){

    if(headIndexedWord == null) return null;

    if(dependency.getRoots().isEmpty()) return null;
    // root relation
    if(dependency.getFirstRoot().equals(headIndexedWord)) return "root";
    if(!dependency.vertexSet().contains(dependency.getParent(headIndexedWord))) return null;
    GrammaticalRelation relation = dependency.reln(dependency.getParent(headIndexedWord), headIndexedWord);

    // adjunct relations
    if(relation.toString().startsWith("prep") || relation == EnglishGrammaticalRelations.PREPOSITIONAL_OBJECT || relation == EnglishGrammaticalRelations.TEMPORAL_MODIFIER || relation == EnglishGrammaticalRelations.ADV_CLAUSE_MODIFIER || relation == EnglishGrammaticalRelations.ADVERBIAL_MODIFIER || relation == EnglishGrammaticalRelations.PREPOSITIONAL_COMPLEMENT) return "adjunct";

    // subject relations
    if(relation == EnglishGrammaticalRelations.NOMINAL_SUBJECT || relation == EnglishGrammaticalRelations.CLAUSAL_SUBJECT || relation == EnglishGrammaticalRelations.CONTROLLING_SUBJECT) return "subject";
    if(relation == EnglishGrammaticalRelations.NOMINAL_PASSIVE_SUBJECT || relation == EnglishGrammaticalRelations.CLAUSAL_PASSIVE_SUBJECT) return "subject";

    // verbal argument relations
    if(relation == EnglishGrammaticalRelations.ADJECTIVAL_COMPLEMENT || relation == EnglishGrammaticalRelations.ATTRIBUTIVE || relation == EnglishGrammaticalRelations.CLAUSAL_COMPLEMENT || relation == EnglishGrammaticalRelations.XCLAUSAL_COMPLEMENT || relation == EnglishGrammaticalRelations.AGENT || relation == EnglishGrammaticalRelations.DIRECT_OBJECT || relation == EnglishGrammaticalRelations.INDIRECT_OBJECT) return "verbArg";

    // noun argument relations
    if(relation == EnglishGrammaticalRelations.RELATIVE_CLAUSE_MODIFIER || relation == EnglishGrammaticalRelations.NOUN_COMPOUND_MODIFIER || relation == EnglishGrammaticalRelations.ADJECTIVAL_MODIFIER || relation == EnglishGrammaticalRelations.APPOSITIONAL_MODIFIER || relation == EnglishGrammaticalRelations.POSSESSION_MODIFIER) return "nounArg";

    return null;
  }
 
开发者ID:benblamey,项目名称:stanford-nlp,代码行数:26,代码来源:Mention.java

示例8: getModifiers

import edu.stanford.nlp.trees.GrammaticalRelation; //导入依赖的package包/类
public int getModifiers(Dictionaries dict){

    if(headIndexedWord == null) return 0;

    int count = 0;
    List<Pair<GrammaticalRelation, IndexedWord>> childPairs = dependency.childPairs(headIndexedWord);
    for(Pair<GrammaticalRelation, IndexedWord> childPair : childPairs) {
      GrammaticalRelation gr = childPair.first;
      IndexedWord word = childPair.second;
      if(gr == EnglishGrammaticalRelations.ADJECTIVAL_MODIFIER || gr == EnglishGrammaticalRelations.PARTICIPIAL_MODIFIER
          || gr == EnglishGrammaticalRelations.RELATIVE_CLAUSE_MODIFIER || gr == EnglishGrammaticalRelations.INFINITIVAL_MODIFIER
          || gr.toString().startsWith("prep_")) {
        count++;
      }
      // add noun modifier when the mention isn't a NER
      if(nerString.equals("O") && gr == EnglishGrammaticalRelations.NOUN_COMPOUND_MODIFIER) {
        count++;
      }

      // add possessive if not a personal determiner
      if(gr == EnglishGrammaticalRelations.POSSESSION_MODIFIER && !dict.determiners.contains(word.lemma())) {
        count++;
      }
    }
    return count;
  }
 
开发者ID:benblamey,项目名称:stanford-nlp,代码行数:27,代码来源:Mention.java

示例9: getNegation

import edu.stanford.nlp.trees.GrammaticalRelation; //导入依赖的package包/类
public int getNegation(Dictionaries dict) {

    if(headIndexedWord == null) return 0;

    // direct negation in a child
    Collection<IndexedWord> children = dependency.getChildren(headIndexedWord);
    for(IndexedWord child : children) {
      if(dict.negations.contains(child.lemma())) return 1;
    }

    // or has a sibling
    Collection<IndexedWord> siblings = dependency.getSiblings(headIndexedWord);
    for(IndexedWord sibling : siblings) {
      if(dict.negations.contains(sibling.lemma()) && !dependency.hasParentWithReln(headIndexedWord, EnglishGrammaticalRelations.NOMINAL_SUBJECT)) return 1;
    }
    // check the parent
    List<Pair<GrammaticalRelation,IndexedWord>> parentPairs = dependency.parentPairs(headIndexedWord);
    if (!parentPairs.isEmpty()) {
      Pair<GrammaticalRelation,IndexedWord> parentPair = parentPairs.get(0);
      GrammaticalRelation gr = parentPair.first;
      // check negative prepositions
      if(dict.neg_relations.contains(gr.toString())) return 1;
    }
    return 0;
  }
 
开发者ID:benblamey,项目名称:stanford-nlp,代码行数:26,代码来源:Mention.java

示例10: getSubject

import edu.stanford.nlp.trees.GrammaticalRelation; //导入依赖的package包/类
protected IndexedWord getSubject(String sentence, SemanticGraph dependencies) throws NotApplicableTransformerException {
    Collection<IndexedWord> roots = dependencies.getRoots();
    IndexedWord subject = null;
    for (IndexedWord root : roots) {
        List<IndexedWord> rootChildes = dependencies.getChildList(root);
        for (IndexedWord child : rootChildes) {
            SemanticGraphEdge edge = dependencies.getEdge(root, child);
            GrammaticalRelation relation = edge.getRelation();
            if (isSubject(relation.getShortName())) {
                subject = edge.getDependent();
                LOGGER.info("Found subject " + subject);
            }
        }
    }
    if (subject == null) {
        throw new NotApplicableTransformerException("Bad subject in sentence \"" + sentence + "\" ");
    }
    return subject;
}
 
开发者ID:pesua,项目名称:Project-Poppins,代码行数:20,代码来源:QuestionToSubjectBuilder.java

示例11: findFirstOfRelation

import edu.stanford.nlp.trees.GrammaticalRelation; //导入依赖的package包/类
/** Finds the first occurrence of a grammatical relation in a set of edges */
public static SemanticGraphEdge findFirstOfRelation(List<SemanticGraphEdge> edges, GrammaticalRelation rel) {
    for (SemanticGraphEdge e : edges) {
        if (rel.equals(e.getRelation())) {
            return e;
        }
    }
    return null;
}
 
开发者ID:gkiril,项目名称:minie,代码行数:10,代码来源:DpUtils.java

示例12: findFirstOfRelationOrDescendent

import edu.stanford.nlp.trees.GrammaticalRelation; //导入依赖的package包/类
/** Finds the first occurrence of a grammatical relation or its descendants in a set of edges */
public static SemanticGraphEdge findFirstOfRelationOrDescendent(List<SemanticGraphEdge> edges, GrammaticalRelation rel) {
    for (SemanticGraphEdge e : edges) {
        if (rel.isAncestor(e.getRelation())) {
            return e;
        }
    }
    return null;
}
 
开发者ID:gkiril,项目名称:minie,代码行数:10,代码来源:DpUtils.java

示例13: findDescendantRelativeRelation

import edu.stanford.nlp.trees.GrammaticalRelation; //导入依赖的package包/类
/** Finds the first occurrence of a grammatical relation or its descendants for a relative pronoun */
public static SemanticGraphEdge findDescendantRelativeRelation(SemanticGraph semanticGraph, IndexedWord root, 
        GrammaticalRelation rel) {
    List<SemanticGraphEdge> outedges = semanticGraph.getOutEdgesSorted(root);
    for (SemanticGraphEdge e : outedges) {
        if (e.getDependent().tag().charAt(0) == 'W' && rel.isAncestor(e.getRelation())) {
            return e;
        } else
            return findDescendantRelativeRelation(semanticGraph, e.getDependent(), rel);
    }
    return null;
}
 
开发者ID:gkiril,项目名称:minie,代码行数:13,代码来源:DpUtils.java

示例14: getEdges

import edu.stanford.nlp.trees.GrammaticalRelation; //导入依赖的package包/类
/** Finds all occurrences of a grammatical relation or its descendants in a set of edges */
public static List<SemanticGraphEdge> getEdges(List<SemanticGraphEdge> edges, GrammaticalRelation rel) {
    List<SemanticGraphEdge> result = new ArrayList<SemanticGraphEdge>();
    for (SemanticGraphEdge e : edges) {
        if (rel.isAncestor(e.getRelation())) {
            result.add(e);
        }
    }
    return result;
}
 
开发者ID:gkiril,项目名称:minie,代码行数:11,代码来源:DpUtils.java

示例15: removeEdges

import edu.stanford.nlp.trees.GrammaticalRelation; //导入依赖的package包/类
/** Removes some edges from the given semantic graph.
 * 
 * This method traverses the semantic graph starting from the given root. An edge is removed if
 * (1) its child appears in <code>excludeVertexes</code>, (2) its relation appears in
 * <code>excludeRelations</code>, or (3) the edge has the root as parent and its relation
 * appears in <code>excludeRelationsTop</code>. */
public static void removeEdges(SemanticGraph graph, IndexedWord root, Collection<IndexedWord> excludeVertexes,
        Collection<GrammaticalRelation> excludeRelations, Collection<GrammaticalRelation> excludeRelationsTop) {
    if (!excludeVertexes.contains(root)) {
        Set<SemanticGraphEdge> edgesToRemove = new HashSet<SemanticGraphEdge>();
        subgraph(graph, root, excludeVertexes, excludeRelations, excludeRelationsTop, edgesToRemove, 0);
        for (SemanticGraphEdge edge : edgesToRemove) {
            graph.removeEdge(edge);
        }
    }
}
 
开发者ID:gkiril,项目名称:minie,代码行数:17,代码来源:DpUtils.java


注:本文中的edu.stanford.nlp.trees.GrammaticalRelation类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。