From e7a7657713e6feb2b048eb717a28ba82f2a64fdd Mon Sep 17 00:00:00 2001 From: John Bauer Date: Fri, 3 Mar 2023 18:13:41 -0800 Subject: [PATCH] Get rid of setRelation - it was simply broken as the various edges might be in Set or Map, and changing the relation would change the hash key --- .../nlp/naturalli/NaturalLogicAnnotator.java | 2 +- src/edu/stanford/nlp/semgraph/SemanticGraph.java | 9 +++++++++ .../stanford/nlp/semgraph/SemanticGraphEdge.java | 6 +----- .../UniversalEnglishGrammaticalStructure.java | 16 ++++++++-------- .../trees/ud/UniversalGrammaticalStructure.java | 6 +++--- 5 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/edu/stanford/nlp/naturalli/NaturalLogicAnnotator.java b/src/edu/stanford/nlp/naturalli/NaturalLogicAnnotator.java index 1a09b3316a..a0630c5053 100644 --- a/src/edu/stanford/nlp/naturalli/NaturalLogicAnnotator.java +++ b/src/edu/stanford/nlp/naturalli/NaturalLogicAnnotator.java @@ -394,7 +394,7 @@ private void addNegationToDependencyGraph(SemanticGraph tree) { while (matcher.find()) { IndexedWord gov = matcher.getNode("gov"); IndexedWord dep = matcher.getNode("quantifier"); - tree.getEdge(gov, dep).setRelation(UniversalEnglishGrammaticalRelations.NEGATION_MODIFIER); + tree.updateEdge(tree.getEdge(gov, dep), UniversalEnglishGrammaticalRelations.NEGATION_MODIFIER); } // System.out.println("becomes"); // System.out.println(tree); diff --git a/src/edu/stanford/nlp/semgraph/SemanticGraph.java b/src/edu/stanford/nlp/semgraph/SemanticGraph.java index 3613c28df4..02dd67b84c 100644 --- a/src/edu/stanford/nlp/semgraph/SemanticGraph.java +++ b/src/edu/stanford/nlp/semgraph/SemanticGraph.java @@ -133,6 +133,15 @@ public boolean removeVertex(IndexedWord vertex) { return graph.removeVertex(vertex); } + public boolean updateEdge(SemanticGraphEdge edge, GrammaticalRelation reln) { + boolean removed = removeEdge(edge); + if (removed) { + SemanticGraphEdge newEdge = new SemanticGraphEdge(edge.getSource(), edge.getTarget(), reln, edge.getWeight(), edge.isExtra()); + addEdge(newEdge); + } + return removed; + } + /** * This returns an ordered list of vertices (based upon their * indices in the sentence). This creates and sorts a list, so diff --git a/src/edu/stanford/nlp/semgraph/SemanticGraphEdge.java b/src/edu/stanford/nlp/semgraph/SemanticGraphEdge.java index 0b41a14716..739ffca147 100644 --- a/src/edu/stanford/nlp/semgraph/SemanticGraphEdge.java +++ b/src/edu/stanford/nlp/semgraph/SemanticGraphEdge.java @@ -20,7 +20,7 @@ public class SemanticGraphEdge public static boolean printOnlyRelation = false; // a hack for displaying SemanticGraph in JGraph. Should be redone better. - private GrammaticalRelation relation; + private final GrammaticalRelation relation; private final double weight; private final boolean isExtra; @@ -63,10 +63,6 @@ public GrammaticalRelation getRelation() { return relation; } - public void setRelation(GrammaticalRelation relation) { - this.relation = relation; - } - public IndexedWord getSource() { return source; } diff --git a/src/edu/stanford/nlp/trees/UniversalEnglishGrammaticalStructure.java b/src/edu/stanford/nlp/trees/UniversalEnglishGrammaticalStructure.java index 0396d38cf2..0188a7d4f9 100644 --- a/src/edu/stanford/nlp/trees/UniversalEnglishGrammaticalStructure.java +++ b/src/edu/stanford/nlp/trees/UniversalEnglishGrammaticalStructure.java @@ -404,7 +404,7 @@ private static void addPassiveAgentToReln(SemanticGraph sg, IndexedWord gov, IndexedWord mod, IndexedWord caseMarker) { SemanticGraphEdge edge = sg.getEdge(gov, mod); - edge.setRelation(UniversalEnglishGrammaticalRelations.AGENT); + sg.updateEdge(edge, UniversalEnglishGrammaticalRelations.AGENT); } @@ -446,7 +446,7 @@ private static void addCaseMarkersToReln(SemanticGraph sg, IndexedWord gov, Inde lastCaseMarkerIndex = cm.index(); } GrammaticalRelation reln = getCaseMarkedRelation(edge.getRelation(), sb.toString().toLowerCase()); - edge.setRelation(reln); + sg.updateEdge(edge, reln); } private static final SemgrexPattern PREP_CONJP_PATTERN = SemgrexPattern.compile("{} >/(case|mark)/ ({}=gov >conj ({} >cc {}=cc) >conj {}=conj)" + @@ -752,7 +752,7 @@ private static void addConjToReln(SemanticGraph sg, for (IndexedWord conjDep : conjDeps) { SemanticGraphEdge edge = sg.getEdge(gov, conjDep); if (edge.getRelation() == CONJUNCT || conjDep.index() > ccDep.index()) { - edge.setRelation(conjValue(ccDep, sg)); + sg.updateEdge(edge, conjValue(ccDep, sg)); } } } @@ -876,23 +876,23 @@ private static void convertRel(SemanticGraph sg) { if (nmod.getGovernor().tag().startsWith("NN") || nmod.getGovernor().tag().startsWith("PRN") || nmod.getGovernor().tag().startsWith("DT")) { - nmod.setRelation(NOMINAL_MODIFIER); + sg.updateEdge(nmod, NOMINAL_MODIFIER); } else { - nmod.setRelation(OBLIQUE_MODIFIER); + sg.updateEdge(nmod, OBLIQUE_MODIFIER); } } break; } - if ( ! changedPrep) { - prep.setRelation(OBLIQUE_MODIFIER); + if (!changedPrep) { + sg.updateEdge(prep, OBLIQUE_MODIFIER); } } /* Rename remaining "rel" relations. */ for (SemanticGraphEdge edge : sg.findAllRelns(RELATIVE)) { - edge.setRelation(DIRECT_OBJECT); + sg.updateEdge(edge, DIRECT_OBJECT); } } diff --git a/src/edu/stanford/nlp/trees/ud/UniversalGrammaticalStructure.java b/src/edu/stanford/nlp/trees/ud/UniversalGrammaticalStructure.java index 3aef665f4d..edffdbf8d5 100644 --- a/src/edu/stanford/nlp/trees/ud/UniversalGrammaticalStructure.java +++ b/src/edu/stanford/nlp/trees/ud/UniversalGrammaticalStructure.java @@ -411,7 +411,7 @@ public static void addCaseMarkerForConjunctions(SemanticGraph sg) { if (edge1.getRelation().toString().startsWith(relnName) && edge1.getRelation().getSpecific() != null) { changed = true; - sg.getEdge(edge.getGovernor(), edge.getDependent(), edge.getRelation()).setRelation(edge1.getRelation()); + sg.updateEdge(edge, edge1.getRelation()); break; } } @@ -442,7 +442,7 @@ private static void addCaseMarkersToReln(SemanticGraph sg, IndexedWord gov, Inde //GrammaticalRelation reln = getCaseMarkedRelation(edge.getRelation(), relnName.toLowerCase() + ":ENH_CASE"); GrammaticalRelation reln = getCaseMarkedRelation(edge.getRelation(), relnName.toLowerCase()); - edge.setRelation(reln); + sg.updateEdge(edge, reln); } /** @@ -538,7 +538,7 @@ private static void addConjToReln(SemanticGraph sg, if (relnName.matches("[^a-zA-Z_]")) { continue; } - edge.setRelation(UniversalGrammaticalRelations.getConj(relnName)); + sg.updateEdge(edge, UniversalGrammaticalRelations.getConj(relnName)); } } }