All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.stanford.nlp.trees.tregex.tsurgeon.ReplaceNode Maven / Gradle / Ivy

Go to download

Stanford Parser processes raw text in English, Chinese, German, Arabic, and French, and extracts constituency parse trees.

There is a newer version: 3.9.2
Show newest version
package edu.stanford.nlp.trees.tregex.tsurgeon;

import java.util.List;
import java.util.Map;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.tregex.TregexMatcher;
import edu.stanford.nlp.util.ArrayUtils;
import edu.stanford.nlp.util.CollectionUtils;
import java.util.function.Function;

/**
 * @author Roger Levy ([email protected])
 */
class ReplaceNode extends TsurgeonPattern {

  public ReplaceNode(TsurgeonPattern oldNode, TsurgeonPattern ... newNodes) {
    super("replace", ArrayUtils.concatenate(new TsurgeonPattern[] { oldNode }, newNodes));
  }

  public ReplaceNode(TsurgeonPattern oldNode, List trees) {
    this(oldNode, CollectionUtils.transformAsList(trees, convertAuxiliaryToHold).toArray(new TsurgeonPattern[trees.size()]));
  }

  private static final Function convertAuxiliaryToHold = t -> new HoldTreeNode(t);

  @Override
  public TsurgeonMatcher matcher(Map newNodeNames, CoindexationGenerator coindexer) {
    return new Matcher(newNodeNames, coindexer);
  }

  private class Matcher extends TsurgeonMatcher {
    public Matcher(Map newNodeNames, CoindexationGenerator coindexer) {
      super(ReplaceNode.this, newNodeNames, coindexer);
    }

    @Override
    public Tree evaluate(Tree tree, TregexMatcher tregex) {
      Tree oldNode = childMatcher[0].evaluate(tree, tregex);
      if (oldNode==tree) {
        if (children.length > 2) {
          throw new TsurgeonRuntimeException("Attempted to replace a root node with more than one node, unable to proceed");
        }
        return childMatcher[1].evaluate(tree, tregex);
      }
      Tree parent = oldNode.parent(tree);
      int i = parent.objectIndexOf(oldNode);
      parent.removeChild(i);
      for (int j = 1; j < children.length; ++j) {
        Tree newNode = childMatcher[j].evaluate(tree, tregex);
        parent.insertDtr(newNode.deepCopy(), i + j - 1);
      }
      return tree;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy