From: <seb...@us...> - 2011-12-08 10:43:31
|
Revision: 3490 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3490&view=rev Author: sebastianwtr Date: 2011-12-08 10:43:20 +0000 (Thu, 08 Dec 2011) Log Message: ----------- [tbsl exploration] removed nullpointer error Modified Paths: -------------- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/GetRessourcePropertys.java trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/SparqlObject.java Modified: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/GetRessourcePropertys.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/GetRessourcePropertys.java 2011-12-08 09:01:12 UTC (rev 3489) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/GetRessourcePropertys.java 2011-12-08 10:43:20 UTC (rev 3490) @@ -47,12 +47,8 @@ */ String vergleichorig = vergleich; - /*String bla123 = vergleich; - //to get only the name - bla123=bla123.replace("http://dbpedia.org/resource/Category:",""); - bla123=bla123.replace("http://dbpedia.org/resource/",""); - vergleich=bla123;*/ + String tmp_left="http://greententacle.techfak.uni-bielefeld.de:5171/sparql?default-graph-uri=&query="+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="; //System.out.println("property right!!! : " +tmp_right); String tmp_right="http://greententacle.techfak.uni-bielefeld.de:5171/sparql?default-graph-uri=&query="+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="; 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 2011-12-08 09:01:12 UTC (rev 3489) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/SparqlObject.java 2011-12-08 10:43:20 UTC (rev 3490) @@ -925,176 +925,6 @@ } - /** - * Is the function for the Case, you are in Iteration one and have only one triple of condition (s,p,o). - * @param querylist - * @param query - * @return a list with answers from the Server - * @throws SQLException - * @throws JWNLException - */ - private ArrayList<String> simpleIteration1Case(ArrayList<String> querylist, String query) throws SQLException, - JWNLException { - //asking server - String answer; - ArrayList<String> final_answer=new ArrayList<String>(); - - /* - * First try the original query on the server. If that doesnt work, try it with Iteration - */ - answer=sendServerQuestionRequest(query); - - if(answer.contains("EmtyAnswer")){ - - String resource=""; - String property_to_compare_with=""; - String sideOfProperty="LEFT"; - - - int tmpcounter=0; - for(String s : querylist){ - //we dont need the first one, because thats the query itself - tmpcounter=tmpcounter+1; - if(tmpcounter>=1){ - if(s.contains("LEFT")){ - sideOfProperty="LEFT"; - resource=s.replace("LEFT",""); - } - if(s.contains("RIGHT")){ - sideOfProperty="RIGHT"; - resource=s.replace("RIGHT",""); - } - if(s.contains("PROPERTY")){ - property_to_compare_with=s.replace("PROPERTY",""); - } - - } - } - System.out.println("Property to compare:: "+ property_to_compare_with); - System.out.println("Resource: "+ resource); - //contains uri AND string, every second is the string - HashMap<String,String> properties = new HashMap<String, String>(); - GetRessourcePropertys property = new GetRessourcePropertys(); - Boolean goOnAfterProperty = true; - try { - /* - * TODO: Have to check now, if we need a right Property or a left one - */ - properties=property.getPropertys(getUriFromIndex(resource.toLowerCase(),0),sideOfProperty); - if (properties==null){ - //final_answer.add("Error in getting Properties\n"); - - final_answer.add("Begin:\n"+query +"\nError in getting Properties \n End"); - goOnAfterProperty=false; - } - //System.out.println(properties); - } catch (IOException e) { - // TODO Auto-generated catch block - //e.printStackTrace(); - - final_answer.add("Begin:\n"+query +"\nError in getting Properties \n End"); - goOnAfterProperty=false; - - } - if(goOnAfterProperty==true){ - //property_to_compare_with mit der Liste der propertys vergleichen, und wenn der normalisierte Wert >= LvenstheinMin ist, einbauen und neue query erzeugen. - ArrayList<String> new_queries= new ArrayList<String>(); - for (Entry<String, String> entry : properties.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - double tmp=Levenshtein.computeLevenshteinDistance(property_to_compare_with.toLowerCase(), key); - - /* - * TODO: Implement Normalised levensthein - */ - if(tmp<=3.0){ - //alte property uri mit neuer ersetzen: - String query_tmp=query; - String test = getUriFromIndex(property_to_compare_with.toLowerCase(),1); - //query_tmp=query_tmp.replace(test,properties.get(i-1)); - query_tmp=query_tmp.replace(test,value); - new_queries.add(query_tmp); - } - - } - - System.out.println("Start Iterating Wordnet with "+property_to_compare_with+" and deept of "+explorationdepthwordnet); - ArrayList<String> semantics=new ArrayList<String>(); - ArrayList<String> tmp_semantics=new ArrayList<String>(); - ArrayList<String> result_SemanticsMatchProperties=new ArrayList<String>(); - semantics.add(property_to_compare_with); - - //first check, if there is a singular form in the wordnet dictionary.. eg children -> child - String _temp_=myindex.getWordnetHelp(property_to_compare_with); - if(_temp_==null){ - tmp_semantics=semantics; - } - else{ - semantics.clear(); - semantics.add(_temp_); - tmp_semantics=semantics; - } - Boolean goOnAfterWordnet = true; - for(int i=0;i<=explorationdepthwordnet;i++){ - - try { - tmp_semantics=getSemantics(tmp_semantics); - if (tmp_semantics==null){ - goOnAfterWordnet=false; - final_answer.add("Begin:\n"+query +"\n Error in searching Wordnet with word "+semantics+" \n End"); - - } - else{ - //each word only one time - for(String k : tmp_semantics){ - if(!semantics.contains(k)) semantics.add(k); - } - } - - } catch (IOException e) { - // TODO Auto-generated catch block - //e.printStackTrace(); - goOnAfterWordnet=false; - final_answer.add("Begin:\n"+query +"\n Error in searching Wordnet with word "+semantics+" \n End"); - - } - - } - - if(goOnAfterWordnet==true){ - - for (Entry<String, String> entry : properties.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - - for(String b : semantics){ - if(key.contains(b.toLowerCase())){ - if(!result_SemanticsMatchProperties.contains(key)){ - //create new query - result_SemanticsMatchProperties.add(key); - String query_tmp=query; - String test = getUriFromIndex(property_to_compare_with.toLowerCase(),1); - query_tmp=query_tmp.replace(test,value); - System.out.println("New query after wordnet: "+ query_tmp); - new_queries.add(query_tmp); - } - } - } - } - - for(String bla : new_queries){ - String answer_tmp; - answer_tmp=sendServerQuestionRequest(bla); - System.out.println("Antwort vom Server: "+answer_tmp); - final_answer.add("Begin:\n"+bla +"\n"+answer_tmp+" \n End"); - } - } - } - } - - return final_answer; - } - @@ -1102,6 +932,7 @@ + /** * Iterates thru the conditions and returns an array, where one can see, if the Property is left or right from the resource @@ -1167,109 +998,136 @@ ArrayList<String> lstquerupsidedown = new ArrayList<String>(); String query; String selTerms =""; - for(SPARQL_Term terms :temp.getSelTerms()) selTerms=selTerms+(terms.toString())+" "; - System.out.println(selTerms); + boolean addQuery=true; + //sometimes there isnt an Selectterm, so dont use this query + try{ + for(SPARQL_Term terms :temp.getSelTerms()) selTerms=selTerms+(terms.toString())+" "; + } + catch (Exception e){ + selTerms=""; + addQuery=false; + } + + String conditions = ""; - for(Path condition: temp.getConditions()) conditions=conditions+(condition.toString())+"."; + try{ + for(Path condition: temp.getConditions()) conditions=conditions+(condition.toString())+"."; + } + catch (Exception e){ + conditions=""; + addQuery=false; + } String filters=""; - for(SPARQL_Filter tmp : temp.getFilters()) filters=filters+tmp+" "; - //System.out.println("\n"); - System.out.println("\n"); - query="PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "+temp.getQt().toString()+" "+selTerms+" WHERE {"+ conditions.replace("--","") + filters+"}"; - - String conditions_new = ""; - for(Path condition: temp.getConditions()){ - //make conditions up-side-down - String[] tmp_upside = condition.toString().split(" -- "); - String tmp_conditions_new=""; - for(String con : tmp_upside) tmp_conditions_new = con +" "+tmp_conditions_new; - //remove all dots befor end - tmp_conditions_new=tmp_conditions_new.replace(".", ""); - //at the end ein . - tmp_conditions_new = tmp_conditions_new + "."; + try{ + for(SPARQL_Filter tmp : temp.getFilters()) filters=filters+tmp+" "; + } + catch(Exception e){ + filters=""; + addQuery=false; + } + if(addQuery==true){ + query="PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "+temp.getQt().toString()+" "+selTerms+" WHERE {"+ conditions.replace("--","") + filters+"}"; + + String conditions_new = ""; + for(Path condition: temp.getConditions()){ + //make conditions up-side-down + String[] tmp_upside = condition.toString().split(" -- "); + String tmp_conditions_new=""; + for(String con : tmp_upside) tmp_conditions_new = con +" "+tmp_conditions_new; + //remove all dots befor end + tmp_conditions_new=tmp_conditions_new.replace(".", ""); + //at the end ein . + tmp_conditions_new = tmp_conditions_new + "."; + + //conditions_new=tmp_conditions_new; + + conditions_new=conditions_new + tmp_conditions_new; + } - //conditions_new=tmp_conditions_new; + - conditions_new=conditions_new + tmp_conditions_new; + /*System.out.println("Conditions: " + conditions); + System.out.println("Conditions_new: " + conditions_new);*/ + + + String query_upside_down = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "+temp.getQt().toString()+" "+selTerms+" WHERE {"+ conditions_new.replace("--","") +filters+ "}"; + String[] slots= null; + int slotcounter=1; + for(Slot slot : temp.getSlots()){ + + //see below + slotcounter=slotcounter+1; + + + String slotstring=slot.toString().replace("UNSPEC","").replace("RESOURCE","").replace("{","").replace("}",""); + slotstring=slotstring.replace(" ",""); + //System.out.println(tmp); + //damit auch wirklich nur ?y und nicht ?y0 ersetzt wird, einfach nach "?y " suchen. + String[] array = slotstring.split(":"); + String replace; + if(array[0].length()<2)replace = "?"+array[0]+" "; + else replace="?"+array[0]; + + + //TODO: Hotfix: get rid of " PROPERTY " + String _ThingGettingURIfor_=array[1]; + _ThingGettingURIfor_=_ThingGettingURIfor_.replace(" PROPERTY ","").toLowerCase(); + String hm_result=getUriFromIndex(_ThingGettingURIfor_,0); + try + { + if(hm_result.contains("Category:")) hm_result=hm_result.replace("Category:",""); + } + catch ( Exception e ) + { + + } + + /*always the middle slot is the property + * so count and always take the second of third to become a property + */ + if(slotcounter%2==0){ + hm_result=getUriFromIndex(_ThingGettingURIfor_,1); + } + //set back to 0 to start new + if(slotcounter==3) slotcounter=0; + query=query.replace(replace, "<"+hm_result+">"); + query_upside_down=query_upside_down.replace(replace, "<"+hm_result+">"); + + } + + lstquerupsidedown.add(query_upside_down); + lstquerynew.add(query); + + + + ArrayList<String> lsttmp=createLeftAndRightPropertyArray(query); + //if its lower than three, we dont have any conditions and dont need to check it. + //also if the size%3 isnt 0, than something else is wrong and we dont need to test the query + if(lsttmp.size()>=3&&lsttmp.size()%3==0)for(String i : lsttmp) lstquerynew.add(i); + else{ + lstquerynew.clear(); + lstquerynew.add("ERROR"); + addQuery=false; + } + + lsttmp.clear(); + lsttmp=createLeftAndRightPropertyArray(query_upside_down); + if(lsttmp.size()>=3&&lsttmp.size()%3==0)for(String i : lsttmp) lstquerupsidedown.add(i); + else{ + lstquerupsidedown.clear(); + lstquerupsidedown.add("ERROR"); + addQuery=false; + } + + if(addQuery==true){ + lstquery.add(lstquerynew); + lstquery.add(lstquerupsidedown); + } } + } - - - System.out.println("Conditions: " + conditions); - System.out.println("Conditions_new: " + conditions_new); - - - String query_upside_down = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "+temp.getQt().toString()+" "+selTerms+" WHERE {"+ conditions_new.replace("--","") +filters+ "}"; - String[] slots= null; - int slotcounter=1; - for(Slot slot : temp.getSlots()){ - - //see below - slotcounter=slotcounter+1; - - - String slotstring=slot.toString().replace("UNSPEC","").replace("RESOURCE","").replace("{","").replace("}",""); - slotstring=slotstring.replace(" ",""); - //System.out.println(tmp); - //damit auch wirklich nur ?y und nicht ?y0 ersetzt wird, einfach nach "?y " suchen. - String[] array = slotstring.split(":"); - String replace; - if(array[0].length()<2)replace = "?"+array[0]+" "; - else replace="?"+array[0]; - - - //TODO: Hotfix: get rid of " PROPERTY " - String _ThingGettingURIfor_=array[1]; - _ThingGettingURIfor_=_ThingGettingURIfor_.replace(" PROPERTY ","").toLowerCase(); - String hm_result=getUriFromIndex(_ThingGettingURIfor_,0); - try - { - if(hm_result.contains("Category:")) hm_result=hm_result.replace("Category:",""); - } - catch ( Exception e ) - { - - } - - /*always the middle slot is the property - * so count and always take the second of third to become a property - */ - if(slotcounter%2==0){ - hm_result=getUriFromIndex(_ThingGettingURIfor_,1); - } - //set back to 0 to start new - if(slotcounter==3) slotcounter=0; - query=query.replace(replace, "<"+hm_result+">"); - query_upside_down=query_upside_down.replace(replace, "<"+hm_result+">"); - - } - lstquerupsidedown.add(query_upside_down); - lstquerynew.add(query); - - - - ArrayList<String> lsttmp=createLeftAndRightPropertyArray(query); - //if its lower than three, we dont have any conditions and dont need to check it. - //also if the size%3 isnt 0, than something else is wrong and we dont need to test the query - if(lsttmp.size()>=3&&lsttmp.size()%3==0)for(String i : lsttmp) lstquerynew.add(i); - else{ - lstquerynew.clear(); - lstquerynew.add("ERROR"); - } - - lsttmp.clear(); - lsttmp=createLeftAndRightPropertyArray(query_upside_down); - if(lsttmp.size()>=3&&lsttmp.size()%3==0)for(String i : lsttmp) lstquerupsidedown.add(i); - else{ - lstquerupsidedown.clear(); - lstquerupsidedown.add("ERROR"); - } - - lstquery.add(lstquerynew); - lstquery.add(lstquerupsidedown); - } return lstquery; } @@ -1351,6 +1209,10 @@ + /* + * TODO: if for example title,name,label is given, replace , and get for each thing the semantics + * + */ private static ArrayList<String> getSemantics (ArrayList<String> semantics) throws IOException, JWNLException { ArrayList<String> result = new ArrayList<String>(); //result.clear(); @@ -1492,24 +1354,23 @@ private String createAnswer(String string){ - string=string.replace("table",""); - string=string.replace("<tr>", ""); - string=string.replace("</tr>", ""); - string=string.replace("</>",""); - string=string.replace("<th>l</th>",""); - string=string.replace("<th>x</th>",""); - string=string.replace("< class=\"sparql\" border=\"1\">",""); - string=string.replace("\n",""); - string=string.replace(" ",""); - string=string.replace("</td>",""); - string=string.replace("<td>",""); - string=string.replace("<th>callret-0</th>", ""); - string=string.replace("<th>y</th>",""); - while (string.contains(" ")) string=string.replace(" ",""); - if (string.length()==0) string="EmtyAnswer"; - //System.out.println("Stringlänge: "+string.length()); - return string; + //<td>Klaus Wowereit</td> + + //get with regex all between <td> </td> + + Pattern p = Pattern.compile (".*<td>(.*)</td>.*"); + Matcher m = p.matcher (string); + String result=""; + while (m.find()) { + if(m.group(1)!=null) + result = result+" "+ m.group(1); + } + + if (result.length()==0) result="EmtyAnswer"; + + return result; + } @@ -1561,6 +1422,183 @@ +/* + * Backup original Iteration function + * + */ + +/* + * + * + // Is the function for the Case, you are in Iteration one and have only one triple of condition (s,p,o). + // @param querylist + // @param query + // @return a list with answers from the Server + // @throws SQLException + // @throws JWNLException + + private ArrayList<String> simpleIteration1Case(ArrayList<String> querylist, String query) throws SQLException, + JWNLException { + //asking server + String answer; + ArrayList<String> final_answer=new ArrayList<String>(); + + //First try the original query on the server. If that doesnt work, try it with Iteration + + answer=sendServerQuestionRequest(query); + + if(answer.contains("EmtyAnswer")){ + + String resource=""; + String property_to_compare_with=""; + String sideOfProperty="LEFT"; + + + int tmpcounter=0; + for(String s : querylist){ + //we dont need the first one, because thats the query itself + tmpcounter=tmpcounter+1; + if(tmpcounter>=1){ + if(s.contains("LEFT")){ + sideOfProperty="LEFT"; + resource=s.replace("LEFT",""); + } + if(s.contains("RIGHT")){ + sideOfProperty="RIGHT"; + resource=s.replace("RIGHT",""); + } + if(s.contains("PROPERTY")){ + property_to_compare_with=s.replace("PROPERTY",""); + } + + } + } + System.out.println("Property to compare:: "+ property_to_compare_with); + System.out.println("Resource: "+ resource); + //contains uri AND string, every second is the string + HashMap<String,String> properties = new HashMap<String, String>(); + GetRessourcePropertys property = new GetRessourcePropertys(); + Boolean goOnAfterProperty = true; + try { + properties=property.getPropertys(getUriFromIndex(resource.toLowerCase(),0),sideOfProperty); + if (properties==null){ + //final_answer.add("Error in getting Properties\n"); + + final_answer.add("Begin:\n"+query +"\nError in getting Properties \n End"); + goOnAfterProperty=false; + } + //System.out.println(properties); + } catch (IOException e) { + // TODO Auto-generated catch block + //e.printStackTrace(); + + final_answer.add("Begin:\n"+query +"\nError in getting Properties \n End"); + goOnAfterProperty=false; + + } + if(goOnAfterProperty==true){ + //property_to_compare_with mit der Liste der propertys vergleichen, und wenn der normalisierte Wert >= LvenstheinMin ist, einbauen und neue query erzeugen. + ArrayList<String> new_queries= new ArrayList<String>(); + for (Entry<String, String> entry : properties.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + double tmp=Levenshtein.computeLevenshteinDistance(property_to_compare_with.toLowerCase(), key); + + + //TODO: Implement Normalised levensthein + + if(tmp<=3.0){ + //alte property uri mit neuer ersetzen: + String query_tmp=query; + String test = getUriFromIndex(property_to_compare_with.toLowerCase(),1); + //query_tmp=query_tmp.replace(test,properties.get(i-1)); + query_tmp=query_tmp.replace(test,value); + new_queries.add(query_tmp); + } + + } + + System.out.println("Start Iterating Wordnet with "+property_to_compare_with+" and deept of "+explorationdepthwordnet); + ArrayList<String> semantics=new ArrayList<String>(); + ArrayList<String> tmp_semantics=new ArrayList<String>(); + ArrayList<String> result_SemanticsMatchProperties=new ArrayList<String>(); + semantics.add(property_to_compare_with); + + //first check, if there is a singular form in the wordnet dictionary.. eg children -> child + String _temp_=myindex.getWordnetHelp(property_to_compare_with); + if(_temp_==null){ + tmp_semantics=semantics; + } + else{ + semantics.clear(); + semantics.add(_temp_); + tmp_semantics=semantics; + } + Boolean goOnAfterWordnet = true; + for(int i=0;i<=explorationdepthwordnet;i++){ + + try { + tmp_semantics=getSemantics(tmp_semantics); + if (tmp_semantics==null){ + goOnAfterWordnet=false; + final_answer.add("Begin:\n"+query +"\n Error in searching Wordnet with word "+semantics+" \n End"); + + } + else{ + //each word only one time + for(String k : tmp_semantics){ + if(!semantics.contains(k)) semantics.add(k); + } + } + + } catch (IOException e) { + // TODO Auto-generated catch block + //e.printStackTrace(); + goOnAfterWordnet=false; + final_answer.add("Begin:\n"+query +"\n Error in searching Wordnet with word "+semantics+" \n End"); + + } + + } + + if(goOnAfterWordnet==true){ + + for (Entry<String, String> entry : properties.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + + for(String b : semantics){ + if(key.contains(b.toLowerCase())){ + if(!result_SemanticsMatchProperties.contains(key)){ + //create new query + result_SemanticsMatchProperties.add(key); + String query_tmp=query; + String test = getUriFromIndex(property_to_compare_with.toLowerCase(),1); + query_tmp=query_tmp.replace(test,value); + System.out.println("New query after wordnet: "+ query_tmp); + new_queries.add(query_tmp); + } + } + } + } + + for(String bla : new_queries){ + String answer_tmp; + answer_tmp=sendServerQuestionRequest(bla); + System.out.println("Antwort vom Server: "+answer_tmp); + final_answer.add("Begin:\n"+bla +"\n"+answer_tmp+" \n End"); + } + } + } + } + + return final_answer; + } + + */ + + + /** * Cluster function */ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |