package classification;
/***************************************************************************
Clinical Named Entity Recognizer and Normalizer(Clinical NERC), (v0.1).
Copyright (C) 2013 Azad Dehghan
Contact: a.dehghan@manchester.ac.uk
*****************************************************************************/
import io.FileOps;
import java.io.File;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import config.Conf;
import gate.Annotation;
import gov.nih.nlm.nls.metamap.Ev;
import gov.nih.nlm.nls.metamap.Mapping;
import gov.nih.nlm.nls.metamap.MetaMapApi;
import gov.nih.nlm.nls.metamap.MetaMapApiImpl;
import gov.nih.nlm.nls.metamap.PCM;
import gov.nih.nlm.nls.metamap.Result;
import gov.nih.nlm.nls.metamap.Utterance;
/**
* this class is a wrapper for MetaMap
* @author dehghana
*
*/
public class MappingPipeline {
//initialise MetaMap api
static MetaMapApi api;
public MappingPipeline() throws MalformedURLException
{
Properties confProperties = Conf.loadConfiguration(new File("conf/metamap.conf"));
/**
* set parameters (set parameters in conf/metamap.properties)
*/
api = new MetaMapApiImpl();
api.setHost(confProperties.getProperty("serverHostname"));
api.setPort(Integer.parseInt(confProperties.getProperty("serverPort")));
List<String> theOptions = FileOps.getFileContentAsList(new File("conf/metamap.parameters").toURI().toURL());
if (theOptions.size() > 0) {
api.setOptions(theOptions);
}
}
private static Map<Object, String> getClassification(String term) throws Exception
{
//String[] classfication = new String[3];
Map<Object, String> mp = new HashMap<Object, String>();
//Certain characters may cause MetaMap to throw an exception;
//filter terms before passing to mm.
term = term.replaceAll("'", "");
term = term.replaceAll("\"", "");
List<Result> resultList = api.processCitationsFromString(term);
Result result = resultList.get(0);
for (Utterance utterance: result.getUtteranceList())
{ for (PCM pcm: utterance.getPCMList()) {
/*for (Ev ev: pcm.getCandidatesInstance().getEvList()) {
System.out.println(" Candidate:");
System.out.println(" Score: " + ev.getScore());
System.out.println(" Concept Id: " + ev.getConceptId());
System.out.println(" Concept Name: " + ev.getConceptName());
System.out.println(" Preferred Name: " + ev.getPreferredName());
System.out.println(" Matched Words: " + ev.getMatchedWords());
System.out.println(" Semantic Types: " + ev.getSemanticTypes());
System.out.println(" MatchMap: " + ev.getMatchMap());
System.out.println(" MatchMap alt. repr.: " + ev.getMatchMapList());
System.out.println(" is Head?: " + ev.isHead());
System.out.println(" is Overmatch?: " + ev.isOvermatch());
System.out.println(" Sources: " + ev.getSources());
System.out.println(" Positional Info: " + ev.getPositionalInfo());
}*/
for (Mapping map: pcm.getMappingList()) {
for (Ev mapEv: map.getEvList()) {
mp.put(new Integer(1), mapEv.getConceptId());
mp.put(new Integer(2), mapEv.getSemanticTypes().toString());
mp.put(new Integer(3), mapEv.getConceptName());
// add further attributes here (and in run(gateDoc) --> gateMap.put(///))
}
}
}
}
return mp;
}
/**
* final classification pipeline
*
* @param gateDoc - input
* @return gateDoc - output
* @throws Exception
*/
public gate.Document run(gate.Document gateDoc) throws Exception
{
List<Annotation> as_problem = new ArrayList<Annotation>( gateDoc.getAnnotations().get("problem") );
for(Annotation s: as_problem)
{
String str = gate.Utils.stringFor(gateDoc, s);
Map<Object, String> mp = getClassification(str);
gate.FeatureMap gateMap = s.getFeatures();
//if mapping result > 0.
if(mp.size()>0)
{
gateMap.put("conceptId", mp.get(1)); //concept unique identifier
gateMap.put("semType", mp.get(2)); // semantic type
gateMap.put("conceptName", mp.get(3)); //concept name
// add further attributes here ....
s.setFeatures(gateMap);
}
}
List<Annotation> as_treatment = new ArrayList<Annotation>( gateDoc.getAnnotations().get("treatment") );
for(Annotation s: as_treatment)
{
String str = gate.Utils.stringFor(gateDoc, s);
//String[] classification = getClassification(str);
Map<Object, String> mp = getClassification(str);
gate.FeatureMap gateMap = s.getFeatures();
if(mp.size()>0)
{
gateMap.put("conceptId", mp.get(1)); //concept unique identifier
gateMap.put("semType", mp.get(2)); // semantic type
gateMap.put("conceptName", mp.get(3)); //concept name
s.setFeatures(gateMap);
}
}
List<Annotation> as_test = new ArrayList<Annotation>( gateDoc.getAnnotations().get("test") );
for(Annotation s: as_test)
{
String str = gate.Utils.stringFor(gateDoc, s);
//String[] classification = getClassification(str);
Map<Object, String> mp = getClassification(str);
gate.FeatureMap gateMap = s.getFeatures();
if(mp.size()>0)
{
gateMap.put("conceptId", mp.get(1)); //concept unique identifier
gateMap.put("semType", mp.get(2)); // semantic type
gateMap.put("conceptName", mp.get(3)); //concept name
s.setFeatures(gateMap);
}
}
return gateDoc;
}
}