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