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

edu.stanford.nlp.pipeline.CoreMapAggregator 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.pipeline;

import edu.stanford.nlp.process.CoreLabelTokenFactory;
import edu.stanford.nlp.util.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * Function that aggregates several core maps into one
 *
 * @author Angel Chang
 */
public class CoreMapAggregator implements Function, CoreMap> {
  public static final CoreMapAggregator DEFAULT_AGGREGATOR = getAggregator(CoreMapAttributeAggregator.getDefaultAggregators());
  public static final CoreMapAggregator DEFAULT_NUMERIC_TOKENS_AGGREGATOR = getAggregator(CoreMapAttributeAggregator.DEFAULT_NUMERIC_TOKENS_AGGREGATORS);

  Map aggregators;
  Class mergedKey = null;  // Keeps chunks that were merged to form this one
  CoreLabelTokenFactory tokenFactory = null; // Should we be creating tokens?

  public CoreMapAggregator(Map aggregators) {
    this.aggregators = aggregators;
  }

  public CoreMapAggregator(Map aggregators, Class mergedKey, CoreLabelTokenFactory tokenFactory) {
    this.aggregators = aggregators;
    this.mergedKey = mergedKey;
    this.tokenFactory = tokenFactory;
  }

  public CoreMap merge(List in, int start, int end)
  {
    CoreMap merged = ChunkAnnotationUtils.getMergedChunk(in, start, end, aggregators, tokenFactory);
    if (mergedKey != null) {
      merged.set(mergedKey, new ArrayList<>(in.subList(start, end)));
    }
    return merged;
  }

  public CoreMap merge(List in) {
    return merge(in, 0, in.size());
  }

  public CoreMap apply(List in) {
    return merge(in, 0, in.size());
  }

  public static CoreMapAggregator getDefaultAggregator()
  {
    return DEFAULT_AGGREGATOR;
  }

  public static CoreMapAggregator getAggregator(Map aggregators)
  {
    return new CoreMapAggregator(aggregators);
  }

  public static CoreMapAggregator getAggregator(Map aggregators, Class key)
  {
    return new CoreMapAggregator(aggregators, key, null);
  }

  public static CoreMapAggregator getAggregator(Map aggregators, Class key, CoreLabelTokenFactory tokenFactory)
  {
    return new CoreMapAggregator(aggregators, key, tokenFactory);
  }

  public List merge(List list, List> matched)
  {
    return CollectionUtils.mergeList(list, matched, this);
  }

  public  List merge(List list, List matched, Function> toIntervalFunc)
  {
    return CollectionUtils.mergeList(list, matched, toIntervalFunc, this);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy