You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ctakes.apache.org by tm...@apache.org on 2021/06/18 23:53:12 UTC

svn commit: r1890898 - in /ctakes/trunk/ctakes-web-rest/src/main/java/org/apache/ctakes/rest: service/CtakesRestController.java util/JCasFormatter.java

Author: tmill
Date: Fri Jun 18 23:53:12 2021
New Revision: 1890898

URL: http://svn.apache.org/viewvc?rev=1890898&view=rev
Log:
Added xmi output format and a filtered json format to the ctakes-web-rest module.

Modified:
    ctakes/trunk/ctakes-web-rest/src/main/java/org/apache/ctakes/rest/service/CtakesRestController.java
    ctakes/trunk/ctakes-web-rest/src/main/java/org/apache/ctakes/rest/util/JCasFormatter.java

Modified: ctakes/trunk/ctakes-web-rest/src/main/java/org/apache/ctakes/rest/service/CtakesRestController.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-web-rest/src/main/java/org/apache/ctakes/rest/service/CtakesRestController.java?rev=1890898&r1=1890897&r2=1890898&view=diff
==============================================================================
--- ctakes/trunk/ctakes-web-rest/src/main/java/org/apache/ctakes/rest/service/CtakesRestController.java (original)
+++ ctakes/trunk/ctakes-web-rest/src/main/java/org/apache/ctakes/rest/service/CtakesRestController.java Fri Jun 18 23:53:12 2021
@@ -74,18 +74,19 @@ public class CtakesRestController {
         }
         final PipelineRunner runner = _pipelineRunners.get(pipeline);
 
-        boolean summarize=true;
         if(outputParam.isPresent()){
-            if(outputParam.get().toLowerCase().equals("full")){
-                summarize=false;
+            String format = outputParam.get().toLowerCase();
+            if(format.equals("full")){
+                return runner.process(analysisText, JCasFormatter::getJsonFullFormat);
+            }else if(format.equals("xmi")){
+                return runner.process(analysisText, JCasFormatter::getXmiFormat);
+            }else if(format.equals("filtered")){
+                return runner.process(analysisText, JCasFormatter::getJsonFilteredFormat);
             }
-        }
-
-        if(summarize) {
-            return runner.process(analysisText, JCasFormatter::getSummaryFormat);
         }else{
-            return runner.process(analysisText, JCasFormatter::getFullFormat);
+            return runner.process(analysisText, JCasFormatter::getJsonSummaryFormat);
         }
+        return "";
     }
 
     static private final class PipelineRunner {

Modified: ctakes/trunk/ctakes-web-rest/src/main/java/org/apache/ctakes/rest/util/JCasFormatter.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-web-rest/src/main/java/org/apache/ctakes/rest/util/JCasFormatter.java?rev=1890898&r1=1890897&r2=1890898&view=diff
==============================================================================
--- ctakes/trunk/ctakes-web-rest/src/main/java/org/apache/ctakes/rest/util/JCasFormatter.java (original)
+++ ctakes/trunk/ctakes-web-rest/src/main/java/org/apache/ctakes/rest/util/JCasFormatter.java Fri Jun 18 23:53:12 2021
@@ -23,14 +23,28 @@ import com.fasterxml.jackson.databind.Ob
 import org.apache.ctakes.drugner.type.*;
 import org.apache.ctakes.rest.service.CuiResponse;
 import org.apache.ctakes.typesystem.type.textsem.*;
+import org.apache.uima.UIMAException;
+import org.apache.uima.cas.Type;
+import org.apache.uima.cas.TypeSystem;
+import org.apache.uima.cas.impl.XmiCasSerializer;
+import org.apache.uima.fit.factory.JCasFactory;
+import org.apache.uima.fit.factory.TypeSystemDescriptionFactory;
 import org.apache.uima.fit.util.JCasUtil;
 import org.apache.uima.jcas.JCas;
 import org.apache.uima.jcas.tcas.Annotation;
 import org.apache.uima.json.JsonCasSerializer;
+import org.apache.uima.resource.ResourceInitializationException;
+import org.apache.uima.resource.metadata.FeatureDescription;
+import org.apache.uima.resource.metadata.TypeDescription;
+import org.apache.uima.resource.metadata.TypeSystemDescription;
+import org.apache.uima.resource.metadata.impl.TypeSystemDescription_impl;
+import org.xml.sax.SAXException;
 
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.StringWriter;
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
@@ -68,12 +82,37 @@ final public class JCasFormatter {
            MeasurementAnnotation.class
    );
 
+   /* There are a lot of types in cTAKES that are populated by "linguistic" modules, then these types are added to
+      the CAS for use in multiple downstream AEs. But most end users won't care about these types. We can filter them
+      out to send back a smaller return package. The way to do that is create a new typesystem that has all the same
+      types except the ones with "syntax" in the package name. This is a blunt weapon but I couldn't find a "real" way
+      to do this using UIMA library calls.
+    */
+   static TypeSystem filteredTypeSystem;
+   static {
+      try {
+         TypeSystemDescription tsd = TypeSystemDescriptionFactory.createTypeSystemDescription();
+         TypeSystemDescription filteredTypeSystemDescription = new TypeSystemDescription_impl();
+         for(TypeDescription td : tsd.getTypes()){
+            if(!td.getName().contains(("syntax"))) {
+               TypeDescription newType = filteredTypeSystemDescription.addType(td.getName(), td.getDescription(), td.getSupertypeName());
+               newType.setFeatures(td.getFeatures());
+            }
+         }
+         JCas tsCas = JCasFactory.createJCas(filteredTypeSystemDescription);
+         filteredTypeSystem = tsCas.getTypeSystem();
+      } catch (UIMAException e) {
+         e.printStackTrace();
+         throw new RuntimeException(e);
+      }
+   }
+
    /**
     * @param jcas ye olde ...
     * @return A Map, key is annotation class name (type), value is a list of {@link CuiResponse},
     * one for each annotation of interest.
     */
-   public static String getSummaryFormat( final JCas jcas ) { //throws Exception {
+   public static String getJsonSummaryFormat( final JCas jcas ) { //throws Exception {
 
       Map<String, List<CuiResponse>> struct = JCasUtil.select( jcas, Annotation.class ).stream()
                      .filter( a -> semClasses.contains( a.getClass() ) )
@@ -87,13 +126,50 @@ final public class JCasFormatter {
       }
    }
 
-   public static String getFullFormat( final JCas jcas ) { //throws Exception {
+   /**
+    * @param jcas ye olde ...
+    * @return The UIMA-generated JSON string representation of the CAS
+    */
+   public static String getJsonFullFormat( final JCas jcas ) { //throws Exception {
       StringWriter writer = new StringWriter();
       try {
          JsonCasSerializer.jsonSerialize(jcas.getCas(), jcas.getTypeSystem(), writer);
          return writer.toString();
       }catch(IOException e){
-         return "{\"Status\": \"Error caused during serialization of JCas on REST server.\"}";
+         return "{\"Status\": \"Error caused during JSON serialization of JCas on REST server.\"}";
+      }
+   }
+
+   /**
+    * @param jcas ye olde ...
+    * @return The UIMA-generated JSON string representation of the CAS
+    */
+   public static String getJsonFilteredFormat( final JCas jcas ) { //throws Exception {
+      StringWriter writer = new StringWriter();
+      try {
+         JsonCasSerializer.jsonSerialize(jcas.getCas(), filteredTypeSystem, writer);
+         return writer.toString();
+      }catch(IOException e){
+         return "{\"Status\": \"Error caused during JSON serialization of JCas on REST server.\"}";
+      }
+   }
+
+   /**
+    * @param jcas ye olde ...
+    * @return The XML generated by UIMA (specifically. their xmi format).
+    */
+   public static String getXmiFormat( final JCas jcas ) {
+      ObjectMapper mapper = new ObjectMapper();
+      ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+      try {
+         XmiCasSerializer.serialize(jcas.getCas(), baos);
+         String xmiString = baos.toString();
+         Map<String,String> jsonDict = new HashMap<>();
+         jsonDict.put("xml", xmiString);
+         return mapper.writeValueAsString(jsonDict);
+      }catch(SAXException | JsonProcessingException e){
+         return "{\"Status\": \"Error caused during XMI serialization of JCas on REST server.\"}";
       }
    }
 }