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/09/05 19:12:28 UTC

svn commit: r1165365 - in /incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/sentdetect: SentenceContentProvider.java SentenceDetectorJob.java SentenceDetectorViewPage.java

Author: joern
Date: Mon Sep  5 17:12:27 2011
New Revision: 1165365

URL: http://svn.apache.org/viewvc?rev=1165365&view=rev
Log:
OPENNLP-235 Added very basic sentence detector support.

Modified:
    incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/sentdetect/SentenceContentProvider.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/sentdetect/SentenceDetectorViewPage.java

Modified: incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/sentdetect/SentenceContentProvider.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/sentdetect/SentenceContentProvider.java?rev=1165365&r1=1165364&r2=1165365&view=diff
==============================================================================
--- incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/sentdetect/SentenceContentProvider.java (original)
+++ incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/sentdetect/SentenceContentProvider.java Mon Sep  5 17:12:27 2011
@@ -17,21 +17,78 @@
 
 package org.apache.opennlp.caseditor.sentdetect;
 
+import org.apache.opennlp.caseditor.namefinder.Entity;
+import org.apache.uima.caseditor.editor.ICasDocument;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.jobs.IJobChangeEvent;
+import org.eclipse.core.runtime.jobs.JobChangeAdapter;
 import org.eclipse.jface.viewers.IStructuredContentProvider;
+import org.eclipse.jface.viewers.TableViewer;
 import org.eclipse.jface.viewers.Viewer;
+import org.eclipse.swt.widgets.Display;
 
 public class SentenceContentProvider implements IStructuredContentProvider {
 
-  @Override
-  public void dispose() {
+  private ICasDocument document;
+  
+  private SentenceDetectorJob sentenceDetector;
+  
+  private TableViewer sentenceList;
+  
+  public SentenceContentProvider(SentenceDetectorJob sentenceDetector, TableViewer sentenceList) {
+    this.sentenceDetector = sentenceDetector;
+    this.sentenceList = sentenceList;
+    
+    sentenceDetector.addJobChangeListener(new JobChangeAdapter() {
+      public void done(final IJobChangeEvent event) {
+        Display.getDefault().asyncExec(new Runnable() {
+          
+          @Override
+          public void run() {
+            IStatus status = event.getResult();
+            
+            if (status.getSeverity() == IStatus.OK) {
+              
+              Entity sentences[] = SentenceContentProvider.this.
+                  sentenceDetector.getDetectedSentences();
+              
+              SentenceContentProvider.this.sentenceList.refresh();
+              SentenceContentProvider.this.sentenceList.add(sentences);
+            }
+          }
+        });
+      }
+    });
+    
   }
-
+  
   @Override
   public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
+    
+    if (oldInput != null) {
+      // Remove listeners ...
+    }
+    
+    if (newInput != null) {
+      document = (ICasDocument) newInput;
+    }
+  }
+  
+  void triggerSentenceDetector() {
+
+    // Add paragraph support ...
+    
+    sentenceDetector.setText(document.getCAS().getDocumentText());
+    sentenceDetector.schedule();
   }
+  
 
   @Override
   public Object[] getElements(Object inputElement) {
-    return null;
+    return new Object[0];
+  }
+  
+  @Override
+  public void dispose() {
   }
 }

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=1165365&r1=1165364&r2=1165365&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 Mon Sep  5 17:12:27 2011
@@ -26,6 +26,7 @@ import opennlp.tools.util.Span;
 
 import org.apache.opennlp.caseditor.ModelUtil;
 import org.apache.opennlp.caseditor.OpenNLPPlugin;
+import org.apache.opennlp.caseditor.namefinder.Entity;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
@@ -39,7 +40,7 @@ public class SentenceDetectorJob extends
   
   private String text;
   
-  private Span detectedSentences[];
+  private Entity detectedSentences[];
   
   public SentenceDetectorJob() {
     super("Sentence Detector Job");
@@ -79,13 +80,21 @@ public class SentenceDetectorJob extends
     
     // do detection only within container annotation ...
     
-    detectedSentences = sentenceDetector.sentPosDetect(text);
+    Span sentenceSpans[] = sentenceDetector.sentPosDetect(text);
+    double confidence[] = sentenceDetector.getSentenceProbabilities();
+    
+    detectedSentences = new Entity[sentenceSpans.length];
+    
+    for (int i = 0; i < sentenceSpans.length; i++) {
+      Span sentenceSpan = sentenceSpans[i];
+      detectedSentences[i] = new Entity(sentenceSpan.getStart(), sentenceSpan.getEnd(),
+          sentenceSpan.getCoveredText(text).toString(), confidence[i], false);
+    }
     
     return new Status(IStatus.OK, OpenNLPPlugin.ID, "OK");
   }
 
-  // retrieve proposed annotations ...
-  Span[] getDetectedSentences() {
+  Entity[] getDetectedSentences() {
     return detectedSentences;
   }
 }

Modified: incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/sentdetect/SentenceDetectorViewPage.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/sentdetect/SentenceDetectorViewPage.java?rev=1165365&r1=1165364&r2=1165365&view=diff
==============================================================================
--- incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/sentdetect/SentenceDetectorViewPage.java (original)
+++ incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/sentdetect/SentenceDetectorViewPage.java Mon Sep  5 17:12:27 2011
@@ -17,37 +17,134 @@
 
 package org.apache.opennlp.caseditor.sentdetect;
 
+import org.apache.opennlp.caseditor.OpenNLPPlugin;
+import org.apache.opennlp.caseditor.OpenNLPPreferenceConstants;
+import org.apache.opennlp.caseditor.namefinder.ConfirmAnnotationAction;
+import org.apache.opennlp.caseditor.namefinder.Entity;
+import org.apache.opennlp.caseditor.namefinder.EntityLabelProvider;
+import org.apache.uima.caseditor.editor.AnnotationEditor;
 import org.apache.uima.caseditor.editor.ICasEditor;
+import org.eclipse.jface.action.IMenuManager;
+import org.eclipse.jface.action.IStatusLineManager;
+import org.eclipse.jface.action.IToolBarManager;
+import org.eclipse.jface.preference.IPreferenceStore;
+import org.eclipse.jface.viewers.ISelectionChangedListener;
+import org.eclipse.jface.viewers.SelectionChangedEvent;
+import org.eclipse.jface.viewers.StructuredSelection;
+import org.eclipse.jface.viewers.TableViewer;
+import org.eclipse.jface.viewers.TableViewerColumn;
 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.actions.BaseSelectionListenerAction;
 import org.eclipse.ui.part.Page;
 
 public class SentenceDetectorViewPage extends Page {
   
   private ICasEditor editor;
   
-  private Label testLabel; 
+  private TableViewer sentenceList; 
+  
+  private SentenceContentProvider contentProvider;
+  
+  private String modelPath;
+  private String sentenceTypeName;
   
   public SentenceDetectorViewPage(ICasEditor editor) {
     this.editor = editor;
+   
+    IPreferenceStore store = OpenNLPPlugin.getDefault().getPreferenceStore();
+    modelPath = store.getString(OpenNLPPreferenceConstants.SENTENCE_DETECTOR_MODEL_PATH);
+    sentenceTypeName = store.getString(OpenNLPPreferenceConstants.SENTENCE_TYPE);
   }
 
   @Override
   public void createControl(Composite parent) {
-    // Lets start with a button to trigger
-    // the sentence detector in the background ...
-    testLabel = new Label(parent, SWT.NONE);
-    testLabel.setText("I am the sentence detector view :)");
+    sentenceList = new TableViewer(parent, SWT.NONE);
+    
+    Table entityTable = sentenceList.getTable();
+    entityTable.setHeaderVisible(true);
+    entityTable.setLinesVisible(true);
+    
+    TableViewerColumn confidenceViewerColumn = new TableViewerColumn(sentenceList, SWT.NONE);
+    TableColumn confidenceColumn = confidenceViewerColumn.getColumn();
+    confidenceColumn.setText("%");
+    confidenceColumn.setWidth(40);
+    
+    TableViewerColumn entityViewerColumn = new TableViewerColumn(sentenceList, SWT.NONE);
+    TableColumn entityColumn = entityViewerColumn.getColumn();
+    entityColumn.setText("Sentence");
+    entityColumn.setWidth(135);
+    
+    // TODO: Label provider needs support to display being and end of long texts ...
+    //       text in-between can be replaced by three dots.
+    sentenceList.setLabelProvider(new EntityLabelProvider());
+    
+    SentenceDetectorJob sentenceDetector = new SentenceDetectorJob();
+    
+    sentenceDetector.setModelPath(modelPath);
+    
+    contentProvider = new SentenceContentProvider(sentenceDetector, sentenceList);
+    
+    sentenceList.setContentProvider(contentProvider);
+    getSite().setSelectionProvider(sentenceList);
+    sentenceList.setInput(editor.getDocument());
+
+    sentenceList.addSelectionChangedListener(new ISelectionChangedListener() {
+
+      @Override
+      public void selectionChanged(SelectionChangedEvent event) {
+        // if confirmed, send selection event for FS
+        // else, do selectAndReveal
+        StructuredSelection selection = (StructuredSelection) event
+            .getSelection();
+
+        if (!selection.isEmpty()) {
+          Entity entity = (Entity) selection.getFirstElement();
+
+          if (entity.isConfirmed()) {
+            // TODO: Send corresponding annotation selection event ...
+          } else {
+            if (editor instanceof AnnotationEditor) {
+              ((AnnotationEditor) editor).selectAndReveal(
+                  entity.getBeginIndex(),
+                  entity.getEndIndex() - entity.getBeginIndex());
+            }
+          }
+        }
+      }
+    });
   }
 
   @Override
   public Control getControl() {
-    return testLabel;
+    return sentenceList.getTable();
   }
 
   @Override
   public void setFocus() {
+    sentenceList.getTable().setFocus();
+  }
+  
+  @Override
+  public void makeContributions(IMenuManager menuManager,
+      IToolBarManager toolBarManager, IStatusLineManager statusLineManager) {
+    super.makeContributions(menuManager, toolBarManager, statusLineManager);
+    
+    BaseSelectionListenerAction detectAction = new BaseSelectionListenerAction("Detect") {
+      @Override
+      public void run() {
+        contentProvider.triggerSentenceDetector();
+      }
+    };
+        
+    toolBarManager.add(detectAction);
+    
+    BaseSelectionListenerAction confirmAction =
+        new ConfirmAnnotationAction(sentenceList, editor.getDocument(), sentenceTypeName);
+    getSite().getSelectionProvider().addSelectionChangedListener(confirmAction); // need also to unregister!!!!
+    toolBarManager.add(confirmAction);
   }
 }