From: <seb...@us...> - 2012-02-28 15:33:09
|
Revision: 3595 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3595&view=rev Author: sebastianwtr Date: 2012-02-28 15:32:59 +0000 (Tue, 28 Feb 2012) Log Message: ----------- [tbsl exploration] fixed bug in generating Hypothesis Modified Paths: -------------- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/Template.java trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/TemplateBuilder.java trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/testClass_new.java trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/utils_new.java Modified: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/Template.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/Template.java 2012-02-28 13:39:21 UTC (rev 3594) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/Template.java 2012-02-28 15:32:59 UTC (rev 3595) @@ -78,6 +78,7 @@ int anzahl = 1; for(ArrayList<Hypothesis> x : hypothesen){ System.out.println("\nSet of Hypothesen"+anzahl+":"); + anzahl+=1; for ( Hypothesis z : x){ z.printAll(); } Modified: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/TemplateBuilder.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/TemplateBuilder.java 2012-02-28 13:39:21 UTC (rev 3594) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/TemplateBuilder.java 2012-02-28 15:32:59 UTC (rev 3595) @@ -32,13 +32,13 @@ Set<BasicQueryTemplate> querytemps = btemplator.buildBasicQueries(question); for (BasicQueryTemplate bqt : querytemps) { ArrayList<ArrayList<String>> condition = new ArrayList<ArrayList<String>>(); - ArrayList<ArrayList<Hypothesis>> hypotesen = new ArrayList<ArrayList<Hypothesis>>(); + //ArrayList<ArrayList<Hypothesis>> hypotesen = new ArrayList<ArrayList<Hypothesis>>(); String selectTerm = ""; String having= ""; String filter= ""; String OrderBy= ""; String limit= ""; - String condition_String = ""; + //String condition_String = ""; boolean addTemplate=true; try{ @@ -49,23 +49,24 @@ addTemplate=false; } - ArrayList<String> temp_array = new ArrayList<String>(); + //ArrayList<String> temp_array = new ArrayList<String>(); try{ - for(Path conditions1: bqt.getConditions()) condition_String=condition_String+(conditions1.toString())+"."; for(Path conditions1: bqt.getConditions()) { - temp_array.clear(); + ArrayList<String> temp_array = new ArrayList<String>(); String[] tmp_array = conditions1.toString().split(" -- "); for(String s: tmp_array){ + //System.out.println(s); temp_array.add(s); } condition.add(temp_array); - } - + + } } catch (Exception e){ - condition_String=""; + //condition_String=""; addTemplate=false; } + try{ for(SPARQL_Filter tmp : bqt.getFilters()) filter=filter+tmp+" "; @@ -108,17 +109,20 @@ if(addTemplate!=false){ + + /* + * SLOT_title: PROPERTY {title,name,label} mitfuehren + */ //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Template template = new Template(condition, having, filter, selectTerm,OrderBy, limit); //TODO: Iterate over slots ArrayList<Hypothesis> list_of_hypothesis = new ArrayList<Hypothesis>(); for(Slot slot : bqt.getSlots()){ - - if(slot.toString().contains("USPEC")){ + if(slot.toString().contains("UNSPEC")){ String tmp= slot.toString().replace(" UNSPEC {", ""); tmp=tmp.replace("}",""); String[] tmp_array = tmp.split(":"); - Hypothesis tmp_hypothesis = new Hypothesis("?"+tmp_array[0], tmp_array[1], "USPEC", 0); + Hypothesis tmp_hypothesis = new Hypothesis("?"+tmp_array[0], tmp_array[1], "UNSPEC", 0); list_of_hypothesis.add(tmp_hypothesis); } if(slot.toString().contains("PROPERTY")){ @@ -138,7 +142,6 @@ } } ArrayList<ArrayList<Hypothesis>> final_list_set_hypothesis = new ArrayList<ArrayList<Hypothesis>>(); - //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% for(Hypothesis x : list_of_hypothesis){ @@ -152,12 +155,17 @@ } for(String s : result){ ArrayList<Hypothesis> new_list = new ArrayList<Hypothesis>(); - new_list=list_of_hypothesis; - for(Hypothesis z : new_list){ - if(z.getUri().equals(x.getUri())){ - z.setUri(s); - z.setRank(1); + + //String variable, String uri, String type, float rank + for(Hypothesis h : list_of_hypothesis){ + if (h.getUri().equals(x.getUri())){ + Hypothesis new_h = new Hypothesis(h.getVariable(), s, h.getType(), 1); + new_list.add(new_h); } + else{ + Hypothesis new_h = new Hypothesis(h.getVariable(), h.getUri(), h.getType(), h.getRank()); + new_list.add(new_h); + } } final_list_set_hypothesis.add(new_list); } @@ -165,10 +173,11 @@ } + //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% for(ArrayList<Hypothesis> x : final_list_set_hypothesis){ for(Hypothesis h : x){ - if(h.getType().contains("PROPERTY")){ + if(h.getType().contains("PROPERTY") || h.getType().contains("UNSPEC")){ ArrayList<String> result= new ArrayList<String>(); try { result = utils_new.searchIndex(h.getUri(), 1, myindex); @@ -178,6 +187,8 @@ } else{ + String tmp = "http://dbpedia.org/ontology/"+h.getUri().toLowerCase(); + h.setUri(tmp); h.setRank(0); } } catch (SQLException e) { @@ -209,9 +220,9 @@ resultArrayList.add(template_reverse_conditions); } } - for(Template temp : resultArrayList){ + /*for(Template temp : resultArrayList){ temp.printAll(); - } + }*/ return resultArrayList; } } Modified: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/testClass_new.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/testClass_new.java 2012-02-28 13:39:21 UTC (rev 3594) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/testClass_new.java 2012-02-28 15:32:59 UTC (rev 3595) @@ -1,7 +1,17 @@ package org.dllearner.algorithm.tbsl.exploration.Sparql; -import java.net.MalformedURLException; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStreamReader; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class testClass_new { @@ -9,22 +19,205 @@ * @param args * @throws SQLException * @throws ClassNotFoundException - * @throws MalformedURLException + * @throws IOException */ - public static void main(String[] args) throws MalformedURLException, ClassNotFoundException, SQLException { + public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException { // TODO Auto-generated method stub + ArrayList<Template> temp_list_result = new ArrayList<Template>(); + TemplateBuilder testobject = new TemplateBuilder(); - String question = "Which books are written by Daniele Steel?"; - testobject.createTemplates(question); + //String question = "Is the wife of president Obama called Michelle?"; + //String question = "Who is the mayor of Berlin?"; + //temp_list_result=testobject.createTemplates(question); - question = "Give me all german femal chancelors"; - testobject.createTemplates(question); - question = "Who owns Aldi?"; - testobject.createTemplates(question); - question = "In which programming language is \"GIMP\" written?"; - testobject.createTemplates(question); + ArrayList<queryInformation> list_of_structs = new ArrayList<queryInformation>(); + //if you dont want to use the hints in the questions, use false + list_of_structs=generateStruct("/home/swalter/Dokumente/dbpedia-train.xml"); + System.out.println("Start Templating"); + for(queryInformation s : list_of_structs){ + System.out.println("In For Schleife"); + ArrayList<Template> temp_list = new ArrayList<Template>(); + temp_list=testobject.createTemplates(s.getQuery().replace("<[CDATA[", "").replace("]]>", "")); + for(Template t : temp_list){ + temp_list_result.add(t); + } + + } + + String result =""; + for(Template t: temp_list_result){ + //t.printAll(); + result+="###### Template ######\n"; + result+="condition: "+t.getCondition()+"\n"; + //System.out.println("hypotesen: "+hypothesen); + int anzahl = 1; + for(ArrayList<Hypothesis> x : t.getHypothesen()){ + result+="\nSet of Hypothesen"+anzahl+":\n"; + anzahl+=1; + for ( Hypothesis z : x){ + result+="%%%%%%%%%%%"+"\n"; + result+="Variable: "+z.getVariable()+"\n"; + result+="Uri: " + z.getUri()+"\n"; + result+="Type: " + z.getType()+"\n"; + result+="Rank: "+z.getRank()+"\n"; + result+="%%%%%%%%%%%"+"\n"; + } + } + result+="\n"; + result+="selectTerm: "+t.getSelectTerm()+"\n"; + result+="having: "+t.getHaving()+"\n"; + result+="filter: "+t.getFilter()+"\n"; + result+="OrderBy: "+t.getOrderBy()+"\n"; + result+="limit: "+t.getLimit()+"\n"; + result+="###### Template printed ######\n"; + } + + //System.out.println(result); + + File file = new File("/home/swalter/Dokumente/Ausgabe_temp.txt"); + BufferedWriter bw = new BufferedWriter(new FileWriter(file)); + + bw.write(result); + bw.flush(); + bw.close(); + } + + +private static ArrayList<queryInformation> generateStruct(String filename) { + System.out.println("In generate Struct"); + String XMLType=null; + + BufferedReader in = null; + + String tmp=""; + // Lies Textzeilen aus der Datei in einen Vector: + try { + in = new BufferedReader( + new InputStreamReader( + new FileInputStream(filename) ) ); + String s; + while( null != (s = in.readLine()) ) { + tmp=tmp+s; + //System.out.println(tmp); + } + } catch( FileNotFoundException ex ) { + } catch( Exception ex ) { + System.out.println( ex ); + } finally { + if( in != null ) + try { + in.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + // System.out.println("XML read in"); + //System.out.println(tmp); + String string=tmp; + Pattern p = Pattern.compile (".*\\<question(.*)\\</question\\>.*"); + Matcher m = p.matcher (string); + + + if(string.contains("id=\"dbpedia-train\"><question")){ + string=string.replace("id=\"dbpedia-train\"><question", ""); + XMLType="dbpedia-train"; + System.out.println("dbpedia-train"); + } + if(string.contains("id=\"dbpedia-test\"><question")){ + string=string.replace("id=\"dbpedia-test\"><question", ""); + XMLType="dbpedia-test"; + System.out.println("dbpedia-test"); + } + ArrayList<queryInformation> querylist = new ArrayList<queryInformation>(); + if(string.contains("</question><question")){ + System.out.println("true"); + } + else System.out.println("false"); + String [] bla = string.split("</question><question"); + System.out.println(bla.length); + for(String s : bla){ + System.out.println("in bla"); + String query=""; + String type=""; + boolean fusion=false; + boolean aggregation=false; + boolean yago=false; + String id=""; + + //Pattern p1= Pattern.compile("(id.*)\\</string\\>\\<keywords\\>.*\\</keywords\\>\\<query\\>.*"); + Pattern p1= Pattern.compile("(id.*)\\</string\\>\\<keywords\\>.*"); + Matcher m1 = p1.matcher(s); + //System.out.println(""); + while(m1.find()){ + //System.out.println(m1.group(1)); + Pattern p2= Pattern.compile(".*><string>(.*)"); + Matcher m2 = p2.matcher(m1.group(1)); + while(m2.find()){ + System.out.println("Query: "+ m2.group(1)); + query=m2.group(1).replace("<[CDATA[", ""); + query=query.replace("CDATA", ""); + query=query.replace("CDATA", ""); + query=query.replace("[", ""); + query=query.replace("<", ""); + } + Pattern p3= Pattern.compile("id=\"(.*)\" answer.*"); + Matcher m3 = p3.matcher(m1.group(1)); + while(m3.find()){ + //System.out.println("Id: "+ m3.group(1)); + id=m3.group(1); + } + + Pattern p4= Pattern.compile(".*answertype=\"(.*)\" fusion.*"); + Matcher m4 = p4.matcher(m1.group(1)); + while(m4.find()){ + //System.out.println("answertype: "+ m4.group(1)); + type=m4.group(1); + } + + Pattern p5= Pattern.compile(".*fusion=\"(.*)\" aggregation.*"); + Matcher m5 = p5.matcher(m1.group(1)); + while(m5.find()){ + //System.out.println("fusion: "+ m5.group(1)); + if(m5.group(1).contains("true"))fusion=true; + else fusion=false; + } + + Pattern p6= Pattern.compile(".*aggregation=\"(.*)\" yago.*"); + Matcher m6 = p6.matcher(m1.group(1)); + while(m6.find()){ + //System.out.println("aggregation: "+ m6.group(1)); + if(m6.group(1).contains("true"))aggregation=true; + else aggregation=false; + } + + Pattern p7= Pattern.compile(".*yago=\"(.*)\" ><string>.*"); + Matcher m7 = p7.matcher(m1.group(1)); + while(m7.find()){ + //System.out.println("yago: "+ m7.group(1)); + if(m7.group(1).contains("true"))yago=true; + else yago=false; + } + + + + } + queryInformation blaquery=new queryInformation(query, id,type,fusion,aggregation,yago,XMLType,false); + if(id!=""&&id!=null) querylist.add(blaquery); + } + /* for(queryInformation s : querylist){ + System.out.println(""); + if(s.getId()==""||s.getId()==null)System.out.println("NO"); + System.out.println("ID: "+s.getId()); + System.out.println("Query: "+s.getQuery()); + System.out.println("Type: "+s.getType()); + System.out.println("XMLType: "+s.getXMLtype()); + }*/ + return querylist; } +} \ No newline at end of file Modified: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/utils_new.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/utils_new.java 2012-02-28 13:39:21 UTC (rev 3594) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/utils_new.java 2012-02-28 15:32:59 UTC (rev 3595) @@ -35,6 +35,7 @@ tmp2=myindex.getYagoURI(string.toLowerCase()); if(tmp1!=null) result_List.add(tmp1); if(tmp2!=null) result_List.add(tmp2); + //result_List.add("www.TEST.de"); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |