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 2018/12/31 17:16:59 UTC
svn commit: r1850060 - in
/ctakes/trunk/ctakes-web-rest/src/main/java/org/apache/ctakes/rest:
service/CtakesRestController.java service/CuiResponse.java
util/JCasParser.java
Author: tmill
Date: Mon Dec 31 17:16:59 2018
New Revision: 1850060
URL: http://svn.apache.org/viewvc?rev=1850060&view=rev
Log:
Merge branch 'fix-rest'
Added:
ctakes/trunk/ctakes-web-rest/src/main/java/org/apache/ctakes/rest/service/CuiResponse.java
ctakes/trunk/ctakes-web-rest/src/main/java/org/apache/ctakes/rest/util/JCasParser.java
Modified:
ctakes/trunk/ctakes-web-rest/src/main/java/org/apache/ctakes/rest/service/CtakesRestController.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=1850060&r1=1850059&r2=1850060&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 Mon Dec 31 17:16:59 2018
@@ -18,24 +18,19 @@
*/
package org.apache.ctakes.rest.service;
-import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.ctakes.core.pipeline.PipelineBuilder;
import org.apache.ctakes.core.pipeline.PiperFileReader;
-import org.apache.ctakes.rest.util.XMLParser;
+import org.apache.ctakes.rest.util.JCasParser;
import org.apache.log4j.Logger;
import org.apache.uima.UIMAFramework;
import org.apache.uima.analysis_engine.AnalysisEngine;
import org.apache.uima.analysis_engine.AnalysisEngineDescription;
-import org.apache.uima.cas.impl.XmiCasSerializer;
import org.apache.uima.jcas.JCas;
import org.apache.uima.util.JCasPool;
import org.springframework.web.bind.annotation.*;
import javax.annotation.PostConstruct;
import javax.servlet.ServletException;
-import java.io.ByteArrayInputStream;
-import java.nio.file.Files;
-import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -60,12 +55,12 @@ public class CtakesRestController {
public void init() throws ServletException {
LOGGER.info("Initializing analysis engines and jcas pools");
_pipelineRunners.put(DEFAULT_PIPELINE, new PipelineRunner(DEFAULT_PIPER_FILE_PATH));
- _pipelineRunners.put(FULL_PIPELINE, new PipelineRunner(FULL_PIPER_FILE_PATH));
+// _pipelineRunners.put(FULL_PIPELINE, new PipelineRunner(FULL_PIPER_FILE_PATH));
}
@RequestMapping(value = "/analyze", method = RequestMethod.POST)
@ResponseBody
- public Map<String, Map<String, List<String>>> getAnalyzedJSON(@RequestBody String analysisText,
+ public Map<String, List<CuiResponse>> getAnalyzedJSON(@RequestBody String analysisText,
@RequestParam("pipeline") Optional<String> pipelineOptParam)
throws Exception {
String pipeline = DEFAULT_PIPELINE;
@@ -79,13 +74,9 @@ public class CtakesRestController {
}
- static private Map<String, Map<String, List<String>>> formatResults(JCas jcas) throws Exception {
- ByteArrayOutputStream output = new ByteArrayOutputStream();
- XmiCasSerializer.serialize(jcas.getCas(), output);
- String outputStr = output.toString();
- // For debugging: Files.write(Paths.get("Result.xml"), outputStr.getBytes());
- XMLParser parser = new XMLParser();
- return parser.parse(new ByteArrayInputStream(outputStr.getBytes()));
+ static private Map<String, List<CuiResponse>> formatResults(JCas jcas) throws Exception {
+ JCasParser parser = new JCasParser();
+ return parser.parse(jcas);
}
static private final class PipelineRunner {
@@ -105,9 +96,9 @@ public class CtakesRestController {
}
}
- public Map<String, Map<String, List<String>>> process(final String text) throws ServletException {
+ public Map<String, List<CuiResponse>> process(final String text) throws ServletException {
JCas jcas = null;
- Map<String, Map<String, List<String>>> resultMap = null;
+ Map<String, List<CuiResponse>> resultMap = null;
if (text != null) {
try {
jcas = _pool.getJCas(-1);
Added: ctakes/trunk/ctakes-web-rest/src/main/java/org/apache/ctakes/rest/service/CuiResponse.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-web-rest/src/main/java/org/apache/ctakes/rest/service/CuiResponse.java?rev=1850060&view=auto
==============================================================================
--- ctakes/trunk/ctakes-web-rest/src/main/java/org/apache/ctakes/rest/service/CuiResponse.java (added)
+++ ctakes/trunk/ctakes-web-rest/src/main/java/org/apache/ctakes/rest/service/CuiResponse.java Mon Dec 31 17:16:59 2018
@@ -0,0 +1,61 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.ctakes.rest.service;
+
+import org.apache.ctakes.typesystem.type.refsem.UmlsConcept;
+import org.apache.ctakes.typesystem.type.textsem.IdentifiedAnnotation;
+import org.apache.uima.fit.util.JCasUtil;
+import org.apache.uima.jcas.tcas.Annotation;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Created by tmill on 12/20/18.
+ */
+public class CuiResponse {
+ public int begin;
+ public int end;
+ public String text;
+ public int polarity;
+ public List<Map<String,String>> conceptAttributes = new ArrayList<>();
+
+ public CuiResponse(Annotation annotation){
+ begin = annotation.getBegin();
+ end = annotation.getEnd();
+ text = annotation.getCoveredText();
+
+ if(annotation instanceof IdentifiedAnnotation) {
+ IdentifiedAnnotation ia = (IdentifiedAnnotation) annotation;
+ polarity = ia.getPolarity();
+ if(ia.getOntologyConceptArr() != null) {
+ for (UmlsConcept concept : JCasUtil.select(ia.getOntologyConceptArr(), UmlsConcept.class)) {
+ Map<String, String> atts = new HashMap<>();
+ atts.put("codingScheme", concept.getCodingScheme());
+ atts.put("cui", concept.getCui());
+ atts.put("code", concept.getCode());
+ atts.put("tui", concept.getTui());
+ conceptAttributes.add(atts);
+ }
+ }
+ }
+ }
+}
Added: ctakes/trunk/ctakes-web-rest/src/main/java/org/apache/ctakes/rest/util/JCasParser.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-web-rest/src/main/java/org/apache/ctakes/rest/util/JCasParser.java?rev=1850060&view=auto
==============================================================================
--- ctakes/trunk/ctakes-web-rest/src/main/java/org/apache/ctakes/rest/util/JCasParser.java (added)
+++ ctakes/trunk/ctakes-web-rest/src/main/java/org/apache/ctakes/rest/util/JCasParser.java Mon Dec 31 17:16:59 2018
@@ -0,0 +1,74 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.ctakes.rest.util;
+
+import org.apache.ctakes.drugner.type.*;
+import org.apache.ctakes.rest.service.CuiResponse;
+import org.apache.ctakes.typesystem.type.textsem.*;
+import org.apache.uima.fit.util.JCasUtil;
+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;
+
+/**
+ * Created by tmill on 12/20/18.
+ */
+public class JCasParser {
+ List<Class<? extends Annotation>> semClasses = new ArrayList<>();
+
+ public JCasParser(){
+ // CUI types:
+ semClasses.add(DiseaseDisorderMention.class);
+ semClasses.add(SignSymptomMention.class);
+ semClasses.add(ProcedureMention.class);
+ semClasses.add(AnatomicalSiteMention.class);
+ semClasses.add(MedicationMention.class);
+
+ // Temporal types:
+ semClasses.add(TimeMention.class);
+ semClasses.add(DateAnnotation.class);
+
+ // Drug-related types:
+ semClasses.add(FractionStrengthAnnotation.class);
+ semClasses.add(DrugChangeStatusAnnotation.class);
+ semClasses.add(StrengthUnitAnnotation.class);
+ semClasses.add(StrengthAnnotation.class);
+ semClasses.add(RouteAnnotation.class);
+ semClasses.add(FrequencyUnitAnnotation.class);
+ semClasses.add(MeasurementAnnotation.class);
+ }
+
+ public Map<String, List<CuiResponse>> parse(JCas jcas) throws Exception {
+
+ Map<String, List<CuiResponse>> responseMap = new HashMap<>();
+ for(Class<? extends Annotation> semClass : semClasses){
+ List<CuiResponse> annotations = new ArrayList<>();
+ for(Annotation annot : JCasUtil.select(jcas, semClass)){
+ CuiResponse response = new CuiResponse(annot);
+ annotations.add(response);
+ }
+ responseMap.put(semClass.getSimpleName(), annotations);
+ }
+ return responseMap;
+ }
+}