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

edu.stanford.nlp.ling.tokensregex.NodePattern 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.ling.tokensregex;

import edu.stanford.nlp.util.StringUtils;

import java.io.Serializable;
import java.util.List;

/**
 * Matches a Node (i.e a Token).
 *
 * @author Angel Chang
 */
public abstract class NodePattern implements Serializable{

  public static final NodePattern ANY_NODE = new AnyNodePattern();

  // Flags for string annotations
  public static final int CASE_INSENSITIVE = 0x02;
  public static final int NORMALIZE = 0x04;
  public static final int UNICODE_CASE = 0x40;

  /**
   * Returns true if the input node matches this pattern
   * @param node - node to match
   * @return true if the node matches the pattern, false otherwise
   */
  public abstract boolean match(T node);

  /**
   * Returns result associated with the match
   * @param node node to match
   * @return null if not matched, TRUE if there is a match but no other result associated with the match.
   *         Any other value is treated as the result value of the match.
   */
  public Object matchWithResult(T node) {
    if (match(node)) return Boolean.TRUE;
    else return null;
  }

  /**
   * Matches any node
   * @param 
   */
  public static class AnyNodePattern extends NodePattern {
    protected AnyNodePattern() {
    }

    @Override
    public boolean match(T node) {
      return true;
    }

    public String toString() {
      return "*";
    }
  }

  /**
   * Matches a constant value of type T using equals()
   * @param 
   */
  public static class EqualsNodePattern extends NodePattern {
    T t;

    public EqualsNodePattern(T t) {
      this.t = t;
    }

    public boolean match(T node)
    {
      return t.equals(node);
    }

    public String toString() {
      return "[" + t + "]";
    }
  }

  /**
   * Given a node pattern p, a node x matches if p does not match x
   * @param 
   */
  public static class NegateNodePattern extends NodePattern {
    NodePattern p;

    public NegateNodePattern(NodePattern p) {
      this.p = p;
    }

    @Override
    public boolean match(T node)
    {
      return !p.match(node);
    }

    public String toString() {
      return "!" + p;
    }
  }

  /**
   * Given a list of patterns p1,...,pn, matches if all patterns p1,...,pn matches
   * @param 
   */
  public static class ConjNodePattern extends NodePattern {
    List> nodePatterns;

    public ConjNodePattern(List> nodePatterns) {
      this.nodePatterns = nodePatterns;
    }

    @Override
    public boolean match(T node)
    {
      boolean matched = true;
      for (NodePattern p:nodePatterns) {
        if (!p.match(node)) {
          matched = false;
          break;
        }
      }
      return matched;
    }

    public String toString() {
      return StringUtils.join(nodePatterns, " & ");
    }
  }

  /**
   * Given a list of patterns p1,...,pn, matches if one of the patterns p1,...,pn matches
   * @param 
   */
  public static class DisjNodePattern extends NodePattern {
    List> nodePatterns;

    public DisjNodePattern(List> nodePatterns) {
      this.nodePatterns = nodePatterns;
    }

    @Override
    public boolean match(T node)
    {
      boolean matched = false;
      for (NodePattern p:nodePatterns) {
        if (p.match(node)) {
          matched = true;
          break;
        }
      }
      return matched;
    }

    public String toString() {
      return StringUtils.join(nodePatterns, " | ");
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy