001package org.maltparser.parser.history.kbest;
002
003import org.maltparser.core.exception.MaltChainedException;
004import org.maltparser.parser.history.action.SingleDecision;
005/**
006*
007* @author Johan Hall
008**/
009public class ScoredKBestList extends KBestList{
010        
011        public ScoredKBestList(SingleDecision action) {
012                this(-1, action);
013        }
014
015        public ScoredKBestList(Integer k, SingleDecision action) {
016                super(k, action);
017        }
018        
019        protected void initKBestList() {
020                for (int i=0; i < this.k; i++) {
021                        kBestList.add(new ScoredCandidate());
022                }
023        }
024        
025        public void add(int actionCode, float score) throws MaltChainedException {
026                if (k != -1 && addCandidateIndex >= k) { return; }
027                if (addCandidateIndex >= kBestList.size()) { kBestList.add(new ScoredCandidate()); }
028                if (!(kBestList.get(addCandidateIndex) instanceof ScoredCandidate)) {
029                        super.add(actionCode);
030                        return;
031                }
032                final ScoredCandidate scand = (ScoredCandidate)kBestList.get(addCandidateIndex);
033                scand.setActionCode(actionCode);
034                scand.setScore(score);
035                if (addCandidateIndex == 0) {
036                        if (decision instanceof SingleDecision) {
037                                ((SingleDecision)decision).addDecision(actionCode);
038                        }
039                        topCandidateIndex++;
040                }
041                addCandidateIndex++;
042        }
043        
044        public void add(String symbol, float score) throws MaltChainedException {
045                if (decision instanceof SingleDecision) {
046                        this.add(((SingleDecision)decision).getDecisionCode(symbol), score);
047                }
048        }
049        
050        public float peekNextKBestScore() {
051                if (!(kBestList.get(addCandidateIndex) instanceof ScoredCandidate)) {
052                        return Float.NaN;
053                }
054                if (addCandidateIndex != 0 && topCandidateIndex < addCandidateIndex && topCandidateIndex < kBestList.size()) {
055                        return ((ScoredCandidate)kBestList.get(topCandidateIndex)).getScore();
056                }
057                return Float.NaN;
058        }
059        
060        /* (non-Javadoc)
061         * @see java.lang.Object#toString()
062         */
063        public String toString() {
064                return super.toString();
065        }
066}