1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package de.fu_berlin.ties.combi;
23
24 import java.util.Collections;
25 import java.util.Set;
26 import java.util.SortedSet;
27 import java.util.TreeSet;
28
29 import de.fu_berlin.ties.classify.Prediction;
30 import de.fu_berlin.ties.classify.PredictionDistribution;
31 import de.fu_berlin.ties.text.TokenDetails;
32
33 /***
34 * The trivial combination strategy simply uses a single tag for each class;
35 * no prefixes are used. Thus is cannot express the instances of the same
36 * class immediately following one after other -- they would be joined in a
37 * single instance. "A" is used for anything else/outside of any class (thus it
38 * cannot be used as the name of a class).
39 *
40 * <p>{@link de.fu_berlin.ties.combi.CombinationState#isEnd()} is not
41 * supported by this class and will always return <code>false</code>.
42 *
43 * @author Christian Siefkes
44 * @version $Revision: 1.13 $, $Date: 2006/10/21 16:04:01 $, $Author: siefkes $
45 */
46 public class TrivialStrategy extends CombinationStrategy {
47
48 /***
49 * String used to classify outside/other. Actually called "A" so it will be
50 * picked in case of a tie.
51 */
52 private static final String OUTSIDE = "A";
53
54 /***
55 * An immutable set of all classes (Strings) that can possible occur
56 * during classification.
57 */
58 private final SortedSet[] allClasses;
59
60 /***
61 * Creates a new instance.
62 *
63 * @param theClasses a set of valid class names (String)
64 */
65 public TrivialStrategy(final Set<String> theClasses) {
66 super(theClasses);
67 final SortedSet<String> allValidClasses =
68 new TreeSet<String>(theClasses);
69 allValidClasses.add(OUTSIDE);
70 allClasses = new SortedSet[] {
71 Collections.unmodifiableSortedSet(allValidClasses)
72 };
73 }
74
75 /***
76 * Builds a set of class names (Strings) to pass to the classifier
77 * to consider for the next decision. This implementation returns the
78 * classes in alphabetic order (using a {@link SortedSet}).
79 *
80 * @return a set of class names
81 */
82 public Set[] activeClasses() {
83
84 return allClasses();
85 }
86
87 /***
88 * {@inheritDoc}
89 */
90 public Set[] allClasses() {
91 return allClasses;
92 }
93
94 /***
95 * {@inheritDoc}
96 */
97 public String[] translateCurrentState(final CombinationState currentState)
98 throws IllegalArgumentException {
99 final String result;
100 final String type = currentState.getType();
101
102 if (type == null) {
103
104 result = OUTSIDE;
105 } else {
106
107 result = type;
108 }
109 return new String[] {result};
110 }
111
112 /***
113 * {@inheritDoc}
114 */
115 public CombinationState translateResult(
116 final PredictionDistribution[] predictions,
117 final TokenDetails details)
118 throws IllegalArgumentException {
119 final CombinationState result;
120 final Prediction best = predictions[0].best();
121 final String predictedClass = best.getType();
122
123 if (OUTSIDE.equals(predictedClass)) {
124
125 result = CombinationState.OUTSIDE;
126 } else {
127 final boolean isBegin;
128 if (predictedClass.equals(state().getType())) {
129 isBegin = false;
130 } else {
131 isBegin = true;
132 }
133
134 if (getValidClasses().contains(predictedClass)) {
135 result = new CombinationState(predictedClass, isBegin, false,
136 best.getProbability());
137 } else {
138 throw new IllegalArgumentException(
139 "Predicted type " + predictedClass + " is invalid");
140 }
141 }
142 return result;
143 }
144
145 }