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 2011/10/12 16:16:35 UTC

svn commit: r1182388 - in /incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor: ./ namefinder/ sentdetect/ tokenize/

Author: joern
Date: Wed Oct 12 14:16:34 2011
New Revision: 1182388

URL: http://svn.apache.org/viewvc?rev=1182388&view=rev
Log:
OPENNLP-315 Added first error reporting to the name finder view. 

Modified:
    incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/ModelUtil.java
    incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/namefinder/EntityContentProvider.java
    incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/namefinder/MultiModelNameFinder.java
    incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/namefinder/NameFinderJob.java
    incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/namefinder/NameFinderViewPage.java
    incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/sentdetect/SentenceDetectorJob.java
    incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/tokenize/TokenizerJob.java

Modified: incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/ModelUtil.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/ModelUtil.java?rev=1182388&r1=1182387&r2=1182388&view=diff
==============================================================================
--- incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/ModelUtil.java (original)
+++ incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/ModelUtil.java Wed Oct 12 14:16:34 2011
@@ -17,6 +17,7 @@
 
 package org.apache.opennlp.caseditor;
 
+import java.io.IOException;
 import java.io.InputStream;
 
 import org.eclipse.core.resources.IFile;
@@ -31,7 +32,7 @@ public class ModelUtil {
   //       used to detect an updated model, if model is updated,
   //       load it and replace the old one!
   
-  public static InputStream openModelIn(String modelPath) {
+  public static InputStream openModelIn(String modelPath) throws IOException {
     InputStream modelIn = null;
     IResource modelResource = ResourcesPlugin.getWorkspace().
         getRoot().findMember(modelPath);
@@ -41,11 +42,14 @@ public class ModelUtil {
       try {
         modelIn = modelFile.getContents();
       } catch (CoreException e) {
-        // TODO: Improve error handling here ...
-        OpenNLPPlugin.log(e);      
+        throw new IOException(e.getMessage());
       }
     }
     
+    if (modelIn == null) {
+      throw new IOException("Model path is incorrect!");
+    }
+    
     return modelIn;
   }
 }

Modified: incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/namefinder/EntityContentProvider.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/namefinder/EntityContentProvider.java?rev=1182388&r1=1182387&r2=1182388&view=diff
==============================================================================
--- incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/namefinder/EntityContentProvider.java (original)
+++ incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/namefinder/EntityContentProvider.java Wed Oct 12 14:16:34 2011
@@ -220,11 +220,14 @@ public class EntityContentProvider imple
   private List<Entity> confirmedEntities = new ArrayList<Entity>();
   
   private String nameTypeNames[];
+
+  private NameFinderViewPage nameFinderView;
   
-  EntityContentProvider(AnnotationEditor editor, NameFinderJob nameFinder, TableViewer entityList) {
+  EntityContentProvider(NameFinderViewPage nameFinderView, AnnotationEditor editor, NameFinderJob nameFinder, TableViewer entityList) {
     this.nameFinder = nameFinder;
     this.entityListViewer = entityList;
     this.editor = editor;
+    this.nameFinderView = nameFinderView;
     
     IPreferenceStore store = editor.getCasDocumentProvider().getTypeSystemPreferenceStore(editor.getEditorInput());
     nameTypeNames = store.getString(OpenNLPPreferenceConstants.NAME_TYPE).split(",");
@@ -242,7 +245,8 @@ public class EntityContentProvider imple
           public void run() {
             IStatus status = event.getResult();
             
-            if (status.getSeverity() == IStatus.OK) {
+            if (status.isOK()) {
+              EntityContentProvider.this.nameFinderView.setMessage(null);
               
               List<Entity> detectedEntities = EntityContentProvider.this.nameFinder.getNames();
               
@@ -298,6 +302,9 @@ public class EntityContentProvider imple
                 }
               }
             }
+            else {
+              EntityContentProvider.this.nameFinderView.setMessage(status.getMessage());
+            }
           }
         });
       };
@@ -358,10 +365,11 @@ public class EntityContentProvider imple
     String sentenceTypeName = store.getString(OpenNLPPreferenceConstants.SENTENCE_TYPE);
     
     // TODO: Add check for sentence type name
-    if (sentenceTypeName.isEmpty())
+    if (sentenceTypeName.isEmpty()) {
+      nameFinderView.setMessage("Sentence type is not set!");
       return;
+    }
     
-    String additionalSentenceTypes = store.getString(OpenNLPPreferenceConstants.ADDITIONAL_SENTENCE_TYPE);
 
     // TODO: Add check for additional sentence type names
     
@@ -370,16 +378,15 @@ public class EntityContentProvider imple
     for (int i = 0; i < modelPathes.length; i++) {
       modelPathes[i] = modelPathes[i].trim();
       
-      // TODO: Add checks for model path names
-      if (modelPathes[i].isEmpty())
+      if (modelPathes[i].isEmpty()) {
+        nameFinderView.setMessage("Model path is not set!");
         return;
+      }
     }
-
     
     CAS cas = input.getCAS();
     
-    // just get it from preference store?!
-    // Should have a good way to display an error when the type is incorrect ...
+    String additionalSentenceTypes = store.getString(OpenNLPPreferenceConstants.ADDITIONAL_SENTENCE_TYPE);
     
     String text = cas.getDocumentText();
 
@@ -391,8 +398,11 @@ public class EntityContentProvider imple
       
       for (String typeName : sentenceTypeNames) {
         Type sentenceType = cas.getTypeSystem().getType(typeName.trim()); 
-      
-        // TODO: If type cannot be mapped, it throws a null pointer exception ...
+        
+        if (sentenceType == null) {
+          nameFinderView.setMessage("Sentence type does not exist in type system!");
+          return;
+        }
         
         FSIndex<AnnotationFS> sentenceAnnotations = cas
             .getAnnotationIndex(sentenceType);
@@ -434,6 +444,11 @@ public class EntityContentProvider imple
         
         Type nameType = cas.getTypeSystem().getType(nameTypeName); 
   
+        if (nameType == null) {
+          nameFinderView.setMessage("Name type " + nameTypeName + " does not exist in type system!");
+          return;
+        }
+        
         FSIndex<AnnotationFS> nameAnnotations = cas
             .getAnnotationIndex(nameType);
   
@@ -453,7 +468,7 @@ public class EntityContentProvider imple
       nameFinder.setTokens(tokens.toArray(new Span[tokens.size()]));
       nameFinder.setVerifiedNames(nameSpans.toArray(new Span[nameSpans.size()]));
       nameFinder.setModelPath(modelPathes, nameTypeNames);
-      
+      nameFinder.setSystem(true);
       nameFinder.schedule();
     }
   }

Modified: incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/namefinder/MultiModelNameFinder.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/namefinder/MultiModelNameFinder.java?rev=1182388&r1=1182387&r2=1182388&view=diff
==============================================================================
--- incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/namefinder/MultiModelNameFinder.java (original)
+++ incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/namefinder/MultiModelNameFinder.java Wed Oct 12 14:16:34 2011
@@ -94,7 +94,7 @@ public class MultiModelNameFinder implem
   // TODO: We need one per name finder instance ...
   private RestrictedSequencesValidator sequenceValidators[];
   
-  MultiModelNameFinder(String modelPathes[], String modelTypes[]) {
+  MultiModelNameFinder(String modelPathes[], String modelTypes[]) throws IOException {
     
     this.modelTypes = modelTypes;
     

Modified: incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/namefinder/NameFinderJob.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/namefinder/NameFinderJob.java?rev=1182388&r1=1182387&r2=1182388&view=diff
==============================================================================
--- incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/namefinder/NameFinderJob.java (original)
+++ incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/namefinder/NameFinderJob.java Wed Oct 12 14:16:34 2011
@@ -17,6 +17,7 @@
 
 package org.apache.opennlp.caseditor.namefinder;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -83,12 +84,20 @@ public class NameFinderJob extends Job {
 
   // maybe report result, through an interface?!
   // Note: Concurrency issue ... here! Editor might already be closed after model is loaded!
+  // The job change listener in the Entity Content Provider must handle that!
   @Override
   protected synchronized IStatus run(IProgressMonitor monitor) {
 
-    // lazy load model on first run ... how to lazy initialize multiple name finders?
+    
+    // TODO: Check if model path changed, compared to last run, if so reload
+    // TODO: Check if the model itself changed, compared to last run, if so reload
     if (nameFinder == null) {
-      nameFinder = new MultiModelNameFinder(modelPath, modelTypes);
+      try {
+        nameFinder = new MultiModelNameFinder(modelPath, modelTypes);
+      } catch (IOException e) {
+        return new Status(IStatus.CANCEL, OpenNLPPlugin.ID, 
+            "Failed to load model(s): " + e.getMessage());
+      }
     }
 
     if (nameFinder != null) {

Modified: incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/namefinder/NameFinderViewPage.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/namefinder/NameFinderViewPage.java?rev=1182388&r1=1182387&r2=1182388&view=diff
==============================================================================
--- incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/namefinder/NameFinderViewPage.java (original)
+++ incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/namefinder/NameFinderViewPage.java Wed Oct 12 14:16:34 2011
@@ -36,6 +36,7 @@ import org.eclipse.jface.viewers.TableVi
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Table;
 import org.eclipse.swt.widgets.TableColumn;
 import org.eclipse.ui.IActionBars;
@@ -43,6 +44,7 @@ import org.eclipse.ui.ISelectionListener
 import org.eclipse.ui.IWorkbenchPart;
 import org.eclipse.ui.actions.BaseSelectionListenerAction;
 import org.eclipse.ui.part.Page;
+import org.eclipse.ui.part.PageBook;
 
 
 // TODO: Selected entities should be highlighted in the annotation editor!
@@ -56,6 +58,9 @@ class NameFinderViewPage extends Page im
   
   private ICasEditor editor;
 
+  private PageBook book;
+
+  private Label messageLabel;
   private TableViewer entityList;
 
   NameFinderViewPage(ICasEditor editor, ICasDocument document) {
@@ -64,9 +69,12 @@ class NameFinderViewPage extends Page im
 
   public void createControl(Composite parent) {
     
-    getSite().getPage().addSelectionListener(this);
+    book = new PageBook(parent, SWT.NONE);
+    
+    messageLabel = new Label(book, SWT.NONE);
+    messageLabel.setText("Loading name finder models ...");
     
-    entityList = new TableViewer(parent, SWT.NONE);
+    entityList = new TableViewer(book, SWT.NONE);
     
     Table entityTable = entityList.getTable();
     entityTable.setHeaderVisible(true);
@@ -88,7 +96,7 @@ class NameFinderViewPage extends Page im
     typeColumn.setWidth(40);
     
     entityList.setLabelProvider(new EntityLabelProvider());
-    entityList.setContentProvider(new EntityContentProvider((AnnotationEditor) editor, new NameFinderJob(), entityList));
+    entityList.setContentProvider(new EntityContentProvider(this, (AnnotationEditor) editor, new NameFinderJob(), entityList));
     getSite().setSelectionProvider(entityList);
     
     entityList.setComparator(new EntityComperator());
@@ -122,6 +130,11 @@ class NameFinderViewPage extends Page im
 			}
 		}
 	});
+    
+    // Display the messageLabel after start up
+    book.showPage(messageLabel);
+    
+    getSite().getPage().addSelectionListener(this);
   }
 
   public void selectionChanged(IWorkbenchPart part, ISelection selection) {
@@ -149,13 +162,25 @@ class NameFinderViewPage extends Page im
   }
 
   public Control getControl() {
-    return entityList.getControl();
+    return book;
   }
 
   public void setFocus() {
-    entityList.getControl().setFocus();
+    getControl().setFocus();
   }
 
+  void setMessage(String message) {
+    
+    if (message != null) {
+      messageLabel.setText(message);
+      book.showPage(messageLabel);
+    }
+    else {
+      messageLabel.setText("");
+      book.showPage(entityList.getControl());
+    }
+  }
+  
   @Override
   public void setActionBars(IActionBars actionBars) {
     super.setActionBars(actionBars);

Modified: incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/sentdetect/SentenceDetectorJob.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/sentdetect/SentenceDetectorJob.java?rev=1182388&r1=1182387&r2=1182388&view=diff
==============================================================================
--- incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/sentdetect/SentenceDetectorJob.java (original)
+++ incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/sentdetect/SentenceDetectorJob.java Wed Oct 12 14:16:34 2011
@@ -73,7 +73,12 @@ public class SentenceDetectorJob extends
     
     // lazy load model
     if (sentenceDetector == null) {
-      InputStream modelIn = ModelUtil.openModelIn(modelPath);
+      InputStream modelIn;
+      try {
+        modelIn = ModelUtil.openModelIn(modelPath);
+      } catch (IOException e1) {
+        return new Status(IStatus.ERROR, OpenNLPPlugin.ID, "Failed to load sentence detector model!");
+      }
       
       try {
         SentenceModel model = new SentenceModel(modelIn);

Modified: incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/tokenize/TokenizerJob.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/tokenize/TokenizerJob.java?rev=1182388&r1=1182387&r2=1182388&view=diff
==============================================================================
--- incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/tokenize/TokenizerJob.java (original)
+++ incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/tokenize/TokenizerJob.java Wed Oct 12 14:16:34 2011
@@ -73,7 +73,12 @@ public class TokenizerJob extends Job {
       tokenizer = SimpleTokenizer.INSTANCE;
     } else if (OpenNLPPreferenceConstants.TOKENIZER_ALGO_STATISTICAL.equals(algorithm)) {
       if (tokenizer == null) {
-        InputStream modelIn = ModelUtil.openModelIn(modelPath);
+        InputStream modelIn;
+        try {
+          modelIn = ModelUtil.openModelIn(modelPath);
+        } catch (IOException e1) {
+          return new Status(IStatus.ERROR, OpenNLPPlugin.ID, "Failed to load tokenizer model!");
+        }
         
         try {
           TokenizerModel model = new TokenizerModel(modelIn);