ctakes-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dlig...@apache.org
Subject svn commit: r1696462 - in /ctakes/sandbox/ctakes-wsd/src/main/java/org/apache/ctakes/index: ./ IndexSentences.java
Date Tue, 18 Aug 2015 17:21:25 GMT
Author: dligach
Date: Tue Aug 18 17:21:25 2015
New Revision: 1696462

URL: http://svn.apache.org/r1696462
Log:
iterate over sentences in xmi files and index them

Added:
    ctakes/sandbox/ctakes-wsd/src/main/java/org/apache/ctakes/index/
    ctakes/sandbox/ctakes-wsd/src/main/java/org/apache/ctakes/index/IndexSentences.java  
(with props)

Added: ctakes/sandbox/ctakes-wsd/src/main/java/org/apache/ctakes/index/IndexSentences.java
URL: http://svn.apache.org/viewvc/ctakes/sandbox/ctakes-wsd/src/main/java/org/apache/ctakes/index/IndexSentences.java?rev=1696462&view=auto
==============================================================================
--- ctakes/sandbox/ctakes-wsd/src/main/java/org/apache/ctakes/index/IndexSentences.java (added)
+++ ctakes/sandbox/ctakes-wsd/src/main/java/org/apache/ctakes/index/IndexSentences.java Tue
Aug 18 17:21:25 2015
@@ -0,0 +1,120 @@
+/**
+ * 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.index;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.ctakes.typesystem.type.textspan.Sentence;
+import org.apache.ctakes.utils.Utils;
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.FSDirectory;
+import org.apache.lucene.util.Version;
+import org.apache.uima.UimaContext;
+import org.apache.uima.analysis_engine.AnalysisEngine;
+import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
+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 org.apache.uima.resource.ResourceInitializationException;
+
+import com.lexicalscope.jewel.cli.CliFactory;
+import com.lexicalscope.jewel.cli.Option;
+
+/**
+ * Read cTAKES annotations from XMI files.
+ *  
+ * @author dmitriy dligach
+ */
+public class IndexSentences {
+  
+  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 consumer = AnalysisEngineFactory.createEngine(RelationContextPrinter.class);
+		SimplePipeline.runPipeline(collectionReader, consumer);
+	}
+
+  /**
+   * Iterate over sentences and index them.
+   */
+  public static class RelationContextPrinter extends JCasAnnotator_ImplBase {
+    
+    IndexWriter indexWriter;
+    
+    @Override
+    public void initialize(UimaContext aContext) throws ResourceInitializationException {
+      
+      super.initialize(aContext);
+      try {
+        Directory directory = FSDirectory.open(new File("/Users/dima/Boston/Data/DeepPhe/Index"));
+        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
+        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_40, analyzer);
+        indexWriter = new IndexWriter(directory, indexWriterConfig);
+      } catch (IOException e) {
+        System.out.println("Index directory not available!");
+        throw new ResourceInitializationException(e);
+      }
+    }
+
+    @Override
+    public void collectionProcessComplete() throws AnalysisEngineProcessException {
+
+      try {
+        indexWriter.close();
+      } catch (IOException e) {
+        throw new AnalysisEngineProcessException(e);
+      }
+    }
+    
+    @Override
+    public void process(JCas jCas) throws AnalysisEngineProcessException {
+      
+      for(Sentence sentence : JCasUtil.select(jCas, Sentence.class)) {
+        Document document = new Document();
+        document.add(new Field("content", sentence.getCoveredText(), Field.Store.YES, Field.Index.ANALYZED,
Field.TermVector.YES));
+        try {
+          indexWriter.addDocument(document);
+        } catch (IOException e) {
+          throw new AnalysisEngineProcessException(e);
+        }
+      }
+    }
+  }
+}
+
+  
\ No newline at end of file

Propchange: ctakes/sandbox/ctakes-wsd/src/main/java/org/apache/ctakes/index/IndexSentences.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message