View Javadoc

1   /*
2    * Copyright (C) 2004 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 library is free software; you can redistribute it and/or
8    * modify it under the terms of the GNU Lesser General Public
9    * License as published by the Free Software Foundation; either
10   * version 2.1 of the License, or (at your option) any later version.
11   *
12   * This library 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 GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this library; if not, visit
19   * http://www.gnu.org/licenses/lgpl.html or write to the Free Software
20   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, 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.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          // all classes are always allowed
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             // null: outside
102             result = OUTSIDE;
103         } else {
104             // return type as is
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             // outside any instance
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 }