Skip to content

Commit 7e82e0b

Browse files
authored
Merge pull request #1154 from stanfordnlp/english_enhanced
UniversalEnhancer can process English separately from the others, sin…
2 parents 89c93e1 + ba2ca8a commit 7e82e0b

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/edu/stanford/nlp/trees/UniversalEnglishGrammaticalStructure.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -896,11 +896,10 @@ private static void convertRel(SemanticGraph sg) {
896896
}
897897
}
898898

899-
@Override
900-
protected void addEnhancements(List<TypedDependency> list, EnhancementOptions options) {
901-
902-
SemanticGraph sg = new SemanticGraph(list);
903-
899+
/**
900+
* Manipulates the given SemanticGraph to add enhancements. Note that this modifies the input graph.
901+
*/
902+
public static void addEnhancements(SemanticGraph sg, EnhancementOptions options) {
904903
if (DEBUG) {
905904
printListSorted("addEnhancements: before correctDependencies()", sg.typedDependencies());
906905
}
@@ -977,6 +976,15 @@ protected void addEnhancements(List<TypedDependency> list, EnhancementOptions op
977976
}
978977

979978
correctSubjPass(sg);
979+
}
980+
981+
@Override
982+
protected void addEnhancements(List<TypedDependency> list, EnhancementOptions options) {
983+
984+
SemanticGraph sg = new SemanticGraph(list);
985+
986+
addEnhancements(sg, options);
987+
980988
list.clear();
981989
list.addAll(sg.typedDependencies());
982990

src/edu/stanford/nlp/trees/ud/ProcessUniversalEnhancerRequest.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import edu.stanford.nlp.semgraph.SemanticGraph;
1313
import edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations;
1414
import edu.stanford.nlp.trees.EnglishPatterns;
15+
import edu.stanford.nlp.trees.UniversalEnglishGrammaticalStructure;
1516
import edu.stanford.nlp.util.CoreMap;
1617
import edu.stanford.nlp.util.ProcessProtobufRequest;
1718

@@ -29,14 +30,36 @@ public static void enhanceDependencies(Pattern relativePronounsPattern, Annotati
2930
}
3031
}
3132

33+
/**
34+
* Enhance the dependencies on a single sentence, using the English-specific rules for enhancement
35+
*/
36+
public static void enhanceEnglishDependencies(Annotation annotation) {
37+
for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
38+
SemanticGraph basic = sentence.get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class);
39+
40+
SemanticGraph enhanced = new SemanticGraph(basic);
41+
UniversalEnglishGrammaticalStructure.addEnhancements(enhanced, UniversalEnglishGrammaticalStructure.ENHANCED_OPTIONS);
42+
sentence.set(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class, enhanced);
43+
44+
SemanticGraph plusplus = new SemanticGraph(basic);
45+
UniversalEnglishGrammaticalStructure.addEnhancements(plusplus, UniversalEnglishGrammaticalStructure.ENHANCED_PLUS_PLUS_OPTIONS);
46+
sentence.set(SemanticGraphCoreAnnotations.EnhancedPlusPlusDependenciesAnnotation.class, plusplus);
47+
}
48+
}
49+
3250
/**
3351
* Process all sentences in the document, enhancing the basic dependencies on each sentence
3452
*/
3553
public static CoreNLPProtos.Document processRequest(Pattern relativePronounsPattern, CoreNLPProtos.DependencyEnhancerRequest request) {
3654
ProtobufAnnotationSerializer serializer = new ProtobufAnnotationSerializer();
3755
Annotation annotation = serializer.fromProto(request.getDocument());
3856

39-
enhanceDependencies(relativePronounsPattern, annotation);
57+
if (request.hasLanguage() &&
58+
(request.getLanguage() == CoreNLPProtos.Language.English || request.getLanguage() == CoreNLPProtos.Language.UniversalEnglish)) {
59+
enhanceEnglishDependencies(annotation);
60+
} else {
61+
enhanceDependencies(relativePronounsPattern, annotation);
62+
}
4063

4164
return serializer.toProto(annotation);
4265
}

0 commit comments

Comments
 (0)