|
| 1 | +package edu.stanford.nlp.trees.ud; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.Comparator; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.TreeMap; |
| 9 | + |
| 10 | +/** |
| 11 | + * A subclass of TreeMap with a toString() that looks like a CoNLLUFeatures |
| 12 | + * and a method for extracting the features from a CoNLLU string |
| 13 | + * <br> |
| 14 | + * This is a TreeMap so that the features are sorted by their key, |
| 15 | + * which is necessary for the CoNLLU format |
| 16 | + */ |
| 17 | +public class CoNLLUFeatures extends TreeMap<String, String> { |
| 18 | + /** |
| 19 | + * Parses the value of the feature column in a CoNLL-U file |
| 20 | + * and returns them in a HashMap with the feature names as keys |
| 21 | + * and the feature values as values. |
| 22 | + * |
| 23 | + * @param featureString |
| 24 | + * @return A {@code HashMap<String,String>} with the feature values. |
| 25 | + */ |
| 26 | + public CoNLLUFeatures(String featureString) { |
| 27 | + super(); |
| 28 | + |
| 29 | + if (!featureString.equals("_")) { |
| 30 | + String[] featValPairs = featureString.split("\\|"); |
| 31 | + for (String p : featValPairs) { |
| 32 | + String[] featValPair = p.split("="); |
| 33 | + this.put(featValPair[0], featValPair[1]); |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public CoNLLUFeatures(Map<String, String> features) { |
| 39 | + super(features); |
| 40 | + } |
| 41 | + |
| 42 | + public CoNLLUFeatures() { |
| 43 | + super(); |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + public static class FeatureNameComparator implements Comparator<String> { |
| 48 | + @Override |
| 49 | + public int compare(String featureName1, String featureName2) { |
| 50 | + return featureName1.toLowerCase().compareTo(featureName2.toLowerCase()); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Converts the features to a feature string to be used |
| 56 | + * in a CoNLL-U file. |
| 57 | + * |
| 58 | + * @return The feature string. |
| 59 | + */ |
| 60 | + public static String toFeatureString(Map<String,String> features) { |
| 61 | + StringBuilder sb = new StringBuilder(); |
| 62 | + boolean first = true; |
| 63 | + if (features != null) { |
| 64 | + List<String> sortedKeys = new ArrayList<>(features.keySet()); |
| 65 | + Collections.sort(sortedKeys, new FeatureNameComparator()); |
| 66 | + for (String key : sortedKeys) { |
| 67 | + if (!first) { |
| 68 | + sb.append("|"); |
| 69 | + } else { |
| 70 | + first = false; |
| 71 | + } |
| 72 | + sb.append(key) |
| 73 | + .append("=") |
| 74 | + .append(features.get(key)); |
| 75 | + |
| 76 | + } |
| 77 | + } |
| 78 | + /* Empty feature list. */ |
| 79 | + if (first) { |
| 80 | + sb.append("_"); |
| 81 | + } |
| 82 | + |
| 83 | + return sb.toString(); |
| 84 | + } |
| 85 | + |
| 86 | + public String toString() { |
| 87 | + return toFeatureString(this); |
| 88 | + } |
| 89 | +} |
0 commit comments