CLEiM
Cross Lingual Education in Medicine
 All Classes Namespaces Files Functions Variables
CallFBToFile.java
Go to the documentation of this file.
00001 package com.uem.gsi.cleim.rpd;
00002 
00003 import java.io.BufferedWriter;
00004 import java.io.FileOutputStream;
00005 import java.io.IOException;
00006 import java.io.OutputStreamWriter;
00007 import java.io.Writer;
00008 
00009 import net.sf.json.*;
00010 
00011 import com.freebase.api.Freebase;
00012 import com.freebase.json.JSON;
00013 import com.uem.gsi.cleim.util.Constants;
00014 
00015 import static com.freebase.json.JSON.o;
00016 import static com.freebase.json.JSON.a;
00017 
00018 public class CallFBToFile {
00019         private JSON query;
00020         private JSON envelope;
00021         //Disease, symptom or medical_treatment
00022         private String fbparam;
00023         //Search limit
00024         private int limit;
00025         //Pagination
00026         private int page;
00027         //Language
00028         private String lan;
00029         public CallFBToFile(String pfbparam) {
00030                 this.fbparam=pfbparam;
00031                 this.query=o();
00032                 this.envelope=o();
00033                 this.limit=2000;
00034                 this.page=0;
00035                 this.lan="en";
00036         }
00037         public String doQuery(String fbtype){
00038                 if (fbtype.equals("Disease"))
00039                         queryFBDisease();
00040                 else if(fbtype.equals("Symptom"))
00041                         queryFBSymptom();
00042                 else if(fbtype.equals("Treatment"))
00043                         queryFBTreatment();
00044                 
00045                 Freebase freebase = Freebase.getFreebase();
00046                 JSON mqlJsonResult = freebase.mqlread(this.query,this.envelope,null);
00047                 return mqlJsonResult.toJSONString();
00048         }
00049 
00050         public void queryFBDisease(){
00051                 this.envelope=o("page", this.page,"cursor", true, "lang", "/lang/"+this.lan);
00052                 this.query = a(o(
00053                                 "type", "/medicine/disease", "name~=", "*"+this.fbparam+"*",
00054                                 "id",null,"name",null,"optional",false,"limit",this.limit, "sort","name",
00055                                 "/common/topic/article",o("guid",null,"limit",1,"optional",true)
00056                                 )
00057                         );
00058         }
00059         public void queryFBSymptom(){
00060                 this.envelope=o("page", this.page,"cursor", true, "lang", "/lang/"+this.lan);
00061                 this.query = a(o(
00062                                 "type", "/medicine/symptom", "name~=", "*"+this.fbparam+"*",
00063                                 "id",null,"name",null,"limit",this.limit, "sort","name",
00064                                 "/common/topic/article",o("guid",null,"limit",1,"optional",true))
00065                         );
00066         }
00067         public void queryFBTreatment(){
00068                 this.envelope=o("page", this.page,"cursor", true, "lang", "/lang/"+this.lan);
00069                 this.query = a(o(
00070                                 "type", "/medicine/medical_treatment", "name~=", "*"+this.fbparam+"*",
00071                                 "id",null,"name",null,"limit",this.limit, "sort","name",
00072                                 "/common/topic/article",o("guid",null,"limit",1,"optional",true))
00073                         );
00074         }
00075         public void loadListFile(String filePath,JSONArray jsonres,String group, boolean append){
00076                 try {
00077                         Writer out = new BufferedWriter(new OutputStreamWriter(
00078                 new FileOutputStream(filePath, append), "UTF8"));
00079                         //      Write diseases to the file
00080                         for (int i=0;i<jsonres.size();i++){
00081                                 JSONObject jsonresobj=jsonres.getJSONObject(i);
00082                                 //out.write(jsonresobj.get("name")+"&name="+jsonresobj.get("name")+"&url="+Constants.FREEBASE_VIEW+jsonresobj.get("id")+"&groups="+group+"\n");
00083                                 out.write(jsonresobj.get("name")+"&url="+Constants.FREEBASE_VIEW+jsonresobj.get("id")+"&groups="+group+"\n");
00084                         }
00085             // Close file writer
00086             out.close();
00087                 } catch (IOException e) {
00088                         e.printStackTrace();
00089                 }
00090         }
00091                 
00092         public void loadResults(String fbpath,String fbtype){
00093                 String jsonquery="";
00094                 JSONArray jsonres;
00095                 try{
00096                         this.page=0;
00097                         jsonquery=doQuery(fbtype);
00098                         jsonres=JSONObject.fromObject(jsonquery).getJSONArray("result");
00099                         System.out.println("Writing "+fbtype+" to file '"+fbpath+"'");
00100                         loadListFile(fbpath, jsonres, fbtype, false);
00101                         while(JSONObject.fromObject(jsonquery).getString("cursor")!="false"){
00102                                 this.page++;
00103                                 try{
00104                                         jsonquery=doQuery(fbtype);
00105                                         jsonres=JSONObject.fromObject(jsonquery).getJSONArray("result");
00106                                         System.out.println("Appending "+fbtype+" to file '"+fbpath+"'");
00107                                         loadListFile(fbpath, jsonres, fbtype, true);
00108                                 }catch(Exception ex){
00109                                         System.out.println("Exception: "+ex.toString());
00110                                         this.page--;
00111                                         System.out.println("Retrying page: "+this.page);
00112                                 }
00113                         }
00114                 }catch (Exception e){
00115                         System.out.println("Exception: "+e.toString());
00116                 }
00117         }
00118         public static void main (String[] args){
00119                 String fbparam="";
00120                 String webinf="WebContent/WEB-INF";
00121                 CallFBToFile h=new CallFBToFile(fbparam);
00122                 try{
00123                         //English files
00124                         h.lan="en";
00125                         System.out.println("Quering Freebase diseases in english");
00126                         h.loadResults(webinf+Constants.PATH_DISEASE_LST_EN, "Disease");
00127                         System.out.println("Quering Freebase symptoms in english");
00128                         h.loadResults(webinf+Constants.PATH_SYMPTOM_LST_EN, "Symptom");
00129                         System.out.println("Quering Freebase treatments in english");
00130                         h.loadResults(webinf+Constants.PATH_TREATMENT_LST_EN, "Treatment");
00131                         //Spanish files
00132                         h.lan="es";
00133                         System.out.println("Quering Freebase diseases in spanish");
00134                         h.loadResults(webinf+Constants.PATH_DISEASE_LST_SP, "Disease");
00135                         System.out.println("Quering Freebase symptoms in english");
00136                         h.loadResults(webinf+Constants.PATH_SYMPTOM_LST_SP, "Symptom");
00137                         System.out.println("Quering Freebase treatments in spanish");
00138                         h.loadResults(webinf+Constants.PATH_TREATMENT_LST_SP, "Treatment");
00139                 }catch (Exception e){
00140                         System.out.println("Exception: "+e.toString());
00141                 }
00142                 System.out.println("done");
00143         }
00144         
00145 }