You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opennlp.apache.org by jo...@apache.org on 2014/03/06 10:46:07 UTC

svn commit: r1574820 - /opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/postag/POSTaggerME.java

Author: joern
Date: Thu Mar  6 09:46:07 2014
New Revision: 1574820

URL: http://svn.apache.org/r1574820
Log:
OPENNLP-641 Completed migration to sequence classification model

Modified:
    opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/postag/POSTaggerME.java

Modified: opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/postag/POSTaggerME.java
URL: http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/postag/POSTaggerME.java?rev=1574820&r1=1574819&r2=1574820&view=diff
==============================================================================
--- opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/postag/POSTaggerME.java (original)
+++ opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/postag/POSTaggerME.java Thu Mar  6 09:46:07 2014
@@ -56,11 +56,8 @@ import opennlp.tools.util.model.ModelTyp
  */
 public class POSTaggerME implements POSTagger {
   
-  /**
-   * The maximum entropy model to use to evaluate contexts.
-   */
-  protected MaxentModel posModel;
-
+  private POSModel modelPackage;
+  
   /**
    * The feature context generator.
    */
@@ -101,7 +98,10 @@ public class POSTaggerME implements POST
    */
   public POSTaggerME(POSModel model, int beamSize, int cacheSize) {
     POSTaggerFactory factory = model.getFactory();
-    posModel = model.getPosModel();
+    
+    modelPackage = model;
+    
+    // TODO: Why is this the beam size?! not cache size?
     contextGen = factory.getPOSContextGenerator(beamSize);
     tagDictionary = factory.getTagDictionary();
     size = beamSize;
@@ -133,9 +133,15 @@ public class POSTaggerME implements POST
    * @return the number of different tags predicted by this model.
    */
   public int getNumTags() {
-    return posModel.getNumOutcomes();
+    
+    // TODO: Lets discuss on the dev list how to do this properly!
+    // Nobody needs the number of tags, if the tags are not available.
+    
+    return model.getOutcomes().length;
   }
 
+  // TODO: Add method to get tags ?! 
+  
   @Deprecated
   public List<String> tag(List<String> sentence) {
     bestSequence = model.bestSequence(sentence.toArray(new String[sentence.size()]), null, contextGen, sequenceValidator);
@@ -221,27 +227,35 @@ public class POSTaggerME implements POST
   }
 
   public String[] getOrderedTags(List<String> words, List<String> tags, int index,double[] tprobs) {
-    double[] probs = posModel.eval(contextGen.getContext(index,
-        words.toArray(new String[words.size()]),
-        tags.toArray(new String[tags.size()]),null));
-
-    String[] orderedTags = new String[probs.length];
-    for (int i = 0; i < probs.length; i++) {
-      int max = 0;
-      for (int ti = 1; ti < probs.length; ti++) {
-        if (probs[ti] > probs[max]) {
-          max = ti;
+    
+    if (modelPackage.getPosModel() != null) {
+      
+      MaxentModel posModel = modelPackage.getPosModel();
+      
+      double[] probs = posModel.eval(contextGen.getContext(index,
+          words.toArray(new String[words.size()]),
+          tags.toArray(new String[tags.size()]),null));
+  
+      String[] orderedTags = new String[probs.length];
+      for (int i = 0; i < probs.length; i++) {
+        int max = 0;
+        for (int ti = 1; ti < probs.length; ti++) {
+          if (probs[ti] > probs[max]) {
+            max = ti;
+          }
         }
+        orderedTags[i] = posModel.getOutcome(max);
+        if (tprobs != null){
+          tprobs[i]=probs[max];
+        }
+        probs[max] = 0;
       }
-      orderedTags[i] = posModel.getOutcome(max);
-      if (tprobs != null){
-        tprobs[i]=probs[max];
-      }
-      probs[max] = 0;
+      return orderedTags;
+    }
+    else {
+      throw new UnsupportedOperationException("This method can only be called if the "
+          + "classifcation model is an event model!");
     }
-    return orderedTags;
-    
-    
   }
   
   public static POSModel train(String languageCode,