|
| 1 | +package edu.stanford.nlp.trees; |
| 2 | + |
| 3 | +import java.io.Reader; |
| 4 | +import java.io.StringReader; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Collections; |
| 7 | +import java.util.Comparator; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Iterator; |
| 10 | +import java.util.List; |
| 11 | +import java.util.function.Function; |
| 12 | + |
| 13 | +import edu.stanford.nlp.international.Language; |
| 14 | +import edu.stanford.nlp.ling.CoreAnnotations; |
| 15 | +import edu.stanford.nlp.ling.IndexedWord; |
| 16 | +import edu.stanford.nlp.objectbank.DelimitRegExIterator; |
| 17 | +import edu.stanford.nlp.objectbank.IteratorFromReaderFactory; |
| 18 | +import edu.stanford.nlp.objectbank.ObjectBank; |
| 19 | +import edu.stanford.nlp.semgraph.SemanticGraph; |
| 20 | +import edu.stanford.nlp.trees.GrammaticalRelation; |
| 21 | +import edu.stanford.nlp.trees.TypedDependency; |
| 22 | + |
| 23 | +/** |
| 24 | + * Reader for ConLL-U formatted dependency treebanks. |
| 25 | + * |
| 26 | + * @author Sebastian Schuster |
| 27 | + */ |
| 28 | + |
| 29 | + |
| 30 | +public class CoNLLUDocumentReader implements |
| 31 | + IteratorFromReaderFactory<SemanticGraph> { |
| 32 | + |
| 33 | + |
| 34 | + private IteratorFromReaderFactory<SemanticGraph> ifrf; |
| 35 | + |
| 36 | + public CoNLLUDocumentReader() { |
| 37 | + this.ifrf = DelimitRegExIterator.getFactory("\n(\\s*\n)+", new SentenceProcessor()); |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + @Override |
| 42 | + public Iterator<SemanticGraph> getIterator(Reader r) { |
| 43 | + return ifrf.getIterator(r); |
| 44 | + } |
| 45 | + |
| 46 | + private static class SentenceProcessor implements Function<String,SemanticGraph> { |
| 47 | + public SemanticGraph apply(String line) { |
| 48 | + if (line == null) return null; |
| 49 | + Function<String,IndexedWord> func = new WordProcessor(); |
| 50 | + ObjectBank<IndexedWord> words = ObjectBank.getLineIterator(new StringReader(line), func); |
| 51 | + List<IndexedWord> sorted = new ArrayList<IndexedWord>(words); |
| 52 | + Collections.sort(sorted); |
| 53 | + |
| 54 | + |
| 55 | + /* Construct a semantic graph. */ |
| 56 | + List<TypedDependency> deps = new ArrayList<TypedDependency>(sorted.size()); |
| 57 | + for (IndexedWord word : sorted) { |
| 58 | + GrammaticalRelation reln = GrammaticalRelation.valueOf(Language.UniversalEnglish, word.get(CoreAnnotations.CoNLLDepTypeAnnotation.class)); |
| 59 | + int govIdx = word.get(CoreAnnotations.CoNLLDepParentIndexAnnotation.class); |
| 60 | + IndexedWord gov; |
| 61 | + if (govIdx == 0) { |
| 62 | + gov = new IndexedWord(word.docID(), word.sentIndex(), 0); |
| 63 | + gov.setValue("ROOT"); |
| 64 | + if (word.get(CoreAnnotations.CoNLLDepTypeAnnotation.class).equals("root")) { |
| 65 | + reln = GrammaticalRelation.ROOT; |
| 66 | + } |
| 67 | + } else { |
| 68 | + gov = sorted.get(govIdx - 1); |
| 69 | + } |
| 70 | + TypedDependency dep = new TypedDependency(reln, gov, word); |
| 71 | + deps.add(dep); |
| 72 | + } |
| 73 | + |
| 74 | + return new SemanticGraph(deps); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private static class WordProcessor implements Function<String,IndexedWord> { |
| 79 | + public IndexedWord apply(String line) { |
| 80 | + String[] bits = line.split("\\s+"); |
| 81 | + IndexedWord word = new IndexedWord(); |
| 82 | + word.set(CoreAnnotations.IndexAnnotation.class, Integer.parseInt(bits[0])); |
| 83 | + word.set(CoreAnnotations.TextAnnotation.class, bits[1]); |
| 84 | + word.set(CoreAnnotations.LemmaAnnotation.class, bits[2]); |
| 85 | + word.set(CoreAnnotations.CoarseTagAnnotation.class, bits[3]); |
| 86 | + word.set(CoreAnnotations.PartOfSpeechAnnotation.class, bits[4]); |
| 87 | + |
| 88 | + word.set(CoreAnnotations.CoNLLDepParentIndexAnnotation.class, Integer.parseInt(bits[6])); |
| 89 | + word.set(CoreAnnotations.CoNLLDepTypeAnnotation.class, bits[7]); |
| 90 | + word.set(CoreAnnotations.CoNLLUSecondaryDepsAnnotation.class, bits[8]); |
| 91 | + word.set(CoreAnnotations.CoNLLUMisc.class, bits[9]); |
| 92 | + |
| 93 | + word.setIndex(Integer.parseInt(bits[0])); |
| 94 | + word.setValue(bits[1]); |
| 95 | + |
| 96 | + /* Parse features. */ |
| 97 | + HashMap<String, String> features = parseFeatures(bits[5]); |
| 98 | + |
| 99 | + word.set(CoreAnnotations.CoNLLUFeats.class, features); |
| 100 | + |
| 101 | + |
| 102 | + return word; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + /** |
| 108 | + * Parses the value of the feature column in a CoNLL-U file |
| 109 | + * and returns them in a HashMap with the feature names as keys |
| 110 | + * and the feature values as values. |
| 111 | + * |
| 112 | + * @param featureString |
| 113 | + * @return A HashMap<String,String> with the feature values. |
| 114 | + */ |
| 115 | + public static HashMap<String,String> parseFeatures(String featureString) { |
| 116 | + HashMap<String, String> features = new HashMap<String, String>(); |
| 117 | + if (! featureString.equals("_")) { |
| 118 | + String[] featValPairs = featureString.split("\\|"); |
| 119 | + for (String p : featValPairs) { |
| 120 | + String[] featValPair = p.split("="); |
| 121 | + features.put(featValPair[0], featValPair[1]); |
| 122 | + } |
| 123 | + } |
| 124 | + return features; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Converts a feature HashMap to a feature string to be used |
| 129 | + * in a CoNLL-U file. |
| 130 | + * |
| 131 | + * @return The feature string. |
| 132 | + */ |
| 133 | + |
| 134 | + public static String toFeatureString(HashMap<String,String> features) { |
| 135 | + StringBuffer sb = new StringBuffer(); |
| 136 | + boolean first = true; |
| 137 | + List<String> sortedKeys = new ArrayList<String>(features.keySet()); |
| 138 | + Collections.sort(sortedKeys, new FeatureNameComparator()); |
| 139 | + for (String key : sortedKeys) { |
| 140 | + if ( ! first) { |
| 141 | + sb.append("|"); |
| 142 | + } else { |
| 143 | + first = false; |
| 144 | + } |
| 145 | + |
| 146 | + sb.append(key) |
| 147 | + .append("=") |
| 148 | + .append(features.get(key)); |
| 149 | + |
| 150 | + } |
| 151 | + |
| 152 | + /* Empty feature list. */ |
| 153 | + if (first) { |
| 154 | + sb.append("_"); |
| 155 | + } |
| 156 | + |
| 157 | + return sb.toString(); |
| 158 | + } |
| 159 | + |
| 160 | + public static class FeatureNameComparator implements Comparator<String> { |
| 161 | + |
| 162 | + @Override |
| 163 | + public int compare(String featureName1, String featureName2) { |
| 164 | + return featureName1.toLowerCase().compareTo(featureName2.toLowerCase()); |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments