clerezza-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tomm...@apache.org
Subject svn commit: r923088 - in /incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src: main/java/org/apache/clerezza/uima/utils/ main/resources/ test/java/org/apache/clerezza/uima/utils/
Date Mon, 15 Mar 2010 07:01:49 GMT
Author: tommaso
Date: Mon Mar 15 07:01:48 2010
New Revision: 923088

URL: http://svn.apache.org/viewvc?rev=923088&view=rev
Log:
[CLEREZZA-124] - ExternalServicesFacadeTest added, added possibility of adding overriding
defalut config parameters (to input the API key for external services)

Added:
    incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/test/java/org/apache/clerezza/uima/utils/ExternalServicesFacadeTest.java
Modified:
    incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/main/java/org/apache/clerezza/uima/utils/AEProvider.java
    incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/main/java/org/apache/clerezza/uima/utils/ExternalServicesFacade.java
    incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/main/java/org/apache/clerezza/uima/utils/UIMAExecutor.java
    incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/main/java/org/apache/clerezza/uima/utils/UIMAUtils.java
    incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/main/resources/TextLanguageDetectionAEDescriptor.xml

Modified: incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/main/java/org/apache/clerezza/uima/utils/AEProvider.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/main/java/org/apache/clerezza/uima/utils/AEProvider.java?rev=923088&r1=923087&r2=923088&view=diff
==============================================================================
--- incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/main/java/org/apache/clerezza/uima/utils/AEProvider.java
(original)
+++ incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/main/java/org/apache/clerezza/uima/utils/AEProvider.java
Mon Mar 15 07:01:48 2010
@@ -1,19 +1,21 @@
 package org.apache.clerezza.uima.utils;
 
 import java.net.URL;
+import java.util.Map;
 
 import org.apache.uima.UIMAFramework;
 import org.apache.uima.analysis_engine.AnalysisEngine;
+import org.apache.uima.analysis_engine.AnalysisEngineDescription;
 import org.apache.uima.resource.ResourceInitializationException;
 import org.apache.uima.resource.ResourceSpecifier;
+import org.apache.uima.resource.metadata.ConfigurationParameterSettings;
+import org.apache.uima.resource.metadata.impl.ConfigurationParameterSettings_impl;
 import org.apache.uima.util.XMLInputSource;
+import org.apache.uima.util.XMLParser;
 
 /**
  * provide the AnalysisEngine using the default descriptor or using a custom descriptor (absolute)
  * path
- * 
- * @author tommaso
- * 
  */
 public class AEProvider {
 
@@ -33,9 +35,10 @@ public class AEProvider {
 
   /**
    * get an Analysis Engine using the default path (specified in constructor)
-   * 
+   *
    * @return
    * @throws ResourceInitializationException
+   *
    */
   public AnalysisEngine getDefaultAE() throws ResourceInitializationException {
     return getAE(defaultXMLPath);
@@ -43,10 +46,11 @@ public class AEProvider {
 
   /**
    * get an Analysis Engine from a different descriptor path
-   * 
+   *
    * @param filePath
    * @return
    * @throws ResourceInitializationException
+   *
    */
   public AnalysisEngine getAE(String filePath) throws ResourceInitializationException {
     AnalysisEngine ae = null;
@@ -55,7 +59,6 @@ public class AEProvider {
       URL url = this.getClass().getClassLoader().getResource(filePath);
       XMLInputSource in = new XMLInputSource(url);
       ResourceSpecifier specifier = UIMAFramework.getXMLParser().parseResourceSpecifier(in);
-
       // create AE here
       ae = UIMAFramework.produceAnalysisEngine(specifier);
     } catch (Exception e) {
@@ -64,4 +67,27 @@ public class AEProvider {
 
     return ae;
   }
+
+  public AnalysisEngine getAE(String filePath, Map<String, Object> parameterSettings)
throws ResourceInitializationException {
+    AnalysisEngine ae = null;
+    // get Resource Specifier from XML file
+    try {
+      URL url = this.getClass().getClassLoader().getResource(filePath);
+      XMLInputSource in = new XMLInputSource(url);
+
+      // eventually add/override descriptor's configuration parameters
+      AnalysisEngineDescription desc = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(in);
+      for (String parameter : parameterSettings.keySet()) {
+        desc.getAnalysisEngineMetaData().getConfigurationParameterSettings().setParameterValue(parameter,parameterSettings.get(parameter));
+      }
+
+      // create AE here
+      ae = UIMAFramework.produceAnalysisEngine(desc);
+
+    } catch (Exception e) {
+      throw new ResourceInitializationException(e);
+    }
+
+    return ae;
+  }
 }
\ No newline at end of file

Modified: incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/main/java/org/apache/clerezza/uima/utils/ExternalServicesFacade.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/main/java/org/apache/clerezza/uima/utils/ExternalServicesFacade.java?rev=923088&r1=923087&r2=923088&view=diff
==============================================================================
--- incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/main/java/org/apache/clerezza/uima/utils/ExternalServicesFacade.java
(original)
+++ incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/main/java/org/apache/clerezza/uima/utils/ExternalServicesFacade.java
Mon Mar 15 07:01:48 2010
@@ -1,24 +1,26 @@
 package org.apache.clerezza.uima.utils;
 
-import java.util.ArrayList;
-import java.util.List;
-
 import org.apache.uima.UIMAException;
 import org.apache.uima.alchemy.ts.keywords.KeywordFS;
 import org.apache.uima.alchemy.ts.language.LanguageFS;
 import org.apache.uima.cas.FeatureStructure;
 import org.apache.uima.jcas.JCas;
+import org.apache.uima.jcas.tcas.Annotation;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 /**
  * Facade for querying UIMA external services
- * 
- * @author tommaso
- * 
  */
 public class ExternalServicesFacade {
 
   private UIMAExecutor uimaExecutor;
 
+  private Map<String, Object> parameterSetting = new HashMap<String, Object>();
+
   public ExternalServicesFacade() {
     this.uimaExecutor = new UIMAExecutor("ExtServicesAE.xml").withResults();
   }
@@ -29,7 +31,7 @@ public class ExternalServicesFacade {
 
     try {
       // analyze the document
-      uimaExecutor.analyzeDocument(document,"TextKeywordExtractionAEDescriptor.xml");
+      uimaExecutor.analyzeDocument(document, "TextKeywordExtractionAEDescriptor.xml", getParameterSetting());
 
       tags = new ArrayList<String>();
 
@@ -56,7 +58,7 @@ public class ExternalServicesFacade {
     try {
 
       // analyze the document
-      uimaExecutor.analyzeDocument(document,"TextLanguageDetectionAEDescriptor.xml");
+      uimaExecutor.analyzeDocument(document, "TextLanguageDetectionAEDescriptor.xml", getParameterSetting());
 
       // get execution results
       JCas jcas = uimaExecutor.getResults();
@@ -73,4 +75,39 @@ public class ExternalServicesFacade {
     return language;
   }
 
+  public List<String> getCalaisEntities(String document) throws UIMAException {
+
+    List<String> entities = new ArrayList<String>();
+
+    try {
+
+      // analyze the document
+      uimaExecutor.analyzeDocument(document, "OpenCalaisAnnotator.xml", getParameterSetting());
+
+      // get execution results
+      JCas jcas = uimaExecutor.getResults();
+
+      // extract entities using OpenCalaisAnnotator
+      List<Annotation> calaisAnnotations = UIMAUtils.getAllAnnotationsOfType(org.apache.uima.calais.BaseType.type,
jcas);
+
+      // TODO should change return value to a list of richer type wrapping UIMA Annotations
+      for (Annotation calaisAnnotation : calaisAnnotations) {
+        entities.add(calaisAnnotation.getCoveredText());
+      }
+
+    } catch (Exception e) {
+      throw new UIMAException(e);
+    }
+    return entities;
+  }
+
+  public Map<String, Object> getParameterSetting() {
+    return parameterSetting;
+  }
+
+  public void setParameterSetting(Map<String, Object> parameterSetting) {
+    this.parameterSetting = parameterSetting;
+  }
+
+
 }

Modified: incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/main/java/org/apache/clerezza/uima/utils/UIMAExecutor.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/main/java/org/apache/clerezza/uima/utils/UIMAExecutor.java?rev=923088&r1=923087&r2=923088&view=diff
==============================================================================
--- incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/main/java/org/apache/clerezza/uima/utils/UIMAExecutor.java
(original)
+++ incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/main/java/org/apache/clerezza/uima/utils/UIMAExecutor.java
Mon Mar 15 07:01:48 2010
@@ -4,6 +4,7 @@ import org.apache.clerezza.uima.utils.ex
 import org.apache.uima.analysis_engine.AnalysisEngine;
 import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
 import org.apache.uima.jcas.JCas;
+import org.apache.uima.resource.ResourceConfigurationException;
 import org.apache.uima.resource.ResourceInitializationException;
 
 import java.util.HashMap;
@@ -12,7 +13,6 @@ import java.util.Map;
 /**
  * Executes a UIMA pipeline
  *
- * @author tommaso
  */
 public class UIMAExecutor {
 
@@ -73,11 +73,7 @@ public class UIMAExecutor {
      */
     public void analyzeDocument(String doc, String xmlPath, Map<String, Object> aeParameterSettings)
throws AnalysisEngineProcessException {
         try {
-            AnalysisEngine engine = aeProvider.getAE(xmlPath);
-            // set runtime parameters
-            for (String parameter : aeParameterSettings.keySet()) {
-                engine.setConfigParameterValue(parameter, aeParameterSettings.get(parameter));
-            }
+            AnalysisEngine engine = aeProvider.getAE(xmlPath,aeParameterSettings);
             this.executeAE(engine, doc);
         } catch (ResourceInitializationException e) {
             throw new AnalysisEngineProcessException(e);

Modified: incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/main/java/org/apache/clerezza/uima/utils/UIMAUtils.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/main/java/org/apache/clerezza/uima/utils/UIMAUtils.java?rev=923088&r1=923087&r2=923088&view=diff
==============================================================================
--- incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/main/java/org/apache/clerezza/uima/utils/UIMAUtils.java
(original)
+++ incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/main/java/org/apache/clerezza/uima/utils/UIMAUtils.java
Mon Mar 15 07:01:48 2010
@@ -7,13 +7,13 @@ import org.apache.clerezza.uima.utils.ex
 import org.apache.clerezza.uima.utils.exception.NotSingletonFeatureStructureException;
 import org.apache.uima.cas.FSIterator;
 import org.apache.uima.cas.FeatureStructure;
+import org.apache.uima.cas.text.AnnotationIndex;
 import org.apache.uima.jcas.JCas;
+import org.apache.uima.jcas.tcas.Annotation;
 
 /**
  * Utilities for managing UIMA data and features
  * 
- * @author tommaso
- * 
  */
 public class UIMAUtils {
 
@@ -46,4 +46,13 @@ public class UIMAUtils {
     return featureStructure;
   }
 
+  public static List<Annotation> getAllAnnotationsOfType(int type, JCas cas) {
+      List<Annotation> foundAnnotations = new ArrayList<Annotation>();
+      AnnotationIndex<Annotation> annotationIndex = cas.getAnnotationIndex(type);
+      for (Annotation annotation : annotationIndex) {
+          foundAnnotations.add(annotation);
+      }
+      return foundAnnotations;
+  }
+
 }

Modified: incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/main/resources/TextLanguageDetectionAEDescriptor.xml
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/main/resources/TextLanguageDetectionAEDescriptor.xml?rev=923088&r1=923087&r2=923088&view=diff
==============================================================================
--- incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/main/resources/TextLanguageDetectionAEDescriptor.xml
(original)
+++ incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/main/resources/TextLanguageDetectionAEDescriptor.xml
Mon Mar 15 07:01:48 2010
@@ -4,7 +4,7 @@
   <primitive>true</primitive>
   <annotatorImplementationName>org.apache.uima.alchemy.annotator.TextLanguageDetectionAnnotator</annotatorImplementationName>
   <analysisEngineMetaData>
-    <name>TextCategorizationAEDescriptor</name>
+    <name>TextLanguageDetectionAEDescriptor</name>
     <description/>
     <version>1.0</version>
     <vendor/>

Added: incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/test/java/org/apache/clerezza/uima/utils/ExternalServicesFacadeTest.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/test/java/org/apache/clerezza/uima/utils/ExternalServicesFacadeTest.java?rev=923088&view=auto
==============================================================================
--- incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/test/java/org/apache/clerezza/uima/utils/ExternalServicesFacadeTest.java
(added)
+++ incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.uima/org.apache.clerezza.uima.utils/src/test/java/org/apache/clerezza/uima/utils/ExternalServicesFacadeTest.java
Mon Mar 15 07:01:48 2010
@@ -0,0 +1,76 @@
+package org.apache.clerezza.uima.utils;
+
+import org.apache.uima.UIMAException;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.Assert.*;
+
+/**
+ * Testcase for {@link ExternalServicesFacade}
+ */
+public class ExternalServicesFacadeTest {
+
+    private static final String AN_ENGLISH_TEXT = "this is a document supposed to be recognized
as written in the language of Queen Elizabeth";
+
+    @Test
+    public void getLanguageTest() {
+        try {
+            ExternalServicesFacade externalServicesFacade = new ExternalServicesFacade();
+            Map<String, Object> parameterSettings = new HashMap<String, Object>();
+            parameterSettings.put("apikey", "04490000a72fe7ec5cb3497f14e77f338c86f2fe");
+            externalServicesFacade.setParameterSetting(parameterSettings);
+            String language = externalServicesFacade.getLanguage(AN_ENGLISH_TEXT);
+            assertEquals(language, "english");
+        } catch (Exception e) {
+            e.printStackTrace();
+            fail(e.getLocalizedMessage());
+        }
+
+    }
+
+
+    @Test
+    public void getTagsTest() {
+        try {
+            ExternalServicesFacade externalServicesFacade = new ExternalServicesFacade();
+            Map<String, Object> parameterSettings = new HashMap<String, Object>();
+            parameterSettings.put("apikey", "04490000a72fe7ec5cb3497f14e77f338c86f2fe");
+            externalServicesFacade.setParameterSetting(parameterSettings);
+            List<String> tags = externalServicesFacade.getTags(AN_ENGLISH_TEXT);
+            assertTrue(tags != null);
+            assertTrue(!tags.isEmpty());
+            assertTrue(tags.size() == 1);
+            assertTrue(tags.get(0).equals("Queen Elizabeth"));
+        } catch (Exception e) {
+            e.printStackTrace();
+            fail(e.getLocalizedMessage());
+        }
+
+    }
+
+    @Test
+    public void getCalaisEntities() {
+        try {
+            ExternalServicesFacade externalServicesFacade = new ExternalServicesFacade();
+            String licenseId = "g6h9zamsdtwhb93nc247ecrs";
+            Map<String, Object> parameterSettings = new HashMap<String, Object>();
+            parameterSettings.put("licenseID", licenseId);
+            externalServicesFacade.setParameterSetting(parameterSettings);
+            List<String> entities = externalServicesFacade.getCalaisEntities(AN_ENGLISH_TEXT);
+            assertTrue(entities != null);
+            assertTrue(!entities.isEmpty());
+            assertTrue(entities.size() == 1);
+            assertTrue(entities.get(0).equals("Queen Elizabeth"));
+        }
+        catch (Exception e) {
+            e.printStackTrace();
+            fail(e.getLocalizedMessage());
+        }
+    }
+
+}



Mime
View raw message