java.util.function.Functionedu.stanford.nlp.ling.CoreAnnotations



Project stanfordnlp/CoreNLP in file ...b7f493c8/src.edu.stanford.nlp.trees.TreePrint.java (2014-09-09)
@@ -1,6 +1,7 @@
 package edu.stanford.nlp.trees;
 
 import edu.stanford.nlp.ling.*;
+import edu.stanford.nlp.ling.CoreAnnotations;
 import edu.stanford.nlp.process.PTBTokenizer;
 import edu.stanford.nlp.trees.international.pennchinese.ChineseEnglishWordMap;
 import edu.stanford.nlp.util.*;
@@ -8,7 +9,6 @@ import edu.stanford.nlp.util.XMLUtils;
 
 import java.io.*;
 import java.util.*;
-import java.util.function.Function;
 
 
 /**
@@ -412,18 +412,20 @@ public class TreePrint {
     }
 
     if (transChinese) {
-      TreeTransformer tt = t1 -> {
-        t1 = t1.treeSkeletonCopy();
-        for (Tree subtree : t1) {
-          if (subtree.isLeaf()) {
-            Label oldLabel = subtree.label();
-            String translation = ChineseEnglishWordMap.getInstance().getFirstTranslation(oldLabel.value());
-            if (translation == null) translation = "[UNK]";
-            Label newLabel = new StringLabel(oldLabel.value() + ':' + translation);
-            subtree.setLabel(newLabel);
+      TreeTransformer tt = new TreeTransformer() {
+        public Tree transformTree(Tree t) {
+          t = t.treeSkeletonCopy();
+          for (Tree subtree : t) {
+            if (subtree.isLeaf()) {
+              Label oldLabel = subtree.label();
+              String translation = ChineseEnglishWordMap.getInstance().getFirstTranslation(oldLabel.value());
+              if (translation == null) translation = "[UNK]";
+              Label newLabel = new StringLabel(oldLabel.value() + ':' + translation);
+              subtree.setLabel(newLabel);
+            }
           }
+          return t;
         }
-        return t1;
       };
       outputPSTree = tt.transformTree(outputPSTree);
     }
@@ -842,7 +844,11 @@ public class TreePrint {
       if (argsMap.keySet().contains("-useTLPTreeReader")) {
         trf = tlp.treeReaderFactory();
       } else {
-        trf = in -> new PennTreeReader(in, new LabeledScoredTreeFactory(new StringLabelFactory()), new TreeNormalizer());
+        trf = new TreeReaderFactory() {
+          public TreeReader newTreeReader(Reader in) {
+            return new PennTreeReader(in, new LabeledScoredTreeFactory(new StringLabelFactory()), new TreeNormalizer());
+          }
+        };
       }
       trees = new DiskTreebank(trf);
       trees.loadPath(args[0]);
@@ -927,7 +933,7 @@ public class TreePrint {
    *         typed dependencies
    */
   private static String toString(Collection<TypedDependency> dependencies, boolean extraSep, boolean includeTags) {
-    CoreLabel.OutputFormat labelFormat = (includeTags) ? CoreLabel.OutputFormat.VALUE_TAG_INDEX : CoreLabel.OutputFormat.VALUE_INDEX;
+    String labelFormat = (includeTags) ? "value-tag-index" : "value-index";
     StringBuilder buf = new StringBuilder();
     if (extraSep) {
       List<TypedDependency> extraDeps =  new ArrayList<TypedDependency>();
Project stanfordnlp/CoreNLP in file ...41a7fa0b/src.edu.stanford.nlp.trees.TreePrint.java (2014-09-24)
@@ -1,6 +1,7 @@
 package edu.stanford.nlp.trees;
 
 import edu.stanford.nlp.ling.*;
+import edu.stanford.nlp.ling.CoreAnnotations;
 import edu.stanford.nlp.process.PTBTokenizer;
 import edu.stanford.nlp.trees.international.pennchinese.ChineseEnglishWordMap;
 import edu.stanford.nlp.util.*;
@@ -8,7 +9,6 @@ import edu.stanford.nlp.util.XMLUtils;
 
 import java.io.*;
 import java.util.*;
-import java.util.function.Function;
 
 
 /**
@@ -412,18 +412,20 @@ public class TreePrint {
     }
 
     if (transChinese) {
-      TreeTransformer tt = t1 -> {
-        t1 = t1.treeSkeletonCopy();
-        for (Tree subtree : t1) {
-          if (subtree.isLeaf()) {
-            Label oldLabel = subtree.label();
-            String translation = ChineseEnglishWordMap.getInstance().getFirstTranslation(oldLabel.value());
-            if (translation == null) translation = "[UNK]";
-            Label newLabel = new StringLabel(oldLabel.value() + ':' + translation);
-            subtree.setLabel(newLabel);
+      TreeTransformer tt = new TreeTransformer() {
+        public Tree transformTree(Tree t) {
+          t = t.treeSkeletonCopy();
+          for (Tree subtree : t) {
+            if (subtree.isLeaf()) {
+              Label oldLabel = subtree.label();
+              String translation = ChineseEnglishWordMap.getInstance().getFirstTranslation(oldLabel.value());
+              if (translation == null) translation = "[UNK]";
+              Label newLabel = new StringLabel(oldLabel.value() + ':' + translation);
+              subtree.setLabel(newLabel);
+            }
           }
+          return t;
         }
-        return t1;
       };
       outputPSTree = tt.transformTree(outputPSTree);
     }
@@ -842,7 +844,11 @@ public class TreePrint {
       if (argsMap.keySet().contains("-useTLPTreeReader")) {
         trf = tlp.treeReaderFactory();
       } else {
-        trf = in -> new PennTreeReader(in, new LabeledScoredTreeFactory(new StringLabelFactory()), new TreeNormalizer());
+        trf = new TreeReaderFactory() {
+          public TreeReader newTreeReader(Reader in) {
+            return new PennTreeReader(in, new LabeledScoredTreeFactory(new StringLabelFactory()), new TreeNormalizer());
+          }
+        };
       }
       trees = new DiskTreebank(trf);
       trees.loadPath(args[0]);
@@ -927,7 +933,7 @@ public class TreePrint {
    *         typed dependencies
    */
   private static String toString(Collection<TypedDependency> dependencies, boolean extraSep, boolean includeTags) {
-    CoreLabel.OutputFormat labelFormat = (includeTags) ? CoreLabel.OutputFormat.VALUE_TAG_INDEX : CoreLabel.OutputFormat.VALUE_INDEX;
+    String labelFormat = (includeTags) ? "value-tag-index" : "value-index";
     StringBuilder buf = new StringBuilder();
     if (extraSep) {
       List<TypedDependency> extraDeps =  new ArrayList<TypedDependency>();
Project stanfordnlp/CoreNLP in file ...ford.nlp.sequences.MUCDocumentReaderAndWriter.java (2015-03-14)
@@ -12,14 +12,14 @@ import java.util.regex.Pattern;
 
 import edu.stanford.nlp.ling.CoreAnnotations;
 import edu.stanford.nlp.ling.CoreLabel;
+import edu.stanford.nlp.ling.CoreAnnotations;
 import edu.stanford.nlp.objectbank.IteratorFromReaderFactory;
 import edu.stanford.nlp.objectbank.XMLBeginEndIterator;
-import java.util.function.Function;
+import edu.stanford.nlp.util.Function;
 import edu.stanford.nlp.process.PTBTokenizer;
 
 /**
  * DocumentReader for MUC format.
- *
  * @author Jenny Finkel
  */
 public class MUCDocumentReaderAndWriter implements DocumentReaderAndWriter<CoreLabel> {
@@ -29,7 +29,7 @@ public class MUCDocumentReaderAndWriter implements DocumentReaderAndWriter<CoreL
    */
   private static final long serialVersionUID = -8334720781758500037L;
   private SeqClassifierFlags flags;
-  private IteratorFromReaderFactory<List<CoreLabel>> factory;
+  private IteratorFromReaderFactory factory;
 
   public void init(SeqClassifierFlags flags) {
     this.flags = flags;
@@ -42,9 +42,9 @@ public class MUCDocumentReaderAndWriter implements DocumentReaderAndWriter<CoreL
 
   static class MUCDocumentParser implements Function<String, List<CoreLabel>> {
 
-    private static final Pattern sgml = Pattern.compile("<([^>\\s]*)[^>]*>");
-    private static final Pattern beginEntity = Pattern.compile("<(ENAMEX|TIMEX|NUMEX) TYPE=\"([a-z]+)\"[^>]*>", Pattern.CASE_INSENSITIVE);
-    private static final Pattern endEntity = Pattern.compile("</(ENAMEX|TIMEX|NUMEX)>");
+    private static Pattern sgml = Pattern.compile("<([^>\\s]*)[^>]*>");
+    private static Pattern beginEntity = Pattern.compile("<(ENAMEX|TIMEX|NUMEX) TYPE=\"([a-z]+)\"[^>]*>", Pattern.CASE_INSENSITIVE);
+    private static Pattern endEntity = Pattern.compile("</(ENAMEX|TIMEX|NUMEX)>");
 
     public List<CoreLabel> apply(String doc) {
 
@@ -61,7 +61,7 @@ public class MUCDocumentReaderAndWriter implements DocumentReaderAndWriter<CoreL
       PTBTokenizer ptb = PTBTokenizer.newPTBTokenizer(new BufferedReader(new StringReader(doc)), false, true);
       List<CoreLabel> words = ptb.tokenize();
 
-      List<CoreLabel> result = new ArrayList<CoreLabel>();
+      List<CoreLabel> result = new ArrayList();
 
       CoreLabel prev = null;
       String prevString = "";
Project stanfordnlp/CoreNLP in file ...b26760aa/src.edu.stanford.nlp.trees.TreePrint.java (2015-03-14)
@@ -1,6 +1,7 @@
 package edu.stanford.nlp.trees;
 
 import edu.stanford.nlp.ling.*;
+import edu.stanford.nlp.ling.CoreAnnotations;
 import edu.stanford.nlp.process.PTBTokenizer;
 import edu.stanford.nlp.trees.international.pennchinese.ChineseEnglishWordMap;
 import edu.stanford.nlp.util.*;
@@ -8,8 +9,6 @@ import edu.stanford.nlp.util.XMLUtils;
 
 import java.io.*;
 import java.util.*;
-import java.util.function.Function;
-import java.util.function.Predicate;
 
 
 /**
@@ -40,7 +39,6 @@ public class TreePrint {
     "typedDependencies",
     "typedDependenciesCollapsed",
     "latexTree",
-    "xmlTree",
     "collocations",
     "semanticGraph",
     "conllStyleDependencies",
@@ -62,13 +60,11 @@ public class TreePrint {
   private final boolean CCPropagatedDependencies;
   private final boolean treeDependencies;
 
-  private final boolean includeTags;
-
   private final HeadFinder hf;
   private final TreebankLanguagePack tlp;
   private final WordStemmer stemmer;
-  private final Predicate<Dependency<Label, Label, Object>> dependencyFilter;
-  private final Predicate<Dependency<Label, Label, Object>> dependencyWordFilter;
+  private final Filter<Dependency<Label, Label, Object>> dependencyFilter;
+  private final Filter<Dependency<Label, Label, Object>> dependencyWordFilter;
   private final GrammaticalStructureFactory gsf;
 
   /** Pool use of one WordNetConnection.  I don't really know if
@@ -113,7 +109,7 @@ public class TreePrint {
    *
    * @param formatString A comma separated list of ways to print each Tree.
    *                For instance, "penn" or "words,typedDependencies".
-   *                Known formats are: oneline, penn, latexTree, xmlTree, words,
+   *                Known formats are: oneline, penn, latexTree, words,
    *                wordsAndTags, rootSymbolOnly, dependencies,
    *                typedDependencies, typedDependenciesCollapsed,
    *                collocations, semanticGraph, conllStyleDependencies,
@@ -133,7 +129,7 @@ public class TreePrint {
    *                xml, removeTopBracket, transChinese,
    *                includePunctuationDependencies, basicDependencies, treeDependencies,
    *                CCPropagatedDependencies, collapsedDependencies, nonCollapsedDependencies,
-   *                nonCollapsedDependenciesSeparated, includeTags
+   *                nonCollapsedDependenciesSeparated
    *                </code>.
    * @param tlp     The TreebankLanguagePack used to do things like delete
    *                or ignore punctuation in output
@@ -155,7 +151,7 @@ public class TreePrint {
     boolean includePunctuationDependencies;
     includePunctuationDependencies = propertyToBoolean(this.options,
                                                        "includePunctuationDependencies");
-    Predicate<String> puncWordFilter;
+    Filter<String> puncWordFilter;
     if (includePunctuationDependencies) {
       dependencyFilter = Filters.acceptFilter();
       dependencyWordFilter = Filters.acceptFilter();
@@ -171,8 +167,7 @@ public class TreePrint {
       stemmer = null;
     }
     if (formats.containsKey("typedDependenciesCollapsed") ||
-        formats.containsKey("typedDependencies") ||
-        (formats.containsKey("conll2007") && tlp.supportsGrammaticalStructures())) {
+        formats.containsKey("typedDependencies")) {
       gsf = tlp.grammaticalStructureFactory(puncWordFilter, typedDependencyHF);
     } else {
       gsf = null;
@@ -190,8 +185,6 @@ public class TreePrint {
     nonCollapsedDependenciesSeparated = propertyToBoolean(this.options, "nonCollapsedDependenciesSeparated");
     treeDependencies = propertyToBoolean(this.options, "treeDependencies");
 
-    includeTags = propertyToBoolean(this.options, "includeTags");
-
     // if no option format for the dependencies is specified, CCPropagated is the default
     if ( ! basicDependencies && ! collapsedDependencies && ! nonCollapsedDependencies && ! nonCollapsedDependenciesSeparated && ! treeDependencies) {
       CCPropagatedDependencies = true;
@@ -413,18 +406,20 @@ public class TreePrint {
     }
 
     if (transChinese) {
-      TreeTransformer tt = t1 -> {
-        t1 = t1.treeSkeletonCopy();
-        for (Tree subtree : t1) {
-          if (subtree.isLeaf()) {
-            Label oldLabel = subtree.label();
-            String translation = ChineseEnglishWordMap.getInstance().getFirstTranslation(oldLabel.value());
-            if (translation == null) translation = "[UNK]";
-            Label newLabel = new StringLabel(oldLabel.value() + ':' + translation);
-            subtree.setLabel(newLabel);
+      TreeTransformer tt = new TreeTransformer() {
+        public Tree transformTree(Tree t) {
+          t = t.treeSkeletonCopy();
+          for (Tree subtree : t) {
+            if (subtree.isLeaf()) {
+              Label oldLabel = subtree.label();
+              String translation = ChineseEnglishWordMap.getInstance().getFirstTranslation(oldLabel.value());
+              if (translation == null) translation = "[UNK]";
+              Label newLabel = new StringLabel(oldLabel.value() + ':' + translation);
+              subtree.setLabel(newLabel);
+            }
           }
+          return t;
         }
-        return t1;
       };
       outputPSTree = tt.transformTree(outputPSTree);
     }
@@ -458,11 +453,6 @@ public class TreePrint {
         pw.println(".]");
         pw.println("  </tree>");
       }
-      if (formats.containsKey("xmlTree")) {
-        pw.println("<tree style=\"xml\">");
-        outputTree.indentedXMLPrint(pw,false);
-        pw.println("</tree>");
-      }
       if (formats.containsKey("dependencies")) {
         Tree indexedTree = outputTree.deepCopy(outputTree.treeFactory(),
                                                  CoreLabel.factory());
@@ -482,24 +472,24 @@ public class TreePrint {
       if (formats.containsKey("typedDependencies")) {
         GrammaticalStructure gs = gsf.newGrammaticalStructure(outputTree);
         if (basicDependencies) {
-          print(gs.typedDependencies(), "xml", includeTags, pw);
+          print(gs.typedDependencies(), "xml", pw);
         }
         if (nonCollapsedDependencies || nonCollapsedDependenciesSeparated) {
-          print(gs.allTypedDependencies(), "xml", includeTags, pw);
+          print(gs.allTypedDependencies(), "xml", pw);
         }
         if (collapsedDependencies) {
-          print(gs.typedDependenciesCollapsed(GrammaticalStructure.Extras.MAXIMAL), "xml", includeTags, pw);
+          print(gs.typedDependenciesCollapsed(true), "xml", pw);
         }
         if (CCPropagatedDependencies) {
-          print(gs.typedDependenciesCCprocessed(), "xml", includeTags, pw);
+          print(gs.typedDependenciesCCprocessed(), "xml", pw);
         }
         if(treeDependencies) {
-          print(gs.typedDependenciesCollapsedTree(), "xml", includeTags, pw);
+          print(gs.typedDependenciesCollapsedTree(), "xml", pw);
         }
       }
       if (formats.containsKey("typedDependenciesCollapsed")) {
         GrammaticalStructure gs = gsf.newGrammaticalStructure(outputTree);
-        print(gs.typedDependenciesCCprocessed(), "xml", includeTags, pw);
+        print(gs.typedDependenciesCCprocessed(), "xml", pw);
       }
 
       // This makes parser require jgrapht.  Bad.
@@ -528,13 +518,12 @@ public class TreePrint {
         outputTree.indentedListPrint(pw,false);
         pw.println(".]");
       }
-      if (formats.containsKey("xmlTree")) {
-        outputTree.indentedXMLPrint(pw,false);
-      }
       if (formats.containsKey("dependencies")) {
         Tree indexedTree = outputTree.deepCopy(outputTree.treeFactory());
         indexedTree.indexLeaves();
-        List<Dependency<Label, Label, Object>> sortedDeps = getSortedDeps(indexedTree, dependencyWordFilter);
+        Set<Dependency<Label, Label, Object>> depsSet = indexedTree.mapDependencies(dependencyWordFilter, hf);
+        List<Dependency<Label, Label, Object>> sortedDeps = new ArrayList<Dependency<Label, Label, Object>>(depsSet);
+        Collections.sort(sortedDeps, Dependencies.dependencyIndexComparator());
         for (Dependency<Label, Label, Object> d : sortedDeps) {
           pw.println(d.toString("predicate"));
         }
@@ -546,28 +535,23 @@ public class TreePrint {
         //      that dependencies for other languages can be printed.
         // wsg2011: This code currently ignores the dependency label since the present implementation
         //          of mapDependencies() returns UnnamedDependency objects.
-        // TODO: if there is a GrammaticalStructureFactory available, use that instead of mapDependencies
+
         Tree it = outputTree.deepCopy(outputTree.treeFactory(), CoreLabel.factory());
         it.indexLeaves();
 
         List<CoreLabel> tagged = it.taggedLabeledYield();
-        List<Dependency<Label, Label, Object>> sortedDeps = getSortedDeps(it, Filters.<Dependency<Label, Label, Object>>acceptFilter());
+        Set<Dependency<Label, Label, Object>> depsSet = it.mapDependencies(dependencyFilter, hf, "root");
+        List<Dependency<Label, Label, Object>> sortedDeps = new ArrayList<Dependency<Label, Label, Object>>(depsSet);
+        Collections.sort(sortedDeps, Dependencies.dependencyIndexComparator());
 
-        for (int i = 0; i < sortedDeps.size(); i++) {
+        for (int i = 0; i < tagged.size(); i++) {
+          CoreLabel w = tagged.get(i);
           Dependency<Label, Label, Object> d = sortedDeps.get(i);
-          if (!dependencyFilter.test(d)) {
-            continue;
-          }
-          if (!(d.dependent() instanceof HasIndex) || !(d.governor() instanceof HasIndex)) {
-            throw new IllegalArgumentException("Expected labels to have indices");
-          }
-          HasIndex dep = (HasIndex) d.dependent();
-          HasIndex gov = (HasIndex) d.governor();
-
-          int depi = dep.index();
-          int govi = gov.index();
+          CoreMap dep = (CoreMap) d.dependent();
+          CoreMap gov = (CoreMap) d.governor();
 
-          CoreLabel w = tagged.get(depi-1);
+          Integer depi = dep.get(CoreAnnotations.IndexAnnotation.class);
+          Integer govi = gov.get(CoreAnnotations.IndexAnnotation.class);
 
           // Used for both course and fine POS tag fields
           String tag = PTBTokenizer.ptbToken2Text(w.tag());
@@ -577,12 +561,7 @@ public class TreePrint {
           String feats = "_";
           String pHead = "_";
           String pDepRel = "_";
-          String depRel;
-          if (d.name() != null) {
-            depRel = d.name().toString();
-          } else {
-            depRel = (govi == 0) ? "ROOT" : "NULL";
-          }
+          String depRel = (govi == 0) ? "ROOT" : "NULL";
 
           // The 2007 format has 10 fields
           pw.printf("%d\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t%s%n", depi,word,lemma,tag,tag,feats,govi,depRel,pHead,pDepRel);
@@ -615,7 +594,7 @@ public class TreePrint {
           System.err.println(t);
           System.err.println();
         } else {
-          Map<Integer,Integer> deps = Generics.newHashMap();
+          Map<Integer,Integer> deps = new HashMap<Integer, Integer>();
           for (Dependency<Label, Label, Object> dep : depsSet) {
             CoreLabel child = (CoreLabel)dep.dependent();
             CoreLabel parent = (CoreLabel)dep.governor();
@@ -647,27 +626,27 @@ public class TreePrint {
       if (formats.containsKey("typedDependencies")) {
         GrammaticalStructure gs = gsf.newGrammaticalStructure(outputTree);
         if (basicDependencies) {
-          print(gs.typedDependencies(), includeTags, pw);
+          print(gs.typedDependencies(), pw);
         }
         if (nonCollapsedDependencies) {
-          print(gs.allTypedDependencies(), includeTags, pw);
+          print(gs.allTypedDependencies(), pw);
         }
         if (nonCollapsedDependenciesSeparated) {
-          print(gs.allTypedDependencies(), "separator", includeTags, pw);
+          print(gs.allTypedDependencies(), "separator", pw);
         }
         if (collapsedDependencies) {
-          print(gs.typedDependenciesCollapsed(GrammaticalStructure.Extras.MAXIMAL), includeTags, pw);
+          print(gs.typedDependenciesCollapsed(true), pw);
         }
         if (CCPropagatedDependencies) {
-          print(gs.typedDependenciesCCprocessed(), includeTags, pw);
+          print(gs.typedDependenciesCCprocessed(), pw);
         }
         if (treeDependencies) {
-          print(gs.typedDependenciesCollapsedTree(), includeTags, pw);
+          print(gs.typedDependenciesCollapsedTree(), pw);
         }
       }
       if (formats.containsKey("typedDependenciesCollapsed")) {
         GrammaticalStructure gs = gsf.newGrammaticalStructure(outputTree);
-        print(gs.typedDependenciesCCprocessed(), includeTags, pw);
+        print(gs.typedDependenciesCCprocessed(), pw);
       }
       // This makes parser require jgrapht.  Bad
       // if (formats.containsKey("semanticGraph")) {
@@ -680,24 +659,6 @@ public class TreePrint {
     pw.flush();
   }
 
-  private List<Dependency<Label, Label, Object>> getSortedDeps(Tree tree, Predicate<Dependency<Label, Label, Object>> filter) {
-    if (gsf != null) {
-      GrammaticalStructure gs = gsf.newGrammaticalStructure(tree);
-      Collection<TypedDependency> deps = gs.typedDependencies(GrammaticalStructure.Extras.NONE);
-      List<Dependency<Label, Label, Object>> sortedDeps = new ArrayList<Dependency<Label, Label, Object>>();
-      for (TypedDependency dep : deps) {
-        sortedDeps.add(new NamedDependency(dep.gov(), dep.dep(), dep.reln().toString()));
-      }
-      Collections.sort(sortedDeps, Dependencies.dependencyIndexComparator());
-      return sortedDeps;
-    } else {
-      Set<Dependency<Label, Label, Object>> depsSet = tree.mapDependencies(filter, hf, "root");
-      List<Dependency<Label, Label, Object>> sortedDeps = new ArrayList<Dependency<Label, Label, Object>>(depsSet);
-      Collections.sort(sortedDeps, Dependencies.dependencyIndexComparator());
-      return sortedDeps;
-    }
-  }
-
 
   /** For the input tree, collapse any collocations in it that exist in
    *  WordNet and are contiguous in the tree into a single node.
@@ -801,7 +762,7 @@ public class TreePrint {
     String options = "";
     String tlpName = "edu.stanford.nlp.trees.PennTreebankLanguagePack";
     String hfName = null;
-    Map<String,Integer> flagMap = Generics.newHashMap();
+    Map<String,Integer> flagMap = new HashMap<String,Integer>();
     flagMap.put("-format", 1);
     flagMap.put("-options", 1);
     flagMap.put("-tLP", 1);
@@ -846,7 +807,11 @@ public class TreePrint {
       if (argsMap.keySet().contains("-useTLPTreeReader")) {
         trf = tlp.treeReaderFactory();
       } else {
-        trf = in -> new PennTreeReader(in, new LabeledScoredTreeFactory(new StringLabelFactory()), new TreeNormalizer());
+        trf = new TreeReaderFactory() {
+          public TreeReader newTreeReader(Reader in) {
+            return new PennTreeReader(in, new LabeledScoredTreeFactory(new StringLabelFactory()), new TreeNormalizer());
+          }
+        };
       }
       trees = new DiskTreebank(trf);
       trees.loadPath(args[0]);
@@ -904,15 +869,15 @@ public class TreePrint {
    * @return a <code>String</code> representation of the typed
    *         dependencies in this <code>GrammaticalStructure</code>
    */
-  private static String toString(Collection<TypedDependency> dependencies, String format, boolean includeTags) {
+  private static String toString(Collection<TypedDependency> dependencies, String format) {
     if (format != null && format.equals("xml")) {
-      return toXMLString(dependencies, includeTags);
+      return toXMLString(dependencies);
     } else if (format != null && format.equals("readable")) {
       return toReadableString(dependencies);
     } else if (format != null && format.equals("separator")) {
-      return toString(dependencies, true, includeTags);
+        return toString(dependencies, true);
     } else {
-      return toString(dependencies, false, includeTags);
+      return toString(dependencies, false);
     }
   }
 
@@ -930,28 +895,28 @@ public class TreePrint {
    * @return a <code>String</code> representation of this set of
    *         typed dependencies
    */
-  private static String toString(Collection<TypedDependency> dependencies, boolean extraSep, boolean includeTags) {
-    CoreLabel.OutputFormat labelFormat = (includeTags) ? CoreLabel.OutputFormat.VALUE_TAG_INDEX : CoreLabel.OutputFormat.VALUE_INDEX;
+  private static String toString(Collection<TypedDependency> dependencies, boolean extraSep) {
     StringBuilder buf = new StringBuilder();
     if (extraSep) {
       List<TypedDependency> extraDeps =  new ArrayList<TypedDependency>();
       for (TypedDependency td : dependencies) {
         if (td.extra()) {
           extraDeps.add(td);
-        } else {
-          buf.append(td.toString(labelFormat)).append('\n');
+        }
+        else {
+          buf.append(td.toString()).append('\n');
         }
       }
       // now we print the separator for extra dependencies, and print these if there are some
       if (!extraDeps.isEmpty()) {
         buf.append("======\n");
         for (TypedDependency td : extraDeps) {
-          buf.append(td.toString(labelFormat)).append('\n');
+          buf.append(td.toString()).append('\n');
         }
       }
     } else {
       for (TypedDependency td : dependencies) {
-        buf.append(td.toString(labelFormat)).append('\n');
+        buf.append(td.toString()).append('\n');
       }
     }
     return buf.toString();
@@ -969,39 +934,35 @@ public class TreePrint {
   }
 
   // NO OUTSIDE USE
-  private static String toXMLString(Collection<TypedDependency> dependencies, boolean includeTags) {
+  private static String toXMLString(Collection<TypedDependency> dependencies) {
     StringBuilder buf = new StringBuilder("<dependencies style=\"typed\">\n");
     for (TypedDependency td : dependencies) {
       String reln = td.reln().toString();
       String gov = td.gov().value();
-      String govTag = td.gov().tag();
       int govIdx = td.gov().index();
       String dep = td.dep().value();
-      String depTag = td.dep().tag();
       int depIdx = td.dep().index();
       boolean extra = td.extra();
       // add an attribute if the node is a copy
       // (this happens in collapsing when different prepositions are conjuncts)
       String govCopy = "";
-      int copyGov = td.gov().copyCount();
-      if (copyGov > 0) {
+      Integer copyGov = td.gov().label.get(CoreAnnotations.CopyAnnotation.class);
+      if (copyGov != null) {
         govCopy = " copy=\"" + copyGov + '\"';
       }
       String depCopy = "";
-      int copyDep = td.dep().copyCount();
-      if (copyDep > 0) {
+      Integer copyDep = td.dep().label.get(CoreAnnotations.CopyAnnotation.class);
+      if (copyDep != null) {
         depCopy = " copy=\"" + copyDep + '\"';
       }
-      String govTagAttribute = (includeTags && govTag != null) ? " tag=\"" + govTag + "\"" : "";
-      String depTagAttribute = (includeTags && depTag != null) ? " tag=\"" + depTag + "\"" : "";
       // add an attribute if the typed dependency is an extra relation (do not preserve the tree structure)
       String extraAttr = "";
       if (extra) {
         extraAttr = " extra=\"yes\"";
       }
       buf.append("  <dep type=\"").append(XMLUtils.escapeXML(reln)).append('\"').append(extraAttr).append(">\n");
-      buf.append("    <governor idx=\"").append(govIdx).append('\"').append(govCopy).append(govTagAttribute).append('>').append(XMLUtils.escapeXML(gov)).append("</governor>\n");
-      buf.append("    <dependent idx=\"").append(depIdx).append('\"').append(depCopy).append(depTagAttribute).append('>').append(XMLUtils.escapeXML(dep)).append("</dependent>\n");
+      buf.append("    <governor idx=\"").append(govIdx).append('\"').append(govCopy).append('>').append(XMLUtils.escapeXML(gov)).append("</governor>\n");
+      buf.append("    <dependent idx=\"").append(depIdx).append('\"').append(depCopy).append('>').append(XMLUtils.escapeXML(dep)).append("</dependent>\n");
       buf.append("  </dep>\n");
     }
     buf.append("</dependencies>");
@@ -1015,8 +976,8 @@ public class TreePrint {
    * @param dependencies The collection of TypedDependency to print
    * @param pw Where to print them
    */
-  public static void print(Collection<TypedDependency> dependencies, boolean includeTags, PrintWriter pw) {
-    pw.println(toString(dependencies, false, includeTags));
+  public static void print(Collection<TypedDependency> dependencies, PrintWriter pw) {
+    pw.println(toString(dependencies, false));
   }
 
   /**
@@ -1027,8 +988,8 @@ public class TreePrint {
    * @param format "xml" or "readable" or other
    * @param pw Where to print them
    */
-  public static void print(Collection<TypedDependency> dependencies, String format, boolean includeTags, PrintWriter pw) {
-    pw.println(toString(dependencies, format, includeTags));
+  public static void print(Collection<TypedDependency> dependencies, String format, PrintWriter pw) {
+    pw.println(toString(dependencies, format));
   }
 
 }
Project stanfordnlp/CoreNLP in file ...d3eab26760aa/src.edu.stanford.nlp.trees.Trees.java (2015-03-14)
@@ -1,11 +1,11 @@
 package edu.stanford.nlp.trees;
 
 import edu.stanford.nlp.io.IOUtils;
-import java.util.function.Function;
-import edu.stanford.nlp.util.Generics;
+import edu.stanford.nlp.util.Function;
 import edu.stanford.nlp.util.MutableInteger;
 import edu.stanford.nlp.util.StringUtils;
 import edu.stanford.nlp.ling.*;
+import edu.stanford.nlp.ling.CoreAnnotations;
 
 import java.util.*;
 import java.io.*;
@@ -180,47 +180,6 @@ public class Trees {
     }
   }
 
-  /**
-   * Given a tree, set the tags on the leaf nodes if they are not
-   * already set.  Do this by using the preterminal's value as a tag.
-   */
-  public static void setLeafTagsIfUnset(Tree tree) {
-    if (tree.isPreTerminal()) {
-      Tree leaf = tree.children()[0];
-      if (!(leaf.label() instanceof HasTag)) {
-        return;
-      }
-      HasTag label = (HasTag) leaf.label();
-      if (label.tag() == null) {
-        label.setTag(tree.value());
-      }
-    } else {
-      for (Tree child : tree.children()) {
-        setLeafTagsIfUnset(child);
-      }
-    }
-  }
-
-  /**
-   * Replace the labels of the leaves with the given leaves.
-   */
-  public static void setLeafLabels(Tree tree, List<Label> labels) {
-    Iterator<Tree> leafIterator = tree.getLeaves().iterator();
-    Iterator<Label> labelIterator = labels.iterator();
-    while (leafIterator.hasNext() && labelIterator.hasNext()) {
-      Tree leaf = leafIterator.next();
-      Label label = labelIterator.next();
-      leaf.setLabel(label);
-      //leafIterator.next().setLabel(labelIterator.next());
-    }
-    if (leafIterator.hasNext()) {
-      throw new IllegalArgumentException("Tree had more leaves than the labels provided");
-    }
-    if (labelIterator.hasNext()) {
-      throw new IllegalArgumentException("More labels provided than tree had leaves");
-    }
-  }
-
 
   /**
    * returns the maximal projection of <code>head</code> in
@@ -352,29 +311,23 @@ public class Trees {
     return -1;
   }
 
-  /** Returns a String reporting what kinds of Tree and Label nodes this
-   *  Tree contains.
-   *
+  /** Return information about the objects in this Tree.
    *  @param t The tree to examine.
-   *  @return A human-readable String reporting what kinds of Tree and Label nodes this
-   *      Tree contains.
+   *  @return A human-readable String
    */
-  public static String toStructureDebugString(Tree t) {
+  public static String toDebugStructureString(Tree t) {
+    StringBuilder sb = new StringBuilder();
     String tCl = StringUtils.getShortClassName(t);
     String tfCl = StringUtils.getShortClassName(t.treeFactory());
     String lCl = StringUtils.getShortClassName(t.label());
     String lfCl = StringUtils.getShortClassName(t.label().labelFactory());
-    Set<String> otherClasses = Generics.newHashSet();
-    String leafLabels = null;
-    String tagLabels = null;
-    String phraseLabels = null;
-    String leaves = null;
-    String nodes = null;
+    Set<String> otherClasses = new HashSet<String>();
     for (Tree st : t) {
       String stCl = StringUtils.getShortClassName(st);
       String stfCl = StringUtils.getShortClassName(st.treeFactory());
       String slCl = StringUtils.getShortClassName(st.label());
       String slfCl = StringUtils.getShortClassName(st.label().labelFactory());
+
       if ( ! tCl.equals(stCl)) {
         otherClasses.add(stCl);
       }
@@ -387,60 +340,18 @@ public class Trees {
       if ( ! lfCl.equals(slfCl)) {
         otherClasses.add(slfCl);
       }
-      if (st.isPhrasal()) {
-        if (nodes == null) {
-          nodes = stCl;
-        } else if ( ! nodes.equals(stCl)) {
-          nodes = "mixed";
-        }
-        if (phraseLabels == null) {
-          phraseLabels = slCl;
-        } else if ( ! phraseLabels.equals(slCl)) {
-          phraseLabels = "mixed";
-        }
-      } else if (st.isPreTerminal()) {
-        if (nodes == null) {
-          nodes = stCl;
-        } else if ( ! nodes.equals(stCl)) {
-          nodes = "mixed";
-        }
-        if (tagLabels == null) {
-          tagLabels = StringUtils.getShortClassName(slCl);
-        } else if ( ! tagLabels.equals(slCl)) {
-          tagLabels = "mixed";
-        }
-      } else if (st.isLeaf()) {
-        if (leaves == null) {
-          leaves = stCl;
-        } else if ( ! leaves.equals(stCl)) {
-          leaves = "mixed";
-        }
-        if (leafLabels == null) {
-          leafLabels = slCl;
-        } else if ( ! leafLabels.equals(slCl)) {
-          leafLabels = "mixed";
-        }
-      } else {
-        throw new IllegalStateException("Bad tree state: " + t);
-      }
-    } // end for Tree st : this
-    StringBuilder sb = new StringBuilder();
+    }
     sb.append("Tree with root of class ").append(tCl).append(" and factory ").append(tfCl);
-    sb.append(" and root label class ").append(lCl).append(" and factory ").append(lfCl);
+    sb.append(" with label class ").append(lCl).append(" and factory ").append(lfCl);
     if ( ! otherClasses.isEmpty()) {
-      sb.append(" and the following classes also found within the tree: ").append(otherClasses);
-      return " with " + nodes + " interior nodes and " + leaves +
-        " leaves, and " + phraseLabels + " phrase labels, " +
-        tagLabels + " tag labels, and " + leafLabels + " leaf labels.";
-    } else {
-      sb.append(" (and uniform use of these Tree and Label classes throughout the tree).");
+      sb.append(" with the following classes also found within the tree: ").append(otherClasses);
     }
     return sb.toString();
   }
 
 
   /** Turns a sentence into a flat phrasal tree.
-   *  The structure is S -&gt; tag*.  And then each tag goes to a word.
+   *  The structure is S -> tag*.  And then each tag goes to a word.
    *  The tag is either found from the label or made "WD".
    *  The tag and phrasal node have a StringLabel.
    *
@@ -452,7 +363,7 @@ public class Trees {
   }
 
   /** Turns a sentence into a flat phrasal tree.
-   *  The structure is S -&gt; tag*.  And then each tag goes to a word.
+   *  The structure is S -> tag*.  And then each tag goes to a word.
    *  The tag is either found from the label or made "WD".
    *  The tag and phrasal node have a StringLabel.
    *
@@ -761,7 +672,6 @@ public class Trees {
     return commonAncestor;
   }
 
-  // todo [cdm 2015]: These next two methods duplicate the Tree.valueOf methods!
   /**
    * Simple tree reading utility method.  Given a tree formatted as a PTB string, returns a Tree made by a specific TreeFactory.
    */
@@ -798,36 +708,4 @@ public class Trees {
     }
   }
 
-  /**
-   * Converts the tree labels to CoreLabels.
-   * We need this because we store additional info in the CoreLabel, like token span.
-   * @param tree
-   */
-  public static void convertToCoreLabels(Tree tree) {
-    Label l = tree.label();
-    if (!(l instanceof CoreLabel)) {
-      CoreLabel cl = new CoreLabel();
-      cl.setValue(l.value());
-      tree.setLabel(cl);
-    }
-
-    for (Tree kid : tree.children()) {
-      convertToCoreLabels(kid);
-    }
-  }
-
-
-  /**
-   * Set the sentence index of all the leaves in the tree
-   * (only works on CoreLabel)
-   */
-  public static void setSentIndex(Tree tree, int sentIndex) {
-    List<Label> leaves = tree.yield();
-    for (Label leaf : leaves) {
-      if (!(leaf instanceof CoreLabel)) {
-        throw new IllegalArgumentException("Only works on CoreLabel");
-      }
-      ((CoreLabel) leaf).setSentIndex(sentIndex);
-    }
-  }
 }