public void writeTree(Tree test) { testWriter.println((test == null) ? "(())" : test.toString()); count++; // log.info("Wrote EVALB lines."); }
private String treeToKey(Tree t) { int idx = getHeadIndex(t); String key = Integer.toString(idx) + ':' + t.toString(); return key; }
private static void writeTrees(String filename, List<Tree> trees, List<Integer> treeIds) { try { FileOutputStream fos = new FileOutputStream(filename); BufferedWriter bout = new BufferedWriter(new OutputStreamWriter(fos)); for (Integer id : treeIds) { bout.write(trees.get(id).toString()); bout.write("\n"); } bout.flush(); fos.close(); } catch (IOException e) { throw new RuntimeIOException(e); } }
public static void writeTrees(List<Tree> trees, String outputFile) { logger.info("Writing new trees to " + outputFile); try { BufferedWriter out = new BufferedWriter(new FileWriter(outputFile)); for (Tree tree : trees) { out.write(tree.toString()); out.write("\n"); } out.close(); } catch (IOException e) { throw new RuntimeIOException(e); } }
public void evaluate(ParserQuery query, Tree gold, PrintWriter pw) { if (!(query instanceof ShiftReduceParserQuery)) { throw new IllegalArgumentException("This evaluator only works for the ShiftReduceParser"); } ShiftReduceParserQuery srquery = (ShiftReduceParserQuery) query; try { switch (mode) { case BINARIZED: out.write(srquery.getBestBinarizedParse().toString()); break; case DEBINARIZED: out.write(srquery.debinarized.toString()); break; default: throw new IllegalArgumentException("Unknown mode " + mode); } out.newLine(); } catch (IOException e) { throw new RuntimeIOException(e); } }
private static String toString(Tree tree, boolean plainPrint) { if (!plainPrint) return tree.toString(); StringBuilder sb = new StringBuilder(); List<Tree> leaves = tree.getLeaves(); for (Tree leaf : leaves) { sb.append(leaf.label().value()).append(' '); } return sb.toString(); }
public byte[] convertToBytes(List<Tree> input) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); GZIPOutputStream gos = new GZIPOutputStream(bos); ObjectOutputStream oos = new ObjectOutputStream(gos); List<Tree> transformed = CollectionUtils.transformAsList(input, treeBasicCategories); List<Tree> filtered = CollectionUtils.filterAsList(transformed, treeFilter); oos.writeObject(filtered.size()); for (Tree tree : filtered) { oos.writeObject(tree.toString()); } oos.close(); gos.close(); bos.close(); return bos.toByteArray(); } catch (IOException e) { throw new RuntimeIOException(e); } }
/** * Returns the result of applying the parser to arg as a string. */ public void handleParse(String arg, OutputStream outStream, boolean binarized) throws IOException { Tree tree = parse(arg, binarized); if (tree == null) { return; } log.info(tree); if (tree != null) { OutputStreamWriter osw = new OutputStreamWriter(outStream, "utf-8"); osw.write(tree.toString()); osw.write("\n"); osw.flush(); } }
private static void resolveDummyTags(File treeFile, TwoDimensionalCounter<String, String> pretermLabel, TwoDimensionalCounter<String, String> unigramTagger) { try { BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(treeFile), "UTF-8")); TreeReaderFactory trf = new FrenchTreeReaderFactory(); TreeReader tr = trf.newTreeReader(br); PrintWriter pw = new PrintWriter(new PrintStream(new FileOutputStream(new File(treeFile + ".fixed")),false,"UTF-8")); int nTrees = 0; for(Tree t; (t = tr.readTree()) != null;nTrees++) { traverseAndFix(t, pretermLabel, unigramTagger); pw.println(t.toString()); } pw.close(); tr.close(); System.out.println("Processed " +nTrees+ " trees"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
/** * @param args */ public static void main(String[] args) { if(args.length != 1) { System.err.printf("Usage: java %s tree_file > trees%n", HebrewTreeReaderFactory.class.getName()); System.exit(-1); } TreebankLanguagePack tlp = new HebrewTreebankLanguagePack(); File treeFile = new File(args[0]); try { TreeReaderFactory trf = new HebrewTreeReaderFactory(); BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(treeFile), tlp.getEncoding())); TreeReader tr = trf.newTreeReader(br); int numTrees = 0; for(Tree t; ((t = tr.readTree()) != null); numTrees++) System.out.println(t.toString()); tr.close(); System.err.printf("Processed %d trees.%n",numTrees); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
public String apply(TregexMatcher m) { if(mwCounter == null) throw new RuntimeException("Cannot enable POSSequence features without POS sequence map. Use option -frenchMWMap."); Tree t = m.getMatch(); StringBuilder sb = new StringBuilder(); for(Tree kid : t.children()) { if( ! kid.isPreTerminal()) throw new RuntimeException("Not POS sequence for tree: " + t.toString()); String tag = doBasicCat ? tlp.basicCategory(kid.value()) : kid.value(); sb.append(tag).append(" "); } if(mwCounter.getCount(t.value(), sb.toString().trim()) > cutoff) return annotationMark + sb.toString().replaceAll("\\s+", "").toLowerCase(); else return ""; }
for(Tree t; (t = tr.readTree()) != null;nTrees++) { Tree fixedT = tt.transformTree(t); System.out.println(fixedT.toString());
public AuxiliaryTree(Tree tree, boolean mustHaveFoot) { originalTreeString = tree.toString(); this.tree = tree; this.foot = findFootNode(tree); if (foot == null && mustHaveFoot) { throw new TsurgeonParseException("Error -- no foot node found for " + originalTreeString); } namesToNodes = Generics.newHashMap(); nodesToNames = new IdentityHashMap<>(); initializeNamesNodesMaps(tree); }
public TreeFromFile(Tree t) { this.treeString = t.toString(); sentence = SentenceUtils.listToString(t.yield()); if(t.label() instanceof HasIndex) { sentId = ((CoreLabel)t.label()).sentIndex(); filename = ((CoreLabel)t.label()).docID(); if(sentId != -1 && filename != null && !filename.equals("")) sentence = String.format("%s-%d %s", filename,sentId,sentence); } }
/** * * @param args */ public static void main(String[] args) { if(args.length != 1) { System.exit(-1); } ArabicTreebankParserParams tlpp = new ArabicTreebankParserParams(); String[] options = {"-arabicFactored"}; tlpp.setOptionFlag(options, 0); DiskTreebank tb = tlpp.diskTreebank(); tb.loadPath(args[0], "txt", false); for(Tree t : tb) { for(Tree subtree : t) { tlpp.transformTree(subtree, t); } System.out.println(t.toString()); } } }
private static Tree findFootNodeHelper(Tree t) { Tree foundDtr = null; if (t.isLeaf()) { Matcher m = footNodeLabelPattern.matcher(t.label().value()); if (m.matches()) { t.label().setValue(m.group(1)); return t; } else { return null; } } for (Tree child : t.children()) { Tree thisFoundDtr = findFootNodeHelper(child); if (thisFoundDtr != null) { if (foundDtr != null) { throw new TsurgeonParseException("Error -- two foot nodes in subtree" + t.toString()); } else { foundDtr = thisFoundDtr; } } } Matcher m = escapedFootNodeCharacter.matcher(t.label().value()); t.label().setValue(m.replaceAll(footNodeCharacter)); return foundDtr; }
private Pair<Tree,Tree> copyHelper(Tree node, Map<String,Tree> newNamesToNodes, TreeFactory treeFactory, LabelFactory labelFactory) { Tree clone; Tree newFoot = null; if (node.isLeaf()) { if (node == foot) { // found the foot node; pass it up. clone = treeFactory.newTreeNode(node.label(), new ArrayList<>(0)); newFoot = clone; } else { clone = treeFactory.newLeaf(labelFactory.newLabel(node.label())); } } else { List<Tree> newChildren = new ArrayList<>(node.children().length); for (Tree child : node.children()) { Pair<Tree,Tree> newChild = copyHelper(child, newNamesToNodes, treeFactory, labelFactory); newChildren.add(newChild.first()); if (newChild.second() != null) { if (newFoot != null) { log.info("Error -- two feet found when copying auxiliary tree " + tree.toString() + "; using last foot found."); } newFoot = newChild.second(); } } clone = treeFactory.newTreeNode(labelFactory.newLabel(node.label()),newChildren); } if (nodesToNames.containsKey(node)) newNamesToNodes.put(nodesToNames.get(node),clone); return new Pair<>(clone, newFoot); }
@Override public Tree transformTree(Tree t, Tree root) { String baseCat = t.value(); StringBuilder newCategory = new StringBuilder(); //Add manual state splits for (Pair<TregexPattern,Function<TregexMatcher,String>> e : activeAnnotations) { TregexMatcher m = e.first().matcher(root); if (m.matchesAt(t)) newCategory.append(e.second().apply(m)); } // WSGDEBUG //Add morphosyntactic features if this is a POS tag if(t.isPreTerminal() && tagSpec != null) { if( !(t.firstChild().label() instanceof CoreLabel) || ((CoreLabel) t.firstChild().label()).originalText() == null ) throw new RuntimeException(String.format("%s: Term lacks morpho analysis: %s",this.getClass().getName(),t.toString())); String morphoStr = ((CoreLabel) t.firstChild().label()).originalText(); MorphoFeatures feats = tagSpec.strToFeatures(morphoStr); baseCat = feats.getTag(baseCat); } //Update the label(s) String newCat = baseCat + newCategory.toString(); t.setValue(newCat); if (t.isPreTerminal() && t.label() instanceof HasTag) ((HasTag) t.label()).setTag(newCat); return t; }
@Override public Tree transformTree(Tree t, Tree root) { // Perform tregex-powered annotations t = super.transformTree(t, root); String cat = t.value(); //Add morphosyntactic features if this is a POS tag if(t.isPreTerminal() && tagSpec != null) { if( !(t.firstChild().label() instanceof CoreLabel) || ((CoreLabel) t.firstChild().label()).originalText() == null ) throw new RuntimeException(String.format("%s: Term lacks morpho analysis: %s",this.getClass().getName(),t.toString())); String morphoStr = ((CoreLabel) t.firstChild().label()).originalText(); Pair<String,String> lemmaMorph = MorphoFeatureSpecification.splitMorphString("", morphoStr); MorphoFeatures feats = tagSpec.strToFeatures(lemmaMorph.second()); cat = feats.getTag(cat); } //Update the label(s) t.setValue(cat); if (t.isPreTerminal() && t.label() instanceof HasTag) ((HasTag) t.label()).setTag(cat); return t; }