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 EEDBB10274 for ; Tue, 4 Jun 2013 19:27:42 +0000 (UTC) Received: (qmail 53415 invoked by uid 500); 4 Jun 2013 19:27:42 -0000 Delivered-To: apmail-ctakes-commits-archive@ctakes.apache.org Received: (qmail 53381 invoked by uid 500); 4 Jun 2013 19:27:42 -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 53374 invoked by uid 99); 4 Jun 2013 19:27:42 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 04 Jun 2013 19:27:42 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 04 Jun 2013 19:27:39 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 7969B23888E7; Tue, 4 Jun 2013 19:27:18 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1489573 - in /ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/data/analysis: EventContextAnalysisConsumer.java EventContextAnalysisPipeline.java Date: Tue, 04 Jun 2013 19:27:18 -0000 To: commits@ctakes.apache.org From: dligach@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130604192718.7969B23888E7@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: dligach Date: Tue Jun 4 19:27:18 2013 New Revision: 1489573 URL: http://svn.apache.org/r1489573 Log: moved the consumer inside the pipeline class definition Removed: ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/data/analysis/EventContextAnalysisConsumer.java Modified: ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/data/analysis/EventContextAnalysisPipeline.java Modified: ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/data/analysis/EventContextAnalysisPipeline.java URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/data/analysis/EventContextAnalysisPipeline.java?rev=1489573&r1=1489572&r2=1489573&view=diff ============================================================================== --- ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/data/analysis/EventContextAnalysisPipeline.java (original) +++ ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/data/analysis/EventContextAnalysisPipeline.java Tue Jun 4 19:27:18 2013 @@ -18,21 +18,34 @@ */ package org.apache.ctakes.temporal.data.analysis; +import java.io.BufferedWriter; import java.io.File; +import java.io.FileWriter; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.apache.ctakes.relationextractor.eval.XMIReader; +import org.apache.ctakes.typesystem.type.syntax.BaseToken; +import org.apache.ctakes.typesystem.type.textsem.EventMention; 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.jcas.JCas; +import org.apache.uima.jcas.tcas.Annotation; import org.cleartk.util.Options_ImplBase; import org.kohsuke.args4j.Option; +import org.uimafit.component.JCasAnnotator_ImplBase; +import org.uimafit.descriptor.ConfigurationParameter; import org.uimafit.factory.AnalysisEngineFactory; import org.uimafit.factory.CollectionReaderFactory; import org.uimafit.pipeline.SimplePipeline; +import org.uimafit.util.JCasUtil; /** + * Print events and tokens with contexts to two separeate files. * * @author dmitriy dligach */ @@ -74,7 +87,7 @@ public class EventContextAnalysisPipelin CollectionReader collectionReader = getCollectionReader(trainFiles); AnalysisEngine annotationConsumer = AnalysisEngineFactory.createPrimitive( - EventContextAnalysisConsumer.class, + EventAndTokenContextWriter.class, "TokenOutputFile", options.tokenOutputFile, "EventOutputFile", @@ -84,7 +97,104 @@ public class EventContextAnalysisPipelin SimplePipeline.runPipeline(collectionReader, annotationConsumer); } - + + /** + * Print all tokens with contexts and all events with contexts for further analysis. + */ + public static class EventAndTokenContextWriter extends JCasAnnotator_ImplBase { + + @ConfigurationParameter( + name = "TokenOutputFile", + mandatory = true, + description = "path to the file that stores token contexts") + private String tokenOutputFile; + + @ConfigurationParameter( + name = "EventOutputFile", + mandatory = true, + description = "path to the file that stores event contexts") + private String eventOutputFile; + + @ConfigurationParameter( + name = "ContextSize", + mandatory = true, + description = "context size in characters") + private int contextSize; + + @Override + public void process(JCas jCas) throws AnalysisEngineProcessException { + + JCas goldView; + try { + goldView = jCas.getView("GoldView"); + } catch (CASException e) { + throw new AnalysisEngineProcessException(e); + } + + JCas systemView; + try { + systemView = jCas.getView("_InitialView"); + } catch (CASException e) { + throw new AnalysisEngineProcessException(e); + } + + BufferedWriter tokenWriter = getWriter(tokenOutputFile, true); + BufferedWriter eventWriter = getWriter(eventOutputFile, true); + try { + for(BaseToken baseToken : JCasUtil.select(systemView, BaseToken.class)) { + String tokenText = baseToken.getCoveredText().toLowerCase(); + String output = String.format("%s|%s\n", tokenText, getAnnotationContext(baseToken, contextSize)); + + try { + tokenWriter.write(output); + } catch (IOException e) { + throw new AnalysisEngineProcessException(e); + } + } + + for(EventMention eventMention : JCasUtil.select(goldView, EventMention.class)) { + String eventText = eventMention.getCoveredText().toLowerCase(); + String output = String.format("%s|%s\n", eventText, getAnnotationContext(eventMention, contextSize)); + + try { + eventWriter.write(output); + } catch (IOException e) { + throw new AnalysisEngineProcessException(e); + } + } + } finally { + try { + tokenWriter.close(); + eventWriter.close(); + } catch (IOException e) { + throw new AnalysisEngineProcessException(e); + } + } + } + + private static String getAnnotationContext(Annotation annotation, int maxContextWindowSize) { + + String text = annotation.getCAS().getDocumentText(); + int begin = Math.max(0, annotation.getBegin() - maxContextWindowSize); + int end = Math.min(text.length(), annotation.getEnd() + maxContextWindowSize); + + return text.substring(begin, end).replaceAll("[\r\n]", " "); + } + + private static BufferedWriter getWriter(String filePath, boolean append) { + + BufferedWriter bufferedWriter = null; + try { + FileWriter fileWriter = new FileWriter(filePath, append); + bufferedWriter = new BufferedWriter(fileWriter); + } catch (IOException e) { + e.printStackTrace(); + } + + return bufferedWriter; + } + } + private static CollectionReader getCollectionReader(List inputFiles) throws Exception { List fileNames = new ArrayList();