public static List<String> toTagList(List<State> states) { List<String> tags = new ArrayList<String>(); if (states.size() > 0) { tags.add(states.get(0).getPreviousPreviousTag()); for (State state : states) { tags.add(state.getPreviousTag()); } } return tags; }
/** * Builds a Trellis over a sentence, by starting at the state State, and advancing through all * legal extensions of each state already in the trellis. You should not have to modify this * code (or even read it, really). */ private Trellis<State> buildTrellis(List<String> sentence) { Trellis<State> trellis = new Trellis<State>(); trellis.setStartState(State.getStartState()); State stopState = State.getStopState(sentence.size() + 2); trellis.setStopState(stopState); Set<State> states = Collections.singleton(State.getStartState()); for (int position = 0; position <= sentence.size() + 1; position++) { Set<State> nextStates = new HashSet<State>(); for (State state : states) { if (state.equals(stopState)) continue; LocalTrigramContext localTrigramContext = new LocalTrigramContext( sentence, position, state.getPreviousPreviousTag(), state.getPreviousTag()); Counter<String> tagScores = localTrigramScorer.getLogScoreCounter(localTrigramContext); for (String tag : tagScores.keySet()) { double score = tagScores.getCount(tag); State nextState = state.getNextState(tag); trellis.setTransitionCount(state, nextState, score); nextStates.add(nextState); } } // System.out.println("States: "+nextStates); states = nextStates; } return trellis; }
// to tag a sentence: build its trellis and find a path through that trellis public List<String> tag(List<String> sentence) { Trellis<State> trellis = buildTrellis(sentence); List<State> states = trellisDecoder.getBestPath(trellis); List<String> tags = State.toTagList(states); tags = stripBoundaryTags(tags); return tags; }
private State(State state) { setState(state.getPreviousPreviousTag(), state.getPreviousTag(), state.getPosition()); }
public State getPreviousState(String tag) { return State.buildState(tag, getPreviousPreviousTag(), getPosition() - 1); }
public State getNextState(String tag) { return State.buildState(getPreviousTag(), tag, getPosition() + 1); }
public static State buildState(String previousPreviousTag, String previousTag, int position) { tempState.setState(previousPreviousTag, previousTag, position); return stateInterner.intern(tempState); }