1212import edu .stanford .nlp .semgraph .SemanticGraph ;
1313import edu .stanford .nlp .semgraph .SemanticGraphCoreAnnotations ;
1414import edu .stanford .nlp .trees .EnglishPatterns ;
15+ import edu .stanford .nlp .trees .UniversalEnglishGrammaticalStructure ;
1516import edu .stanford .nlp .util .CoreMap ;
1617import 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