From: <seb...@us...> - 2012-02-23 15:16:25
|
Revision: 3584 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3584&view=rev Author: sebastianwtr Date: 2012-02-23 15:16:14 +0000 (Thu, 23 Feb 2012) Log Message: ----------- [tbsl exploration] now generates templates with a set of hypothesis Modified Paths: -------------- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/Hypothesis.java 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 Added Paths: ----------- 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/Hypothesis.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/Hypothesis.java 2012-02-22 21:03:09 UTC (rev 3583) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/Hypothesis.java 2012-02-23 15:16:14 UTC (rev 3584) @@ -1,9 +1,17 @@ package org.dllearner.algorithm.tbsl.exploration.Sparql; +import edu.stanford.nlp.io.EncodingPrintWriter.out; + public class Hypothesis { private String variable; private String uri; private float rank; + +/** + * RESOURCE,PROPERTY,UNSPEC + */ +private String type; + public String getUri() { return uri; } @@ -23,9 +31,27 @@ this.rank = rank; } -public Hypothesis(String variable, String uri, float rank){ +public Hypothesis(String variable, String uri, String type, float rank){ setRank(rank); setVariable(variable); setUri(uri); + setType(type); } + +public String getType() { + return type; } +public void setType(String type) { + this.type = type; +} + +public void printAll(){ + System.out.println("%%%%%%%%%%%"); + System.out.println("Variable: "+variable); + System.out.println("Uri: " + uri); + System.out.println("Type: " + type); + System.out.println("Rank: "+rank); + System.out.println("%%%%%%%%%%%"); +} + +} 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-22 21:03:09 UTC (rev 3583) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/Template.java 2012-02-23 15:16:14 UTC (rev 3584) @@ -5,7 +5,7 @@ public class Template { private ArrayList<ArrayList<String>> condition = new ArrayList<ArrayList<String>>(); - private ArrayList<ArrayList<Hypothesis>> hypotesen = new ArrayList<ArrayList<Hypothesis>>(); + private ArrayList<ArrayList<Hypothesis>> hypothesen = new ArrayList<ArrayList<Hypothesis>>(); private String selectTerm; private String having; private String filter; @@ -59,22 +59,30 @@ public void setCondition(ArrayList<ArrayList<String>> condition) { this.condition = condition; } - public ArrayList<ArrayList<Hypothesis>> getHypotesen() { - return hypotesen; + public ArrayList<ArrayList<Hypothesis>> getHypothesen() { + return hypothesen; } - public void setHypotesen(ArrayList<ArrayList<Hypothesis>> hypotesen) { - this.hypotesen = hypotesen; + public void setHypothesen(ArrayList<ArrayList<Hypothesis>> hypotesen) { + this.hypothesen = hypotesen; } public void addHypothese(ArrayList<Hypothesis> ht){ - this.hypotesen.add(ht); + this.hypothesen.add(ht); } public void printAll(){ System.out.println("###### Template ######"); System.out.println("condition: "+condition); - System.out.println("hypotesen: "+hypotesen); + //System.out.println("hypotesen: "+hypothesen); + int anzahl = 1; + for(ArrayList<Hypothesis> x : hypothesen){ + System.out.println("\nSet of Hypothesen"+anzahl+":"); + for ( Hypothesis z : x){ + z.printAll(); + } + } + System.out.print("\n"); System.out.println("selectTerm: "+selectTerm); System.out.println("having: "+having); System.out.println("filter: "+filter); 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-22 21:03:09 UTC (rev 3583) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/TemplateBuilder.java 2012-02-23 15:16:14 UTC (rev 3584) @@ -10,18 +10,20 @@ import org.dllearner.algorithm.tbsl.sparql.SPARQL_Filter; import org.dllearner.algorithm.tbsl.sparql.SPARQL_Having; import org.dllearner.algorithm.tbsl.sparql.SPARQL_Term; +import org.dllearner.algorithm.tbsl.sparql.Slot; import org.dllearner.algorithm.tbsl.templator.BasicTemplator; public class TemplateBuilder { - static BasicTemplator btemplator; +static BasicTemplator btemplator; +private static mySQLDictionary myindex; public TemplateBuilder() throws MalformedURLException, ClassNotFoundException, SQLException{ TemplateBuilder.btemplator = new BasicTemplator(); //btemplator.UNTAGGED_INPUT = false; - //Object_new.myindex = new mySQLDictionary(); + TemplateBuilder.myindex = new mySQLDictionary(); } @@ -84,7 +86,6 @@ OrderBy="ORDER BY "; try{ for(SPARQL_Term tmp : bqt.getOrderBy()) { - System.out.println("Yeah"); OrderBy=OrderBy+tmp+" "; } if((bqt.getOrderBy()).size()==0)OrderBy=""; @@ -105,13 +106,108 @@ addTemplate=false; } - Template template = new Template(condition, having, filter, selectTerm,OrderBy, limit); - - //TODO: Add Hypothesis - //TODO: Take Template like it is and change Condition - - - resultArrayList.add(template); + if(addTemplate!=false){ + + //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + 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")){ + 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); + list_of_hypothesis.add(tmp_hypothesis); + } + if(slot.toString().contains("PROPERTY")){ + String tmp= slot.toString().replace(" PROPERTY {", ""); + tmp=tmp.replace("}",""); + String[] tmp_array = tmp.split(":"); + Hypothesis tmp_hypothesis = new Hypothesis("?"+tmp_array[0], tmp_array[1], "PROPERTY", 0); + list_of_hypothesis.add(tmp_hypothesis); + + } + if(slot.toString().contains("RESOURCE")){ + String tmp= slot.toString().replace(" RESOURCE {", ""); + tmp=tmp.replace("}",""); + String[] tmp_array = tmp.split(":"); + Hypothesis tmp_hypothesis = new Hypothesis("?"+tmp_array[0], tmp_array[1], "RESOURCE", 0); + list_of_hypothesis.add(tmp_hypothesis); + } + } + ArrayList<ArrayList<Hypothesis>> final_list_set_hypothesis = new ArrayList<ArrayList<Hypothesis>>(); + + //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + for(Hypothesis x : list_of_hypothesis){ + if(x.getType().contains("RESOURCE")){ + ArrayList<String> result= new ArrayList<String>(); + try { + result = utils_new.searchIndex(x.getUri(), 3, myindex); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + 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); + } + } + final_list_set_hypothesis.add(new_list); + } + } + } + + + //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + for(ArrayList<Hypothesis> x : final_list_set_hypothesis){ + for(Hypothesis h : x){ + if(h.getType().contains("PROPERTY")){ + ArrayList<String> result= new ArrayList<String>(); + try { + result = utils_new.searchIndex(h.getUri(), 1, myindex); + if(!result.isEmpty()){ + h.setUri(result.get(0)); + h.setRank(1); + } + + else{ + h.setRank(0); + } + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + } + + template.setHypothesen(final_list_set_hypothesis); + //TODO: Take Template like it is and change Condition + Template template_reverse_conditions = new Template(template.getCondition(), template.getHaving(), template.getFilter(), template.getSelectTerm(), template.getOrderBy(), template.getLimit()); + + //= template; + ArrayList<ArrayList<String>> condition_template_reverse_conditions = template_reverse_conditions.getCondition(); + ArrayList<ArrayList<String>> condition_reverse_new= new ArrayList<ArrayList<String>>(); + for (ArrayList<String> x : condition_template_reverse_conditions){ + ArrayList<String> new_list = new ArrayList<String>(); + new_list.add(x.get(2)); + new_list.add(x.get(1)); + new_list.add(x.get(0)); + condition_reverse_new.add(new_list); + } + + template_reverse_conditions.setCondition(condition_reverse_new); + + resultArrayList.add(template); + resultArrayList.add(template_reverse_conditions); + } } for(Template temp : resultArrayList){ temp.printAll(); 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-22 21:03:09 UTC (rev 3583) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/testClass_new.java 2012-02-23 15:16:14 UTC (rev 3584) @@ -16,6 +16,15 @@ TemplateBuilder testobject = new TemplateBuilder(); String question = "Which books are written by Daniele Steel?"; 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); } } Added: 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 (rev 0) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/utils_new.java 2012-02-23 15:16:14 UTC (rev 3584) @@ -0,0 +1,54 @@ +package org.dllearner.algorithm.tbsl.exploration.Sparql; + +import java.sql.SQLException; +import java.util.ArrayList; + +public class utils_new { + + /** + * + * @param string + * @param fall 1=Property, 0=Resource, 2=OntologyClass/Yago, 2=resource+yago+ontlogy + * @return + * @throws SQLException + */ + public static ArrayList<String> searchIndex(String string, int fall, mySQLDictionary myindex) throws SQLException{ + + String originalString=string; + string=string.replace("_", " "); + string=string.replace("-", " "); + string=string.replace(".", " "); + String result=null; + String tmp1=null; + String tmp2 = null; + ArrayList<String> result_List = new ArrayList<String>(); + + if(fall==0 || fall==3){ + + result=myindex.getResourceURI(string.toLowerCase()); + result_List.add(result); + + } + if(fall==2||fall==3){ + + tmp1=myindex.getontologyClassURI(string.toLowerCase()); + tmp2=myindex.getYagoURI(string.toLowerCase()); + if(tmp1!=null) result_List.add(tmp1); + if(tmp2!=null) result_List.add(tmp2); + } + + + if(fall==1){ + tmp1=myindex.getPropertyURI(string.toLowerCase()); + tmp2=myindex.getontologyURI(string.toLowerCase()); + if(tmp1!=null) result_List.add(tmp1); + if(tmp2!=null) result_List.add(tmp2); + + } + + return result_List; + } + + + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |