in ctakes-temporal/src/main/java/org/apache/ctakes/temporal/eval/Evaluation_ImplBase.java [1102:1312]
public void process( JCas jcas ) throws AnalysisEngineProcessException {
try {
// get the output file name from the input file name and output directory.
File inFile = new File( ViewUriUtil.getURI( jcas ) );
String outFile = inFile.getName().replace( ".txt", "" );
File outDir = new File( outputDir, outFile );
if ( !outDir.exists() ) {
outDir.mkdirs();
}
// get maps from ids to entities and relations:
JCas probView = (probViewname == null ? null : jcas.getView(probViewname));
Map<Integer, List<EventMention>> mentions = probViewname == null? null : getMentionIdMap(jcas, probView);
Map<String, List<TemporalTextRelation>> rels = probViewname == null ? null : getRelationIdMap(probView);
// build the xml
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement( "data" );
//info element
Element infoElement = doc.createElement( "info" );
Element saveTime = doc.createElement( "savetime" );
saveTime.setTextContent( "2015-0123-10:21" );
Element progress = doc.createElement( "progress" );
progress.setTextContent( "completed" );
infoElement.appendChild( saveTime );
infoElement.appendChild( progress );
//schema element
Element schema = doc.createElement( "schema" );
schema.setAttribute( "path", "./" );
schema.setAttribute( "protocol", "file" );
schema.setTextContent( "temporal-schema.xml" );
Element annoElement = doc.createElement( "annotations" );
Map<IdentifiedAnnotation, String> argToId = new HashMap<>();
int id = 1;
for ( EventMention event : JCasUtil.select( jcas, EventMention.class ) ) {
if ( event.getClass().equals( EventMention.class ) ) {
// this ensures we are only looking at THYME events and not ctakes-dictionary-lookup events
Element eventEl = doc.createElement( "entity" );
String eventID = id + "@e@" + outFile + "@system";
id++;
argToId.put( event, eventID );
Element idE = doc.createElement( "id" );
idE.setTextContent( eventID );
Element spanE = doc.createElement( "span" );
spanE.setTextContent( String.valueOf( event.getBegin() ) + "," + String.valueOf( event.getEnd() ) );
Element typeE = doc.createElement( "type" );
typeE.setTextContent( "EVENT" );
Element parentTE = doc.createElement( "parentsType" );
parentTE.setTextContent( "TemporalEntities" );
//add properties
Element property = doc.createElement( "properties" );
Element docTimeRE = doc.createElement( "DocTimeRel" );
String dtrContent = null;
if(probViewname == null){
dtrContent = event.getEvent().getProperties().getDocTimeRel();
}else{
StringBuffer buff = new StringBuffer();
for(EventMention probMention : mentions.get(event.getId())){
buff.append(probMention.getEvent().getProperties().getDocTimeRel());
buff.append(':');
buff.append(probMention.getConfidence());
buff.append("::");
}
dtrContent = buff.substring(0, buff.length()-2);
}
docTimeRE.setTextContent( dtrContent );
Element eventTypeE = doc.createElement( "Type" );
eventTypeE.setTextContent( "N/A" );
Element degreeE = doc.createElement( "Degree" );
degreeE.setTextContent( "N/A" );
Element polarityE = doc.createElement( "Polarity" );
String polarity = "UNKNOWN";
int polarityInt = event.getPolarity();
if ( polarityInt == CONST.NE_POLARITY_NEGATION_ABSENT ) {
polarity = "POS";
} else if ( polarityInt == CONST.NE_POLARITY_NEGATION_PRESENT ) {
polarity = "NEG";
}
polarityE.setTextContent( polarity );
Element ctexModE = doc.createElement( "ContextualModality" );
ctexModE.setTextContent( event.getEvent().getProperties().getContextualModality() );
Element ctexAspE = doc.createElement( "ContextualAspect" );
ctexAspE.setTextContent( event.getEvent().getProperties().getContextualAspect() );
Element permE = doc.createElement( "Permanence" );
permE.setTextContent( "UNDETERMINED" );
property.appendChild( docTimeRE );
property.appendChild( polarityE );
property.appendChild( degreeE );
property.appendChild( eventTypeE );
property.appendChild( ctexModE );
property.appendChild( ctexAspE );
property.appendChild( permE );
eventEl.appendChild( idE );
eventEl.appendChild( spanE );
eventEl.appendChild( typeE );
eventEl.appendChild( parentTE );
eventEl.appendChild( property );
annoElement.appendChild( eventEl );
}
}
for ( TimeMention timex : JCasUtil.select( jcas, TimeMention.class ) ) {
Element timexElement = doc.createElement( "entity" );
String timexID = id + "@e@" + outFile + "@system";
id++;//18@e@ID006_clinic_016@gold
argToId.put( timex, timexID );
Element idE = doc.createElement( "id" );
idE.setTextContent( timexID );
Element spanE = doc.createElement( "span" );
spanE.setTextContent( String.valueOf( timex.getBegin() ) + "," + String.valueOf( timex.getEnd() ) );
Element typeE = doc.createElement( "type" );
Element parentTE = doc.createElement( "parentsType" );
parentTE.setTextContent( "TemporalEntities" );
//add properties
Element property = doc.createElement( "properties" );
String timeClass = timex.getTimeClass();
//add normalized timex
try{
String value = Utils.getTimexMLValue(timex.getCoveredText());
property.setTextContent( value );
}catch (Exception e) {
LOGGER.error("time norm error: "+ e.getMessage());
}
if ( timeClass!=null && (timeClass.equals( "DOCTIME" ) || timeClass.equals( "SECTIONTIME" ) ) ) {
typeE.setTextContent( timeClass );
property.setTextContent( "" );
} else {
typeE.setTextContent( "TIMEX3" );
Element classE = doc.createElement( "Class" );
classE.setTextContent( timeClass );
property.appendChild( classE );
}
timexElement.appendChild( idE );
timexElement.appendChild( spanE );
timexElement.appendChild( typeE );
timexElement.appendChild( property );
annoElement.appendChild( timexElement );
}
id = 1;
if(probViewname == null){
for ( TemporalTextRelation rel : JCasUtil.select( jcas, TemporalTextRelation.class ) ) {
Annotation arg1 = rel.getArg1().getArgument();
Annotation arg2 = rel.getArg2().getArgument();
String arg1Content = argToId.get( arg1 );
String arg2Content = argToId.get( arg2 );
String relContent = rel.getCategory();
annoElement.appendChild(addRelationElement(doc, id, relContent, arg1Content, arg2Content, outFile));
id++;
}
}else{
// need to keep track of which relations we've printed since they don't get grouped in the CAS
for(String key : rels.keySet()){
String arg1Content = null;
String arg2Content = null;
StringBuffer buff = new StringBuffer();
for(TemporalTextRelation probRel : rels.get(key)){
buff.append(probRel.getCategory());
buff.append(':');
buff.append(probRel.getConfidence());
buff.append("::");
if(arg1Content == null){
arg1Content = argToId.get(probRel.getArg1().getArgument());
arg2Content = argToId.get(probRel.getArg2().getArgument());
}
}
String relContent = buff.substring(0, buff.length()-2);
annoElement.appendChild(addRelationElement(doc, id, relContent, arg1Content, arg2Content, outFile));
id++;
}
}
rootElement.appendChild( infoElement );
rootElement.appendChild( schema );
rootElement.appendChild( annoElement );
doc.appendChild( rootElement );
// boilerplate xml-writing code:
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
transformer.setOutputProperty( OutputKeys.METHOD, "xml" );
DOMSource source = new DOMSource( doc );
StreamResult result = new StreamResult( new File( outDir, outFile + ".xml" ) );
transformer.transform( source, result );
} catch ( ParserConfigurationException e ) {
e.printStackTrace();
throw new AnalysisEngineProcessException( e );
} catch ( TransformerConfigurationException e ) {
e.printStackTrace();
throw new AnalysisEngineProcessException( e );
} catch ( TransformerException e ) {
e.printStackTrace();
throw new AnalysisEngineProcessException( e );
} catch (CASException e) {
e.printStackTrace();
throw new AnalysisEngineProcessException( e );
}
}