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