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

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

Go to download

Stanford CoreNLP provides a set of natural language analysis tools which can take raw English language text input and give the base forms of words, their parts of speech, whether they are names of companies, people, etc., normalize dates, times, and numeric quantities, mark up the structure of sentences in terms of phrases and word dependencies, and indicate which noun phrases refer to the same entities. It provides the foundational building blocks for higher level text understanding applications.

There is a newer version: 4.5.7
Show newest version
package edu.stanford.nlp.ling.tokensregex;

import edu.stanford.nlp.pipeline.ChunkAnnotationUtils;
import edu.stanford.nlp.pipeline.CoreMapAggregator;
import edu.stanford.nlp.util.CollectionUtils;
import edu.stanford.nlp.util.CoreMap;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Performs a action on a matched sequence
 *
 * @author Angel Chang
 */
public abstract class CoreMapSequenceMatchAction implements SequenceMatchAction {

  public final static class AnnotateAction extends CoreMapSequenceMatchAction {
    Map attributes;   // TODO: Preconvert, handle when to overwrite existing attributes

    public AnnotateAction(Map attributes) {
      this.attributes = attributes;
    }

    public SequenceMatchResult apply(SequenceMatchResult matchResult, int... groups) {
      for (int group:groups) {
        int groupStart = matchResult.start(group);
        if (groupStart >=0) {
          int groupEnd = matchResult.end(group);
          ChunkAnnotationUtils.annotateChunks(matchResult.elements(), groupStart, groupEnd, attributes);
        }
      }
      return matchResult;
    }

  }

  public final static MergeAction DEFAULT_MERGE_ACTION = new MergeAction();

  public final static class MergeAction extends CoreMapSequenceMatchAction {
    CoreMapAggregator aggregator = CoreMapAggregator.getDefaultAggregator();

    public MergeAction() {}
    
    public MergeAction(CoreMapAggregator aggregator) {
      this.aggregator = aggregator;
    }

    public SequenceMatchResult apply(SequenceMatchResult matchResult, int... groups)
    {
      BasicSequenceMatchResult res = matchResult.toBasicSequenceMatchResult();

      List elements = matchResult.elements();
      List mergedElements = new ArrayList<>();
      res.elements = mergedElements;

      int last = 0;
      int mergedGroup = 0;
      int offset = 0;
      List orderedGroups = CollectionUtils.asList(groups);
      Collections.sort(orderedGroups);
      for (int group:orderedGroups) {
        int groupStart = matchResult.start(group);
        if (groupStart >= last) {
          // Add elements from last to start of group to merged elements
          mergedElements.addAll(elements.subList(last,groupStart));
          // Fiddle with matched group indices
          for (; mergedGroup < group; mergedGroup++) {
            if (res.matchedGroups[mergedGroup] != null) {
              res.matchedGroups[mergedGroup].matchBegin -= offset;
              res.matchedGroups[mergedGroup].matchEnd -= offset;
            }
          }
          // Get merged element
          int groupEnd = matchResult.end(group);
          if (groupEnd - groupStart >= 1) {
            CoreMap merged = aggregator.merge(elements, groupStart, groupEnd);
            mergedElements.add(merged);
            last = groupEnd;

            // Fiddle with matched group indices
            res.matchedGroups[mergedGroup].matchBegin = mergedElements.size()-1;
            res.matchedGroups[mergedGroup].matchEnd = mergedElements.size();
            mergedGroup++;
            while (mergedGroup < res.matchedGroups.length)  {
              if (res.matchedGroups[mergedGroup] != null) {
                if (res.matchedGroups[mergedGroup].matchBegin == matchResult.start(group) &&
                        res.matchedGroups[mergedGroup].matchEnd == matchResult.end(group)) {
                  res.matchedGroups[mergedGroup].matchBegin = res.matchedGroups[group].matchBegin;
                  res.matchedGroups[mergedGroup].matchEnd = res.matchedGroups[group].matchEnd;
                } else if (res.matchedGroups[mergedGroup].matchEnd <= matchResult.end(group)) {
                  res.matchedGroups[mergedGroup] = null;
                } else {
                  break;
                }
              }
              mergedGroup++;
            }
            offset = matchResult.end(group) - res.matchedGroups[group].matchEnd;
          }
        }
      }
      // Add rest of elements
      mergedElements.addAll(elements.subList(last, elements.size()));
      // Fiddle with matched group indices
      for (; mergedGroup < res.matchedGroups.length; mergedGroup++) {
        if (res.matchedGroups[mergedGroup] != null) {
          res.matchedGroups[mergedGroup].matchBegin -= offset;
          res.matchedGroups[mergedGroup].matchEnd -= offset;
        }
      }
      return res;
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy