Return-Path: X-Original-To: apmail-ctakes-commits-archive@www.apache.org Delivered-To: apmail-ctakes-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 4917C1089C for ; Tue, 18 Aug 2015 17:21:26 +0000 (UTC) Received: (qmail 35590 invoked by uid 500); 18 Aug 2015 17:21:26 -0000 Delivered-To: apmail-ctakes-commits-archive@ctakes.apache.org Received: (qmail 35546 invoked by uid 500); 18 Aug 2015 17:21:26 -0000 Mailing-List: contact commits-help@ctakes.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ctakes.apache.org Delivered-To: mailing list commits@ctakes.apache.org Received: (qmail 35533 invoked by uid 99); 18 Aug 2015 17:21:26 -0000 Received: from eris.apache.org (HELO hades.apache.org) (140.211.11.105) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 18 Aug 2015 17:21:26 +0000 Received: from hades.apache.org (localhost [127.0.0.1]) by hades.apache.org (ASF Mail Server at hades.apache.org) with ESMTP id E8417AC062C for ; Tue, 18 Aug 2015 17:21:25 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@ctakes.apache.org From: dligach@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20150818172125.E8417AC062C@hades.apache.org> 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