Java源码示例:edu.stanford.nlp.trees.GrammaticalRelation
示例1
@Override public void process(JCas jCas) throws AnalysisEngineProcessException {
for (Dependency d : JCasUtil.select(jCas, Dependency.class)) {
if (!d.getDependencyType().equals("--")) {
GrammaticalRelation rel = relmaps.get(d.getDependencyType());
String s = rel.toString();
d.setDependencyType(s);
} else {
if (d.getDependencyType().equals("--") && d.getDependent().getBegin() == d.getGovernor().getBegin() &&
d.getDependent().getEnd() == d.getGovernor().getEnd()) {
d.setDependencyType(GrammaticalRelation.ROOT.toString());
}
}
}
}
示例2
private static void subgraph(SemanticGraph graph, IndexedWord root, Collection<IndexedWord> excludeVertexes,
Collection<GrammaticalRelation> excludeRelations, Collection<GrammaticalRelation> excludeRelationsTop,
Collection<SemanticGraphEdge> edgesToRemove, Set<SemanticGraphEdge> exploredEdges) {
List<SemanticGraphEdge> edges = graph.getOutEdgesSorted(root);
for (SemanticGraphEdge e : edges) {
if(exploredEdges.contains(e)) {
continue;
}
IndexedWord child = e.getDependent();
exploredEdges.add(e);
if (excludeVertexes.contains(child)
|| excludeRelations.contains(e.getRelation())
|| excludeRelationsTop.contains(e.getRelation())
|| containsRelationOrDescendant(excludeRelations, e.getRelation())
|| containsRelationOrDescendant(excludeRelationsTop, e.getRelation())) {
edgesToRemove.add(graph.getEdge(root, child));
} else {
subgraph(graph, child, excludeVertexes, excludeRelations, Collections.<GrammaticalRelation>emptySet(), edgesToRemove, exploredEdges);
}
}
}
示例3
private static boolean countRelevantOutgoing(SemanticGraphEdge edge, SemanticGraph semanticGraph, boolean b) {
Set<GrammaticalRelation> relationsGovernor = collectRelations(semanticGraph.getOutEdgesSorted(edge.getGovernor()));
Set<GrammaticalRelation> relationsDependent = collectRelations(semanticGraph.getOutEdgesSorted(edge.getGovernor()));
if(relationsGovernor.size() < 2) {
relationsGovernor.removeAll(relationsDependent);
if(relationsGovernor.size() == 0) {
return true;
}
} else if(relationsDependent.size() < 2) {
relationsDependent.removeAll(relationsGovernor);
if(relationsDependent.size() == 0) {
return true;
}
}
return false;
}
示例4
public static SemanticGraph semanticGraphUniversalEnglishToEnglish(SemanticGraph semanticGraph) {
for (SemanticGraphEdge edge: semanticGraph.edgeListSorted()) {
GrammaticalRelation oldRel = edge.getRelation();
edge.setRelation(EnglishGrammaticalRelations.shortNameToGRel.get(oldRel.getShortName()));
}
return semanticGraph;
}
示例5
private void connectHeads(SemanticGraph semanticGraph) {
Constituent vc = constituents.get(verb);
if(vc instanceof TextConstituent) {
return;
}
IndexedConstituent vic = (IndexedConstituent)vc;
for (int i = 0; i < constituents.size(); i++) {
if (i == verb || constituents.get(i) instanceof TextConstituent) {
continue;
}
IndexedConstituent dic = (IndexedConstituent) constituents.get(i);
semanticGraph.addEdge(vic.root, dic.root, GrammaticalRelation.DEPENDENT, 0.0, false);
}
}
示例6
/** 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;
}
示例7
/** Finds the first occurrence of a grammatical relation in a set of edges */
public static List<SemanticGraphEdge> findAllRelations(List<SemanticGraphEdge> edges, GrammaticalRelation rel) {
List<SemanticGraphEdge> result = new ArrayList<>();
for (SemanticGraphEdge e : edges) {
if (rel.equals(e.getRelation())) {
result.add(e);
}
}
return result;
}
示例8
/** 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;
}
示例9
/** 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;
}
示例10
/** Finds all occurrences of a grammatical relation or its descendants in a list of edges */
public static List<SemanticGraphEdge> getEdges(Collection<SemanticGraphEdge> edges, GrammaticalRelation rel) {
List<SemanticGraphEdge> result = new ArrayList<SemanticGraphEdge>();
for (SemanticGraphEdge e : edges) {
if (rel.isAncestor(e.getRelation())) {
result.add(e);
}
}
return result;
}
示例11
/** 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)) {
List<SemanticGraphEdge> edgesToRemove = new ArrayList<SemanticGraphEdge>();
subgraph(graph, root, excludeVertexes, excludeRelations, excludeRelationsTop, edgesToRemove);
for (SemanticGraphEdge edge : edgesToRemove) {
graph.removeEdge(edge);
}
}
}
示例12
public static void replaceNodeFromSemanticGraph(IndexedWord original, IndexedWord replace, SemanticGraph semanticGraph, boolean onlyIncoming) {
List<SemanticGraphEdge> edgesToAdd = new ArrayList<>();
edgesToAdd.addAll(semanticGraph.getIncomingEdgesSorted(original));
if(!onlyIncoming) {
edgesToAdd.addAll(semanticGraph.getOutEdgesSorted(original));
}
edgesToAdd.addAll(DpUtils.findAllRelationsOrDescendant(semanticGraph.getOutEdgesSorted(original), EnglishGrammaticalRelations.SUBJECT));
edgesToAdd.addAll(DpUtils.findAllRelationsOrDescendant(semanticGraph.getOutEdgesSorted(original), EnglishGrammaticalRelations.COPULA));
Set<GrammaticalRelation> relations = DpUtils.getIncommingRelations(replace, semanticGraph);
for(SemanticGraphEdge edge: edgesToAdd) {
IndexedWord governor = replace;
IndexedWord dependent = edge.getDependent();
if(edge.getGovernor().equals(original)) {
governor = replace;
dependent = edge.getDependent();
} else {
if(relations.contains(edge.getRelation())) {
continue;
}
governor = edge.getGovernor();
dependent = replace;
}
semanticGraph.addEdge(governor, dependent, edge.getRelation(), edge.getWeight(), edge.isExtra());
}
if(semanticGraph.getRoots().contains(original)) {
semanticGraph.resetRoots();
semanticGraph.setRoot(replace);
}
disconectNodeFromSemanticGraph(original, semanticGraph);
}
示例13
private static Collection<? extends SemanticGraphEdge> findAllRelationsOrDescendant(List<SemanticGraphEdge> edges, GrammaticalRelation rel) {
List<SemanticGraphEdge> result = new ArrayList<>();
for (SemanticGraphEdge e : edges) {
if (rel.isAncestor(e.getRelation())) {
result.add(e);
}
}
return result;
}
示例14
private static Set<GrammaticalRelation> getIncommingRelations(IndexedWord node, SemanticGraph semanticGraph) {
Set<GrammaticalRelation> relations = new HashSet<>();
for(SemanticGraphEdge edge: semanticGraph.getIncomingEdgesSorted(node)){
relations.add(edge.getRelation());
}
return relations;
}
示例15
/** Implementation for
* {@link #removeEdges(SemanticGraph, IndexedWord, Collection, Collection, Collection)} */
private static void subgraph(SemanticGraph graph, IndexedWord root, Collection<IndexedWord> excludeVertexes,
Collection<GrammaticalRelation> excludeRelations, Collection<GrammaticalRelation> excludeRelationsTop,
Collection<SemanticGraphEdge> edgesToRemove) {
subgraph(graph, root, excludeVertexes,
excludeRelations, excludeRelationsTop,
edgesToRemove, new HashSet<>());
}
示例16
private static boolean containsRelationOrDescendant(Collection<GrammaticalRelation> relations, GrammaticalRelation rel) {
for(GrammaticalRelation relation: relations) {
if(relation.equals(GrammaticalRelation.DEPENDENT)) {
continue;
}
if(relation.isAncestor(rel)) {
return true;
}
}
return false;
}
示例17
/** Check if an edge is descendant of any grammatical relation in the given set */
private static boolean containsAncestor(Collection<GrammaticalRelation> rels, SemanticGraphEdge edge) {
for (GrammaticalRelation rel : rels) {
if (rel.isAncestor(edge.getRelation())) return true;
}
return false;
}
示例18
/** Generates a textual representation of a given constituent in a given clause*/
public void generate(Clause clause, int constituentIndex, int factPosition, Proposition proposition) {
Set<GrammaticalRelation> excludeRelations = EXCLUDE_RELATIONS;
if (clause.getVerb() == constituentIndex) {
excludeRelations = EXCLUDE_RELATIONS_VERB;
}
generate(clause, constituentIndex, factPosition, excludeRelations, Collections.<GrammaticalRelation>emptySet(), proposition);
}
示例19
/** 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();
}
}
示例20
private static void addEdges(List<SemanticGraphEdge> outEdgesSorted, IndexedWord governor, SemanticGraph semanticGraph) {
Set<GrammaticalRelation> relations = collectRelations(outEdgesSorted);
Set<GrammaticalRelation> noRelations = collectRelations(semanticGraph.getOutEdgesSorted(governor));
relations.removeAll(noRelations);
for(SemanticGraphEdge edge: outEdgesSorted) {
if(!relations.contains(edge.getRelation())) {
continue;
}
SemanticGraphEdge nedge = new SemanticGraphEdge(governor, edge.getDependent(), edge.getRelation(), edge.getWeight(), edge.isExtra());
semanticGraph.addEdge(nedge);
}
}
示例21
private static Set<GrammaticalRelation> collectRelations(List<SemanticGraphEdge> outEdgesSorted) {
Set<GrammaticalRelation> relations = new HashSet<>();
for(SemanticGraphEdge edge: outEdgesSorted) {
relations.add(edge.getRelation());
}
return relations;
}
示例22
/** 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;
}
示例23
/** 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;
}
示例24
/** 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;
}
示例25
/** 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;
}
示例26
/** 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);
}
}
}
示例27
/** Implementation for
* {@link #removeEdges(SemanticGraph, IndexedWord, Collection, Collection, Collection)} */
private static int subgraph(SemanticGraph graph, IndexedWord root, Collection<IndexedWord> excludeVertexes,
Collection<GrammaticalRelation> excludeRelations, Collection<GrammaticalRelation> excludeRelationsTop,
Collection<SemanticGraphEdge> edgesToRemove, int counter) {
/* TODO: In some sentences there is infinite recursion. Dirty fix to stop it.
Example sentence:
"Policies on electronic tickets differ ''from airline to airline and airport to airport,'' said Ms. McInerney,
whose group is working with the airline industry on e-ticket policies and the matter of standardizing itineraries
and receipts, perhaps with a universal template to create more readily verifiable printouts that carry uniform
information like a ticket number that can be matched to an airline computer reservation."
*/
counter++;
if (counter > MAX_RECURSION_ITERATIONS){
return counter;
}
List<SemanticGraphEdge> edges = graph.getOutEdgesSorted(root);
for (SemanticGraphEdge e : edges) {
IndexedWord child = e.getDependent();
if (excludeVertexes.contains(child) || excludeRelations.contains(e.getRelation())
|| excludeRelationsTop.contains(e.getRelation())) {
edgesToRemove.add(graph.getEdge(root, child));
} else {
counter = subgraph(graph, child, excludeVertexes, excludeRelations,
Collections.<GrammaticalRelation> emptySet(), edgesToRemove, counter);
}
}
return counter;
}
示例28
/** Check if an edge is descendant of any grammatical relation in the given set */
private static boolean containsAncestor(Collection<GrammaticalRelation> rels, SemanticGraphEdge edge) {
for (GrammaticalRelation rel : rels) {
if (rel.isAncestor(edge.getRelation()))
return true;
}
return false;
}
示例29
private static ObjectArrayList<TypedDependency> getSubgraphTypedDependencies(SemanticGraph sg, IndexedWord parent,
ObjectArrayList<TypedDependency> tds){
Set<IndexedWord> children = sg.getChildren(parent);
for (IndexedWord child: children){
GrammaticalRelation gRel = sg.getEdge(parent, child).getRelation();
tds.add(new TypedDependency(gRel, parent, child));
if (sg.hasChildren(child))
getSubgraphTypedDependencies(sg, child, tds);
}
return tds;
}
示例30
public static SemanticGraph semanticGraphUniversalEnglishToEnglish(SemanticGraph semanticGraph) {
for (SemanticGraphEdge edge: semanticGraph.edgeListSorted()) {
GrammaticalRelation oldRel = edge.getRelation();
edge.setRelation(EnglishGrammaticalRelations.shortNameToGRel.get(oldRel.getShortName()));
}
return semanticGraph;
}