From: <seb...@us...> - 2012-03-20 15:21:12
|
Revision: 3616 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3616&view=rev Author: sebastianwtr Date: 2012-03-20 15:21:00 +0000 (Tue, 20 Mar 2012) Log Message: ----------- [tbsl exploration] started implemanting the main interation function/module Modified Paths: -------------- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Index/SQLiteIndex.java 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/SparqlObject.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/Utils/Query.java trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Utils/QueryPair.java trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Utils/ServerUtil.java trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/exploration_main/testClass_new.java Added Paths: ----------- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/ElementList.java trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/Elements.java trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/modules/ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/modules/IterationModule.java trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/modules/LevenstheinModule.java trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/modules/WordnetModule.java Removed Paths: ------------- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/ElementList_new.java Modified: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Index/SQLiteIndex.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Index/SQLiteIndex.java 2012-03-18 21:44:30 UTC (rev 3615) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Index/SQLiteIndex.java 2012-03-20 15:21:00 UTC (rev 3616) @@ -19,7 +19,7 @@ public class SQLiteIndex { private Connection conn; StanfordLemmatizer lemma; - double lim_levensthein=0.88; + double lim_levensthein=0.92; public SQLiteIndex() throws ClassNotFoundException, SQLException { // TODO Auto-generated constructor stub @@ -76,8 +76,8 @@ while(rs.next()){ if(Levenshtein.nld(rs.getString("name"), original_string)>lim_levensthein){ result.add(rs.getString("uri")); - System.out.print(rs.getString("name")); - System.out.print(" "+rs.getString("uri")+"\n"); + /*System.out.print(rs.getString("name")); + System.out.print(" "+rs.getString("uri")+"\n");*/ } } return result; @@ -101,8 +101,8 @@ //System.out.println("Next: "+rs.getString("uri")); if(Levenshtein.nld(rs.getString("name"), original_string)>lim_levensthein){ result.add(rs.getString("uri")); - System.out.print(rs.getString("name")); - System.out.print(" "+rs.getString("uri")+"\n"); + /*System.out.print(rs.getString("name")); + System.out.print(" "+rs.getString("uri")+"\n");*/ } } return result; @@ -185,8 +185,8 @@ while(rs.next()){ if(Levenshtein.nld(rs.getString("name"), original_string)>lim_levensthein){ result.add(rs.getString("uri")); - System.out.print(rs.getString("name")); - System.out.print(" "+rs.getString("uri")+"\n"); + /*System.out.print(rs.getString("name")); + System.out.print(" "+rs.getString("uri")+"\n");*/ } } return result; Copied: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/ElementList.java (from rev 3613, trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/ElementList_new.java) =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/ElementList.java (rev 0) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/ElementList.java 2012-03-20 15:21:00 UTC (rev 3616) @@ -0,0 +1,73 @@ +package org.dllearner.algorithm.tbsl.exploration.Sparql; + +import java.util.HashMap; + +import org.dllearner.algorithm.tbsl.exploration.Utils.QueryPair; + + +/** + * + * @author swalter + * + */ +public class ElementList { + + /** + * Name of the variable, e.g. ?y0 + */ + private String variablename; + + /** + * URI of the Resource or Class, which was used for getting the depending elements with the uri + */ + private String uri; + /** + * HashMap with name -uri pairs. + */ + private HashMap<String,String> hm = new HashMap<String,String>(); + public String getVariablename() { + return variablename; + } + public void setVariablename(String variablename) { + this.variablename = variablename; + } + public HashMap<String,String> getHm() { + return hm; + } + public void setHm(HashMap<String,String> hm) { + this.hm = hm; + } + public String getURI() { + return uri; + } + public void setURI(String resourceURI) { + this.uri = resourceURI; + } + + public ElementList(String name_new, String resource, HashMap<String,String> hm){ + this.setHm(hm); + this.setURI(resource); + this.setVariablename(name_new); + } + + public void printAll(){ + System.out.println("Name: "+this.getVariablename()); + System.out.println("URI: "+this.getURI()); + System.out.println("List of Elements:"); + for(String key : this.hm.keySet()){ + System.out.println( key + ": "+this.hm.get(key)); + } + } + + public String printToString(){ + String result=""; + result+="Name: "+this.getVariablename()+"\n"; + result+="URI: "+this.getURI()+"\n"; + result+="List of Elements:"+"\n"; + for(String key : this.hm.keySet()){ + result+=key + ": "+this.hm.get(key)+"\n"; + } + + return result; + } +} Deleted: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/ElementList_new.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/ElementList_new.java 2012-03-18 21:44:30 UTC (rev 3615) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/ElementList_new.java 2012-03-20 15:21:00 UTC (rev 3616) @@ -1,49 +0,0 @@ -package org.dllearner.algorithm.tbsl.exploration.Sparql; - -import java.util.HashMap; - -/** - * - * @author swalter - * - */ -public class ElementList_new { - - /** - * Name of the variable, e.g. ?y0 - */ - private String variablename; - - /** - * URI of the Resource or Class, which was used for getting the depending elements with the uri - */ - private String resourceURI; - /** - * HashMap with name -uri pairs. - */ - private HashMap<String,String> hm = new HashMap<String,String>(); - public String getVariablename() { - return variablename; - } - public void setVariablename(String variablename) { - this.variablename = variablename; - } - public HashMap<String,String> getHm() { - return hm; - } - public void setHm(HashMap<String,String> hm) { - this.hm = hm; - } - public String getResourceURI() { - return resourceURI; - } - public void setResourceURI(String resourceURI) { - this.resourceURI = resourceURI; - } - - public ElementList_new(String variable, String resource, HashMap<String,String> hm){ - this.setHm(hm); - this.setResourceURI(resource); - this.setVariablename(variable); - } -} Added: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/Elements.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/Elements.java (rev 0) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/Elements.java 2012-03-20 15:21:00 UTC (rev 3616) @@ -0,0 +1,153 @@ +package org.dllearner.algorithm.tbsl.exploration.Sparql; + +import java.io.IOException; +import java.util.ArrayList; + +import org.dllearner.algorithm.tbsl.exploration.Utils.ServerUtil; + +/** + * Creates Set of Classes and Resources with their properties + * @author swalter + * + */ +public class Elements { + private ArrayList<ElementList> resources = new ArrayList<ElementList>(); + private ArrayList<ElementList> classes = new ArrayList<ElementList>(); + public ArrayList<ElementList> getResources() { + return resources; + } + public void setResources(ArrayList<ElementList> resources) { + this.resources = resources; + } + public ArrayList<ElementList> getClasses() { + return classes; + } + public void setClasses(ArrayList<ElementList> classes) { + this.classes = classes; + } + + private void addClasses(ElementList cl) { + this.classes.add(cl); + } + + private void addResources(ElementList cl) { + this.resources.add(cl); + } + + public Elements(ArrayList<ArrayList<String>> condition, ArrayList<ArrayList<Hypothesis>> hypothesen){ + + /* + * first all Classes! + */ + try { + this.setClasses(createElementsOfClasses(hypothesen)); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + /* + * second all Resources + */ + try { + this.setResources(createElementsOfResources(hypothesen,condition)); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + System.out.println("Created Elements"); + } + + private ArrayList<ElementList> createElementsOfClasses(ArrayList<ArrayList<Hypothesis>> hypothesenList) throws IOException{ + ArrayList<ElementList> classes = new ArrayList<ElementList>(); + + /* + * Iterate over all Hypothesis and look for an IsA + */ + for(ArrayList<Hypothesis> hl : hypothesenList){ + for(Hypothesis h : hl){ + /* + * if isA is found and if Class has uri, get Elements + */ + if(h.getType().contains("isA")&&h.getUri().contains("http")){ + /* + * TODO: improver performance, using geschicktes zwischenspeichern + */ + ElementList el = new ElementList(h.getName(),h.getUri(),ServerUtil.getElementsForGivenClass(h.getUri())); + classes.add(el); + } + } + } + + return classes; + } + + + private ArrayList<ElementList> createElementsOfResources(ArrayList<ArrayList<Hypothesis>> hypothesenList,ArrayList<ArrayList<String>> conditionList) throws IOException{ + ArrayList<ElementList> resources = new ArrayList<ElementList>(); + /* + * Iterate over all Hypothesis and look for an resource + */ + for(ArrayList<Hypothesis> hl : hypothesenList){ + for(Hypothesis h : hl){ + if(h.getType().contains("RESOURCE")&&h.getUri().contains("http")){ + for(ArrayList<String> cl : conditionList){ + if(h.getVariable().equals(cl.get(0))) { + ElementList el = new ElementList(h.getName()+"RIGHT",h.getUri(),ServerUtil.getPropertiesForGivenResource(h.getUri(), "RIGHT")); + resources.add(el); + } + if(h.getVariable().equals(cl.get(2))) { + ElementList el = new ElementList(h.getName()+"LEFT",h.getUri(),ServerUtil.getPropertiesForGivenResource(h.getUri(), "LEFT")); + resources.add(el); + } + } + + + } + } + } + + return resources; + } + + + public void printAll(){ + System.out.println("Resources: "); + for(ElementList el: this.resources){ + el.printAll(); + } + System.out.println("\nClasses: "); + for(ElementList el: this.classes){ + el.printAll(); + } + } + + public String printToString(){ + String result=""; + result+="Resources: \n"; + for(ElementList el: this.resources){ + result+=el.printToString()+"\n"; + } + result+="\nClasses: \n"; + for(ElementList el: this.classes){ + result+=el.printToString()+"\n"; + } + return result; + } + + + public boolean isElementEmty(){ + try { + if(this.getClasses().isEmpty()||this.getResources().isEmpty()) return true; + else return false; + } catch (Exception e) { + // TODO Auto-generated catch block + //e.printStackTrace(); + return true; + } + } + +} + + 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-03-18 21:44:30 UTC (rev 3615) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/Hypothesis.java 2012-03-20 15:21:00 UTC (rev 3616) @@ -4,7 +4,7 @@ public class Hypothesis { private String variable; private String uri; -private float rank; +private double rank; private String name; /** @@ -24,14 +24,14 @@ public void setVariable(String variable) { this.variable = variable; } -public float getRank() { +public double getRank() { return rank; } -public void setRank(float rank) { +public void setRank(double rank) { this.rank = rank; } -public Hypothesis(String variable, String name, String uri, String type, float rank){ +public Hypothesis(String variable, String name, String uri, String type, double rank){ this.setRank(rank); this.setVariable(variable); this.setUri(uri); Modified: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/SparqlObject.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/SparqlObject.java 2012-03-18 21:44:30 UTC (rev 3615) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/SparqlObject.java 2012-03-20 15:21:00 UTC (rev 3616) @@ -585,8 +585,8 @@ // GetRessourcePropertys property = new GetRessourcePropertys(); try { - propertiesleft=ServerUtil.sendServerPropertyRequest(s,"LEFT"); - propertiesright=ServerUtil.sendServerPropertyRequest(s,"RIGHT"); + propertiesleft=ServerUtil.getPropertiesForGivenResource(s,"LEFT"); + propertiesright=ServerUtil.getPropertiesForGivenResource(s,"RIGHT"); } catch (Exception e){ @@ -720,7 +720,7 @@ //gets Propertys left or right from the resource! try { - properties=ServerUtil.sendServerPropertyRequest(queryObject.getHashValue(resource.toLowerCase()),sideOfProperty); + properties=ServerUtil.getPropertiesForGivenResource(queryObject.getHashValue(resource.toLowerCase()),sideOfProperty); if (properties==null){ System.out.println("Begin:\n"+query +"\nError in getting Properties \n End"); @@ -848,8 +848,8 @@ //Get Properties for Resource in condition One and Two from Server try { - propertiesOne=ServerUtil.sendServerPropertyRequest(getUriFromIndex(resourceOne.toLowerCase(),0),sideOfPropertyOne); - propertiesTwo=ServerUtil.sendServerPropertyRequest(getUriFromIndex(resourceTwo.toLowerCase(),0),sideOfPropertyTwo); + propertiesOne=ServerUtil.getPropertiesForGivenResource(getUriFromIndex(resourceOne.toLowerCase(),0),sideOfPropertyOne); + propertiesTwo=ServerUtil.getPropertiesForGivenResource(getUriFromIndex(resourceTwo.toLowerCase(),0),sideOfPropertyTwo); if (propertiesOne==null){ System.out.println("Begin:\n"+query +"\nError in getting Properties \n End"); 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-03-18 21:44:30 UTC (rev 3615) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/Template.java 2012-03-20 15:21:00 UTC (rev 3616) @@ -4,7 +4,7 @@ public class Template { - private ArrayList<ElementList_new> list_of_element_uri_pair = new ArrayList<ElementList_new>(); + private ArrayList<ElementList> list_of_element_uri_pair = new ArrayList<ElementList>(); private ArrayList<ArrayList<String>> condition = new ArrayList<ArrayList<String>>(); private ArrayList<ArrayList<Hypothesis>> hypothesen = new ArrayList<ArrayList<Hypothesis>>(); private String selectTerm; @@ -14,6 +14,7 @@ private String limit; private String question; private String queryType; + private Elements elm; private float overallTime; private float time_Templator; @@ -118,14 +119,14 @@ public void setTime_Templator(float time_Templator) { this.time_Templator = time_Templator; } - public ArrayList<ElementList_new> getList_of_element_uri_pair() { + public ArrayList<ElementList> getList_of_element_uri_pair() { return list_of_element_uri_pair; } - public void setList_of_element_uri_pair(ArrayList<ElementList_new> list_of_element_uri_pair) { + public void setList_of_element_uri_pair(ArrayList<ElementList> list_of_element_uri_pair) { this.list_of_element_uri_pair = list_of_element_uri_pair; } - public void addToList_of_element_uri_pair(ElementList_new newElement) { + public void addToList_of_element_uri_pair(ElementList newElement) { this.list_of_element_uri_pair.add(newElement); } public String getQueryType() { @@ -134,6 +135,12 @@ public void setQueryType(String queryType) { this.queryType = queryType; } + public Elements getElm() { + return elm; + } + public void setElm(Elements elm) { + this.elm = elm; + } } 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-03-18 21:44:30 UTC (rev 3615) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/TemplateBuilder.java 2012-03-20 15:21:00 UTC (rev 3616) @@ -176,7 +176,7 @@ for(ArrayList<String> x : condition){ if(x.get(1).equals("isA") && x.get(2).equals("?"+tmp_array[0])){ no_iaA_found=false; - Hypothesis tmp_hypothesis = new Hypothesis("?"+tmp_array[0],tmp_array[1], tmp_array[1], "ISA", 0); + Hypothesis tmp_hypothesis = new Hypothesis("?"+tmp_array[0],tmp_array[1], tmp_array[1], "ISA", 0.0); //tmp_hypothesis.printAll(); list_of_hypothesis.add(tmp_hypothesis); @@ -185,23 +185,10 @@ */ add_reverse_template = false; } - /* - * Make sure you don't have the case that a class is left of an isA - */ - /*else if (!x.get(1).equals("isA") && x.get(0).equals("?"+tmp_array[0])){ - Hypothesis tmp_hypothesis = new Hypothesis("?"+tmp_array[0],tmp_array[1], tmp_array[1], "PROPERTY", 0); - //tmp_hypothesis.printAll(); - list_of_hypothesis.add(tmp_hypothesis); - } - else if(!x.get(1).equals("isA") && x.get(2).equals("?"+tmp_array[0]) ){ - Hypothesis tmp_hypothesis = new Hypothesis("?"+tmp_array[0],tmp_array[1], tmp_array[1], "PROPERTY", 0); - //tmp_hypothesis.printAll(); - list_of_hypothesis.add(tmp_hypothesis); - }*/ } if(no_iaA_found){ - Hypothesis tmp_hypothesis = new Hypothesis("?"+tmp_array[0],tmp_array[1], tmp_array[1], "PROPERTY", 0); + Hypothesis tmp_hypothesis = new Hypothesis("?"+tmp_array[0],tmp_array[1], tmp_array[1], "PROPERTY", 0.0); //tmp_hypothesis.printAll(); list_of_hypothesis.add(tmp_hypothesis); } @@ -211,7 +198,7 @@ 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],tmp_array[1], "PROPERTY", 0); + Hypothesis tmp_hypothesis = new Hypothesis("?"+tmp_array[0], tmp_array[1],tmp_array[1], "PROPERTY", 0.0); list_of_hypothesis.add(tmp_hypothesis); } @@ -219,7 +206,7 @@ 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], tmp_array[1], "RESOURCE", 0); + Hypothesis tmp_hypothesis = new Hypothesis("?"+tmp_array[0],tmp_array[1], tmp_array[1], "RESOURCE", 0.0); list_of_hypothesis.add(tmp_hypothesis); } } @@ -256,12 +243,12 @@ for(Hypothesis h : list_of_hypothesis){ if (h.getUri().equals(x.getUri())){ if(s!=null){ - Hypothesis new_h = new Hypothesis(h.getVariable(),h.getName(), s, h.getType(), 1); + Hypothesis new_h = new Hypothesis(h.getVariable(),h.getName(), s, h.getType(), 1.0); new_list.add(new_h); //new_h.printAll(); } else{ - Hypothesis new_h = new Hypothesis(h.getVariable(),h.getName(), h.getUri(), h.getType(), 1); + Hypothesis new_h = new Hypothesis(h.getVariable(),h.getName(), h.getUri(), h.getType(), 1.0); new_list.add(new_h); //new_h.printAll(); } @@ -297,10 +284,6 @@ for(Hypothesis h : x){ - //h.printAll(); - - //only if you have a Property or an Unspec, which still has no http:/dbpedia etc - //if(h.getType().contains("PROPERTY") || (h.getType().contains("UNSPEC")&& !h.getUri().contains("http"))){ if(h.getType().contains("PROPERTY")){ ArrayList<String> result= new ArrayList<String>(); try { @@ -313,15 +296,8 @@ } if(!result.isEmpty()){ h.setUri(result.get(0)); - h.setRank(1); + h.setRank(0.0); } - - /* else{ - String tmp = "http://dbpedia.org/ontology/"+h.getUri().toLowerCase().replace(" ", "_"); - - h.setUri(tmp); - h.setRank(0); - }*/ } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -369,9 +345,25 @@ template_reverse_conditions.setCondition(condition_reverse_new); template_reverse_conditions.setHypothesen(template.getHypothesen()); - - resultArrayList.add(template); - if(add_reverse_template) resultArrayList.add(template_reverse_conditions); + + /* + * Before adding Templates, generate for each Template a set of Properties and Elements + */ + Elements elm = new Elements(template.getCondition(),template.getHypothesen()); + /* + * If no Elements are created, dont add Template! + */ + //if(elm.isElementEmty()==false){ + template.setElm(elm); + resultArrayList.add(template); + //} + if(add_reverse_template){ + Elements elm_reverse = new Elements(template_reverse_conditions.getCondition(),template_reverse_conditions.getHypothesen()); + //if(elm_reverse.isElementEmty()==false){ + template.setElm(elm_reverse); + resultArrayList.add(template_reverse_conditions); + //} + } } } Modified: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Utils/Query.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Utils/Query.java 2012-03-18 21:44:30 UTC (rev 3615) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Utils/Query.java 2012-03-20 15:21:00 UTC (rev 3616) @@ -30,7 +30,7 @@ for(ArrayList<Hypothesis> hypothesenList : t.getHypothesen()){ String condition_new = condition; //System.out.println("New_Condition before replacing "+condition_new); - float global_rank=0; + double global_rank=0; boolean addQuery=true; for(Hypothesis h : hypothesenList){ condition_new=condition_new.replace(h.getVariable(), "<"+h.getUri()+">"); Modified: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Utils/QueryPair.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Utils/QueryPair.java 2012-03-18 21:44:30 UTC (rev 3615) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Utils/QueryPair.java 2012-03-20 15:21:00 UTC (rev 3616) @@ -2,17 +2,17 @@ public class QueryPair { private String Query; - private float rank; + private double rank; public String getQuery() { return Query; } public void setQuery(String query) { Query = query; } - public float getRank() { + public double getRank() { return rank; } - public void setRank(float rank) { + public void setRank(double rank) { this.rank = rank; } @@ -21,7 +21,7 @@ System.out.println("Rank :"+this.getRank()); } - public QueryPair(String query_new, float rank_new){ + public QueryPair(String query_new, double rank_new){ this.setQuery(query_new); this.setRank(rank_new); } Modified: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Utils/ServerUtil.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Utils/ServerUtil.java 2012-03-18 21:44:30 UTC (rev 3615) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Utils/ServerUtil.java 2012-03-20 15:21:00 UTC (rev 3616) @@ -73,30 +73,82 @@ /** - * Get an uri and saves the properties of this resource - * @param vergleich + * Uses an URI to get the properties of this resource + * @param uri * @return * @throws IOException */ - public static HashMap<String,String> sendServerPropertyRequest(String vergleich, String side) throws IOException{ + public static HashMap<String,String> getPropertiesForGivenResource(String uri, String side) throws IOException{ - String vergleichorig = vergleich; - String tmp_left=ServerUtil.getServer_Prefix()+"?default-graph-uri=&query="+ServerUtil.createServerRequest("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT DISTINCT ?s ?p WHERE {?y ?p <"+vergleichorig+">. ?p rdfs:label ?s.}")+"%0D%0A&format=text%2Fhtml&debug=on&timeout="; + String query_property_left=ServerUtil.getServer_Prefix()+"?default-graph-uri=&query="+ServerUtil.createServerRequest("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT DISTINCT ?s ?p WHERE {?y ?p <"+uri+">. ?p rdfs:label ?s. FILTER (lang(?s) = 'en') }")+"%0D%0A&format=text%2Fhtml&debug=on&timeout="; - String tmp_right=ServerUtil.getServer_Prefix()+"?default-graph-uri=&query="+ServerUtil.createServerRequest("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT DISTINCT ?s ?p WHERE {<"+vergleichorig+"> ?p ?y. ?p rdfs:label ?s.}")+"%0D%0A&format=text%2Fhtml&debug=on&timeout="; + String query_property_right=ServerUtil.getServer_Prefix()+"?default-graph-uri=&query="+ServerUtil.createServerRequest("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT DISTINCT ?s ?p WHERE {<"+uri+"> ?p ?y. ?p rdfs:label ?s. FILTER (lang(?s) = 'en') }")+"%0D%0A&format=text%2Fhtml&debug=on&timeout="; - String tmp_both=ServerUtil.getServer_Prefix()+"?default-graph-uri=&query="+ServerUtil.createServerRequest("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT DISTINCT ?s ?p WHERE {{?y ?p <"+vergleichorig+">. ?p rdfs:label ?s.} UNION {<"+vergleichorig+"> ?p ?y. ?p rdfs:label ?s.}}")+"%0D%0A&format=text%2Fhtml&debug=on&timeout="; + String query_property_leftANDright=ServerUtil.getServer_Prefix()+"?default-graph-uri=&query="+ServerUtil.createServerRequest("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT DISTINCT ?s ?p WHERE {{?y ?p <"+uri+">. ?p rdfs:label ?s. FILTER (lang(?s) = 'en') } UNION {<"+uri+"> ?p ?y. ?p rdfs:label ?s. FILTER (lang(?s) = 'en') }}")+"%0D%0A&format=text%2Fhtml&debug=on&timeout="; String verarbeitungsurl=null; /*Original*/ - if(side.contains("RIGHT")) verarbeitungsurl=tmp_right; - if(side.contains("LEFT")) verarbeitungsurl=tmp_left; - if(side.contains("BOTH")) verarbeitungsurl=tmp_both; + if(side.contains("RIGHT")) verarbeitungsurl=query_property_right; + if(side.contains("LEFT")) verarbeitungsurl=query_property_left; + if(side.contains("BOTH")) verarbeitungsurl=query_property_leftANDright; - if(!side.contains("LEFT") && !side.contains("RIGHT")) verarbeitungsurl=tmp_left; + if(!side.contains("LEFT") && !side.contains("RIGHT")) verarbeitungsurl=query_property_left; String result=""; + result = getListOfElements(verarbeitungsurl); + + return generateList(result); + } + + /** + * Uses an URI of a Class to get the Elements of the Class and the related URIs + * @param classUri + * @return + * @throws IOException + */ + public static HashMap<String,String> getElementsForGivenClass(String classUri) throws IOException{ + + + String query="PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT ?s ?x WHERE { ?x rdf:type <"+classUri+">. ?x rdfs:label ?s. FILTER (lang(?s) = 'en') }"; + + String result=""; + result = getListOfElements(query); + + return generateList(result); + } + + + + + + + private static HashMap<String, String> generateList(String result) { + HashMap<String,String> hm = new HashMap<String,String>(); + result=result.replace("<th>s</th>",""); + result=result.replace("<th>p</th>",""); + result=result.replace("<table class=\"sparql\" border=\"1\">",""); + result=result.replace("<tr>",""); + result=result.replace("</tr>",""); + result=result.replace("\n", ""); + result=result.replace(" ", ""); + result=result.replaceFirst("<td>", ""); + + + String[] tmp_array=result.split("</td><td>"); + + for(int i =1; i<=tmp_array.length-2;i=i+2) { + hm.put(tmp_array[i-1].toLowerCase(), tmp_array[i]); + } + + return hm; + } + + + + private static String getListOfElements(String verarbeitungsurl) { + + String result=""; HttpURLConnection connection = null; BufferedReader rd = null; StringBuilder sb = null; @@ -139,25 +191,9 @@ sb = null; connection = null; } - - HashMap<String,String> hm = new HashMap<String,String>(); - result=result.replace("<th>s</th>",""); - result=result.replace("<th>p</th>",""); - result=result.replace("<table class=\"sparql\" border=\"1\">",""); - result=result.replace("<tr>",""); - result=result.replace("</tr>",""); - result=result.replace("\n", ""); - result=result.replace(" ", ""); - result=result.replaceFirst("<td>", ""); - - - String[] tmp_array=result.split("</td><td>"); - - for(int i =1; i<=tmp_array.length-2;i=i+2) { - hm.put(tmp_array[i-1].toLowerCase(), tmp_array[i]); - } - - return hm; + return result; } + + } Modified: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/exploration_main/testClass_new.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/exploration_main/testClass_new.java 2012-03-18 21:44:30 UTC (rev 3615) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/exploration_main/testClass_new.java 2012-03-20 15:21:00 UTC (rev 3616) @@ -15,12 +15,14 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.dllearner.algorithm.tbsl.exploration.Sparql.Elements; import org.dllearner.algorithm.tbsl.exploration.Sparql.Hypothesis; import org.dllearner.algorithm.tbsl.exploration.Sparql.Template; import org.dllearner.algorithm.tbsl.exploration.Sparql.TemplateBuilder; import org.dllearner.algorithm.tbsl.exploration.Sparql.queryInformation; import org.dllearner.algorithm.tbsl.exploration.Utils.Query; import org.dllearner.algorithm.tbsl.exploration.Utils.QueryPair; +import org.dllearner.algorithm.tbsl.exploration.modules.IterationModule; public class testClass_new { @@ -37,28 +39,35 @@ TemplateBuilder testobject = new TemplateBuilder(); String filepath = "/home/swalter/Dokumente/Auswertung/"; - //String file="very_small.xml"; - String file="dbpedia-train.xml"; + String file="very_small.xml"; + //String file="dbpedia-train.xml"; long start = System.currentTimeMillis(); - /* - * TODO: WHy is there no Hypothese for the question: "Who is the mayor of Berlin?" - */ - /*String question = "Is the wife of president Obama called Michelle?"; - //String question = "Who is the mayor of Berlin?"; - //temp_list_result=testobject.createTemplates(question); + + //String question = "Is the wife of president Obama called Michelle?"; + String question = "Who is the leader of Hamburg?"; + temp_list_result=testobject.createTemplates(question); Map<QueryPair,String> tm = new HashMap<QueryPair, String>(); - for(Template t : temp_list_result){ + /*for(Template t : temp_list_result){ //t.printAll(); + try { + t.getElm().printAll(); + } catch (Exception e) { + // TODO Auto-generated catch block + //e.printStackTrace(); + } ArrayList<QueryPair> qp = Query.returnSetOfQueries(t); for(QueryPair p : qp){ tm.put(p, t.getQuestion()); } + }*/ + for(Template t : temp_list_result){ + t.getElm().getResources(); + IterationModule.doIteration(t.getElm(),t.getHypothesen(),t.getCondition()); } - writeQueriesInFile(tm,filepath,file,start,stop );*/ @@ -67,20 +76,28 @@ /* * Generate Templates */ - list_of_structs=generateStruct(filepath+"XMLDateien/"+file); + /*list_of_structs=generateStruct(filepath+"XMLDateien/"+file); + String result=""; for(queryInformation s : list_of_structs){ 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); + try { + result+=t.getElm().printToString()+"\n"; + } catch (Exception e) { + // TODO Auto-generated catch block + //e.printStackTrace(); + } } - } + }*/ + /* * Create Query for each Template */ - Map<QueryPair,String> tm = new HashMap<QueryPair, String>(); + /*Map<QueryPair,String> tm = new HashMap<QueryPair, String>(); for(Template t : temp_list_result){ //t.printAll(); @@ -88,7 +105,7 @@ for(QueryPair p : qp){ tm.put(p, t.getQuestion()); } - } + }*/ @@ -96,14 +113,14 @@ * Get Elements for Each Resource and Class */ - long stop = System.currentTimeMillis(); + /*long stop = System.currentTimeMillis(); System.out.println("Duration in ms: " + (stop - start)); - + writeStringToFile(result,filepath,file,start,stop); */ /* * Write Results in File */ - writeQueriesInFile(tm,filepath,file,start,stop ); + //writeQueriesInFile(tm,filepath,file,start,stop ); //writeTemplatesInFile(temp_list_result,filepath,file,start,stop ); } @@ -177,14 +194,23 @@ for(QueryPair key : tm.keySet()){ result+=tm.get(key)+": "+key.getQuery()+" "+key.getRank()+"\n"; } - + result+="OverAll Time: "+(stop-start)+"ms\n"; bw.write(result); bw.flush(); bw.close(); } + private static void writeStringToFile(String result,String filepath,String given_file, float start, float stop ) throws IOException{ + File file = new File(filepath+"Sonstiges"+stop+given_file.replace(".xml", "")+".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"); Added: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/modules/IterationModule.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/modules/IterationModule.java (rev 0) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/modules/IterationModule.java 2012-03-20 15:21:00 UTC (rev 3616) @@ -0,0 +1,127 @@ +package org.dllearner.algorithm.tbsl.exploration.modules; + +import java.sql.SQLException; +import java.util.ArrayList; + +import org.dllearner.algorithm.tbsl.exploration.Sparql.ElementList; +import org.dllearner.algorithm.tbsl.exploration.Sparql.Elements; +import org.dllearner.algorithm.tbsl.exploration.Sparql.Hypothesis; + +/** + * Gets Elements, Condition and Hypothesen and returns HypothesenSets. + * Also the different Modules, like Levensthein, Wordnet are used here. + * @author swalter + * + */ +public class IterationModule { + + /** + * returns ArrayList<ArrayList<Hypothesis>> which than can be added using + * @param elm + * @param hypothesen + * @param condition + * @return ArrayList<ArrayList<Hypothesis>> + */ + public static ArrayList<ArrayList<Hypothesis>> doIteration(Elements elm,ArrayList<ArrayList<Hypothesis>> givenHypothesenList,ArrayList<ArrayList<String>> givenConditionList){ + + for(ArrayList<Hypothesis> hl :givenHypothesenList){ + + /* + * First look for resources and generate List with properties + */ + for(Hypothesis h : hl){ + if(h.getType().contains("RESOURCE")){ + /* + * Get Variable from Resource + */ + String variable = h.getVariable(); + + /* + * Look in Condition for the Set, in which the variable appears + */ + ArrayList<String> propertyVariableList = new ArrayList<String>(); + for(ArrayList<String> cl : givenConditionList){ + for(String s : cl){ + if(s.contains(variable)){ + /* + * Mark Also if the Property is left or right from the Resource + */ + if(s.equals(cl.get(0))) propertyVariableList.add(cl.get(1)+"RIGHT::"+variable); + else propertyVariableList.add(cl.get(1)+"LEFT::"+variable); + } + } + } + + /* + * Now get for each found Property the Name + */ + ArrayList<String> propertyNameList = new ArrayList<String>(); + for(String s : propertyVariableList){ + for(Hypothesis h_t : hl){ + //System.out.println("s in creating propertyNameList: "+s); + String variable_t = s; + variable_t=variable_t.replace("RIGHT", ""); + variable_t=variable_t.replace("LEFT", ""); + if(h_t.getVariable().contains(variable_t)) propertyNameList.add(h_t.getName()+"::"+s); + + } + } + + /* + * Now find for each Property the list of Propertys of the resource in Elements and compare with Levensthein/Wordnet etc + */ + ArrayList<ElementList> resources = elm.getResources(); + for(String s : propertyNameList){ + String[] array = s.split("::"); + + System.out.println("s: "+s); + System.out.println("Array:"); + for(String t : array){ + System.out.println(t); + } + + /* + * array[0] contains name of Property + * array[1] contains LEFT/RIGHT and Variable of Property + * array[2] contains Name of Resource + */ + + String side="LEFT"; + if(array[1].contains("RIGHT")) side="RIGHT"; + + for(ElementList el : resources){ + if(el.getVariablename().contains(array[2]) && el.getVariablename().contains(side)){ + String propertyVariable = array[1]; + propertyVariable=propertyVariable.replace("LEFT", ""); + propertyVariable=propertyVariable.replace("RIGHT", ""); + ArrayList<Hypothesis> resultHypothesenList = new ArrayList<Hypothesis>(); + try { + resultHypothesenList= LevenstheinModule.doLevensthein(propertyVariable,array[0],el.getHm()); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + for(Hypothesis h_p : resultHypothesenList) h_p.printAll(); + + } + } + + } + + } + } + + + + /* + * Iterate over all "founded" properties and generate new Hypothesensets, using the "old" resource and isA case + */ + } + + return null; + + } + + +} Added: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/modules/LevenstheinModule.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/modules/LevenstheinModule.java (rev 0) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/modules/LevenstheinModule.java 2012-03-20 15:21:00 UTC (rev 3616) @@ -0,0 +1,41 @@ +package org.dllearner.algorithm.tbsl.exploration.modules; + +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map.Entry; + +import org.dllearner.algorithm.tbsl.exploration.Sparql.Hypothesis; +import org.dllearner.algorithm.tbsl.exploration.Utils.Levenshtein; + +public class LevenstheinModule { + private final static double LevenstheinMin=0.8; + + public static ArrayList<Hypothesis> doLevensthein(String variable, String property_to_compare_with, HashMap<String, String> properties) + throws SQLException { + ArrayList<Hypothesis> listOfNewHypothesen= new ArrayList<Hypothesis>(); + + + //iterate over properties + for (Entry<String, String> entry : properties.entrySet()) { + String key = entry.getKey(); + key=key.replace("\"",""); + key=key.replace("@en",""); + String value = entry.getValue(); + + //compare property gotten from the resource with the property from the original query + double nld=Levenshtein.nld(property_to_compare_with.toLowerCase(), key); + + //if(nld>=LevenstheinMin||key.contains(lemmatiser.stem(property_to_compare_with))||property_to_compare_with.contains(lemmatiser.stem(key))){ + + if(nld>=LevenstheinMin){ + Hypothesis h = new Hypothesis(variable, key, value, "PROPERTY", nld); + listOfNewHypothesen.add(h); + } + + } + + return listOfNewHypothesen; + } + +} Added: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/modules/WordnetModule.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/modules/WordnetModule.java (rev 0) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/modules/WordnetModule.java 2012-03-20 15:21:00 UTC (rev 3616) @@ -0,0 +1,8 @@ +package org.dllearner.algorithm.tbsl.exploration.modules; + +public class WordnetModule { + + private int explorationdepthwordnet =2; + + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |