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/03 20:59:48 UTC

svn commit: r1164909 - in /incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor: ModelUtil.java namefinder/NameFinderJob.java sentdetect/SentenceDetectorJob.java

Author: joern
Date: Sat Sep  3 18:59:48 2011
New Revision: 1164909

URL: http://svn.apache.org/viewvc?rev=1164909&view=rev
Log:
OPENNLP-235 Added sentence detector job.

Added:
    incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/ModelUtil.java   (with props)
    incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/sentdetect/SentenceDetectorJob.java   (with props)
Modified:
    incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/namefinder/NameFinderJob.java

Added: 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=1164909&view=auto
==============================================================================
--- incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/ModelUtil.java (added)
+++ incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/ModelUtil.java Sat Sep  3 18:59:48 2011
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.opennlp.caseditor;
+
+import java.io.InputStream;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+
+public class ModelUtil {
+
+  // TODO: Loading from a ULR (file, http) should be possible
+  //       if the model is already loaded a time stamp should be
+  //       used to detect an updated model, if model is updated,
+  //       load it and replace the old one!
+  
+  public static InputStream openModelIn(String modelPath) {
+    InputStream modelIn = null;
+    IResource modelResource = ResourcesPlugin.getWorkspace().
+        getRoot().findMember(modelPath);
+    
+    if (modelResource instanceof IFile) {
+      IFile modelFile = (IFile) modelResource;
+      try {
+        modelIn = modelFile.getContents();
+      } catch (CoreException e) {
+        // TODO: Handle this exception correctly ...
+        e.printStackTrace();
+      }
+    }
+    
+    return modelIn;
+  }
+}

Propchange: incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/ModelUtil.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

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=1164909&r1=1164908&r2=1164909&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 Sat Sep  3 18:59:48 2011
@@ -32,6 +32,7 @@ import opennlp.tools.namefind.TokenNameF
 import opennlp.tools.util.Span;
 import opennlp.tools.util.featuregen.StringPattern;
 
+import org.apache.opennlp.caseditor.ModelUtil;
 import org.apache.opennlp.caseditor.OpenNLPPlugin;
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IResource;
@@ -131,23 +132,7 @@ public class NameFinderJob extends Job {
     // lazy load model on first run ...
     if (nameFinder == null) {
     	
-      // TODO: Loading from a ULR (file, http) should be possible
-      //       if the model is already loaded a time stamp should be
-      //       used to detect an updated model, if model is updated,
-      //       load it and replace the old one!
-    	
-      InputStream modelIn = null;
-      IResource modelResource = ResourcesPlugin.getWorkspace().getRoot().findMember(modelPath);
-      
-      if (modelResource instanceof IFile) {
-        IFile modelFile = (IFile) modelResource;
-        try {
-          modelIn = modelFile.getContents();
-        } catch (CoreException e) {
-          // TODO: Handle this exception correctly ...
-          e.printStackTrace();
-        }
-      }
+      InputStream modelIn = ModelUtil.openModelIn(modelPath);
       
       try {
         TokenNameFinderModel model = new TokenNameFinderModel(modelIn);

Added: 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=1164909&view=auto
==============================================================================
--- incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/sentdetect/SentenceDetectorJob.java (added)
+++ incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/sentdetect/SentenceDetectorJob.java Sat Sep  3 18:59:48 2011
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.opennlp.caseditor.sentdetect;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import opennlp.tools.sentdetect.SentenceDetectorME;
+import opennlp.tools.sentdetect.SentenceModel;
+import opennlp.tools.util.Span;
+
+import org.apache.opennlp.caseditor.ModelUtil;
+import org.apache.opennlp.caseditor.OpenNLPPlugin;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.jobs.Job;
+
+public class SentenceDetectorJob extends Job {
+
+  private SentenceDetectorME sentenceDetector;
+  
+  private String modelPath;
+  
+  private String text;
+  
+  private Span detectedSentences[];
+  
+  public SentenceDetectorJob() {
+    super("Sentence Detector Job");
+  }
+  
+  synchronized void setModelPath(String modelPath) {
+    this.modelPath = modelPath;
+  }
+
+  synchronized void setText(String text) {
+    this.text = text;
+  }
+  
+  // user can set a container annotation, e.g. 
+  
+  @Override
+  protected IStatus run(IProgressMonitor monitor) {
+    
+    // lazy load model
+    if (sentenceDetector == null) {
+      InputStream modelIn = ModelUtil.openModelIn(modelPath);
+      
+      try {
+        SentenceModel model = new SentenceModel(modelIn);
+        sentenceDetector = new SentenceDetectorME(model);
+      } catch (IOException e) {
+        e.printStackTrace();
+      } finally {
+        if (modelIn != null) {
+          try {
+            modelIn.close();
+          } catch (IOException e) {
+          }
+        }
+      }
+    }
+    
+    // do detection only within container annotation ...
+    
+    detectedSentences = sentenceDetector.sentPosDetect(text);
+    
+    return new Status(IStatus.OK, OpenNLPPlugin.ID, "OK");
+  }
+
+  // retrieve proposed annotations ...
+  Span[] getDetectedSentences() {
+    return detectedSentences;
+  }
+}

Propchange: incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/sentdetect/SentenceDetectorJob.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain