Package edu.stanford.nlp.trees

Examples of edu.stanford.nlp.trees.Tree


  private Tree doOneSentence(List<ParserConstraint> constraints,
                             List<CoreLabel> words) {
    ParserQuery pq = parser.parserQuery();
    pq.setConstraints(constraints);
    pq.parse(words);
    Tree tree = null;
    try {
      tree = pq.getBestParse();
      if (tree == null) {
        System.err.println("WARNING: Parsing of sentence failed.  " +
                         "Will ignore and continue: " +
                         Sentence.listToString(words));
      } else {
        // -10000 denotes unknown words
        tree.setScore(pq.getPCFGScore() % -10000.0);
      }
    } catch (OutOfMemoryError e) {
      System.err.println("WARNING: Parsing of sentence ran out of memory.  " +
                         "Will ignore and continue: " +
                         Sentence.listToString(words));
View Full Code Here


      throw new RuntimeException("unable to find sentences in: " + annotation);
    }
  }

  private void doOneSentence(CoreMap sentence) {
    Tree tree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class);
    Tree binarized;
    if (isBinarized(tree)) {
      binarized = tree;
    } else {
      binarized = binarizer.transformTree(tree);
    }
View Full Code Here

  }

  /** Returns a 0-based index of the head of the tree.  Assumes the leaves had been indexed from 1 */
  static int headIndex(Tree tree) {
    CoreLabel label = ErasureUtils.uncheckedCast(tree.label());
    Tree head = label.get(TreeCoreAnnotations.HeadWordAnnotation.class);
    CoreLabel headLabel = ErasureUtils.uncheckedCast(head.label());
    return headLabel.index() - 1;
  }
View Full Code Here

      GZIPInputStream gis = new GZIPInputStream(bis);
      ObjectInputStream ois = new ObjectInputStream(gis);
      int size = ErasureUtils.<Integer>uncheckedCast(ois.readObject());
      for (int i = 0; i < size; ++i) {
        String rawTree = ErasureUtils.uncheckedCast(ois.readObject());
        Tree tree = Tree.valueOf(rawTree, trf);
        tree.setSpans();
        output.add(tree);
      }
      ois.close();
      gis.close();
      bis.close();
View Full Code Here

    // We disallow shifting when the previous transition was a right
    // head transition to a partial (binarized) state
    // TODO: I don't have an explanation for this, it was just stated
    // in Zhang & Clark 2009
    if (state.stack.size() > 0) {
      Tree top = state.stack.peek();
      // Temporary node, eg part of a binarized sequence
      if (top.label().value().startsWith("@") && top.children().length == 2 &&
          ShiftReduceUtils.getBinarySide(top) == BinaryTransition.Side.RIGHT) {
        return false;
      }
    }

    if (constraints == null || state.stack.size() == 0) {
      return true;
    }
    final Tree top = state.stack.peek();
    // If there are ParserConstraints, you can only shift if shifting
    // will not make a constraint unsolvable.  This happens if we
    // shift beyond the right end of a constraint which is not solved.
    for (ParserConstraint constraint : constraints) {
      // either went past or haven't gotten to this constraint yet
View Full Code Here

  /**
   * Add the new preterminal to the stack, increment the queue position.
   */
  public State apply(State state, double scoreDelta) {
    Tree tagNode = state.sentence.get(state.tokenPosition);
    if (!tagNode.isPreTerminal()) {
      throw new AssertionError("Only expected preterminal nodes");
    }
    Tree wordNode = tagNode.children()[0];
    String word = wordNode.label().value();
    return new State(state.stack.push(tagNode), state.transitions.push(this), state.separators, state.sentence, state.tokenPosition + 1, state.score + scoreDelta, false);
  }
View Full Code Here

          wordTable.appendChild(wordInfo);
        }
        sentElem.appendChild(wordTable);

        // add tree info
        Tree tree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class);

        if(tree != null) {
          // add the constituent tree for this sentence
          Element parseInfo = new Element("parse", NAMESPACE_URI);
          addConstituentTreeInfo(parseInfo, tree, options.constituentTreePrinter);
          sentElem.appendChild(parseInfo);
        }

        SemanticGraph basicDependencies = sentence.get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class);

        if (basicDependencies != null) {
          // add the dependencies for this sentence
          Element depInfo = buildDependencyTreeInfo("basic-dependencies", sentence.get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class), tokens, NAMESPACE_URI);
          if (depInfo != null) {
            sentElem.appendChild(depInfo);
          }

          depInfo = buildDependencyTreeInfo("collapsed-dependencies", sentence.get(SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation.class), tokens, NAMESPACE_URI);
          if (depInfo != null) {
            sentElem.appendChild(depInfo);
          }

          depInfo = buildDependencyTreeInfo("collapsed-ccprocessed-dependencies", sentence.get(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class), tokens, NAMESPACE_URI);
          if (depInfo != null) {
            sentElem.appendChild(depInfo);
          }
        }

        // add the MR entities and relations
        List<EntityMention> entities = sentence.get(MachineReadingAnnotations.EntityMentionsAnnotation.class);
        List<RelationMention> relations = sentence.get(MachineReadingAnnotations.RelationMentionsAnnotation.class);
        if (entities != null && entities.size() > 0){
          Element mrElem = new Element("MachineReading", NAMESPACE_URI);
          Element entElem = new Element("entities", NAMESPACE_URI);
          addEntities(entities, entElem, NAMESPACE_URI);
          mrElem.appendChild(entElem);

          if(relations != null){
            Element relElem = new Element("relations", NAMESPACE_URI);
            addRelations(relations, relElem, NAMESPACE_URI, options.relationsBeam);
            mrElem.appendChild(relElem);
          }

          sentElem.appendChild(mrElem);
        }

               
        /**
         * Adds sentiment as an attribute of this sentence.
         */
        Tree sentimentTree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class);
        if (sentimentTree != null) {
          int sentiment = RNNCoreAnnotations.getPredictedClass(sentimentTree);
          sentElem.addAttribute(new Attribute("sentimentValue", Integer.toString(sentiment)));
          String sentimentClass = sentence.get(SentimentCoreAnnotations.ClassName.class);
          sentElem.addAttribute(new Attribute("sentiment", sentimentClass.replaceAll(" ", "")));
View Full Code Here

    ShiftReduceParser model = ShiftReduceParser.loadModel(modelPath);

    DocumentPreprocessor tokenizer = new DocumentPreprocessor(new StringReader(text));
    for (List<HasWord> sentence : tokenizer) {
      List<TaggedWord> tagged = tagger.tagSentence(sentence);
      Tree tree = model.apply(tagged);
      System.err.println(tree);
    }
  }
View Full Code Here

                                      "\n Orig: " + Sentence.listToString(originalSentence) +
                                      "\n Pars: " + Sentence.listToString(leaves));
    }
    Iterator<Tree> leafIterator = leaves.iterator();
    for (HasWord word : originalSentence) {
      Tree leaf = leafIterator.next();
      if (!(word instanceof Label)) {
        continue;
      }
      leaf.setLabel((Label) word);
    }
  }
View Full Code Here

  Tree getBestParse(boolean stripSubcat) {
    if (parseSkipped) {
      return null;
    }
    if (bparser != null && parseSucceeded) {
      Tree binaryTree = bparser.getBestParse();

      Tree tree = debinarizer.transformTree(binaryTree);
      if (op.nodePrune) {
        NodePruner np = new NodePruner(pparser, debinarizer);
        tree = np.prune(tree);
      }
      if (stripSubcat) {
View Full Code Here

TOP

Related Classes of edu.stanford.nlp.trees.Tree

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.