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/05/25 15:46:14 UTC

svn commit: r1127530 - in /incubator/opennlp/trunk/opennlp-tools/src: main/java/opennlp/tools/util/featuregen/ test/java/opennlp/tools/util/featuregen/ test/resources/opennlp/tools/util/featuregen/

Author: joern
Date: Wed May 25 13:46:13 2011
New Revision: 1127530

URL: http://svn.apache.org/viewvc?rev=1127530&view=rev
Log:
OPENNLP-17 Added support for custom user defined feature generator classes

Added:
    incubator/opennlp/trunk/opennlp-tools/src/test/resources/opennlp/tools/util/featuregen/CustomClassLoading.xml   (with props)
Modified:
    incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/featuregen/GeneratorFactory.java
    incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/util/featuregen/GeneratorFactoryTest.java

Modified: incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/featuregen/GeneratorFactory.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/featuregen/GeneratorFactory.java?rev=1127530&r1=1127529&r2=1127530&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/featuregen/GeneratorFactory.java (original)
+++ incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/featuregen/GeneratorFactory.java Wed May 25 13:46:13 2011
@@ -406,6 +406,46 @@ public class GeneratorFactory {
     }
   }
 
+  static class CustomFeatureGeneratorFactory implements XmlFeatureGeneratorFactory {
+
+    public AdaptiveFeatureGenerator create(Element generatorElement,
+        FeatureGeneratorResourceProvider resourceManager) throws InvalidFormatException {
+      
+      String featureGeneratorClassName = generatorElement.getAttribute("class");
+      
+      Class<?> featureGenClass;
+      try {
+        featureGenClass = Class.forName(featureGeneratorClassName);
+      } catch (ClassNotFoundException e) {
+        throw new NoClassDefFoundError(e.getMessage());
+      }
+      
+      // TODO: How to inject configuration?
+      // TODO: How to provide access to resources?
+      
+      // Special interface which defines configure method?!
+      // public interface CustomFeatureGenerator {
+      //   void initialize(Map<String, String>, FeatureGeneratoreResourceProvider)
+      //       throws InvalidFormatException;
+      // }
+      
+      AdaptiveFeatureGenerator generator = null;
+      try {
+        generator = (AdaptiveFeatureGenerator) featureGenClass.newInstance();
+      } catch (InstantiationException e) {
+        throw new InvalidFormatException("Failed to instantiate custom class!", e);
+      } catch (IllegalAccessException e) {
+        throw new InvalidFormatException("Failed to instantiate custom class!", e);
+      }
+      
+      return generator;
+    }
+
+    static void register(Map<String, XmlFeatureGeneratorFactory> factoryMap) {
+      factoryMap.put("custom", new CustomFeatureGeneratorFactory());
+    }
+  }
+  
   private static Map<String, XmlFeatureGeneratorFactory> factories =
       new HashMap<String, XmlFeatureGeneratorFactory>();
 
@@ -422,6 +462,7 @@ public class GeneratorFactory {
     BigramNameFeatureGeneratorFactory.register(factories);
     TokenPatternFeatureGeneratorFactory.register(factories);
     WindowFeatureGeneratorFactory.register(factories);
+    CustomFeatureGeneratorFactory.register(factories);
   }
 
   /**

Modified: incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/util/featuregen/GeneratorFactoryTest.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/util/featuregen/GeneratorFactoryTest.java?rev=1127530&r1=1127529&r2=1127530&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/util/featuregen/GeneratorFactoryTest.java (original)
+++ incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/util/featuregen/GeneratorFactoryTest.java Wed May 25 13:46:13 2011
@@ -24,28 +24,20 @@ import java.util.ArrayList;
 import java.util.Collection;
 
 import org.junit.Assert;
-import org.junit.Before;
 import org.junit.Test;
 
 import opennlp.tools.util.InvalidFormatException;
 
 public class GeneratorFactoryTest {
 
-  private InputStream generatorDescriptorIn;
-
-  @Before
-  public void setUp() {
-
-    generatorDescriptorIn = getClass().getResourceAsStream(
+  @Test
+  public void testCreationWihtSimpleDescriptor() throws Exception {
+    InputStream generatorDescriptorIn = getClass().getResourceAsStream(
         "/opennlp/tools/util/featuregen/TestFeatureGeneratorConfig.xml");
-
+    
     // If this fails the generator descriptor could not be found
     // at the expected location
     Assert.assertNotNull(generatorDescriptorIn);
-  }
-
-  @Test
-  public void testCreation() throws Exception {
 
     Collection<String> expectedGenerators = new ArrayList<String>();
     expectedGenerators.add(OutcomePriorFeatureGenerator.class.getName());
@@ -68,6 +60,27 @@ public class GeneratorFactoryTest {
     Assert.assertEquals(0, expectedGenerators.size());
   }
   
+  @Test
+  public void testCreationWithCustomGenerator() throws Exception {
+    InputStream generatorDescriptorIn = getClass().getResourceAsStream(
+        "/opennlp/tools/util/featuregen/CustomClassLoading.xml");
+    
+    // If this fails the generator descriptor could not be found
+    // at the expected location
+    Assert.assertNotNull(generatorDescriptorIn);
+    
+    AggregatedFeatureGenerator aggregatedGenerator =
+      (AggregatedFeatureGenerator) GeneratorFactory.create(generatorDescriptorIn, null);
+    
+    Collection<AdaptiveFeatureGenerator> embeddedGenerator = aggregatedGenerator.getGenerators();
+    
+    Assert.assertEquals(1, embeddedGenerator.size());
+    
+    for (AdaptiveFeatureGenerator generator : embeddedGenerator) {
+      Assert.assertEquals(TokenFeatureGenerator.class.getName(), generator.getClass().getName());
+    }
+  }
+  
   /**
    * Tests the creation from a descriptor which contains an unkown element.
    * The creation should fail with an {@link InvalidFormatException}

Added: incubator/opennlp/trunk/opennlp-tools/src/test/resources/opennlp/tools/util/featuregen/CustomClassLoading.xml
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/test/resources/opennlp/tools/util/featuregen/CustomClassLoading.xml?rev=1127530&view=auto
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/test/resources/opennlp/tools/util/featuregen/CustomClassLoading.xml (added)
+++ incubator/opennlp/trunk/opennlp-tools/src/test/resources/opennlp/tools/util/featuregen/CustomClassLoading.xml Wed May 25 13:46:13 2011
@@ -0,0 +1,22 @@
+<!--
+	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.
+-->
+
+<generators>
+  <custom class="opennlp.tools.util.featuregen.TokenFeatureGenerator"/>
+</generators>
\ No newline at end of file

Propchange: incubator/opennlp/trunk/opennlp-tools/src/test/resources/opennlp/tools/util/featuregen/CustomClassLoading.xml
------------------------------------------------------------------------------
    svn:mime-type = text/plain