edu.stanford.nlp.fsm.DFSAState 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.fsm;
import edu.stanford.nlp.util.Generics;
import edu.stanford.nlp.util.Scored;
import java.util.*;
/**
* DFSAState represents the state of a deterministic finite state
* automaton without epsilon transitions.
*
* @author Dan Klein
* @version 12/14/2000
* @author Sarah Spikes ([email protected]) - cleanup and filling in types
* @param stateID type
* @param transition type
*/
public final class DFSAState implements Scored {
private S stateID;
private Map> inputToTransition;
public boolean accepting;
private DFSA dfsa;
public double score;
public double score() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public DFSA dfsa() {
return dfsa;
}
public void setStateID(S stateID) {
this.stateID = stateID;
}
public S stateID() {
return stateID;
}
public void addTransition(DFSATransition transition) {
inputToTransition.put(transition.input(), transition);
}
public DFSATransition transition(T input) {
return inputToTransition.get(input);
}
public Collection> transitions() {
return inputToTransition.values();
}
public Set continuingInputs() {
return inputToTransition.keySet();
}
public Set> successorStates() {
Set> successors = Generics.newHashSet();
Collection> transitions = inputToTransition.values();
for (DFSATransition transition : transitions) {
successors.add(transition.getTarget());
}
return successors;
}
public void setAccepting(boolean accepting) {
this.accepting = accepting;
}
public boolean isAccepting() {
return accepting;
}
public boolean isContinuable() {
return !inputToTransition.isEmpty();
}
@Override
public String toString() {
return stateID.toString();
}
private int hashCodeCache; // = 0;
@Override
public int hashCode() {
if (hashCodeCache == 0) {
hashCodeCache = stateID.hashCode() ^ dfsa.hashCode();
}
return hashCodeCache;
}
// equals
@SuppressWarnings("unchecked")
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof DFSAState)) {
return false;
}
DFSAState s = (DFSAState) o;
// historically also checked: accepting == s.accepting &&
//inputToTransition.equals(s.inputToTransition))
return dfsa.equals(s.dfsa) && stateID.equals(s.stateID);
}
public Set> statesReachable() {
Set> visited = Generics.newHashSet();
List> toVisit = new ArrayList<>();
toVisit.add(this);
exploreStates(toVisit, visited);
return visited;
}
private void exploreStates(List> toVisit, Set> visited) {
while (!toVisit.isEmpty()) {
DFSAState state = toVisit.get(toVisit.size() - 1);
toVisit.remove(toVisit.size() - 1);
if (!visited.contains(state)) {
toVisit.addAll(state.successorStates());
visited.add(state);
}
}
}
public DFSAState(S id, DFSA dfsa) {
this.dfsa = dfsa;
this.stateID = id;
this.accepting = false;
this.inputToTransition = Generics.newHashMap();
this.score = Double.NEGATIVE_INFINITY;
}
public DFSAState(S id, DFSA dfsa, double score) {
this(id,dfsa);
setScore(score);
}
}