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

edu.stanford.nlp.ling.tokensregex.SequenceMatchAction Maven / Gradle / Ivy

Go to download

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

The newest version!
package edu.stanford.nlp.ling.tokensregex;

import java.util.function.Predicate;

import java.util.Arrays;
import java.util.List;

/**
 * Performs action on a sequence
 *
 * @author Angel Chang
 */
public interface SequenceMatchAction {
  public SequenceMatchResult apply(SequenceMatchResult matchResult, int... groups);

  public final static class BoundAction {
    SequenceMatchAction action;
    int[] groups;

    public SequenceMatchResult apply(SequenceMatchResult seqMatchResult) {
      return action.apply(seqMatchResult, groups);
    }
  }

  public final static class StartMatchAction implements SequenceMatchAction {
    SequencePattern pattern;

    public StartMatchAction(SequencePattern pattern) {
      this.pattern = pattern;
    }

    public SequenceMatchResult apply(SequenceMatchResult seqMatchResult, int... groups) {
      SequenceMatcher matcher = pattern.getMatcher(seqMatchResult.elements());
      if (matcher.find()) {
        return matcher;
      } else {
        return null;
      }
    }
  }

  public final static class NextMatchAction implements SequenceMatchAction {
    public SequenceMatchResult apply(SequenceMatchResult seqMatchResult, int... groups) {
      if (seqMatchResult instanceof SequenceMatcher) {
        SequenceMatcher matcher = (SequenceMatcher) seqMatchResult;
        if (matcher.find()) {
          return matcher;
        } else {
          return null;
        }
      } else {
        return null;
      }
    }
  }

  public final static class BranchAction implements SequenceMatchAction {
    Predicate> filter;
    SequenceMatchAction acceptBranch;
    SequenceMatchAction rejectBranch;

    public BranchAction(Predicate> filter, SequenceMatchAction acceptBranch, SequenceMatchAction rejectBranch) {
      this.filter = filter;
      this.acceptBranch = acceptBranch;
      this.rejectBranch = rejectBranch;
    }

    public SequenceMatchResult apply(SequenceMatchResult seqMatchResult, int... groups) {
      if (filter.test(seqMatchResult)) {
        return (acceptBranch != null)? acceptBranch.apply(seqMatchResult):null;
      } else {
        return (rejectBranch != null)? rejectBranch.apply(seqMatchResult):null;
      }
    }
  }

  public final static class SeriesAction implements SequenceMatchAction {
    List> actions;

    public SeriesAction(SequenceMatchAction... actions) {
      this.actions = Arrays.asList(actions);
    }

    public SeriesAction(List> actions) {
      this.actions = actions;
    }

    public SequenceMatchResult apply(SequenceMatchResult seqMatchResult, int... groups) {
      SequenceMatchResult res = seqMatchResult;
      for (SequenceMatchAction a:actions) {
        res = a.apply(res, groups);
      }
      return res;
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy