|
| 1 | +package edu.stanford.nlp.trees.ud; |
| 2 | + |
| 3 | +import java.io.InputStream; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.OutputStream; |
| 6 | +import java.util.regex.Pattern; |
| 7 | + |
| 8 | +import edu.stanford.nlp.ling.CoreAnnotations;; |
| 9 | +import edu.stanford.nlp.pipeline.Annotation; |
| 10 | +import edu.stanford.nlp.pipeline.CoreNLPProtos; |
| 11 | +import edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer; |
| 12 | +import edu.stanford.nlp.semgraph.SemanticGraph; |
| 13 | +import edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations; |
| 14 | +import edu.stanford.nlp.trees.EnglishPatterns; |
| 15 | +import edu.stanford.nlp.util.CoreMap; |
| 16 | +import edu.stanford.nlp.util.ProcessProtobufRequest; |
| 17 | + |
| 18 | + |
| 19 | +public class ProcessUniversalEnhancerRequest extends ProcessProtobufRequest { |
| 20 | + |
| 21 | + /** |
| 22 | + * Enhance the dependencies on a single sentence, using the given relative pronouns pattern |
| 23 | + */ |
| 24 | + public static void enhanceDependencies(Pattern relativePronounsPattern, Annotation annotation) { |
| 25 | + for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) { |
| 26 | + SemanticGraph basic = sentence.get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class); |
| 27 | + SemanticGraph enhanced = UniversalEnhancer.enhanceGraph(basic, null, false, null, relativePronounsPattern); |
| 28 | + sentence.set(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class, enhanced); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Process all sentences in the document, enhancing the basic dependencies on each sentence |
| 34 | + */ |
| 35 | + public static CoreNLPProtos.Document processRequest(Pattern relativePronounsPattern, CoreNLPProtos.DependencyEnhancerRequest request) { |
| 36 | + ProtobufAnnotationSerializer serializer = new ProtobufAnnotationSerializer(); |
| 37 | + Annotation annotation = serializer.fromProto(request.getDocument()); |
| 38 | + |
| 39 | + enhanceDependencies(relativePronounsPattern, annotation); |
| 40 | + |
| 41 | + return serializer.toProto(annotation); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Figure out a relative patterns pronoun to use based on the information given in the request |
| 46 | + */ |
| 47 | + public static Pattern getRelativePronouns(CoreNLPProtos.DependencyEnhancerRequest request) { |
| 48 | + if (request.hasRelativePronouns()) { |
| 49 | + return Pattern.compile(request.getRelativePronouns()); |
| 50 | + } |
| 51 | + if (request.hasLanguage()) { |
| 52 | + switch(request.getLanguage()) { |
| 53 | + case English: |
| 54 | + case UniversalEnglish: |
| 55 | + return EnglishPatterns.RELATIVIZING_WORD_PATTERN; |
| 56 | + case Chinese: |
| 57 | + case UniversalChinese: |
| 58 | + // there are no relative pronouns in Chinese! |
| 59 | + return null; |
| 60 | + default: |
| 61 | + throw new IllegalArgumentException("Relative word pattern not defined for " + request.getLanguage()); |
| 62 | + } |
| 63 | + } |
| 64 | + throw new IllegalArgumentException("Could not find Language or predefined relative pronouns pattern in the request"); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Process a single request, adding enhanced dependencies to each sentence in the document |
| 69 | + */ |
| 70 | + @Override |
| 71 | + public void processInputStream(InputStream in, OutputStream out) throws IOException { |
| 72 | + CoreNLPProtos.DependencyEnhancerRequest request = CoreNLPProtos.DependencyEnhancerRequest.parseFrom(in); |
| 73 | + |
| 74 | + Pattern relativePronounsPattern = getRelativePronouns(request); |
| 75 | + |
| 76 | + CoreNLPProtos.Document response = processRequest(relativePronounsPattern, request); |
| 77 | + response.writeTo(out); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * The inherited main program will either enhance a single document, |
| 82 | + * or will listen to stdin and enhance every document that comes in |
| 83 | + * until a terminator is sent or the stream closes |
| 84 | + */ |
| 85 | + public static void main(String[] args) throws IOException { |
| 86 | + ProcessProtobufRequest.process(new ProcessUniversalEnhancerRequest(), args); |
| 87 | + } |
| 88 | +} |
0 commit comments