View Javadoc

1   /*
2    * Copyright (C) 2004-2006 Christian Siefkes <christian@siefkes.net>.
3    * Development of this software is supported by the German Research Society,
4    * Berlin-Brandenburg Graduate School in Distributed Information Systems
5    * (DFG grant no. GRK 316).
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, visit
19   * http://www.gnu.org/licenses/gpl.html or write to the Free Software
20   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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          // all classes are always allowed
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             // null: outside
104             result = OUTSIDE;
105         } else {
106             // return type as is
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             // outside any instance
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 }