CLEiM
Cross Lingual Education in Medicine
 All Classes Namespaces Files Functions Variables
TestAnnotations.java
Go to the documentation of this file.
00001 package com.uem.gsi.cleim.test;
00002 
00003 import java.io.BufferedWriter;
00004 import java.io.File;
00005 import java.io.FileNotFoundException;
00006 import java.io.FileOutputStream;
00007 import java.io.IOException;
00008 import java.io.OutputStreamWriter;
00009 import java.io.UnsupportedEncodingException;
00010 import java.io.Writer;
00011 
00012 import org.apache.commons.httpclient.HttpClient;
00013 import org.apache.commons.httpclient.methods.PostMethod;
00014 import org.apache.commons.io.FileUtils;
00015 
00016 import com.uem.gsi.cleim.nlp.GateGazMed;
00017 import com.uem.gsi.cleim.nlp.IntegrateAnnot;
00018 import com.uem.gsi.cleim.nlp.NCBOAnnot;
00019 import com.uem.gsi.cleim.util.Constants;
00020 
00021 public class TestAnnotations {
00022         private String path="WebContent/WEB-INF";
00023         private String text="";
00024         private String onts="";
00025         private String lev="0";
00026         private String[] localSrc=new String[]{"","",""};
00027         private String[] localLan=new String[]{"",""};
00028         private IntegrateAnnot ia;
00029         //private GateGazMed gateAnnot;
00030         //private NCBOAnnot ncboAnnot;
00031         public TestAnnotations() {
00032                 
00033                 //gateAnnot=new GateGazMed(path);
00034                 //ncboAnnot=new NCBOAnnot();
00035         }
00036         public void setText(String ptext){
00037                 text=ptext;
00038         }
00039         public void setOnts(String ponts){
00040                 onts=ponts;
00041         }
00042         public void setLev(String plev){
00043                 lev=plev;
00044         }
00045         public void setLocalSrc(String[] plocalSrc){
00046                 localSrc=plocalSrc;
00047         }
00048         public void setLocalLan(String[] plocalLan){
00049                 localLan=plocalLan;
00050         }
00051         public void initIntegrateAnnot(){
00052                 ia=new IntegrateAnnot(text,path,onts,lev,localSrc,localLan);
00053         }
00054         private long takeTime(){
00055                 //return System.nanoTime();
00056                 return System.currentTimeMillis();
00057         }
00058         private void avoidInitialization(){
00059                 try {
00060                         ia.gateWithoutFormat();
00061                         ia.ncboWithoutFormat();
00062                 } catch (Exception e) {
00063                         System.out.println(e.toString());
00064                 }
00065         }
00069         public long testGate(String formatType){
00070                 String output="";
00071                 long res=0;
00072                 try{
00073                         if (formatType.equals("")){
00074                                 long start = takeTime();
00075                                 ia.gateWithoutFormat();
00076                                 long end = takeTime();
00077                                 res=end-start;
00078                         }else if (formatType.equals("html")){
00079                                 long start = takeTime();
00080                                 output=ia.htmlGateTree();
00081                                 long end = takeTime();
00082                                 res=end-start;
00083                         }else if(formatType.equals("xml")){
00084                                 long start = takeTime();
00085                                 ia.initXmlDocument();
00086                                 ia.xmlGateTree();
00087                                 output=ia.getNormalizedXml();
00088                                 long end = takeTime();
00089                                 res=end-start;
00090                         }
00091                 }catch(Exception e){
00092                         System.out.println("Exception: "+e.toString());
00093                 }
00094                 System.out.println("GATE Time (format="+formatType+"): "+res);
00095                 //System.out.println(output);
00096                 return res;
00097         }
00098         public long testOba(String formatType){
00099                 String output="";
00100                 long res=0;
00101                 try{
00102                         if (formatType.equals("")){
00103                                 long start = takeTime();
00104                                 ia.ncboWithoutFormat();
00105                                 long end = takeTime();
00106                                 res=end-start;
00107                         }else if (formatType.equals("html")){
00108                                 long start = takeTime();
00109                           output=ia.htmlNCBOTree();
00110                                 long end = takeTime();
00111                                 res=end-start;
00112                         }else if(formatType.equals("xml")){
00113                                 long start = takeTime();
00114                                 ia.initXmlDocument();
00115                                 ia.xmlNCBOTree();
00116                                 output=ia.getNormalizedXml();
00117                                 long end = takeTime();
00118                                 res=end-start;
00119                         }
00120                 }catch(Exception e){
00121                         System.out.println("Exception: "+e.toString());
00122                 }
00123                 System.out.println("NCBO Time (format="+formatType+"): "+res);
00124                 //System.out.println(output);
00125                 return res;
00126         }
00127         public void testHTTPGATE(String src){
00128                 String sSource="";
00129                 HttpClient client = new HttpClient();
00130     PostMethod method = new PostMethod("http://orion.esp.uem.es:8080/CLEiM/NLPServ");
00131     // Configure the form parameters
00132     method.addParameter("text", text);
00133     method.addParameter("src", src);
00134     try{
00135         long start = takeTime();
00136             int statusCode = client.executeMethod(method);
00137             sSource = method.getResponseBodyAsString();
00138             long end = takeTime();
00139                         System.out.println("GATE HTTP Time (src="+src+"): "+ (end-start));
00140             if( statusCode != -1 ) {
00141               //sSource = method.getResponseBodyAsString();
00142               method.releaseConnection();
00143               //System.out.println(contents);
00144             }else{
00145                 System.out.println("ERROR, statusCode = "+statusCode);
00146             }
00147     }catch(Exception e){
00148         System.out.println("Exception: "+e.toString());
00149     }
00150         }
00151         public static void main(String[] args) {
00152                 String text="";
00153                 int testNumber=3;
00154                 int longTestNumber=10;
00155                 int caseNumber=6;
00156                 int longCaseNumber=3;
00157                 String testCase="";
00158                 String[] sResults;
00159                 TestAnnotations ta=new TestAnnotations();
00160                 ta.setLev("0");
00161                                 
00162                 //Test UPMC cases
00163                 sResults=new String[testNumber+1];
00164                 for (int i=0;i<sResults.length;i++)
00165                         sResults[i]="";
00166                 
00167                 for (int k=0;k<caseNumber;k++){
00168                         //Text and ontologies
00169                         switch (k) {
00170                         case 0:
00171                                 ta.setOnts("40397"); //P1,P2,P3
00172                                 ta.setLocalSrc(new String[]{"","MedlinePlus",""}); //P4,P5,P6 
00173                                 ta.setLocalLan(new String []{"en",""});
00174                                 testCase="P1;P2;P3;P4;P5;P6";
00175                                 break;
00176                         case 1:
00177                                 ta.setOnts("46116"); //P7,P8,P9
00178                                 ta.setLocalSrc(new String[]{"","","Snomed"}); //P10,P11,P12
00179                                 ta.setLocalLan(new String []{"en",""});
00180                                 testCase="P7;P8;P9;P10;P11;P12";
00181                                 break;
00182                         case 2:
00183                                 ta.setOnts("40397,46116"); //P13,P14,P15
00184                                 ta.setLocalSrc(new String[]{"","MedlinePlus","Snomed"}); //P16,P17,P18
00185                                 ta.setLocalLan(new String []{"en",""});
00186                                 testCase="P13;P14;P15;P16;P17;P18";
00187                                 break;
00188                         case 3:
00189                                 ta.setLocalSrc(new String[]{"","MedlinePlus",""}); //P19,P20,P21
00190                                 ta.setLocalLan(new String []{"en","sp"});
00191                                 testCase="P19;P20;P21";
00192                                 break;
00193                         case 4:
00194                                 ta.setLocalSrc(new String[]{"","","Snomed"}); //P22,P23,P24
00195                                 ta.setLocalLan(new String []{"en","sp"});
00196                                 testCase="P22;P23;P24";
00197                                 break;
00198                         case 5:
00199                                 ta.setLocalSrc(new String[]{"","MedlinePlus","Snomed"}); //P25,P26,P27
00200                                 ta.setLocalLan(new String []{"en","sp"});
00201                                 testCase="P25;P26;P27";
00202                                 break;
00203                         }
00204                         
00205                         System.out.println("********************************");
00206                         System.out.println("TEST CASE: "+testCase);
00207                         System.out.println("********************************");
00208                         
00209                         for (int i=715;i<727;i++){
00210                                 //Text from file
00211                                 try {
00212                                         File fClinicalCase=new File("D:/UEM/MedicalMiner/ClinicalCases/upmc_2012/"+i);
00213                                         text=FileUtils.readFileToString(fClinicalCase);
00214                                 } catch (IOException e1) {
00215                                         System.out.println("Exception: "+e1.toString());
00216                                 }
00217                                 //Text
00218                                 ta.setText(text);
00219                                 ta.initIntegrateAnnot();
00220                                 //Avoid initialization times
00221                                 ta.avoidInitialization();
00222                                 
00223                                 System.out.println(" -> CLINICAL CASE "+i);
00224                                 System.out.println("----------------------");
00225                                 sResults[0]+=(sResults[0].equals(""))?testCase:";"+testCase;
00226                                 //Repeat Test testNumber times
00227                                 for (int n=0;n<testNumber;n++){
00228                                         System.out.println("+++++++++++++++++++++");
00229                                         System.out.println("TEST NUMER: N="+n);
00230                                         System.out.println("+++++++++++++++++++++");
00231                                         //Test GATE
00232                                         System.gc();
00233                                         sResults[n+1]+=(sResults[n+1].equals(""))?ta.testGate(""):";"+ta.testGate("");
00234                                         System.gc();
00235                                         sResults[n+1]+=";"+ta.testGate("html");
00236                                         System.gc();
00237                                         sResults[n+1]+=";"+ta.testGate("xml");
00238                                         
00239                                         //Test OBA
00240                                         if (k<3) {
00241                                                 System.gc();
00242                                                 sResults[n+1]+=";"+ta.testOba("");
00243                                                 System.gc();
00244                                                 sResults[n+1]+=";"+ta.testOba("html");
00245                                                 System.gc();
00246                                                 sResults[n+1]+=";"+ta.testOba("xml");
00247                                         }
00248                                 }
00249                         }
00250                         Writer out;
00251                         try {
00252                                 out = new BufferedWriter(new OutputStreamWriter(
00253                                     new FileOutputStream("data/case"+k+".txt", false), "UTF8"));
00254                         
00255                                 for (int y=0;y<sResults.length;y++){
00256                                         out.write(sResults[y]+"\n");
00257                                         sResults[y]="";
00258                                 }
00259                                 out.close();
00260                         } catch (Exception e) {
00261                                 System.out.println("Exception: "+e.toString());
00262                         }
00263                         
00264                 }
00265                 /*
00266                 //Test long texts
00267                 sResults=new String[longTestNumber+1];
00268                 for (int i=0;i<sResults.length;i++)
00269                         sResults[i]="";
00270                 
00271                 for (int k=0;k<longCaseNumber;k++){ 
00272                         //Text and ontologies
00273                         switch (k) {
00274                         case 0:
00275                                 ta.setLocalSrc(new String[]{"Freebase","MedlinePlus",""}); //P28,P29,P30
00276                                 ta.setLocalLan(new String []{"en","sp"});
00277                                 ta.setOnts("40397"); //P31,P32,P33
00278                                 testCase="P28;P29;P30;P31;P32;P33";
00279                                 break;
00280                         case 1:
00281                                 ta.setLocalSrc(new String[]{"","","Snomed"}); //P34,P35,P36
00282                                 ta.setLocalLan(new String []{"en","sp"});
00283                                 ta.setOnts("46116");  //P37,P38,P39
00284                                 testCase="P34;P35;P36;P37;P38;P39";
00285                                 break;
00286                         case 2:
00287                                 ta.setLocalSrc(new String[]{"Freebase","MedlinePlus","Snomed"}); //P40,P41,P42 
00288                                 ta.setLocalLan(new String []{"en","sp"});
00289                                 ta.setOnts("40397,46116"); //P43,P44,P45
00290                                 testCase="P40;P41;P42;P43;P44;P45";
00291                                 break;
00292                         }
00293                         System.out.println("********************************");
00294                         System.out.println("TEST CASE: "+testCase);
00295                         System.out.println("********************************");
00296                         for (int i=0;i<3;i++){
00297                                 //Text from file
00298                                 try {
00299                                         File fClinicalCase=new File("D:/UEM/MedicalMiner/ClinicalCases/upmc_2012/long"+i);
00300                                         text=FileUtils.readFileToString(fClinicalCase);
00301                                 } catch (IOException e1) {
00302                                         System.out.println("Exception: "+e1.toString());
00303                                 }
00304                                 //Text
00305                                 ta.setText(text);
00306                                 ta.initIntegrateAnnot();
00307                                 //Avoid initialization times
00308                                 ta.avoidInitialization();
00309                                 
00310                                 System.out.println(" -> CLINICAL CASE LONG "+i);
00311                                 System.out.println("----------------------");
00312                                 sResults[0]+=(sResults[0].equals(""))?testCase:";"+testCase;
00313                                 for (int n=0;n<longTestNumber;n++){
00314                                         System.out.println("+++++++++++++++++++++");
00315                                         System.out.println("TEST NUMER: N="+n);
00316                                         System.out.println("+++++++++++++++++++++");
00317                                         
00318                                         //Test GATE
00319                                         System.gc();
00320                                         sResults[n+1]+=(sResults[n+1].equals(""))?ta.testGate(""):";"+ta.testGate("");
00321                                         System.gc();
00322                                         sResults[n+1]+=";"+ta.testGate("html");
00323                                         System.gc();
00324                                         sResults[n+1]+=";"+ta.testGate("xml");
00325                                         //ta.testHTTPGATE("-1");
00326                 
00327                                         //Test OBA
00328                                         System.gc();
00329                                         sResults[n+1]+=";"+ta.testOba("");
00330                                         System.gc();
00331                                         sResults[n+1]+=";"+ta.testOba("html");
00332                                         System.gc();
00333                                         sResults[n+1]+=";"+ta.testOba("xml");
00334                                 }
00335                         }
00336                         Writer out;
00337                         try {
00338                                 out = new BufferedWriter(new OutputStreamWriter(
00339                                     new FileOutputStream("data/caseLong"+k+".txt", false), "UTF8"));
00340                         
00341                                 for (int y=0;y<sResults.length;y++){
00342                                         out.write(sResults[y]+"\n");
00343                                         sResults[y]="";
00344                                 }
00345                                 out.close();
00346                         } catch (Exception e) {
00347                                 System.out.println("Exception: "+e.toString());
00348                         }
00349                 }*/
00350         }
00351 }