You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ctakes.apache.org by dl...@apache.org on 2016/04/28 18:38:11 UTC

svn commit: r1741474 - /ctakes/sandbox/ctakes-wsd/src/main/java/org/apache/ctakes/consumers/DataForWord2Vec.java

Author: dligach
Date: Thu Apr 28 16:38:11 2016
New Revision: 1741474

URL: http://svn.apache.org/viewvc?rev=1741474&view=rev
Log:
extracting what's need to run word2vec from xmi files

Added:
    ctakes/sandbox/ctakes-wsd/src/main/java/org/apache/ctakes/consumers/DataForWord2Vec.java

Added: ctakes/sandbox/ctakes-wsd/src/main/java/org/apache/ctakes/consumers/DataForWord2Vec.java
URL: http://svn.apache.org/viewvc/ctakes/sandbox/ctakes-wsd/src/main/java/org/apache/ctakes/consumers/DataForWord2Vec.java?rev=1741474&view=auto
==============================================================================
--- ctakes/sandbox/ctakes-wsd/src/main/java/org/apache/ctakes/consumers/DataForWord2Vec.java (added)
+++ ctakes/sandbox/ctakes-wsd/src/main/java/org/apache/ctakes/consumers/DataForWord2Vec.java Thu Apr 28 16:38:11 2016
@@ -0,0 +1,118 @@
+/**
+ * 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.ctakes.consumers;
+
+import java.io.File;
+
+import org.apache.ctakes.typesystem.type.syntax.BaseToken;
+import org.apache.ctakes.typesystem.type.textsem.EntityMention;
+import org.apache.ctakes.typesystem.type.textsem.EventMention;
+import org.apache.ctakes.typesystem.type.textsem.IdentifiedAnnotation;
+import org.apache.ctakes.utils.Utils;
+import org.apache.uima.analysis_engine.AnalysisEngine;
+import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
+import org.apache.uima.cas.CASException;
+import org.apache.uima.collection.CollectionReader;
+import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
+import org.apache.uima.fit.factory.AnalysisEngineFactory;
+import org.apache.uima.fit.pipeline.SimplePipeline;
+import org.apache.uima.fit.util.JCasUtil;
+import org.apache.uima.jcas.JCas;
+
+import com.lexicalscope.jewel.cli.CliFactory;
+import com.lexicalscope.jewel.cli.Option;
+
+/**
+ * Read cTAKES annotations from XMI files.
+ *  
+ * @author dmitriy dligach
+ */
+public class DataForWord2Vec {
+
+	static interface Options {
+
+		@Option(
+				longName = "xmi-dir",
+				description = "path to xmi files")
+		public File getInputDirectory();
+	}
+
+	public static void main(String[] args) throws Exception {
+
+		Options options = CliFactory.parseArguments(Options.class, args);
+		CollectionReader collectionReader = Utils.getCollectionReader(options.getInputDirectory());
+		AnalysisEngine annotationConsumer = AnalysisEngineFactory.createEngine(AnnotationPrinter.class);
+		SimplePipeline.runPipeline(collectionReader, annotationConsumer);
+	}
+
+	public static class AnnotationPrinter extends JCasAnnotator_ImplBase {
+
+		@Override
+		public void process(JCas jCas) throws AnalysisEngineProcessException {
+
+			JCas systemView;
+			try {
+				systemView = jCas.getView("_InitialView");
+			} catch (CASException e) {
+				throw new AnalysisEngineProcessException(e);
+			}
+
+		  for(BaseToken token : JCasUtil.select(systemView, BaseToken.class)) { 
+		    String stringValue = tokenToString(token);
+		    System.out.print(stringValue + " ");
+		  }
+		}
+		
+		/**
+		 * Determine what to print based on the token's type.
+		 */
+		private String tokenToString(BaseToken token) {
+		  
+		  String stringValue;
+		  String tokenType = token.getClass().getSimpleName();
+		  String tokenText = token.getCoveredText().toLowerCase();
+
+		  switch(tokenType) {
+		    case "ContractionToken":
+		      stringValue = tokenText;
+		      break;
+		    case "NewlineToken":
+		      stringValue = "";
+		      break;
+		    case "NumToken":
+		      stringValue = "number_token";
+		      break;
+		    case "PunctuationToken":
+		      stringValue = tokenText;
+		      break;
+		    case "SymbolToken":
+		      stringValue = tokenText;
+		      break;
+		    case "WordToken":
+		      stringValue = tokenText;
+		      break;
+		    default:
+		      throw new IllegalArgumentException("Invalid token type: " + tokenType);
+		  }
+		  
+		  return stringValue;
+		}
+	}
+}
+