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 2012/04/24 23:39:06 UTC

svn commit: r1330026 - in /opennlp/sandbox/tagging-server: ./ src/main/java/org/apache/opennlp/tagging_server/

Author: joern
Date: Tue Apr 24 21:39:05 2012
New Revision: 1330026

URL: http://svn.apache.org/viewvc?rev=1330026&view=rev
Log:
OPENNLP-480 First draft of POS Tagging service.

Added:
    opennlp/sandbox/tagging-server/src/main/java/org/apache/opennlp/tagging_server/ModelUtil.java   (with props)
    opennlp/sandbox/tagging-server/src/main/java/org/apache/opennlp/tagging_server/POSTaggerResource.java   (with props)
    opennlp/sandbox/tagging-server/src/main/java/org/apache/opennlp/tagging_server/TaggingServerApplication.java   (with props)
    opennlp/sandbox/tagging-server/src/main/java/org/apache/opennlp/tagging_server/TaggingServerBundle.java   (with props)
Modified:
    opennlp/sandbox/tagging-server/pom.xml

Modified: opennlp/sandbox/tagging-server/pom.xml
URL: http://svn.apache.org/viewvc/opennlp/sandbox/tagging-server/pom.xml?rev=1330026&r1=1330025&r2=1330026&view=diff
==============================================================================
--- opennlp/sandbox/tagging-server/pom.xml (original)
+++ opennlp/sandbox/tagging-server/pom.xml Tue Apr 24 21:39:05 2012
@@ -51,39 +51,40 @@
 	
 	<dependencies>
 	
+		<dependency>
+		  <groupId>org.apache.opennlp</groupId>
+		  <artifactId>opennlp-tools</artifactId>
+		  <version>1.5.2-incubating</version>
+		</dependency>
+		
 	    <dependency>
       		<groupId>javax.servlet</groupId>
       		<artifactId>servlet-api</artifactId>
       		<version>2.5</version>
-      		<scope>provided</scope>
     	</dependency>
     
         <dependency>
             <groupId>org.osgi</groupId>
             <artifactId>org.osgi.core</artifactId>
             <version>4.2.0</version>
-            <scope>provided</scope>
         </dependency>
         
         <dependency>
             <groupId>org.osgi</groupId>
             <artifactId>org.osgi.compendium</artifactId>
             <version>4.2.0</version>
-            <scope>provided</scope>
         </dependency>
 
 		<dependency>
 			<groupId>com.sun.jersey</groupId>
 			<artifactId>jersey-servlet</artifactId>
 			<version>1.12</version>
-			<scope>provided</scope>
 		</dependency>
 		
 		<dependency>
 		    <groupId>com.sun.jersey</groupId>
 		    <artifactId>jersey-json</artifactId>
 		    <version>1.12</version>
-		    <scope>provided</scope>
 		</dependency>
 
 		<dependency>
@@ -131,7 +132,7 @@
                 <configuration>
                     <manifestLocation>${project.build.directory}/META-INF</manifestLocation>
                     <instructions>
-                        <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
+                        <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                         <Import-Package>
                         	!javax.persistence.*,
                         	!javax.servlet.annotation,
@@ -157,8 +158,6 @@
                         	com.sun.jersey.json.*,
                         	org.codehaus.jackson.*,
                         	org.codehaus.jackson.xc,
-                        	org.apache.opennlp.corpus_server,
-                        	org.apache.derby.jdbc,
                         	com.sun.jersey.api.core,
                         	com.sun.jersey.spi.container.servlet</Import-Package>
                         <!-- Import-Package>!com.ibm.jvm,!com.sun.jdmk.comm,!com.sun.net.httpserver,*,com.sun.jersey.api.core,com.sun.jersey.spi.container.servlet</Import-Package-->
@@ -169,6 +168,6 @@
                     </instructions>
                 </configuration>
             </plugin>
-		</plugins>
+	      </plugins>
 	</build>
 </project>
\ No newline at end of file

Added: opennlp/sandbox/tagging-server/src/main/java/org/apache/opennlp/tagging_server/ModelUtil.java
URL: http://svn.apache.org/viewvc/opennlp/sandbox/tagging-server/src/main/java/org/apache/opennlp/tagging_server/ModelUtil.java?rev=1330026&view=auto
==============================================================================
--- opennlp/sandbox/tagging-server/src/main/java/org/apache/opennlp/tagging_server/ModelUtil.java (added)
+++ opennlp/sandbox/tagging-server/src/main/java/org/apache/opennlp/tagging_server/ModelUtil.java Tue Apr 24 21:39:05 2012
@@ -0,0 +1,58 @@
+/*
+ * 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.tagging_server;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.ServiceReference;
+
+public class ModelUtil {
+
+  private ModelUtil() {
+  }
+  
+  static ServiceReference getModelService(Class<?> modelClazz) {
+    Bundle bundle = FrameworkUtil.getBundle(ModelUtil.class);
+    BundleContext context = bundle.getBundleContext();
+
+    return context.getServiceReference(modelClazz.getName());
+  }
+  
+  @SuppressWarnings("unchecked")
+  static <T> T getModel(ServiceReference modelService, Class<T> modelClazz) {
+    
+    T model;
+    if (modelService != null) {
+      BundleContext context = modelService.getBundle().getBundleContext();
+      model = (T) context.getService(modelService);
+    }
+    else {
+      throw new RuntimeException("Model does not exist!");
+    }
+    
+    return model;
+  }
+
+  public static void releaseModel(ServiceReference modelService) {
+    if (modelService != null) {
+      BundleContext context = modelService.getBundle().getBundleContext();
+      context.ungetService(modelService);
+    }
+  }
+}

Propchange: opennlp/sandbox/tagging-server/src/main/java/org/apache/opennlp/tagging_server/ModelUtil.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: opennlp/sandbox/tagging-server/src/main/java/org/apache/opennlp/tagging_server/POSTaggerResource.java
URL: http://svn.apache.org/viewvc/opennlp/sandbox/tagging-server/src/main/java/org/apache/opennlp/tagging_server/POSTaggerResource.java?rev=1330026&view=auto
==============================================================================
--- opennlp/sandbox/tagging-server/src/main/java/org/apache/opennlp/tagging_server/POSTaggerResource.java (added)
+++ opennlp/sandbox/tagging-server/src/main/java/org/apache/opennlp/tagging_server/POSTaggerResource.java Tue Apr 24 21:39:05 2012
@@ -0,0 +1,59 @@
+/*
+ * 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.tagging_server;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+
+import opennlp.tools.postag.POSModel;
+import opennlp.tools.postag.POSTagger;
+import opennlp.tools.postag.POSTaggerME;
+
+import org.osgi.framework.ServiceReference;
+
+@Path("/postagger")
+public class POSTaggerResource {
+
+  @POST
+  @Consumes(MediaType.APPLICATION_JSON)
+  @Produces(MediaType.APPLICATION_JSON)
+  @Path("_tag")
+  // @QueryParam("lang") String lang,
+  // @QueryParam("model") String modelName
+  public String[][] tag(String document[][]) {
+    
+    ServiceReference modelService = ModelUtil.getModelService(POSModel.class);
+    
+    try {
+      String[][] tags = new String[document.length][];
+      
+      POSTagger tagger = new POSTaggerME(ModelUtil.getModel(modelService, POSModel.class));
+      for (int i = 0; i < document.length; i++) {
+        tags[i] = tagger.tag(document[i]);
+      }
+      
+      return tags;
+    }
+    finally {
+      ModelUtil.releaseModel(modelService);
+    }
+  }
+}

Propchange: opennlp/sandbox/tagging-server/src/main/java/org/apache/opennlp/tagging_server/POSTaggerResource.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: opennlp/sandbox/tagging-server/src/main/java/org/apache/opennlp/tagging_server/TaggingServerApplication.java
URL: http://svn.apache.org/viewvc/opennlp/sandbox/tagging-server/src/main/java/org/apache/opennlp/tagging_server/TaggingServerApplication.java?rev=1330026&view=auto
==============================================================================
--- opennlp/sandbox/tagging-server/src/main/java/org/apache/opennlp/tagging_server/TaggingServerApplication.java (added)
+++ opennlp/sandbox/tagging-server/src/main/java/org/apache/opennlp/tagging_server/TaggingServerApplication.java Tue Apr 24 21:39:05 2012
@@ -0,0 +1,33 @@
+/*
+ * 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.tagging_server;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.ws.rs.core.Application;
+
+public class TaggingServerApplication extends Application {
+  
+  @Override
+  public Set<Class<?>> getClasses() {
+    Set<Class<?>> result = new HashSet<Class<?>>();
+    result.add(POSTaggerResource.class);
+    return result;
+  }
+}

Propchange: opennlp/sandbox/tagging-server/src/main/java/org/apache/opennlp/tagging_server/TaggingServerApplication.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: opennlp/sandbox/tagging-server/src/main/java/org/apache/opennlp/tagging_server/TaggingServerBundle.java
URL: http://svn.apache.org/viewvc/opennlp/sandbox/tagging-server/src/main/java/org/apache/opennlp/tagging_server/TaggingServerBundle.java?rev=1330026&view=auto
==============================================================================
--- opennlp/sandbox/tagging-server/src/main/java/org/apache/opennlp/tagging_server/TaggingServerBundle.java (added)
+++ opennlp/sandbox/tagging-server/src/main/java/org/apache/opennlp/tagging_server/TaggingServerBundle.java Tue Apr 24 21:39:05 2012
@@ -0,0 +1,99 @@
+/*
+ * 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.tagging_server;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import javax.servlet.ServletException;
+
+import opennlp.tools.postag.POSModel;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.http.HttpService;
+import org.osgi.service.http.NamespaceException;
+import org.osgi.util.tracker.ServiceTracker;
+
+import com.sun.jersey.spi.container.servlet.ServletContainer;
+
+//import com.sun.jersey.spi.container.servlet.ServletContainer;
+
+public class TaggingServerBundle implements BundleActivator {
+
+  private BundleContext context;
+  
+  private ServiceTracker tracker;
+  
+  @Override
+  public void start(BundleContext context) throws Exception {
+    this.context = context;
+    
+    // For testing register some models here ... !
+    // Load them from class path ...
+    context.registerService(POSModel.class.getName(), 
+        new POSModel(TaggingServerBundle.class.getResourceAsStream("/en-pos-maxent.bin")),null);
+    
+    // Register the Jersey servlet
+    this.tracker = new ServiceTracker(context, HttpService.class.getName(), null) {
+
+      private HttpService httpService;
+
+      @Override
+      public Object addingService(ServiceReference serviceRef) {
+        httpService = (HttpService)super.addingService(serviceRef);
+         
+        Dictionary<String, String> jerseyServletParams = new Hashtable<String, String>();
+        jerseyServletParams.put("javax.ws.rs.Application", TaggingServerApplication.class.getName());
+        jerseyServletParams.put("com.sun.jersey.api.json.POJOMappingFeature", "true");
+        
+        try {
+          httpService.registerServlet("/rest", new ServletContainer(), jerseyServletParams, null);
+        } catch (ServletException e) {
+          throw new RuntimeException(e);
+        } catch (NamespaceException e) {
+          throw new RuntimeException(e);
+        }
+        
+        return httpService;
+      }
+
+      @Override
+      public void removedService(ServiceReference ref, Object service) {
+        if (httpService == service) {
+
+          httpService.unregister("/rest");
+
+          httpService = null;
+        }
+        
+        super.removedService(ref, service);
+      }
+    };
+
+    this.tracker.open();
+  }
+
+  @Override
+  public void stop(BundleContext context) throws Exception {
+    this.context = null;
+
+    tracker.close();
+  }
+}

Propchange: opennlp/sandbox/tagging-server/src/main/java/org/apache/opennlp/tagging_server/TaggingServerBundle.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain