edu.stanford.nlp.tagger.maxent.Extractors Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of stanford-corenlp Show documentation
Show all versions of stanford-corenlp Show documentation
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.
package edu.stanford.nlp.tagger.maxent;
import edu.stanford.nlp.util.logging.Redwood;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import edu.stanford.nlp.util.Pair;
/** Maintains a set of feature extractors for a maxent POS tagger and applies them.
*
* @author Kristina Toutanova
* @version 1.0
*/
public class Extractors implements Serializable {
/** A logger for this class */
private static final Redwood.RedwoodChannels log = Redwood.channels(Extractors.class);
private final Extractor[] v;
private static final boolean DEBUG = false;
transient List>
local, // extractors only looking at current word
localContext, // extractors only looking at words, except those in "local"
dynamic; // extractors depending on class labels
/**
* Set the extractors from an array.
*
* @param extrs The array of extractors. It is copied in this init.
*/
public Extractors(Extractor[] extrs) {
v = new Extractor[extrs.length];
System.arraycopy(extrs, 0, v, 0, extrs.length);
initTypes();
}
/**
* Determine type of each feature extractor.
*/
void initTypes() {
local = new ArrayList<>();
localContext = new ArrayList<>();
dynamic = new ArrayList<>();
for(int i=0; i max) {
max = lf;
}
}
return max;
}
/** Find maximum right context of extractors. Used in TagInference to decide windows for dynamic programming.
* @return The maximum of the right contexts used by all extractors.
*/
int rightContext() {
int max = 0;
for (Extractor extractor : v) {
int lf = extractor.rightContext();
if (lf > max) {
max = lf;
}
}
return max;
}
public int size() {
return v.length;
}
protected void setGlobalHolder(MaxentTagger tagger) {
for (Extractor extractor : v) {
extractor.setGlobalHolder(tagger);
}
}
/*
public void save(String filename) {
try {
DataOutputStream rf = IOUtils.getDataOutputStream(filename);
rf.writeInt(v.length);
for (Extractor extr : v) {
rf.writeBytes(extr.toString());
}
rf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void read(String filename) {
try {
InDataStreamFile rf = new InDataStreamFile(filename);
int len = rf.readInt();
v = new Extractor[len];
//GlobalHolder.init();
} catch (IOException e) {
e.printStackTrace();
}
}
*/
Extractor get(int index) {
return v[index];
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("Extractors[");
for (int i = 0; i < v.length; i++) {
sb.append(v[i]);
if (i < v.length - 1) {
sb.append(", ");
}
}
sb.append(']');
return sb.toString();
}
/**
* Prints out the pair of {@code Extractors} objects found in the
* file that is the first and only argument.
* @param args Filename of extractors file (standardly written with
* {@code .ex} extension)
*/
public static void main(String[] args) {
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(args[0]));
Extractors extrs = (Extractors) in.readObject();
Extractors extrsRare = (Extractors) in.readObject();
in.close();
System.out.println("All words: " + extrs);
System.out.println("Rare words: " + extrsRare);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static final long serialVersionUID = -4777107742414749890L;
}