List of usage examples for edu.stanford.nlp.trees TreePrint printTree
public void printTree(final Tree t, PrintWriter pw)
From source file:Anaphora_Resolution.ParseAllXMLDocuments.java
public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException, TransformerException { // File dataFolder = new File("DataToPort"); // File[] documents; String grammar = "grammar/englishPCFG.ser.gz"; String[] options = { "-maxLength", "100", "-retainTmpSubcategories" }; //LexicalizedParser lp = new LexicalizedParser(grammar, options); LexicalizedParser lp = LexicalizedParser.loadModel("edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz"); ////from w w w .java 2 s. c om // if (dataFolder.isDirectory()) { // documents = dataFolder.listFiles(); // } else { // documents = new File[] {dataFolder}; // } // int currfile = 0; // int totfiles = documents.length; // for (File paper : documents) { // currfile++; // if (paper.getName().equals(".DS_Store")||paper.getName().equals(".xml")) { // currfile--; // totfiles--; // continue; // } // System.out.println("Working on "+paper.getName()+" (file "+currfile+" out of "+totfiles+")."); // // DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); // This is for XML // DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); // Document doc = docBuilder.parse(paper.getAbsolutePath()); // // NodeList textlist = doc.getElementsByTagName("text"); // for(int i=0; i < textlist.getLength(); i++) { // Node currentnode = textlist.item(i); // String wholetext = textlist.item(i).getTextContent(); String wholetext = "How about other changes for example the ways of doing the work and \n" + "\n" + "Iwould say security has , there 's more pressure put on people now than there used to be because obviously , especially after Locherbie , they tightened up on security and there 's a lot more pressure now especially from the ETR and stuff like that \n" + "People do n't feel valued any more , they feel I do n't know I think they feel that nobody cares about them really anyway \n"; //System.out.println(wholetext); //Iterable<List<? extends HasWord>> sentences; ArrayList<Tree> parseTrees = new ArrayList<Tree>(); String asd = ""; int j = 0; StringReader stringreader = new StringReader(wholetext); DocumentPreprocessor dp = new DocumentPreprocessor(stringreader); @SuppressWarnings("rawtypes") ArrayList<List> sentences = preprocess(dp); for (List sentence : sentences) { parseTrees.add(lp.apply(sentence)); // Parsing a new sentence and adding it to the parsed tree ArrayList<Tree> PronounsList = findPronouns(parseTrees.get(j)); // Locating all pronouns to resolve in the sentence Tree corefedTree; for (Tree pronounTree : PronounsList) { parseTrees.set(parseTrees.size() - 1, HobbsResolve(pronounTree, parseTrees)); // Resolving the coref and modifying the tree for each pronoun } StringWriter strwr = new StringWriter(); PrintWriter prwr = new PrintWriter(strwr); TreePrint tp = new TreePrint("penn"); tp.printTree(parseTrees.get(j), prwr); prwr.flush(); asd += strwr.toString(); j++; } String armando = ""; for (Tree sentence : parseTrees) { for (Tree leaf : Trees.leaves(sentence)) armando += leaf + " "; } System.out.println(wholetext); System.out.println(); System.out.println("......"); System.out.println(armando); System.out.println("All done."); // currentnode.setTextContent(asd); // } // TransformerFactory transformerFactory = TransformerFactory.newInstance(); // Transformer transformer = transformerFactory.newTransformer(); // DOMSource source = new DOMSource(doc); // StreamResult result = new StreamResult(paper); // transformer.transform(source, result); // // System.out.println("Done"); // } }
From source file:cc.vidr.parseviz.ParseViz.java
License:Open Source License
public static void printTree(Tree tree, PrintWriter out, String format) { TreePrint treePrint = new TreePrint(format, "", tlp, options.tlpParams.headFinder()); treePrint.printTree(tree, out); }
From source file:DependencyParser.Parser.java
public void CallParser(String text) // start of the main method { try {//from w ww . j a v a 2s . co m TreebankLanguagePack tlp = new PennTreebankLanguagePack(); GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory(); LexicalizedParser lp = LexicalizedParser .loadModel("edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz"); lp.setOptionFlags(new String[] { "-maxLength", "500", "-retainTmpSubcategories" }); TokenizerFactory<CoreLabel> tokenizerFactory = PTBTokenizer.factory(new CoreLabelTokenFactory(), ""); List<CoreLabel> wordList = tokenizerFactory.getTokenizer(new StringReader(text)).tokenize(); Tree tree = lp.apply(wordList); GrammaticalStructure gs = gsf.newGrammaticalStructure(tree); Collection<TypedDependency> tdl = gs.typedDependenciesCCprocessed(true); System.out.println(tdl); PrintWriter pw = new PrintWriter("H:\\Thesis Development\\Thesis\\NLP\\src\\nlp\\Text-Parsed.txt"); TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed"); tp.printTree(tree, pw); pw.close(); Main.writeImage(tree, tdl, "H:\\Thesis Development\\Thesis\\NLP\\src\\nlp\\image.png", 3); assert (new File("image.png").exists()); } catch (FileNotFoundException f) { } catch (Exception ex) { Logger.getLogger(Parser.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:edu.cmu.cs.in.hoop.visualizers.HoopParseTreeViewer.java
License:Open Source License
/** * /* ww w. ja v a2 s. co m*/ */ private void processInput(String aSentence) { debug("processInput (" + aSentence + ")"); Tree thisTree = theLexicalizedParser.apply(aSentence); debug("We have a tree!"); PrintWriter pw = new PrintWriter(System.out, true); TreePrint posPrinter = new TreePrint("wordsAndTags"); posPrinter.printTree(thisTree, pw); ArrayList ar = thisTree.taggedYield(); debug(ar.toString()); for (Tree subtree : thisTree) { if (thisTree.isLeaf() == true) { debug("Tree leaf: " + subtree.label().value()); } else { debug("Tree node: " + subtree.label().value()); } } treePanel.setTree(thisTree); }
From source file:reck.parser.lexparser.RECKLexicalizedParser.java
License:Open Source License
/** * Parse a sentence represented as a List of tokens. * The text must already have been tokenized and * normalized into tokens that are appropriate to the treebank * which was used to train the parser. The tokens can be of * multiple types, and the list items need not be homogeneous as to type * (in particular, only some words might be given tags): * <ul>/* w w w . java 2s .co m*/ * <li>If a token implements HasWord, then the word to be parsed is * given by its word() value.</li> * <li>If a token implements HasTag and the tag() value is not * null or the empty String, then the parser is strongly advised to assign * a part of speech tag that <i>begins</i> with this String.</li> * <li>Otherwise toString() is called on the token, and the returned * value is used as the word to be parsed. In particular, if the * token is already a String, this means that the String is used as * the word to be parsed.</li> * </ul> * * @param sentence The sentence to parse * @return true Iff the sentence was accepted by the grammar * @throws UnsupportedOperationException If the Sentence is too long or * of zero length or the parse * otherwise fails for resource reasons */ @Override public boolean parse(List<? extends HasWord> sentence) { int length = sentence.size(); if (length == 0) { throw new UnsupportedOperationException("Can't parse a zero-length sentence!"); } List<HasWord> sentenceB = new ArrayList<HasWord>(sentence); if (Test.addMissingFinalPunctuation) { addSentenceFinalPunctIfNeeded(sentenceB, length); } if (length > Test.maxLength) { throw new UnsupportedOperationException("Sentence too long: length " + length); } TreePrint treePrint = getTreePrint(); PrintWriter pwOut = op.tlpParams.pw(); parseSucceeded = false; sentenceB.add(new Word(Lexicon.BOUNDARY)); if (op.doPCFG) { if (!pparser.parse(sentenceB)) { return parseSucceeded; } if (Test.verbose) { System.out.println("PParser output"); // pwOut.println(debinarizer.transformTree(pparser.getBestParse())); // with scores on nodes treePrint.printTree(debinarizer.transformTree(pparser.getBestParse()), pwOut); } } if (op.doDep && !Test.useFastFactored) { if (!dparser.parse(sentenceB)) { return parseSucceeded; } // cdm nov 2006: should move these printing bits to the main printing section, // so don't calculate the best parse twice! if (Test.verbose) { System.out.println("DParser output"); treePrint.printTree(dparser.getBestParse(), pwOut); } } if (op.doPCFG && op.doDep) { if (!bparser.parse(sentenceB)) { return parseSucceeded; } else { parseSucceeded = true; } } return true; }
From source file:sg.edu.nus.comp.pdtb.util.Corpus.java
License:Open Source License
public static File[][] prepareParseAndDependecyTrees(File[] inputFiles, String outPath, String ptreeExtension, String dtreeExtension) throws FileNotFoundException { log.info("Generating parse and dependecy trees with Stanford parser..."); File[][] trees = new File[inputFiles.length][2]; int index = 0; LexicalizedParser lp = LexicalizedParser.loadModel(Settings.STANFORD_MODEL); for (File inputFile : inputFiles) { log.info("Generating tree for: " + inputFile.getName()); String outDir = outPath + inputFile.getName(); File parseTree = new File(outDir + ptreeExtension); File dependTree = new File(outDir + dtreeExtension); PrintWriter parse = new PrintWriter(parseTree); TreePrint tp = new TreePrint("penn"); PrintWriter depend = new PrintWriter(dependTree); TreePrint td = new TreePrint("typedDependencies"); DocumentPreprocessor sentence = new DocumentPreprocessor(inputFile.toString()); int lineNumber = 0; for (List<HasWord> sent : sentence) { log.info("Parsing line:" + lineNumber); log.trace("Parsing: " + sent); Tree tree = lp.apply(sent); tp.printTree(tree, parse); parse.flush();//from ww w .ja v a2 s.com td.printTree(tree, depend); depend.flush(); ++lineNumber; } parse.close(); depend.close(); log.info("Tree generation for " + inputFile.getName() + " is done. Trees are in " + outDir); trees[index] = new File[] { parseTree, dependTree }; index++; } return trees; }
From source file:tml.utils.StanfordUtils.java
License:Apache License
public static String getPennString(Tree tree) { String pennTreeString = ""; double time = System.nanoTime(); TreePrint print = new TreePrint("penn"); StringWriter stw = new StringWriter(); print.printTree(tree, new PrintWriter(stw)); pennTreeString = stw.toString();/*from w ww. j av a2 s . c o m*/ time = (System.nanoTime() - time) * 10E-9; logger.debug("Sentence parsed in " + time + " seconds"); return pennTreeString; }