From: <seb...@us...> - 2011-12-07 16:20:49
|
Revision: 3486 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3486&view=rev Author: sebastianwtr Date: 2011-12-07 16:20:39 +0000 (Wed, 07 Dec 2011) Log Message: ----------- [tbsl exploration] Split Interation1 into two different cases and did the "simple" case . Modified the SQLite functions. And changed other functions to get it all running. 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 trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/mySQLDictionary.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-07 16:15:38 UTC (rev 3485) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/GetRessourcePropertys.java 2011-12-07 16:20:39 UTC (rev 3486) @@ -9,34 +9,62 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; import org.dllearner.algorithm.tbsl.exploration.sax.MySaxParser; public class GetRessourcePropertys { - public ArrayList<String> getPropertys(String element) throws IOException{ - try{ - sendServerPropertyRequest(element); - return do_parsing("answer_property"); - } catch (Exception e){ - return null; - } + public HashMap<String,String> getPropertys(String element, String side) throws IOException{ + + return sendServerPropertyRequest(element,side); + } + /** * Get an uri and saves the properties of this resource * @param vergleich + * @return * @throws IOException */ - private void sendServerPropertyRequest(String vergleich) throws IOException{ + private HashMap<String,String> sendServerPropertyRequest(String vergleich, String side) throws IOException{ + /* + * + * For the second Iteration, I can just add the sparql property here. + */ - String bla123 = vergleich; + /* + * + * SELECT DISTINCT ?p WHERE {<http://dbpedia.org/resource/Berlin> ?y ?p.} für Berlin links der Property + * PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT DISTINCT ?s ?p WHERE {<http://dbpedia.org/resource/Berlin> ?p ?y. ?p rdfs:label ?s.} + * + * SELECT DISTINCT ?p WHERE {?y ?p <http://dbpedia.org/resource/Berlin>.} für Berlin rechts der Property + * PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT DISTINCT ?s ?p WHERE {?y ?p <http://dbpedia.org/resource/Berlin>. ?p rdfs:label ?s.} + * http://greententacle.techfak.uni-bielefeld.de:5171/sparql?default-graph-uri=&query=PREFIX+rdfs%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E+SELECT+DISTINCT+%3Fs+%3Fp+WHERE+{%3Fy+%3Fp+%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2FBerlin%3E.+%3Fp+rdfs%3Alabel+%3Fs.}&format=text%2Fhtml&debug=on&timeout= + */ + + 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="http://greententacle.techfak.uni-bielefeld.de:5171/sparql?default-graph-uri=&query=PREFIX+rdfs%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0D%0APREFIX+res%3A+%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2F%3E%0D%0A%0D%0ASELECT+DISTINCT+%3Fp+%3Fl+WHERE++{%0D%0A+{+res%3A"+vergleich+"+%3Fp+%3Fo+.+}%0D%0A+UNION%0D%0A+{+%3Fs+%3Fp+res%3A"+vergleich+"+.+}%0D%0A+{+%3Fp+rdfs%3Alabel+%3Fl+.+}%0D%0A}%0D%0A&format=text%2Fhtml&debug=on&timeout="; + 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="; + + String verarbeitungsstring=null; + if(side.contains("RIGHT")) verarbeitungsstring=tmp_right; + if(side.contains("LEFT")) verarbeitungsstring=tmp_left; + + //just in case..... + if(!side.contains("LEFT") && !side.contains("RIGHT")) verarbeitungsstring=tmp_left; + + //String verarbeitungsstring="http://greententacle.techfak.uni-bielefeld.de:5171/sparql?default-graph-uri=&query=PREFIX+rdfs%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0D%0APREFIX+res%3A+%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2F%3E%0D%0A%0D%0ASELECT+DISTINCT+%3Fp+%3Fl+WHERE++{%0D%0A+{+res%3A"+vergleich+"+%3Fp+%3Fo+.+}%0D%0A+UNION%0D%0A+{+%3Fs+%3Fp+res%3A"+vergleich+"+.+}%0D%0A+{+%3Fp+rdfs%3Alabel+%3Fl+.+}%0D%0A}%0D%0A&format=text%2Fhtml&debug=on&timeout="; URL url; InputStream is; InputStreamReader isr; @@ -45,7 +73,7 @@ String result=""; try { - url = new URL(tmp); + url = new URL(verarbeitungsstring); is = url.openStream(); isr = new InputStreamReader(is); r = new BufferedReader(isr); @@ -60,9 +88,29 @@ System.out.println("Can not connect"); } - FileWriter w = new FileWriter("answer_property"); + /* FileWriter w = new FileWriter("answer_property"); w.write(result); w.close(); + */ + + HashMap<String,String> hm = new HashMap(); + 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]); + //System.out.println(tmp_array[i-1].toLowerCase() + " " +tmp_array[i]); + } + + return hm; } @@ -85,4 +133,47 @@ return indexObject; } + + + + private String createServerRequest(String query){ + String anfrage=null; + anfrage=removeSpecialKeys(query); + anfrage=anfrage.replace("<","<"); + anfrage=anfrage.replace("%gt;",">"); + anfrage=anfrage.replace("&","&"); + //anfrage=anfrage.replaceAll("#>","%23%3E%0D%0A%"); + anfrage=anfrage.replace("#","%23"); + anfrage=anfrage.replace(" ","+"); + anfrage=anfrage.replace("/","%2F"); + anfrage=anfrage.replace(":","%3A"); + anfrage=anfrage.replace("?","%3F"); + anfrage=anfrage.replace("$","%24"); + //anfrage=anfrage.replaceAll("F>+","F%3E%0D%0A"); + anfrage=anfrage.replace(">","%3E"); + anfrage=anfrage.replace("<","%3C"); + anfrage=anfrage.replace("\"","%22"); + anfrage=anfrage.replace("\n","%0D%0A%09"); + anfrage=anfrage.replace("%%0D%0A%09","%09"); + anfrage=anfrage.replace("=","%3D"); + anfrage=anfrage.replace("@","%40"); + anfrage=anfrage.replace("&","%26"); + anfrage=anfrage.replace("(","%28"); + anfrage=anfrage.replace(")","%29"); + anfrage=anfrage.replace("%3E%0D%0A%25","%3E"); + //anfrage=anfrage.replaceAll("\n",".%0D%0A%09"); + return anfrage; + } + + private String removeSpecialKeys(String query){ + query=query.replace("\\",""); + //query=query.replaceAll("\a",""); + query=query.replace("\b",""); + query=query.replace("\f",""); + query=query.replace("\r",""); + query=query.replace("\t",""); + // query=query.replaceAll("\v",""); + return query; + } + } 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-07 16:15:38 UTC (rev 3485) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/SparqlObject.java 2011-12-07 16:20:39 UTC (rev 3486) @@ -15,9 +15,13 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Map.Entry; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.Set; import net.didion.jwnl.JWNLException; +import net.didion.jwnl.data.POS; import org.dllearner.algorithm.tbsl.exploration.sax.ParseXmlHtml; import org.dllearner.algorithm.tbsl.nlp.WordNet; @@ -57,18 +61,15 @@ //Konstruktor public SparqlObject() throws MalformedURLException, ClassNotFoundException, SQLException{ - wordnet = new WordNet(); - //hm=hm_new; - /*hm=ParseXmlHtml.parse_xml("/home/swalter/workspace/qaldEntity2",hm); - hm=ParseXmlHtml.parse_xml("/home/swalter/workspace/qaldEntity1",hm);*/ + this.wordnet = new WordNet(); System.out.println("Loading SPARQL Templator"); - btemplator = new BasicTemplator(); - templator = new Templator(); + this.btemplator = new BasicTemplator(); + this.templator = new Templator(); System.out.println("Loading SPARQL Templator Done\n"); - System.out.println("Start Indexing Wikipedia URI's"); - myindex = new mySQLDictionary(); + System.out.println("Start Indexing"); + this.myindex = new mySQLDictionary(); - System.out.println("Done:Indexing Wikipedia URI's"); + System.out.println("Done:Indexing"); setExplorationdepthwordnet(1); //eigentlich immer mit 0 initialisieren setIterationdepth(1); @@ -128,17 +129,23 @@ System.out.println("The Questionparsing took "+ (endParsingTime-startParsingTime)+ " ms"); ArrayList<String> final_answer = new ArrayList<String>(); - //if(!lstquery.isEmpty()){ + if(lstquery.isEmpty()){ + saveNotParsedQuestions(question); + } //for each querry + //TODO: Add function that no qery is send to the server, if querylist==null for(ArrayList<String> querylist : lstquery){ - /* - * ################################################################################################# - */ - //only testfunction to save the generated queries in the tmp-folder + boolean startIterating=true; String query=""; - query=querylist.get(0).toString(); - if(getIterationdepth()==-1){ + if(querylist.get(0).contains("ERROR"))startIterating=false; + else query=querylist.get(0).toString(); + + //TODO: Somewhere is an error, because sometimes there is an double _ a __ and thats not allowed. + //fixing it now with an replace of "__" to "" + query=query.replace("__", ""); + + if(getIterationdepth()==-1&&startIterating==true){ String tmp = new String(); String s = null; BufferedReader in = null; @@ -181,7 +188,7 @@ * ################################################################################################# */ //Iteration 0 - if(getIterationdepth()==0){ + if(getIterationdepth()==0&&startIterating==true){ String tmp = new String(); String s = null; BufferedReader in = null; @@ -208,170 +215,26 @@ } String answer; answer=sendServerQuestionRequest(query); - final_answer.add(answer); - /*System.out.println(query); - if (query=="" || query==" "||query.length()==0) answer="Could not parse"; - System.out.println("Antwort: " + answer); - String out=tmp + "\n" + "Question: "+question + "\n"+"Query: " + query +"\n Anwer: "+answer+"\n\n##############################"; - - BufferedWriter outfile = new BufferedWriter( - new OutputStreamWriter( - new FileOutputStream( "/tmp/answer.txt" ) ) ); - - outfile.write(out); - outfile.close(); */ + final_answer.add("Begin:\n"+query +"\n"+answer+" \n End"); + } /* * ################################################################################################# */ //Iterration 1 - if(getIterationdepth()==1){ + if(getIterationdepth()==1&&startIterating==true){ - //asking server - String answer; - answer=sendServerQuestionRequest(query); - System.out.println(query); - //if Emty answer, get properties an look up the right property with levensthein - if(answer.contains("EmtyAnswer")){ - //TODO: get all information from the query - //TODO: maybe put the query + information in an array list of arraylist. each arraylist contains the query, the variables and the uris. Then iterate over the List and get the query for sending to server - String rescource=""; - - //get the resource of the query. always the last Item in the array! - //Funktioniert! - String resource_tmp=""; - int tmp_length=querylist.size(); - resource_tmp=querylist.get(tmp_length-1); - String[] array_tmp = resource_tmp.split(":"); - rescource=array_tmp[1]; - - - //the property we are looking for is always the second last in the array! - //Funktioniert! - String property_to_compare_with=""; - tmp_length=querylist.size(); - //second last - property_to_compare_with=querylist.get(tmp_length-2); - array_tmp = property_to_compare_with.split(":"); - property_to_compare_with=array_tmp[1]; - //System.out.println("property_to_compare_with: "+property_to_compare_with); - - - //contains uri AND string, every second is the string - //Funktioniert - ArrayList<String> properties = new ArrayList<String>(); - GetRessourcePropertys property = new GetRessourcePropertys(); - Boolean goOnAfterProperty = true; - try { - //using uri now, not the string - //properties=property.getPropertys(hm.get(rescource.toLowerCase())); - properties=property.getPropertys(getUriFromIndex(rescource.toLowerCase(),0)); - if (properties==null){ - final_answer.add("Error in getting Properties\n"); - goOnAfterProperty=false; - } - //System.out.println(properties); - } catch (IOException e) { - // TODO Auto-generated catch block - //e.printStackTrace(); - final_answer.add("Error in getting Properties\n"); - 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. - Levenshtein levensthein = new Levenshtein(); - ArrayList<String> new_queries= new ArrayList<String>(); - for(int i =1; i<=properties.size()-2;i=i+2){ - //double tmp=levensthein.nld(property_to_compare_with.toLowerCase(), properties.get(i).toLowerCase()); - double tmp=levensthein.computeLevenshteinDistance(property_to_compare_with.toLowerCase(), properties.get(i).toLowerCase()); - //create new query - //System.out.println(tmp); - //if(tmp>=LvenstheinMin){ - if(tmp<=3.0){ - //System.out.println(tmp); - //alte property uri mit neuer ersetzen: - String query_tmp=query; - //query_tmp=query_tmp.replace(hm.get(property_to_compare_with.toLowerCase()),properties.get(i-1)); - query_tmp=query_tmp.replace(getUriFromIndex(property_to_compare_with.toLowerCase(),1),properties.get(i-1)); - //System.out.println("hm.get(property_to_compare_with.toLowerCase(): " + hm.get(property_to_compare_with.toLowerCase())); - 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); - 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("Error in searching Wordnet\n"); - } - 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("Error in searching Wordnet\n"); - - } - - } - - if(goOnAfterWordnet==true){ - // ArrayList<String> new_queries= new ArrayList<String>(); - - //TODO: Try, if it works, if you use only one loop: (b.lowerCase).contains(properties.get(h)) - for(int h=1;h<properties.size()-2;h=h+2){ - for(String b : semantics){ - //System.out.println(properties.get(h)); - //System.out.println(b); - if(properties.get(h).contains(b.toLowerCase())){ - if(!result_SemanticsMatchProperties.contains(properties.get(h))){ - //create new query - result_SemanticsMatchProperties.add(properties.get(h)); - String query_tmp=query; - - //query_tmp=query_tmp.replace(hm.get(property_to_compare_with.toLowerCase()),properties.get(h-1)); - query_tmp=query_tmp.replace(getUriFromIndex(property_to_compare_with.toLowerCase(),1),properties.get(h-1)); - //System.out.println("hm.get(property_to_compare_with.toLowerCase(): " + hm.get(property_to_compare_with.toLowerCase())); - new_queries.add(query_tmp); - } - } - } - } - - for(String bla : new_queries){ - String answer_tmp; - answer_tmp=sendServerQuestionRequest(bla); - if(!answer_tmp.contains("EmtyAnswer")){ - final_answer.add(answer_tmp); - } - } - } - } - } + //4, because of query + three conditions for the simple case + if(querylist.size()==4)final_answer=simpleIteration1Case(querylist, query); + //if we have more conditions, we need to change the way of replacing the uris got from wordnet etc + } /* * ################################################################################################# */ //Iterration 2 - if(getIterationdepth()==2){ + if(getIterationdepth()==2&&startIterating==true){ } } @@ -403,9 +266,21 @@ String out=""; for(String answer : final_answer){ + //only answered question + // if(!answer.contains("Error in searching Wordnet with word") && !answer.contains("EmtyAnswer")&& !answer.contains("Error in getting Properties"))out=out+ "\n"+answer+"\n"; + + /* + //only questions with wordnet error + if(answer.contains("Error in searching Wordnet with word"))out=out+ "\n"+answer+"\n"; - out=out+ "\n"+answer+"\n"; - + //only questions with emty answers + if(answer.contains("EmtyAnswer"))out=out+ "\n"+answer+"\n"; +*/ + //only questions with Error in Properties + if(answer.contains("Error in getting Properties"))out=out+ "\n"+answer+"\n"; + + + } System.out.println(question); System.out.println(out); @@ -416,13 +291,227 @@ outfile.write(tmp+"\n"+question+" :\n"+out); outfile.close(); } - - // string=string.replaceAll("?", ""); - //create_Sparql_query_old(string); - - // } + + /** + * 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; + } + + + /** + * Iterates thru the conditions and returns an array, where one can see, if the Property is left or right from the resource + * @param query + * @return returns an array, where one can see, if the Property is left or right from the resource + */ + private static ArrayList<String> createLeftAndRightPropertyArray(String query){ + query=query.replace(" ", " "); + Pattern p = Pattern.compile (".*\\{(.*\\<http.*)\\}.*"); + Matcher m = p.matcher (query); + ArrayList<String> lstquery = new ArrayList<String>(); + while (m.find()) { + String tmp= m.group(1); + tmp=tmp.replace("http://dbpedia.org/resource/","").replace("http://dbpedia.org/property/", "").replace("http://dbpedia.org/ontology/", ""); + + //split on . for sign for end of conditions + String[] firstArray=tmp.split("\\."); + for(String i : firstArray){ + + String[] secondArray=i.split(" "); + //always in three counts + int counter=0; + for(String j : secondArray){ + counter=counter+1; + //only one condition + if(secondArray.length%3==0){ + if(counter==1&&j.contains("<")){ + //position of Property is right + lstquery.add("RIGHT"+j.replace("<", "").replace(">","")); + } + else if(counter==3&&j.contains("<")){ + //position of Property is left + lstquery.add("RIGHT"+j.replace("<", "").replace(">","")); + } + else if(counter==2){ + lstquery.add("PROPERTY"+j.replace("<", "").replace(">","")); + } + + else if(j.contains("?")) lstquery.add("VARIABLE"); + } + if(counter==0)counter=0; + + + } + } + } + + return lstquery; + } /** * Method gets a String and takes the information from the templator to creat a Sparql query. @@ -435,11 +524,6 @@ Set<BasicQueryTemplate> querytemps = btemplator.buildBasicQueries(question); for (BasicQueryTemplate temp : querytemps) { - /*System.out.println("temp.getQt();" + temp.getQt()); - System.out.println("temp.getSelTerms();" + temp.getSelTerms()); - System.out.println("temp.getVariablesAsStringList();" + temp.getVariablesAsStringList()); - System.out.println("temp.getConditions();" + temp.getConditions()); - System.out.println("temp.getSlots();" + temp.getSlots());*/ ArrayList<String> lstquerynew = new ArrayList<String>(); ArrayList<String> lstquerupsidedown = new ArrayList<String>(); String query; @@ -454,7 +538,7 @@ 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; + 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()){ @@ -478,37 +562,46 @@ 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 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()){ - String tmp= slot.toString(); - tmp= tmp.replace("UNSPEC",""); - tmp= tmp.replace("RESOURCE",""); - tmp= tmp.replace("{",""); - tmp= tmp.replace("}",""); - tmp=tmp.replace(" ",""); + //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 = tmp.split(":"); + String[] array = slotstring.split(":"); String replace; if(array[0].length()<2)replace = "?"+array[0]+" "; else replace="?"+array[0]; - //System.out.println("replace: " + replace); - //hier dann den hm wert von array[1] eintragen + - - //String hm_result=hm.get(array[1].toLowerCase()); - String hm_result=getUriFromIndex(array[1].toLowerCase(),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 ) { - //System.out.println( "Das war keine Zahl!" ); + } + /*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+">"); @@ -516,23 +609,27 @@ lstquerupsidedown.add(query_upside_down); lstquerynew.add(query); - //slots hinzufügen - for(Slot slot : temp.getSlots()){ - String tmp= slot.toString(); - tmp= tmp.replace("UNSPEC",""); - tmp= tmp.replace("RESOURCE",""); - tmp= tmp.replace("{",""); - tmp= tmp.replace("}",""); - tmp=tmp.replace(" ",""); - lstquerupsidedown.add(tmp); - lstquerynew.add(tmp); + + + 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"); } - //System.out.println("Query: "+query); - /*lstquery.add(query); - lstquery.add(query_upside_down);*/ + + 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; @@ -540,6 +637,41 @@ + private void saveNotParsedQuestions(String question) throws IOException{ + BufferedReader in = null; + + String tmp=""; + // Lies Textzeilen aus der Datei in einen Vector: + try { + in = new BufferedReader( + new InputStreamReader( + new FileInputStream( "/tmp/notParsedQuestions" ) ) ); + String s; + while( null != (s = in.readLine()) ) { + tmp=tmp+"\n"+s; + } + } catch( FileNotFoundException ex ) { + } catch( Exception ex ) { + System.out.println( ex ); + } finally { + if( in != null ) + try { + in.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + String out=""; + BufferedWriter outfile = new BufferedWriter( + new OutputStreamWriter( + new FileOutputStream( "/tmp/notParsedQuestions" ) ) ); + + outfile.write(tmp+"\n"+question); + outfile.close(); + } + /** * * @param string @@ -549,27 +681,31 @@ */ private String getUriFromIndex(String string, int fall) throws SQLException{ String result=null; + //just to be sure its only 0 or 1 + if(fall!=0 && fall!=1) fall=0; if(fall==0){ - //result=hm.get(string.toLowerCase()); - //if(result==null)result=myindex.getURI(string); result=myindex.getResourceURI(string.toLowerCase()); if(result==null)result=myindex.getPropertyURI(string.toLowerCase()); } if(fall==1){ - /*result=hm.get(string.toLowerCase()); - if(result==null)result=myindex.getURI(string); - if(result==null)result="http://dbpedia.org/property/"+string.toLowerCase();*/ - //should be alway property an not resource - //result=result.replace("resource", "property"); result=myindex.getPropertyURI(string.toLowerCase()); if(result==null){ result=myindex.getResourceURI(string.toLowerCase()); - result=result.replace("resource", "property"); + if(result!=null) result=result.replace("resource", "property"); } } - - if(result==null) return "http://dbpedia.org/property/"+string.toLowerCase(); + String tmp=""; + tmp=string.toLowerCase(); + tmp=tmp.replace("property",""); + tmp=tmp.replace(" ", "_"); + if(result==null) { + if(fall==1)return "http://dbpedia.org/property/"+tmp; + if(fall==0)return "http://dbpedia.org/resource/"+tmp; + else{ + return result; + } + } else return result; } @@ -578,19 +714,98 @@ private static ArrayList<String> getSemantics (ArrayList<String> semantics) throws IOException, JWNLException { ArrayList<String> result = new ArrayList<String>(); + //result.clear(); + //try{ try{ for(String id :semantics){ - List<String> array =wordnet.getRelatedNouns(id); - for(String i:array){ - if(!result.contains(i))result.add(i); + List<String> array_relatedNouns=null; + List<String> array_bestsynonyms=null; + List<String> array_siterterms=null; + //array.clear(); + System.out.println("Wordnet Word: "+id); + array_relatedNouns =wordnet.getRelatedNouns(id); + + array_bestsynonyms=wordnet.getBestSynonyms(POS.NOUN, id); + + array_siterterms=wordnet.getSisterTerms(POS.NOUN, id); + + if(array_relatedNouns!=null){ + for(String i:array_relatedNouns){ + if(!result.contains(i))result.add(i); + } } + if(array_bestsynonyms!=null){ + for(String i:array_bestsynonyms){ + if(!result.contains(i))result.add(i); + } + } + if(array_siterterms!=null){ + for(String i:array_siterterms){ + if(!result.contains(i))result.add(i); + } + } + } - return result; - } catch (Exception e) { - return null; } + catch(Exception e){ + if(result.isEmpty()) return null; + } + + if(!result.isEmpty()) return result; + else{ + //System.out.println("Didnt find ") + /*this is the case, if the first time nothing was found. + * but sometimes wordnet doesnt find anything e.g. die place... bzt you have also die and place + * so we try to find the seperate words and test them as well + */ + try{ + for(String id :semantics){ + String[] tmp_array=id.split(" "); + if(tmp_array.length>=2){ + for(String tmp : tmp_array){ + List<String> array_relatedNouns=null; + List<String> array_bestsynonyms=null; + List<String> array_siterterms=null; + //array.clear(); + //System.out.println("Wordnet Word: "+tmp); + array_relatedNouns =wordnet.getRelatedNouns(tmp); + + array_bestsynonyms=wordnet.getBestSynonyms(POS.NOUN, tmp); + + array_siterterms=wordnet.getSisterTerms(POS.NOUN, tmp); + + if(array_relatedNouns!=null){ + for(String i:array_relatedNouns){ + if(!result.contains(i))result.add(i); + } + } + if(array_bestsynonyms!=null){ + for(String i:array_bestsynonyms){ + if(!result.contains(i))result.add(i); + } + } + if(array_siterterms!=null){ + for(String i:array_siterterms){ + if(!result.contains(i))result.add(i); + } + } + + } + } + + } + } + catch(Exception e){ + if(result.isEmpty()) return null; + } + + } + + if(!result.isEmpty()) return result; + else return null; + // else{ return result;} } Modified: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/mySQLDictionary.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/mySQLDictionary.java 2011-12-07 16:15:38 UTC (rev 3485) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/mySQLDictionary.java 2011-12-07 16:20:39 UTC (rev 3486) @@ -21,56 +21,13 @@ conn = DriverManager.getConnection("jdbc:sqlite::memory:"); createIndexPropertys(); createIndexResource(); + createWordnetHelp(); //optional!! //createIndexWikipedia(); } -private String createSimpleHashKey(String string){ - string = string.replace("!",""); - string = string.replace(":",""); - string = string.replace("/",""); - string = string.replace("\\",""); - string = string.replace("?",""); - string = string.replace(":",""); - - string = string.replace("a","1"); - string = string.replace("b","2"); - string = string.replace("c","3"); - string = string.replace("d","4"); - string = string.replace("e","5"); - string = string.replace("f","6"); - string = string.replace("g","7"); - string = string.replace("h","8"); - string = string.replace("i","9"); - string = string.replace("j","10"); - string = string.replace("k","11"); - string = string.replace("l","12"); - string = string.replace("m","13"); - string = string.replace("n","14"); - string = string.replace("o","15"); - string = string.replace("p","16"); - string = string.replace("q","17"); - string = string.replace("r","18"); - string = string.replace("s","19"); - string = string.replace("t","20"); - string = string.replace("u","21"); - string = string.replace("v","22"); - string = string.replace("w","23"); - string = string.replace("x","24"); - string = string.replace("y","25"); - string = string.replace("z","26"); - string = string.replace("ä","0"); - string = string.replace("ö","0"); - string = string.replace("ü","0"); - string = string.replace("?","0"); - string = string.replace(" ","0"); - return string; - - -} - public String getResourceURI(String string) throws SQLException{ Statement stat = conn.createStatement(); ResultSet rs; @@ -115,7 +72,81 @@ } + public String getWordnetHelp(String string) throws SQLException{ + Statement stat = conn.createStatement(); + ResultSet rs; + try { + rs = stat.executeQuery("select singular from wordnet where plural='"+string.toLowerCase()+"';"); + return rs.getString("singular"); + } catch (Exception e) { + // TODO Auto-generated catch block + //e.printStackTrace(); + return null; + } + + } + + private void createWordnetHelp() throws SQLException{ /*System.out.println("Start SQL test"); + Class.forName( "org.sqlite.JDBC" ); + conn = DriverManager.getConnection("jdbc:sqlite::memory:");*/ + System.out.println("start generating Wordnet Help-Function"); + Statement stat = conn.createStatement(); + stat.executeUpdate("drop table if exists wordnet;"); + stat.executeUpdate("create table wordnet (plural, singular);"); + PreparedStatement prep = conn.prepareStatement("insert into wordnet values (?, ?);"); + BufferedReader in=null; + // conn.setAutoCommit(false); + int zaehler=0; + try { + in = new BufferedReader( + new InputStreamReader( + new FileInputStream( "/home/swalter/workspace/noun.exc" ) ) ); + String s; + while( null != (s = in.readLine()) ) { + String[] tmp_array =s.split(" "); + if(tmp_array.length>=2){ + prep.setString(1, tmp_array[0]); + prep.setString(2, tmp_array[1]); + String temp=""; + if(tmp_array.length>2){ + for(int i =1;i<tmp_array.length;i++){ + temp=temp+tmp_array[i]+" "; + } + prep.setString(2, temp); + } + prep.addBatch(); + zaehler=zaehler+1; + //if(zaehler%10000==0) System.out.println(zaehler); + if(zaehler%10000==0){ + conn.setAutoCommit(false); + prep.executeBatch(); + conn.setAutoCommit(false); + System.out.println("done"); + } + + } + } + } catch( FileNotFoundException ex ) { + } catch( Exception ex ) { + System.out.println( ex ); + } finally { + if( in != null ) + try { + in.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + conn.setAutoCommit(false); + prep.executeBatch(); + conn.setAutoCommit(true); + System.out.println("Done"); + + } + private void createIndexWikipedia() throws ClassNotFoundException, SQLException{ /*System.out.println("Start SQL test"); Class.forName( "org.sqlite.JDBC" ); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
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. |
From: <seb...@us...> - 2011-12-08 13:05:50
|
Revision: 3491 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3491&view=rev Author: sebastianwtr Date: 2011-12-08 13:05:41 +0000 (Thu, 08 Dec 2011) Log Message: ----------- [tbsl exploration] fixed Levenshtein distance 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/Levenshtein.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 10:43:20 UTC (rev 3490) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/GetRessourcePropertys.java 2011-12-08 13:05:41 UTC (rev 3491) @@ -99,6 +99,7 @@ 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) { Modified: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/Levenshtein.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/Levenshtein.java 2011-12-08 10:43:20 UTC (rev 3490) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/Levenshtein.java 2011-12-08 13:05:41 UTC (rev 3491) @@ -7,59 +7,24 @@ public class Levenshtein { - //http://de.wikipedia.org/wiki/Levenshtein-Distanz - public double nld(String orig, String eing){ - //int result = diff(orig,eing); - int result = computeLevenshteinDistance(orig,eing); - int length=Math.max(orig.length(),eing.length()); + public static double nld(String orig, String eing){ + double result = computeLevenshteinDistance(orig,eing); + //System.out + double length=Math.max(orig.length(),eing.length()); - //if distance between both is zero, then the NLD must be one - if(result==0 ){ + if(result==0.0 ){ return 1; } else{ - BigDecimal m = new BigDecimal(result); - BigDecimal n = new BigDecimal(length); - BigDecimal c = new BigDecimal(0); - c=m.divide(n, 5, BigDecimal.ROUND_FLOOR); - - return c.doubleValue(); + double result_nld =result/length; + return result_nld; } } - public int diff(String orig, String eing) { - - int matrix[][] = new int[orig.length() + 1][eing.length() + 1]; - for (int i = 0; i < orig.length() + 1; i++) { - matrix[i][0] = i; - } - for (int i = 0; i < eing.length() + 1; i++) { - matrix[0][i] = i; - } - for (int a = 1; a < orig.length() + 1; a++) { - for (int b = 1; b < eing.length() + 1; b++) { - int right = 0; - if (orig.charAt(a - 1) != eing.charAt(b - 1)) { - right = 1; - } - int mini = matrix[a - 1][b] + 1; - if (matrix[a][b - 1] + 1 < mini) { - mini = matrix[a][b - 1] + 1; - } - if (matrix[a - 1][b - 1] + right < mini) { - mini = matrix[a - 1][b - 1] + right; - } - matrix[a][b] = mini; - } - } - - return matrix[orig.length()][eing.length()]; - } - //http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#Java private static int minimum(int a, int b, int c) { return Math.min(Math.min(a, b), c); 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 10:43:20 UTC (rev 3490) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/SparqlObject.java 2011-12-08 13:05:41 UTC (rev 3491) @@ -61,13 +61,13 @@ //Konstruktor public SparqlObject() throws MalformedURLException, ClassNotFoundException, SQLException{ - this.wordnet = new WordNet(); + wordnet = new WordNet(); System.out.println("Loading SPARQL Templator"); - this.btemplator = new BasicTemplator(); - this.templator = new Templator(); + btemplator = new BasicTemplator(); + templator = new Templator(); System.out.println("Loading SPARQL Templator Done\n"); System.out.println("Start Indexing"); - this.myindex = new mySQLDictionary(); + myindex = new mySQLDictionary(); System.out.println("Done:Indexing"); setExplorationdepthwordnet(1); @@ -132,8 +132,7 @@ if(lstquery.isEmpty()){ saveNotParsedQuestions(question); } - //for each querry - //TODO: Add function that no qery is send to the server, if querylist==null + for(ArrayList<String> querylist : lstquery){ boolean startIterating=true; @@ -271,7 +270,6 @@ try { in.close(); } catch (IOException e) { - // TODO Auto-generated catch block e.printStackTrace(); } } @@ -279,7 +277,7 @@ String out=""; for(String answer : final_answer){ //only answered question - // if(!answer.contains("Error in searching Wordnet with word") && !answer.contains("EmtyAnswer")&& !answer.contains("Error in getting Properties"))out=out+ "\n"+answer+"\n"; + if(!answer.contains("Error in searching Wordnet with word") && !answer.contains("EmtyAnswer")&& !answer.contains("Error in getting Properties"))out=out+ "\n"+answer+"\n"; /* //only questions with wordnet error @@ -289,7 +287,7 @@ if(answer.contains("EmtyAnswer"))out=out+ "\n"+answer+"\n"; */ //only questions with Error in Properties - if(answer.contains("Error in getting Properties"))out=out+ "\n"+answer+"\n"; + // if(answer.contains("Error in getting Properties"))out=out+ "\n"+answer+"\n"; @@ -367,11 +365,13 @@ String key = entry.getKey(); String value = entry.getValue(); double tmp=Levenshtein.computeLevenshteinDistance(property_to_compare_with.toLowerCase(), key); + double nld=Levenshtein.nld(property_to_compare_with.toLowerCase(), key); /* * TODO: Implement Normalised levensthein */ - if(tmp<=3.0){ + //if(tmp<=3.0){ + if(nld>=LvenstheinMin){ //alte property uri mit neuer ersetzen: String query_tmp=query; String test = getUriFromIndex(property_to_compare_with.toLowerCase(),1); @@ -433,11 +433,11 @@ } if(tmpcounter>4){ if(s.contains("LEFT")){ - sideOfPropertyOne="LEFT"; + sideOfPropertyTwo="LEFT"; resourceTwo=s.replace("LEFT",""); } if(s.contains("RIGHT")){ - sideOfPropertyOne="RIGHT"; + sideOfPropertyTwo="RIGHT"; resourceTwo=s.replace("RIGHT",""); } if(s.contains("PROPERTY")){ @@ -451,10 +451,9 @@ GetRessourcePropertys property = new GetRessourcePropertys(); Boolean goOnAfterProperty = true; try { - /* - * TODO: Have to check now, if we need a right Property or a left one - */ + propertiesOne=property.getPropertys(getUriFromIndex(resourceOne.toLowerCase(),0),sideOfPropertyOne); + propertiesTwo=property.getPropertys(getUriFromIndex(resourceTwo.toLowerCase(),0),sideOfPropertyTwo); if (propertiesOne==null){ //final_answer.add("Error in getting Properties\n"); @@ -463,34 +462,14 @@ } //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; } - try { - /* - * TODO: Have to check now, if we need a right Property or a left one - */ - propertiesTwo=property.getPropertys(getUriFromIndex(resourceOne.toLowerCase(),0),sideOfPropertyTwo); - if (propertiesOne==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>(); @@ -500,8 +479,15 @@ String queryOne=query; String keyOne = entryOne.getKey(); String valueOne = entryOne.getValue(); - double levnstheinDistanzeOne=Levenshtein.computeLevenshteinDistance(property_to_compare_withOne.toLowerCase(), keyOne); - if(levnstheinDistanzeOne<=3.0){ + //double levnstheinDistanzeOne=Levenshtein.computeLevenshteinDistance(property_to_compare_withOne.toLowerCase(), keyOne); + //if(levnstheinDistanzeOne<=3.0){ + double levnstheinDistanzeOne=Levenshtein.nld(property_to_compare_withOne.toLowerCase(), keyOne); + + /* + * TODO: Implement Normalised levensthein + */ + //if(tmp<=3.0){ + if(levnstheinDistanzeOne>=LvenstheinMin){ String test = getUriFromIndex(property_to_compare_withOne.toLowerCase(),1); queryOne=queryOne.replace(test,valueOne); } @@ -509,12 +495,19 @@ for (Entry<String, String> entryTwo : propertiesTwo.entrySet()) { String keyTwo = entryTwo.getKey(); String valueTwo = entryTwo.getValue(); - double levnstheinDistanzeTwo=Levenshtein.computeLevenshteinDistance(property_to_compare_withTwo.toLowerCase(), keyTwo); + // double levnstheinDistanzeTwo=Levenshtein.computeLevenshteinDistance(property_to_compare_withTwo.toLowerCase(), keyTwo); /* * TODO: Implement Normalised levensthein */ - if(levnstheinDistanzeTwo<=3.0){ + //if(levnstheinDistanzeTwo<=3.0){ + double levnstheinDistanzeTwo=Levenshtein.nld(property_to_compare_withTwo.toLowerCase(), keyTwo); + + /* + * TODO: Implement Normalised levensthein + */ + //if(tmp<=3.0){ + if(levnstheinDistanzeTwo>=0.9){ //alte property uri mit neuer ersetzen: String queryTwo=queryOne; String test = getUriFromIndex(property_to_compare_withTwo.toLowerCase(),1); @@ -583,9 +576,6 @@ 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"); @@ -595,7 +585,6 @@ } //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"); @@ -725,11 +714,11 @@ } if(tmpcounter>4){ if(s.contains("LEFT")){ - sideOfPropertyOne="LEFT"; + sideOfPropertyTwo="LEFT"; resourceTwo=s.replace("LEFT",""); } if(s.contains("RIGHT")){ - sideOfPropertyOne="RIGHT"; + sideOfPropertyTwo="RIGHT"; resourceTwo=s.replace("RIGHT",""); } if(s.contains("PROPERTY")){ @@ -740,15 +729,11 @@ } System.out.println("Property to compare:: "+ property_to_compare_withOne); System.out.println("Resource: "+ resourceOne); - //contains uri AND string, every second is the string HashMap<String,String> propertiesOne = new HashMap<String, String>(); HashMap<String,String> propertiesTwo = 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 - */ propertiesOne=property.getPropertys(getUriFromIndex(resourceOne.toLowerCase(),0),sideOfPropertyOne); propertiesTwo=property.getPropertys(getUriFromIndex(resourceTwo.toLowerCase(),0),sideOfPropertyTwo); if (propertiesOne==null){ @@ -774,7 +759,7 @@ //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>(); - System.out.println("Start Iterating Wordnet with "+property_to_compare_withOne+" and deept of "+explorationdepthwordnet); + //System.out.println("Start Iterating Wordnet with "+property_to_compare_withOne+" and deept of "+explorationdepthwordnet); ArrayList<String> semanticsOne=new ArrayList<String>(); ArrayList<String> tmp_semanticsOne=new ArrayList<String>(); ArrayList<String> result_SemanticsMatchPropertiesOne=new ArrayList<String>(); @@ -1335,12 +1320,7 @@ result=result.concat(str); counter=counter+1;} } while (str != null); - - //TODO:if counter = 5 or less, there is an empty answer from the Server! Still to Verify! - /* if(counter<=5){ - System.out.println("Empty Answer from Server"); - return "noanswer"; - }*/ + } catch (MalformedURLException e) { System.out.println("Must enter a valid URL"); } catch (IOException e) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <seb...@us...> - 2011-12-11 11:37:37
|
Revision: 3497 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3497&view=rev Author: sebastianwtr Date: 2011-12-11 11:37:29 +0000 (Sun, 11 Dec 2011) Log Message: ----------- [tbsl expolration] found error in Sqlite function 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 trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/mySQLDictionary.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-11 09:24:58 UTC (rev 3496) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/GetRessourcePropertys.java 2011-12-11 11:37:29 UTC (rev 3497) @@ -31,6 +31,9 @@ * @throws IOException */ private HashMap<String,String> sendServerPropertyRequest(String vergleich, String side) throws IOException{ + + System.out.println("Resource die gesucht wird: "+ vergleich); + System.out.println("Seite die gesucht wird: "+side); /* * * For the second Iteration, I can just add the sparql property here. @@ -53,12 +56,13 @@ //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="; - String verarbeitungsstring=null; - if(side.contains("RIGHT")) verarbeitungsstring=tmp_right; - if(side.contains("LEFT")) verarbeitungsstring=tmp_left; + String verarbeitungsurl=null; + if(side.contains("RIGHT")) verarbeitungsurl=tmp_right; + if(side.contains("LEFT")) verarbeitungsurl=tmp_left; + System.out.println(verarbeitungsurl); //just in case..... - if(!side.contains("LEFT") && !side.contains("RIGHT")) verarbeitungsstring=tmp_left; + if(!side.contains("LEFT") && !side.contains("RIGHT")) verarbeitungsurl=tmp_left; //String verarbeitungsstring="http://greententacle.techfak.uni-bielefeld.de:5171/sparql?default-graph-uri=&query=PREFIX+rdfs%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0D%0APREFIX+res%3A+%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2F%3E%0D%0A%0D%0ASELECT+DISTINCT+%3Fp+%3Fl+WHERE++{%0D%0A+{+res%3A"+vergleich+"+%3Fp+%3Fo+.+}%0D%0A+UNION%0D%0A+{+%3Fs+%3Fp+res%3A"+vergleich+"+.+}%0D%0A+{+%3Fp+rdfs%3Alabel+%3Fl+.+}%0D%0A}%0D%0A&format=text%2Fhtml&debug=on&timeout="; URL url; @@ -69,7 +73,7 @@ String result=""; try { - url = new URL(verarbeitungsstring); + url = new URL(verarbeitungsurl); is = url.openStream(); isr = new InputStreamReader(is); r = new BufferedReader(isr); 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-11 09:24:58 UTC (rev 3496) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/SparqlObject.java 2011-12-11 11:37:29 UTC (rev 3497) @@ -47,29 +47,31 @@ //global Variable dict //start counting with 0 - static int explorationdepthwordnet=1; + static int explorationdepthwordnet=2; static int iterationdepth =0; static int numberofanswers=1; - static double LvenstheinMin = 0.95; + static double LevenstheinMin = 0.8; static WordNet wordnet; BasicTemplator btemplator; Templator templator; -/* private static HashMap<String, String> hm = new HashMap<String, String>(); - private static HashMap<String, String> hm_new = new HashMap<String, String>();*/ private static mySQLDictionary myindex; - //Konstruktor public SparqlObject() throws MalformedURLException, ClassNotFoundException, SQLException{ wordnet = new WordNet(); System.out.println("Loading SPARQL Templator"); + // btemplator = new BasicTemplator(); + //wenn ich das nehme, dann gebe ich dem parser den ideal.... + //btemplator.UNTAGGED_INPUT = false; templator = new Templator(); System.out.println("Loading SPARQL Templator Done\n"); System.out.println("Start Indexing"); myindex = new mySQLDictionary(); System.out.println("Done:Indexing"); + + //normaly 1 setExplorationdepthwordnet(1); //eigentlich immer mit 0 initialisieren setIterationdepth(1); @@ -155,7 +157,7 @@ new InputStreamReader( new FileInputStream( "/tmp/testresult.txt" ) ) ); while( null != (s = in.readLine()) ) { - tmp=tmp+"\n"+s; + tmp+="\n"+s; } } catch( FileNotFoundException ex ) { } catch( Exception ex ) { @@ -198,7 +200,7 @@ new InputStreamReader( new FileInputStream( "/tmp/answer.txt" ) ) ); while( null != (s = in.readLine()) ) { - tmp=tmp+"\n"+s; + tmp+="\n"+s; } } catch( FileNotFoundException ex ) { } catch( Exception ex ) { @@ -221,6 +223,9 @@ * ################################################################################################# */ //Iterration 1 + /* + * Only Levensthein!!! + */ if(getIterationdepth()==1&&startIterating==true){ /* //4, because of query + three conditions for the simple case @@ -240,7 +245,7 @@ */ //Iterration 2 /* - * Only Levensthein!!! + * Only Wordnet!!! */ if(getIterationdepth()==2&&startIterating==true){ if(querylist.size()==4)final_answer=simpleWordnetIteration(querylist, query); @@ -260,7 +265,7 @@ new FileInputStream( "/tmp/answer" ) ) ); String s; while( null != (s = in.readLine()) ) { - tmp=tmp+"\n"+s; + tmp+="\n"+s; } } catch( FileNotFoundException ex ) { } catch( Exception ex ) { @@ -277,7 +282,7 @@ String out=""; for(String answer : final_answer){ //only answered question - if(!answer.contains("Error in searching Wordnet with word") && !answer.contains("EmtyAnswer")&& !answer.contains("Error in getting Properties"))out=out+ "\n"+answer+"\n"; + //if(!answer.contains("Error in searching Wordnet with word") && !answer.contains("EmtyAnswer")&& !answer.contains("Error in getting Properties"))out=out+ "\n"+answer+"\n"; /* //only questions with wordnet error @@ -289,8 +294,8 @@ //only questions with Error in Properties // if(answer.contains("Error in getting Properties"))out=out+ "\n"+answer+"\n"; + out+= "\n"+answer+"\n"; - } System.out.println(question); System.out.println(out); @@ -305,9 +310,8 @@ private ArrayList<String> simpleLevinstheinIteration(ArrayList<String> querylist, String query) throws SQLException, JWNLException { - //asking server - String answer; - ArrayList<String> final_answer=new ArrayList<String>(); + + ArrayList<String> final_answer=new ArrayList<String>(); String resource=""; String property_to_compare_with=""; String sideOfProperty="LEFT"; @@ -334,54 +338,53 @@ } 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; + + System.out.println("URI from Resource "+ resource +": "+getUriFromIndex(resource.toLowerCase(),0)); + + //gets Propertys left or right from the resource! 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); + + System.out.println("Properties from Resource "+resource+": "+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>(); + //iterate over properties 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); + String key = entry.getKey(); + 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); - /* - * TODO: Implement Normalised levensthein - */ - //if(tmp<=3.0){ - if(nld>=LvenstheinMin){ - //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); + //check if nld is greater than Levensthein + if(nld>=LevenstheinMin){ + //if its so, replace old uri with the new one + String querynew=query; + String replacement = getUriFromIndex(property_to_compare_with.toLowerCase(),1); + querynew=querynew.replace(replacement,value); + System.out.println("Simple Levensthein Query: "+ querynew); + new_queries.add(querynew); } } - + + //iterate over all Queries and get answer from Server for(String anfrage : new_queries){ String answer_tmp; answer_tmp=sendServerQuestionRequest(anfrage); @@ -396,15 +399,8 @@ private ArrayList<String> complexeLevinstheinIteration(ArrayList<String> querylist, String query) throws SQLException, JWNLException { - - /* - * in this case we need a inner and outer loop, because the properties are different from the resource or up side down - * Only for questions with two sets of conditions(s p o . s p o) - */ - - //asking server - String answer; - ArrayList<String> final_answer=new ArrayList<String>(); + + ArrayList<String> final_answer=new ArrayList<String>(); String resourceOne=""; String property_to_compare_withOne=""; String resourceTwo=""; @@ -416,6 +412,8 @@ int tmpcounter=0; for(String s : querylist){ //we dont need the first one, because thats the query itself + + //for condition One tmpcounter=tmpcounter+1; if(tmpcounter>=1&&tmpcounter<=4){ if(s.contains("LEFT")){ @@ -431,6 +429,8 @@ } } + + //for condition Two if(tmpcounter>4){ if(s.contains("LEFT")){ sideOfPropertyTwo="LEFT"; @@ -450,19 +450,19 @@ HashMap<String,String> propertiesTwo = new HashMap<String, String>(); GetRessourcePropertys property = new GetRessourcePropertys(); Boolean goOnAfterProperty = true; + + //Get Properties for Resource in condition One and Two from Server try { propertiesOne=property.getPropertys(getUriFromIndex(resourceOne.toLowerCase(),0),sideOfPropertyOne); propertiesTwo=property.getPropertys(getUriFromIndex(resourceTwo.toLowerCase(),0),sideOfPropertyTwo); + if (propertiesOne==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) { - //e.printStackTrace(); final_answer.add("Begin:\n"+query +"\nError in getting Properties \n End"); goOnAfterProperty=false; @@ -471,55 +471,51 @@ 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>(); + //Iterate over property from resource one for (Entry<String, String> entryOne : propertiesOne.entrySet()) { String queryOne=query; String keyOne = entryOne.getKey(); String valueOne = entryOne.getValue(); - //double levnstheinDistanzeOne=Levenshtein.computeLevenshteinDistance(property_to_compare_withOne.toLowerCase(), keyOne); - //if(levnstheinDistanzeOne<=3.0){ + + double levnstheinDistanzeOne=Levenshtein.nld(property_to_compare_withOne.toLowerCase(), keyOne); - - /* - * TODO: Implement Normalised levensthein - */ - //if(tmp<=3.0){ - if(levnstheinDistanzeOne>=LvenstheinMin){ + + /*if distance is higher or equals LevenstheinMin, replace old uri with new uri + * and use that new query, for the property of the second resource + */ + if(levnstheinDistanzeOne>=LevenstheinMin){ String test = getUriFromIndex(property_to_compare_withOne.toLowerCase(),1); queryOne=queryOne.replace(test,valueOne); } + /* + * Iterate now over the second set of properties, but this time not using the original query in which + * to replace the old uri with the new one, but using queryOne from the first step. + */ for (Entry<String, String> entryTwo : propertiesTwo.entrySet()) { String keyTwo = entryTwo.getKey(); String valueTwo = entryTwo.getValue(); - // double levnstheinDistanzeTwo=Levenshtein.computeLevenshteinDistance(property_to_compare_withTwo.toLowerCase(), keyTwo); - - /* - * TODO: Implement Normalised levensthein - */ - //if(levnstheinDistanzeTwo<=3.0){ - double levnstheinDistanzeTwo=Levenshtein.nld(property_to_compare_withTwo.toLowerCase(), keyTwo); + + //again calculate the nld with the property from the second condition and the property from the propertyset + double levnstheinDistanzeTwo=Levenshtein.nld(property_to_compare_withTwo.toLowerCase(), keyTwo); - /* - * TODO: Implement Normalised levensthein - */ - //if(tmp<=3.0){ - if(levnstheinDistanzeTwo>=0.9){ - //alte property uri mit neuer ersetzen: - String queryTwo=queryOne; - String test = getUriFromIndex(property_to_compare_withTwo.toLowerCase(),1); - queryTwo=queryTwo.replace(test,valueTwo); - new_queries.add(queryTwo); - } - + if(levnstheinDistanzeTwo>LevenstheinMin){ + String queryTwo=queryOne; + String replacement = getUriFromIndex(property_to_compare_withTwo.toLowerCase(),1); + queryTwo=queryTwo.replace(replacement,valueTwo); + System.out.println("Complex Levensthein Query: "+ queryTwo); + new_queries.add(queryTwo); + } + } } - + //iterate over all Queries and get answer from Server for(String anfrage : new_queries){ String answer_tmp; answer_tmp=sendServerQuestionRequest(anfrage); @@ -534,16 +530,9 @@ private ArrayList<String> simpleWordnetIteration(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")){ + System.out.println("In simpleWordnetIteration"); String resource=""; String property_to_compare_with=""; @@ -571,28 +560,27 @@ } 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) { - //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>(); System.out.println("Start Iterating Wordnet with "+property_to_compare_with+" and deept of "+explorationdepthwordnet); @@ -600,6 +588,7 @@ ArrayList<String> tmp_semantics=new ArrayList<String>(); ArrayList<String> result_SemanticsMatchProperties=new ArrayList<String>(); semantics.add(property_to_compare_with); + System.out.println("Semantics: "+ semantics); //first check, if there is a singular form in the wordnet dictionary.. eg children -> child String _temp_=myindex.getWordnetHelp(property_to_compare_with); @@ -611,11 +600,21 @@ semantics.add(_temp_); tmp_semantics=semantics; } + + System.out.println("tmp_semantics: "+ tmp_semantics); Boolean goOnAfterWordnet = true; + + + System.out.println("##########################"); + System.out.println("properties for "+getUriFromIndex(resource.toLowerCase(),0)+": "+properties); + System.out.println("Property to compare with: "+property_to_compare_with); + System.out.println("Semantics: "+semantics); + System.out.println("##########################"); for(int i=0;i<=explorationdepthwordnet;i++){ try { tmp_semantics=getSemantics(tmp_semantics); + System.out.println("tmp_semantics in Iteration: "+ tmp_semantics); if (tmp_semantics==null){ goOnAfterWordnet=false; final_answer.add("Begin:\n"+query +"\n Error in searching Wordnet with word "+semantics+" \n End"); @@ -629,8 +628,7 @@ } } 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"); @@ -646,19 +644,23 @@ for(String b : semantics){ if(key.contains(b.toLowerCase())){ + System.out.println("Hey, Iam in too!!!!!!!!!!!"); + //to check, if no property is used twice... if(!result_SemanticsMatchProperties.contains(key)){ //create new query - result_SemanticsMatchProperties.add(key); + System.out.println("Hey, Iam in!!!!!!!!!!!"); + 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); + System.out.println("Simple Wordnet Query: "+ query_tmp); new_queries.add(query_tmp); } } } } + //iterate over all Queries and get answer from Server for(String bla : new_queries){ String answer_tmp; answer_tmp=sendServerQuestionRequest(bla); @@ -667,25 +669,16 @@ } } } - } + return final_answer; } private ArrayList<String> complexWordnetIteration(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 resourceOne=""; String property_to_compare_withOne=""; String resourceTwo=""; @@ -698,6 +691,7 @@ for(String s : querylist){ //we dont need the first one, because thats the query itself tmpcounter=tmpcounter+1; + //get resource and property from the first condtion if(tmpcounter>=1&&tmpcounter<=4){ if(s.contains("LEFT")){ sideOfPropertyOne="LEFT"; @@ -712,6 +706,7 @@ } } + //get resource and property from the second condtion if(tmpcounter>4){ if(s.contains("LEFT")){ sideOfPropertyTwo="LEFT"; @@ -729,24 +724,24 @@ } System.out.println("Property to compare:: "+ property_to_compare_withOne); System.out.println("Resource: "+ resourceOne); + HashMap<String,String> propertiesOne = new HashMap<String, String>(); HashMap<String,String> propertiesTwo = new HashMap<String, String>(); GetRessourcePropertys property = new GetRessourcePropertys(); Boolean goOnAfterProperty = true; + + //gets the properties for both conditions try { propertiesOne=property.getPropertys(getUriFromIndex(resourceOne.toLowerCase(),0),sideOfPropertyOne); propertiesTwo=property.getPropertys(getUriFromIndex(resourceTwo.toLowerCase(),0),sideOfPropertyTwo); if (propertiesOne==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; @@ -756,7 +751,7 @@ /* * #################################### Semantics One############################################# */ - //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>(); //System.out.println("Start Iterating Wordnet with "+property_to_compare_withOne+" and deept of "+explorationdepthwordnet); @@ -775,6 +770,8 @@ semanticsOne.add(_temp_One); tmp_semanticsOne=semanticsOne; } + + //get the "semantics" from wordnet. Iterate as long as the explorationdepthwordnet is reached Boolean goOnAfterWordnet = true; for(int i=0;i<=explorationdepthwordnet;i++){ @@ -793,8 +790,7 @@ } } 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 "+semanticsOne+" \n End"); @@ -823,6 +819,7 @@ tmp_semanticsTwo=semanticsTwo; } + //get the "semantics" from wordnet. Iterate as long as the explorationdepthwordnet is reached for(int i=0;i<=explorationdepthwordnet;i++){ try { @@ -840,8 +837,7 @@ } } 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 "+semanticsTwo+" \n End"); @@ -853,6 +849,8 @@ if(goOnAfterWordnet==true){ + + //start iterating over the propery sets for (Entry<String, String> entryOne : propertiesOne.entrySet()) { String keyOne = entryOne.getKey(); String valueOne = entryOne.getValue(); @@ -878,7 +876,7 @@ String queryTwo=queryOne; String replacementTwo = getUriFromIndex(property_to_compare_withTwo.toLowerCase(),1); queryTwo=queryTwo.replace(replacementTwo,valueTwo); - System.out.println("New query after wordnet: "+ queryTwo); + System.out.println("Complexe Wordnet Query: "+ queryTwo); new_queries.add(queryTwo); } } @@ -895,7 +893,7 @@ - + //iterate over all Queries and get answer from Server for(String bla : new_queries){ String answer_tmp; answer_tmp=sendServerQuestionRequest(bla); @@ -904,7 +902,6 @@ } } } - } return final_answer; } @@ -1033,8 +1030,8 @@ - /*System.out.println("Conditions: " + conditions); - System.out.println("Conditions_new: " + 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+ "}"; @@ -1163,14 +1160,33 @@ */ private String getUriFromIndex(String string, int fall) throws SQLException{ String result=null; + String tmp1=null; + String tmp2 = null; //just to be sure its only 0 or 1 if(fall!=0 && fall!=1) fall=0; if(fall==0){ - result=myindex.getResourceURI(string.toLowerCase()); + + //first try: take always the ontology if existing and not the Resource + tmp1=myindex.getResourceURI(string.toLowerCase()); + tmp2=myindex.getontologyClassURI(string.toLowerCase()); + System.out.println("URI from resource: "+tmp1); + System.out.println("URI from ontologyClass: "+tmp2); + + + System.out.println("value from http://dbpedia.org/resource/WikiLeaks : "+ myindex.getResourceURI("http://dbpedia.org/resource/WikiLeaks")); + System.out.println("value from author : "+ myindex.getResourceURI("author")); + if(tmp1!=null && tmp2!=null) result=tmp2; + if(tmp1!=null && tmp2==null) result=tmp1; + if(tmp1==null && tmp2!=null) result=tmp2; + //result=myindex.getResourceURI(string.toLowerCase()); if(result==null)result=myindex.getPropertyURI(string.toLowerCase()); } if(fall==1){ - result=myindex.getPropertyURI(string.toLowerCase()); + tmp1=myindex.getPropertyURI(string.toLowerCase()); + tmp2=myindex.getontologyURI(string.toLowerCase()); + if(tmp1!=null && tmp2!=null) result=tmp2; + if(tmp1!=null && tmp2==null) result=tmp1; + if(tmp1==null && tmp2!=null) result=tmp2; if(result==null){ result=myindex.getResourceURI(string.toLowerCase()); if(result!=null) result=result.replace("resource", "property"); @@ -1184,10 +1200,11 @@ if(result==null) { if(fall==1)return "http://dbpedia.org/property/"+tmp; if(fall==0)return "http://dbpedia.org/resource/"+tmp; - else{ + else{ + System.out.println("return result: "+result); return result; - } } + } else return result; } @@ -1198,22 +1215,62 @@ * 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 { + private static ArrayList<String> getSemantics (ArrayList<String> semanticsOrig) throws IOException, JWNLException { ArrayList<String> result = new ArrayList<String>(); - //result.clear(); - //try{ - try{ + + //System.out.println("in function get Semantics!"); + + ArrayList<String> semantics = new ArrayList<String>(); + semantics=semanticsOrig; + /*//check out, if in the semantics are still terms, with _ or , + //if so, split on _ and , and add them to the semantic list + for(String id :semanticsOrig){ + if(id.contains("_")){ + System.out.println("in _"); + String[] tmp=id.split("_"); + for(String i: tmp) if(!semantics.contains(i))semantics.add(i); + + //and also add a term without _ + if(!semantics.contains(id.replace("_"," ")))semantics.add(id.replace("_"," ")); + //remove old id + //semantics.remove(id); + } + if(id.contains(",")){ + System.out.println("in ,"); + String[] tmp=id.split(","); + for(String i: tmp) if(!semantics.contains(i))semantics.add(i); + //semantics.remove(id); + } + }*/ + for(String id :semantics){ + //System.out.println("in String id : semantics"); + //System.out.println("ID :"+id); + + //add id also to the result, if its not already in there + if(!result.contains(id))result.add(id); List<String> array_relatedNouns=null; List<String> array_bestsynonyms=null; - List<String> array_siterterms=null; - //array.clear(); - System.out.println("Wordnet Word: "+id); + + System.out.println("Wordnet Word: "+id); + try{ array_relatedNouns =wordnet.getRelatedNouns(id); + } + catch(Exception e){ + //array_relatedNouns.clear(); + } + System.out.println("array_relatedNouns: "+ array_relatedNouns); + //System.out.println("after relatedNouns"); + try{ array_bestsynonyms=wordnet.getBestSynonyms(POS.NOUN, id); + System.out.println("array_bestsynonyms: "+ array_bestsynonyms); + } + catch(Exception e){ + // + } + - array_siterterms=wordnet.getSisterTerms(POS.NOUN, id); if(array_relatedNouns!=null){ for(String i:array_relatedNouns){ @@ -1225,20 +1282,11 @@ if(!result.contains(i))result.add(i); } } - if(array_siterterms!=null){ - for(String i:array_siterterms){ - if(!result.contains(i))result.add(i); - } - } } - } - catch(Exception e){ - if(result.isEmpty()) return null; - } - + if(!result.isEmpty()) return result; else{ //System.out.println("Didnt find ") @@ -1248,20 +1296,33 @@ */ try{ for(String id :semantics){ + //System.out.println("in String id : semantics TWO"); String[] tmp_array=id.split(" "); + //System.out.println("ID TWO:"+id); if(tmp_array.length>=2){ - for(String tmp : tmp_array){ + for(String advanced_id : tmp_array){ List<String> array_relatedNouns=null; List<String> array_bestsynonyms=null; - List<String> array_siterterms=null; - //array.clear(); - //System.out.println("Wordnet Word: "+tmp); - array_relatedNouns =wordnet.getRelatedNouns(tmp); - - array_bestsynonyms=wordnet.getBestSynonyms(POS.NOUN, tmp); + //add id also to the result, if its not already in there + if(!result.contains(advanced_id))result.add(advanced_id); + + try{ + array_relatedNouns =wordnet.getRelatedNouns(advanced_id); + } + catch(Exception e){ + //array_relatedNouns.clear(); + } + System.out.println("array_relatedNouns: "+ array_relatedNouns); + //System.out.println("after relatedNouns"); + + try{ + array_bestsynonyms=wordnet.getBestSynonyms(POS.NOUN, advanced_id); + System.out.println("array_bestsynonyms: "+ array_bestsynonyms); + } + catch(Exception e){ + // + } - array_siterterms=wordnet.getSisterTerms(POS.NOUN, tmp); - if(array_relatedNouns!=null){ for(String i:array_relatedNouns){ if(!result.contains(i))result.add(i); @@ -1272,11 +1333,7 @@ if(!result.contains(i))result.add(i); } } - if(array_siterterms!=null){ - for(String i:array_siterterms){ - if(!result.contains(i))result.add(i); - } - } + } } @@ -1284,7 +1341,7 @@ } } catch(Exception e){ - if(result.isEmpty()) return null; + if(result.isEmpty()) return semanticsOrig; } } Modified: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/mySQLDictionary.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/mySQLDictionary.java 2011-12-11 09:24:58 UTC (rev 3496) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/mySQLDictionary.java 2011-12-11 11:37:29 UTC (rev 3497) @@ -22,6 +22,8 @@ createIndexPropertys(); createIndexResource(); createWordnetHelp(); + createIndexOntology(); + createIndexoOntologyClass(); //optional!! //createIndexWikipedia(); @@ -57,6 +59,38 @@ } + public String getontologyURI(String string) throws SQLException{ + Statement stat = conn.createStatement(); + ResultSet rs; + try { + rs = stat.executeQuery("select uri from ontology where name='"+string.toLowerCase()+"';"); + return rs.getString("uri"); + } catch (Exception e) { + // TODO Auto-generated catch block + //e.printStackTrace(); + return null; + } + + + } + + public String getontologyClassURI(String string) throws SQLException{ + Statement stat = conn.createStatement(); + ResultSet rs; + try { + rs = stat.executeQuery("select uri from ontologyClass where name='"+string.toLowerCase()+"';"); + return rs.getString("uri"); + } catch (Exception e) { + // TODO Auto-generated catch block + //e.printStackTrace(); + return null; + } + + + } + + + public String getWikipediaURI(String string) throws SQLException{ Statement stat = conn.createStatement(); ResultSet rs; @@ -197,40 +231,6 @@ prep.executeBatch(); conn.setAutoCommit(true); System.out.println("Done"); - //Statement stat = conn.createStatement(); - /* long start = System.currentTimeMillis(); - // zu messender Code - - ResultSet rs = stat.executeQuery("select * from people where name='kornyval';"); - while (rs.next()) - { - System.out.println("name = " + rs.getString("name")); - System.out.println("job = " + rs.getString("occupation")); - } - System.out.println("Duration in ms: " + (System.currentTimeMillis() - start)); - - start = System.currentTimeMillis(); - // zu messender Code - - rs = stat.executeQuery("select * from people where name='barack obama';"); - while (rs.next()) - { - System.out.println("name = " + rs.getString("name")); - System.out.println("job = " + rs.getString("occupation")); - } - System.out.println("Duration in ms: " + (System.currentTimeMillis() - start)); - - rs = stat.executeQuery("select * from people where name='kornyval';"); - while (rs.next()) - { - System.out.println("name = " + rs.getString("name")); - System.out.println("job = " + rs.getString("occupation")); - } - System.out.println("Duration in ms: " + (System.currentTimeMillis() - start)); - - - rs.close();*/ - // conn.close(); } private void createIndexPropertys() throws ClassNotFoundException, SQLException{ /*System.out.println("Start SQL test"); @@ -252,8 +252,8 @@ while( null != (s = in.readLine()) ) { String[] tmp_array =s.split(":::"); if(tmp_array.length>=2){ - prep.setString(1, tmp_array[1]); - prep.setString(2, tmp_array[0]); + prep.setString(1, tmp_array[0]); + prep.setString(2, tmp_array[1]); prep.addBatch(); zaehler=zaehler+1; //if(zaehler%10000==0) System.out.println(zaehler); @@ -303,8 +303,8 @@ while( null != (s = in.readLine()) ) { String[] tmp_array =s.split(":::"); if(tmp_array.length>=2){ - prep.setString(1, tmp_array[1]); - prep.setString(2, tmp_array[0]); + prep.setString(1, tmp_array[0]); + prep.setString(2, tmp_array[1]); prep.addBatch(); zaehler=zaehler+1; // if(zaehler%10000==0) System.out.println(zaehler); @@ -312,7 +312,7 @@ conn.setAutoCommit(false); prep.executeBatch(); conn.setAutoCommit(false); - System.out.println("done"); + System.out.println("done"+zaehler); } } @@ -334,7 +334,113 @@ prep.executeBatch(); conn.setAutoCommit(true); System.out.println("Done"); + + } +private void createIndexOntology() throws ClassNotFoundException, SQLException{ + /*System.out.println("Start SQL test");*/ + System.out.println("start indexing Ontology"); + Statement stat = conn.createStatement(); + stat.executeUpdate("drop table if exists ontology;"); + stat.executeUpdate("create table ontology (name, uri);"); + PreparedStatement prep = conn.prepareStatement("insert into ontology values (?, ?);"); + BufferedReader in=null; + // conn.setAutoCommit(false); + int zaehler=0; + try { + in = new BufferedReader( + new InputStreamReader( + new FileInputStream( "/home/swalter/workspace/ontology" ) ) ); + String s; + while( null != (s = in.readLine()) ) { + String[] tmp_array =s.split(":::"); + if(tmp_array.length>=2){ + prep.setString(1, tmp_array[0]); + prep.setString(2, tmp_array[1]); + prep.addBatch(); + zaehler=zaehler+1; + // if(zaehler%10000==0) System.out.println(zaehler); + if(zaehler%1000000==0){ + conn.setAutoCommit(false); + prep.executeBatch(); + conn.setAutoCommit(false); + System.out.println("done" + zaehler); + } + } + } + } catch( FileNotFoundException ex ) { + } catch( Exception ex ) { + System.out.println( ex ); + } finally { + if( in != null ) + try { + in.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + conn.setAutoCommit(false); + prep.executeBatch(); + conn.setAutoCommit(true); + System.out.println("Done"); + + } + +private void createIndexoOntologyClass() throws ClassNotFoundException, SQLException{ + /*System.out.println("Start SQL test");*/ + System.out.println("start indexing ontologyClass"); + Statement stat = conn.createStatement(); + stat.executeUpdate("drop table if exists ontologyClass;"); + stat.executeUpdate("create table ontologyClass (name, uri);"); + PreparedStatement prep = conn.prepareStatement("insert into ontologyClass values (?, ?);"); + BufferedReader in=null; + // conn.setAutoCommit(false); + int zaehler=0; + try { + in = new BufferedReader( + new InputStreamReader( + new FileInputStream( "/home/swalter/workspace/ontologyClass" ) ) ); + String s; + while( null != (s = in.readLine()) ) { + String[] tmp_array =s.split(":::"); + if(tmp_array.length>=2){ + prep.setString(1, tmp_array[0]); + prep.setString(2, tmp_array[1]); + prep.addBatch(); + zaehler=zaehler+1; + // if(zaehler%10000==0) System.out.println(zaehler); + if(zaehler%1000000==0){ + conn.setAutoCommit(false); + prep.executeBatch(); + conn.setAutoCommit(false); + System.out.println("done" + zaehler); + } + + } + } + } catch( FileNotFoundException ex ) { + } catch( Exception ex ) { + System.out.println( ex ); + } finally { + if( in != null ) + try { + in.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + conn.setAutoCommit(false); + prep.executeBatch(); + conn.setAutoCommit(true); + System.out.println("Done"); + + } + + } \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <seb...@us...> - 2011-12-15 11:11:24
|
Revision: 3503 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3503&view=rev Author: sebastianwtr Date: 2011-12-15 11:11:12 +0000 (Thu, 15 Dec 2011) Log Message: ----------- [tbsl exploration] changed getQuery function and dependencies 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-13 11:58:38 UTC (rev 3502) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/GetRessourcePropertys.java 2011-12-15 11:11:12 UTC (rev 3503) @@ -111,6 +111,7 @@ //System.out.println(tmp_array[i-1].toLowerCase() + " " +tmp_array[i]); } + System.out.println("created Properties: "+hm); return hm; } 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-13 11:58:38 UTC (rev 3502) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/SparqlObject.java 2011-12-15 11:11:12 UTC (rev 3503) @@ -62,8 +62,7 @@ System.out.println("Loading SPARQL Templator"); // btemplator = new BasicTemplator(); - //wenn ich das nehme, dann gebe ich dem parser den ideal.... - //btemplator.UNTAGGED_INPUT = false; + btemplator.UNTAGGED_INPUT = false; templator = new Templator(); System.out.println("Loading SPARQL Templator Done\n"); System.out.println("Start Indexing"); @@ -74,7 +73,7 @@ //normaly 1 setExplorationdepthwordnet(1); //eigentlich immer mit 0 initialisieren - setIterationdepth(1); + setIterationdepth(9); setNumberofanswers(1); } @@ -189,7 +188,7 @@ * ################################################################################################# */ //Iteration 0 - if(getIterationdepth()==0&&startIterating==true){ + if(getIterationdepth()==0&&startIterating==true||getIterationdepth()==9&&startIterating==true){ String tmp = new String(); String s = null; BufferedReader in = null; @@ -226,7 +225,7 @@ /* * Only Levensthein!!! */ - if(getIterationdepth()==1&&startIterating==true){ + if(getIterationdepth()==1&&startIterating==true||getIterationdepth()==9&&startIterating==true){ /* //4, because of query + three conditions for the simple case if(querylist.size()==4)final_answer=simpleIteration1Case(querylist, query); @@ -235,10 +234,16 @@ if(querylist.size()>4)final_answer=complexeIteration1Case(querylist, query); */ + ArrayList<String> final_answer_tmp = new ArrayList<String>(); + if(querylist.size()==4)final_answer_tmp=simpleLevinstheinIteration(querylist, query); + if(querylist.size()>4)final_answer_tmp=complexeLevinstheinIteration(querylist, query); - if(querylist.size()==4)final_answer=simpleLevinstheinIteration(querylist, query); - if(querylist.size()>4)final_answer=complexeLevinstheinIteration(querylist, query); + for(String i : final_answer_tmp){ + final_answer.add(i); + } + + } /* * ################################################################################################# @@ -247,9 +252,18 @@ /* * Only Wordnet!!! */ - if(getIterationdepth()==2&&startIterating==true){ - if(querylist.size()==4)final_answer=simpleWordnetIteration(querylist, query); - if(querylist.size()>4)final_answer=complexWordnetIteration(querylist, query); + if(getIterationdepth()==2&&startIterating==true||getIterationdepth()==9&&startIterating==true){ + ArrayList<String> final_answer_tmp = new ArrayList<String>(); + + if(querylist.size()==4)final_answer_tmp=simpleWordnetIteration(querylist, query); + //if(querylist.size()>4)final_answer=complexWordnetIteration(querylist, query); + + //for a test only use: + if(querylist.size()>4)final_answer_tmp=newIteration(querylist,query); + + for(String i : final_answer_tmp){ + final_answer.add(i); + } } @@ -281,8 +295,9 @@ String out=""; for(String answer : final_answer){ + if(answer!=null){ //only answered question - //if(!answer.contains("Error in searching Wordnet with word") && !answer.contains("EmtyAnswer")&& !answer.contains("Error in getting Properties"))out=out+ "\n"+answer+"\n"; + if(!answer.contains("Error in searching Wordnet with word") && !answer.contains("EmtyAnswer")&& !answer.contains("Error in getting Properties"))out=out+ "\n"+answer+"\n"; /* //only questions with wordnet error @@ -294,11 +309,17 @@ //only questions with Error in Properties // if(answer.contains("Error in getting Properties"))out=out+ "\n"+answer+"\n"; - out+= "\n"+answer+"\n"; + //out+= "\n"+answer+"\n"; + } + else{ + System.out.println("Answer was null"); + } } System.out.println(question); + out = out.replace("@en","").replace("\"",""); System.out.println(out); + BufferedWriter outfile = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( "/tmp/answer" ) ) ); @@ -307,6 +328,120 @@ outfile.close(); } + private ArrayList<String> newIteration(ArrayList<String> querylist, String query) throws SQLException, + JWNLException { + //only for special case, that the first condition has a resource + ArrayList<String> final_answer=new ArrayList<String>(); + String firstResource=""; + String firstProperty=""; + String secondProperty=null; + String sideOfProperty=null; + String sideOfPropertyTwo=null; + int tmpcounter=0; + for(String s : querylist){ + //we dont need the first one, because thats the query itself + tmpcounter=tmpcounter+1; + if(tmpcounter>=1&&tmpcounter<=4){ + if(s.contains("LEFT")){ + sideOfProperty="LEFT"; + firstResource=s.replace("LEFT",""); + } + if(s.contains("RIGHT")){ + sideOfProperty="RIGHT"; + firstResource=s.replace("RIGHT",""); + } + if(s.contains("PROPERTY")){ + firstProperty=s.replace("PROPERTY",""); + } + + } + if(tmpcounter>4){ + if(s.contains("LEFT")){ + sideOfPropertyTwo="LEFT"; + } + if(s.contains("RIGHT")){ + sideOfPropertyTwo="RIGHT"; + } + if(s.contains("PROPERTY")){ + secondProperty=s.replace("PROPERTY",""); + } + + } + + } + //first create Query and get the URI's + String firstquery=""; + if(sideOfProperty=="RIGHT"){ + firstquery="PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT ?y WHERE {<"+getUriFromIndex(firstResource.toLowerCase(),0)+"> <"+getUriFromIndex(firstProperty.toLowerCase(),1) +"> ?y}"; + } + if(sideOfProperty=="RIGHT"){ + firstquery="PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT ?y WHERE {<"+getUriFromIndex(firstProperty.toLowerCase(),1)+"> <"+getUriFromIndex(firstResource.toLowerCase(),0) +"> ?y}"; + + } + + //first try without iterating over wordnet and levensthein + ArrayList<String> answer_tmp=new ArrayList<String>(); + answer_tmp=sendServerQuestionRequestArray(firstquery); + + //if answer_tmp is emty try to iterate in this case with wordnet + ArrayList<String>querylist_new=new ArrayList<String>(); + querylist_new.add(firstquery); + querylist_new.add("PROPERTY"+firstProperty); + querylist_new.add(sideOfProperty+firstResource); + if(answer_tmp.isEmpty()){ + answer_tmp=simpleWordnetIterationArray(querylist_new,firstquery); + } + //if answer_tmp is still empty return null and exit function + if(answer_tmp.isEmpty()){final_answer.add("new Iteration didnt work"); + + return final_answer; + } + + ArrayList<ArrayList<String>>secondquerylist=new ArrayList<ArrayList<String>>(); + + //we have now the uri's for the second query and the result answers + //create now for every entry, if it contains something like http an new query + for(String s : answer_tmp){ + System.out.println("!!!!!!!!!!!!!"); + System.out.println("URI found: "+ s); + System.out.println("!!!!!!!!!!!!!"); + String secondquery =""; + ArrayList<String> tmp = new ArrayList<String>(); + if(s.contains("http:")){ + if(sideOfPropertyTwo=="RIGHT"){ + secondquery="PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT ?y WHERE {<"+getUriFromIndex(s.toLowerCase(),0)+"> <"+getUriFromIndex(secondProperty.toLowerCase(),1) +"> ?y}"; + tmp.add(secondquery); + tmp.add("PROPERTY"+secondProperty); + querylist_new.add(sideOfPropertyTwo+s); + secondquerylist.add(tmp); + } + if(sideOfPropertyTwo=="RIGHT"){ + secondquery="PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT ?y WHERE {<"+getUriFromIndex(secondProperty.toLowerCase(),1)+"> <"+getUriFromIndex(s.toLowerCase(),0) +"> ?y}"; + tmp.add(secondquery); + tmp.add("PROPERTY"+secondProperty); + querylist_new.add(sideOfPropertyTwo+s); + secondquerylist.add(tmp); + } + + } + } + + + //TODO: Check this part of the function!!! + for(ArrayList as: secondquerylist){ + ArrayList<String> answer_tmp_two=new ArrayList<String>(); + //answer_tmp_two=sendServerQuestionRequestArray(s); + answer_tmp=simpleWordnetIterationArray(as,as.get(0).toString()); + for(String t :answer_tmp_two){ + final_answer.add(t); + System.out.println("Answer from advanced Iteration: "+ t); + } + } + if(final_answer.isEmpty())final_answer.add("new Iteration didnt work"); + System.out.println("Returning the function"); + return final_answer; + + } private ArrayList<String> simpleLevinstheinIteration(ArrayList<String> querylist, String query) throws SQLException, JWNLException { @@ -335,6 +470,7 @@ } } + } System.out.println("Property to compare:: "+ property_to_compare_with); System.out.println("Resource: "+ resource); @@ -368,7 +504,10 @@ //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); @@ -377,6 +516,9 @@ //if its so, replace old uri with the new one String querynew=query; String replacement = getUriFromIndex(property_to_compare_with.toLowerCase(),1); + if(!querynew.contains(replacement)){ + replacement=replacement.replace("ontology", "property"); + } querynew=querynew.replace(replacement,value); System.out.println("Simple Levensthein Query: "+ querynew); new_queries.add(querynew); @@ -384,6 +526,9 @@ } + + //add original query for iteration + new_queries.add(query); //iterate over all Queries and get answer from Server for(String anfrage : new_queries){ String answer_tmp; @@ -479,6 +624,8 @@ String queryOne=query; String keyOne = entryOne.getKey(); + keyOne=keyOne.replace("\"",""); + keyOne=keyOne.replace("@en",""); String valueOne = entryOne.getValue(); @@ -488,33 +635,43 @@ * and use that new query, for the property of the second resource */ if(levnstheinDistanzeOne>=LevenstheinMin){ - String test = getUriFromIndex(property_to_compare_withOne.toLowerCase(),1); - queryOne=queryOne.replace(test,valueOne); - } + String replacementOne = getUriFromIndex(property_to_compare_withOne.toLowerCase(),1); + if(!queryOne.contains(replacementOne)){ + replacementOne=replacementOne.replace("ontology", "property"); + } + queryOne=queryOne.replace(replacementOne,valueOne); + - /* - * Iterate now over the second set of properties, but this time not using the original query in which - * to replace the old uri with the new one, but using queryOne from the first step. - */ - for (Entry<String, String> entryTwo : propertiesTwo.entrySet()) { - String keyTwo = entryTwo.getKey(); - String valueTwo = entryTwo.getValue(); - - //again calculate the nld with the property from the second condition and the property from the propertyset - double levnstheinDistanzeTwo=Levenshtein.nld(property_to_compare_withTwo.toLowerCase(), keyTwo); - - if(levnstheinDistanzeTwo>LevenstheinMin){ - String queryTwo=queryOne; - String replacement = getUriFromIndex(property_to_compare_withTwo.toLowerCase(),1); - queryTwo=queryTwo.replace(replacement,valueTwo); - System.out.println("Complex Levensthein Query: "+ queryTwo); - new_queries.add(queryTwo); - } - + /* + * Iterate now over the second set of properties, but this time not using the original query in which + * to replace the old uri with the new one, but using queryOne from the first step. + */ + for (Entry<String, String> entryTwo : propertiesTwo.entrySet()) { + String keyTwo = entryTwo.getKey(); + String valueTwo = entryTwo.getValue(); + keyTwo=keyTwo.replace("\"",""); + keyTwo=keyTwo.replace("@en",""); + + //again calculate the nld with the property from the second condition and the property from the propertyset + double levnstheinDistanzeTwo=Levenshtein.nld(property_to_compare_withTwo.toLowerCase(), keyTwo); + + if(levnstheinDistanzeTwo>LevenstheinMin){ + String queryTwo=queryOne; + String replacement = getUriFromIndex(property_to_compare_withTwo.toLowerCase(),1); + if(!queryTwo.contains(replacement)){ + replacement=replacement.replace("ontology", "property"); + } + queryTwo=queryTwo.replace(replacement,valueTwo); + System.out.println("Complex Levensthein Query: "+ queryTwo); + new_queries.add(queryTwo); + } + + } } } - + //add original query for iteration + new_queries.add(query); //iterate over all Queries and get answer from Server for(String anfrage : new_queries){ String answer_tmp; @@ -527,6 +684,150 @@ return final_answer; } + + +private ArrayList<String> simpleWordnetIterationArray(ArrayList<String> querylist, String query) throws SQLException, + JWNLException { + ArrayList<String> final_answer=new ArrayList<String>(); + + System.out.println("In simpleWordnetIteration"); + + 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); + + + 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("Begin:\n"+query +"\nError in getting Properties \n End"); + goOnAfterProperty=false; + } + + } catch (IOException e) { + + final_answer.add("Begin:\n"+query +"\nError in getting Properties \n End"); + goOnAfterProperty=false; + + } + if(goOnAfterProperty==true){ + + ArrayList<String> new_queries= new ArrayList<String>(); + + 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); + System.out.println("Semantics: "+ semantics); + + //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; + } + + System.out.println("tmp_semantics: "+ tmp_semantics); + Boolean goOnAfterWordnet = true; + + for(int i=0;i<=explorationdepthwordnet;i++){ + + try { + tmp_semantics=getSemantics(tmp_semantics); + System.out.println("tmp_semantics in Iteration: "+ 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) { + + 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(); + key=key.replace("\"",""); + key=key.replace("@en",""); + + for(String b : semantics){ + if(key.contains(b.toLowerCase())){ + if(!result_SemanticsMatchProperties.contains(key)){ + result_SemanticsMatchProperties.add(key); + String query_tmp=query; + String replacement = getUriFromIndex(property_to_compare_with.toLowerCase(),1); + if(!query_tmp.contains(replacement)){ + replacement=replacement.replace("ontology", "property"); + } + query_tmp=query_tmp.replace(replacement,value); + System.out.println("Simple Wordnet Query: "+ query_tmp); + new_queries.add(query_tmp); + } + } + } + } + + //add original query for iteration + new_queries.add(query); + //iterate over all Queries and get answer from Server + for(String bla : new_queries){ + ArrayList<String>answer_tmp=new ArrayList<String>(); + answer_tmp=sendServerQuestionRequestArray(bla); + for(String s: answer_tmp)final_answer.add(s); + //final_answer.add("Begin:\n"+bla +"\n"+answer_tmp+" \n End"); + } + } + } + + + return final_answer; +} private ArrayList<String> simpleWordnetIteration(ArrayList<String> querylist, String query) throws SQLException, JWNLException { @@ -637,29 +938,38 @@ } if(goOnAfterWordnet==true){ - + System.out.println("in actual wordnet function"); for (Entry<String, String> entry : properties.entrySet()) { String key = entry.getKey(); + key=key.replace("\"",""); + key=key.replace("@en",""); String value = entry.getValue(); + // System.out.println("Key propery: "+ key); + // System.out.println("Value propery: "+ value); for(String b : semantics){ if(key.contains(b.toLowerCase())){ - System.out.println("Hey, Iam in too!!!!!!!!!!!"); //to check, if no property is used twice... if(!result_SemanticsMatchProperties.contains(key)){ - //create new query - System.out.println("Hey, Iam in!!!!!!!!!!!"); result_SemanticsMatchProperties.add(key); String query_tmp=query; String test = getUriFromIndex(property_to_compare_with.toLowerCase(),1); + + //could happen, that there is an ontology and not a property or upsidedown in the query + if(!query_tmp.contains(test)){ + test=test.replace("ontology", "property"); + } query_tmp=query_tmp.replace(test,value); System.out.println("Simple Wordnet Query: "+ query_tmp); + System.out.println("\n"); new_queries.add(query_tmp); } } } } + //add original query for iteration + new_queries.add(query); //iterate over all Queries and get answer from Server for(String bla : new_queries){ String answer_tmp; @@ -855,6 +1165,8 @@ String keyOne = entryOne.getKey(); String valueOne = entryOne.getValue(); String queryOne=query; + keyOne=keyOne.replace("\"",""); + keyOne=keyOne.replace("@en",""); for(String b : semanticsOne){ if(keyOne.contains(b.toLowerCase())){ @@ -862,11 +1174,16 @@ //create new query result_SemanticsMatchPropertiesOne.add(keyOne); String replacementOne = getUriFromIndex(property_to_compare_withOne.toLowerCase(),1); + if(!queryOne.contains(replacementOne)){ + replacementOne=replacementOne.replace("ontology", "property"); + } queryOne=queryOne.replace(replacementOne,valueOne); for (Entry<String, String> entryTwo : propertiesTwo.entrySet()) { String keyTwo = entryTwo.getKey(); String valueTwo = entryTwo.getValue(); + keyTwo=keyTwo.replace("\"",""); + keyTwo=keyTwo.replace("@en",""); for(String z : semanticsTwo){ if(keyTwo.contains(z.toLowerCase())){ @@ -875,6 +1192,9 @@ result_SemanticsMatchPropertiesTwo.add(keyTwo); String queryTwo=queryOne; String replacementTwo = getUriFromIndex(property_to_compare_withTwo.toLowerCase(),1); + if(!queryTwo.contains(replacementTwo)){ + replacementTwo=replacementTwo.replace("ontology", "property"); + } queryTwo=queryTwo.replace(replacementTwo,valueTwo); System.out.println("Complexe Wordnet Query: "+ queryTwo); new_queries.add(queryTwo); @@ -892,7 +1212,8 @@ } - + //add original query for iteration + new_queries.add(query); //iterate over all Queries and get answer from Server for(String bla : new_queries){ String answer_tmp; @@ -1042,75 +1363,107 @@ //see below slotcounter=slotcounter+1; + //resource will be detectet. + //If its not a resource, it has to be a property! + String resource=""; + String property=""; + String slotstring=""; + if(slot.toString().contains("RESOURCE")){ + resource=slot.toString().replace("{","").replace("}","").replace(" RESOURCE ", ""); + System.out.println("Found Resource in getQuery: "+ resource); + } + else{ + property=slot.toString().replace("UNSPEC","").replace("RESOURCE","").replace("{","").replace("}","").replace(" PROPERTY ",""); + System.out.println("Found Property in getQuery: "+ property); + } - 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 ) - { - - } + //query=query.replace(replace, "<"+hm_result+">"); - /*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+">"); + if(resource!=""){ + String replace=""; + String[] array = resource.split(":"); + if(array[0].length()<2)replace = "?"+array[0]+" "; + else replace="?"+array[0]; + + String hm_result=getUriFromIndex(array[1],0); + //System.out.print("URI for_ThingGettingURIfor: "+hm_result); + try + { + if(hm_result.contains("Category:")) hm_result=hm_result.replace("Category:",""); + } + catch ( Exception e ) + { + + } + + query=query.replace(replace, "<"+hm_result+">"); + //System.out.println("Query: "+query); + query_upside_down=query_upside_down.replace(replace, "<"+hm_result+">"); + //System.out.println("Query Up Side Down: "+query_upside_down); + + } + if(property!=""){ + String replace=""; + String[] array = property.split(":"); + if(array[0].length()<2)replace = "?"+array[0]+" "; + else replace="?"+array[0]; + + String hm_result=getUriFromIndex(array[1],1); + + query=query.replace(replace, "<"+hm_result+">"); + //System.out.println("Query: "+query); + query_upside_down=query_upside_down.replace(replace, "<"+hm_result+">"); + //System.out.println("Query Up Side Down: "+query_upside_down); + + } + } + query_upside_down=query_upside_down.replace("><","> <").replace(">?", "> ?"); + query=query.replace("><","> <").replace(">?", "> ?"); lstquerupsidedown.add(query_upside_down); lstquerynew.add(query); + System.out.println("Query: "+query); + System.out.println("Query Up Side Down: "+query_upside_down); 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); + if(lsttmp.size()>=3&&lsttmp.size()%3==0){ + for(String i : lsttmp) lstquerynew.add(i.replace("__","")); + lstquery.add(lstquerynew); + } else{ lstquerynew.clear(); lstquerynew.add("ERROR"); + System.out.println("ERROR1"); addQuery=false; } lsttmp.clear(); lsttmp=createLeftAndRightPropertyArray(query_upside_down); - if(lsttmp.size()>=3&&lsttmp.size()%3==0)for(String i : lsttmp) lstquerupsidedown.add(i); + if(lsttmp.size()>=3&&lsttmp.size()%3==0){ + for(String i : lsttmp) lstquerupsidedown.add(i.replace("__","")); + lstquery.add(lstquerupsidedown); + } else{ lstquerupsidedown.clear(); lstquerupsidedown.add("ERROR"); + System.out.println("ERROR2"); addQuery=false; } - if(addQuery==true){ - lstquery.add(lstquerynew); - lstquery.add(lstquerupsidedown); - } + + + System.out.println("Add Query: "+addQuery); } } - + System.out.println("List of Query: "+lstquery); return lstquery; } @@ -1159,6 +1512,10 @@ * @throws SQLException */ private String getUriFromIndex(String string, int fall) throws SQLException{ + String originalString=string; + string=string.replace("_", " "); + string=string.replace("-", " "); + string=string.replace(".", " "); String result=null; String tmp1=null; String tmp2 = null; @@ -1169,15 +1526,15 @@ //first try: take always the ontology if existing and not the Resource tmp1=myindex.getResourceURI(string.toLowerCase()); tmp2=myindex.getontologyClassURI(string.toLowerCase()); - System.out.println("URI from resource: "+tmp1); - System.out.println("URI from ontologyClass: "+tmp2); + /*System.out.println("URI from resource: "+tmp1); + System.out.println("URI from ontologyClass: "+tmp2);*/ - System.out.println("value from http://dbpedia.org/resource/WikiLeaks : "+ myindex.getResourceURI("http://dbpedia.org/resource/WikiLeaks")); - System.out.println("value from author : "+ myindex.getResourceURI("author")); + if(tmp1!=null && tmp2!=null) result=tmp2; if(tmp1!=null && tmp2==null) result=tmp1; if(tmp1==null && tmp2!=null) result=tmp2; + //result=myindex.getResourceURI(string.toLowerCase()); if(result==null)result=myindex.getPropertyURI(string.toLowerCase()); } @@ -1194,12 +1551,25 @@ } String tmp=""; - tmp=string.toLowerCase(); + tmp=originalString.toLowerCase(); tmp=tmp.replace("property",""); tmp=tmp.replace(" ", "_"); if(result==null) { if(fall==1)return "http://dbpedia.org/property/"+tmp; - if(fall==0)return "http://dbpedia.org/resource/"+tmp; + if(fall==0) { + String bla ="http://dbpedia.org/resource/"+tmp; + if(tmp.contains("_")){ + String[] newarraytmp=tmp.split("_"); + String tmpneu=""; + for(String s :newarraytmp){ + tmpneu+= "_"+ Character.toUpperCase(s.charAt(0)) + s.substring(1); + } + tmpneu=tmpneu.replaceFirst("_", ""); + bla ="http://dbpedia.org/resource/"+tmpneu; + System.out.println("Hotfix: "+bla); + } + return bla; + } else{ System.out.println("return result: "+result); return result; @@ -1243,49 +1613,54 @@ } }*/ - for(String id :semantics){ - //System.out.println("in String id : semantics"); - //System.out.println("ID :"+id); - - //add id also to the result, if its not already in there - if(!result.contains(id))result.add(id); - List<String> array_relatedNouns=null; - List<String> array_bestsynonyms=null; - - System.out.println("Wordnet Word: "+id); - try{ - array_relatedNouns =wordnet.getRelatedNouns(id); - } - catch(Exception e){ - //array_relatedNouns.clear(); - } - System.out.println("array_relatedNouns: "+ array_relatedNouns); - //System.out.println("after relatedNouns"); - - try{ - array_bestsynonyms=wordnet.getBestSynonyms(POS.NOUN, id); - System.out.println("array_bestsynonyms: "+ array_bestsynonyms); - } - catch(Exception e){ - // - } - + try{ + for(String id :semantics){ + //System.out.println("in String id : semantics"); + //System.out.println("ID :"+id); + //add id also to the result, if its not already in there + if(!result.contains(id))result.add(id); + List<String> array_relatedNouns=null; + List<String> array_bestsynonyms=null; - if(array_relatedNouns!=null){ - for(String i:array_relatedNouns){ - if(!result.contains(i))result.add(i); + System.out.println("Wordnet Word: "+id); + try{ + array_relatedNouns =wordnet.getRelatedNouns(id); } - } - if(array_bestsynonyms!=null){ - for(String i:array_bestsynonyms){ - if(!result.contains(i))result.add(i); + catch(Exception e){ + //array_relatedNouns.clear(); } + System.out.println("array_relatedNouns: "+ array_relatedNouns); + //System.out.println("after relatedNouns"); + + try{ + array_bestsynonyms=wordnet.getBestSynonyms(POS.NOUN, id); + System.out.println("array_bestsynonyms: "+ array_bestsynonyms); + } + catch(Exception e){ + // + } + + + + if(array_relatedNouns!=null){ + for(String i:array_relatedNouns){ + if(!result.contains(i))result.add(i); + } + } + if(array_bestsynonyms!=null){ + for(String i:array_bestsynonyms){ + if(!result.contains(i))result.add(i); + } + } + + + } - - - } + catch(Exception e){ + return null; + } if(!result.isEmpty()) return result; else{ @@ -1356,6 +1731,8 @@ private String sendServerQuestionRequest(String query){ //SPARQL-Endpoint of Semantic Computing Group + + //5171 String tmp="http://greententacle.techfak.uni-bielefeld.de:5171/sparql?default-graph-uri=&query="+createServerRequest(query)+"&format=text%2Fhtml&debug=on&timeout="; System.out.println(tmp); URL url; @@ -1389,6 +1766,40 @@ return createAnswer(result); } + private ArrayList<String> sendServerQuestionRequestArray(String query){ + //SPARQL-Endpoint of Semantic Computing Group + String tmp="http://greententacle.techfak.uni-bielefeld.de:5171/sparql?default-graph-uri=&query="+createServerRequest(query)+"&format=text%2Fhtml&debug=on&timeout="; + System.out.println(tmp); + URL url; + InputStream is; + InputStreamReader isr; + BufferedReader r; + String str=""; + String result=""; + + try { + url = new URL(tmp); + is = url.openStream(); + isr = new InputStreamReader(is); + r = new BufferedReader(isr); + int counter=0; + do { + str = r.readLine(); + if (str != null){ + result=result.concat(str); + counter=counter+1;} + } while (str != null); + + } catch (MalformedURLException e) { + System.out.println("Must enter a valid URL"); + } catch (IOException e) { + System.out.println("Can not connect"); + } + + + + return createAnswerArray(result); + } private String createAnswer(String string){ //<td>Klaus Wowereit</td> @@ -1409,6 +1820,25 @@ return result; } + private ArrayList<String> createAnswerArray(String string){ + //<td>Klaus Wowereit</td> + + //get with regex all between <td> </td> + + Pattern p = Pattern.compile (".*<td>(.*)</td>.*"); + Matcher m = p.matcher (string); + ArrayList<String> result=new ArrayList<String>(); + + while (m.find()) { + if(m.group(1)!=null) + result.add(m.group(1)); + } + + //if (result.length()==0) result="EmtyAnswer"; + + return result; + + } private String createServerRequest(String query){ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <seb...@us...> - 2011-12-18 18:47:15
|
Revision: 3505 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3505&view=rev Author: sebastianwtr Date: 2011-12-18 18:47:08 +0000 (Sun, 18 Dec 2011) Log Message: ----------- [tbsl exploration] collecting all queries 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/Levenshtein.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-15 13:26:09 UTC (rev 3504) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/GetRessourcePropertys.java 2011-12-18 18:47:08 UTC (rev 3505) @@ -32,8 +32,8 @@ */ private HashMap<String,String> sendServerPropertyRequest(String vergleich, String side) throws IOException{ - System.out.println("Resource die gesucht wird: "+ vergleich); - System.out.println("Seite die gesucht wird: "+side); + //System.out.println("Resource die gesucht wird: "+ vergleich); + //System.out.println("Seite die gesucht wird: "+side); /* * * For the second Iteration, I can just add the sparql property here. Modified: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/Levenshtein.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/Levenshtein.java 2011-12-15 13:26:09 UTC (rev 3504) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/Levenshtein.java 2011-12-18 18:47:08 UTC (rev 3505) @@ -13,8 +13,9 @@ double length=Math.max(orig.length(),eing.length()); //if distance between both is zero, then the NLD must be one + //but because they are equal, return a very high value, so that that query will be taken. if(result==0.0 ){ - return 1; + return 10.0; } else{ 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-15 13:26:09 UTC (rev 3504) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/SparqlObject.java 2011-12-18 18:47:08 UTC (rev 3505) @@ -63,7 +63,7 @@ // btemplator = new BasicTemplator(); btemplator.UNTAGGED_INPUT = false; - templator = new Templator(); + //templator = new Templator(); System.out.println("Loading SPARQL Templator Done\n"); System.out.println("Start Indexing"); myindex = new mySQLDictionary(); @@ -73,7 +73,7 @@ //normaly 1 setExplorationdepthwordnet(1); //eigentlich immer mit 0 initialisieren - setIterationdepth(9); + setIterationdepth(1); setNumberofanswers(1); } @@ -127,8 +127,10 @@ long startParsingTime = System.currentTimeMillis(); lstquery=getQuery(question); long endParsingTime = System.currentTimeMillis(); + long startIterationTime = System.currentTimeMillis(); System.out.println("The Questionparsing took "+ (endParsingTime-startParsingTime)+ " ms"); ArrayList<String> final_answer = new ArrayList<String>(); + ArrayList<String> final_query = new ArrayList<String>(); if(lstquery.isEmpty()){ saveNotParsedQuestions(question); @@ -226,24 +228,56 @@ * Only Levensthein!!! */ if(getIterationdepth()==1&&startIterating==true||getIterationdepth()==9&&startIterating==true){ - /* - //4, because of query + three conditions for the simple case - if(querylist.size()==4)final_answer=simpleIteration1Case(querylist, query); - //if we have more conditions, we need to change the way of replacing the uris got from wordnet etc - if(querylist.size()>4)final_answer=complexeIteration1Case(querylist, query); - */ - ArrayList<String> final_answer_tmp = new ArrayList<String>(); - if(querylist.size()==4)final_answer_tmp=simpleLevinstheinIteration(querylist, query); - if(querylist.size()>4)final_answer_tmp=complexeLevinstheinIteration(querylist, query); + ArrayList<String> final_query_tmp=new ArrayList<String>(); + if(querylist.size()==4){ + //System.out.println("YEAH!!!!!"); + //final_answer_tmp=simpleLevinstheinIteration(querylist, query); + final_query_tmp=simpleLevinstheinIteration(querylist, query); + for(String i: final_query_tmp){ + + //do it unnice for first + boolean double_query=false; + for(String s: final_query ){ + + if(s.contains(i)){ + double_query=true; + + } + } + if(double_query==false){ + + final_query.add(i); + } + } + } - for(String i : final_answer_tmp){ - final_answer.add(i); + + if(querylist.size()>4){ + final_query_tmp=complexeLevinstheinIteration(querylist, query); + for(String i: final_query_tmp){ + + //do it unnice for first + boolean double_query=false; + for(String s: final_query ){ + + if(s.contains(i)){ + double_query=true; + + } + } + if(double_query==false){ + + final_query.add(i); + } + } } + + } /* * ################################################################################################# @@ -254,21 +288,67 @@ */ if(getIterationdepth()==2&&startIterating==true||getIterationdepth()==9&&startIterating==true){ ArrayList<String> final_answer_tmp = new ArrayList<String>(); + ArrayList<String> final_query_tmp = new ArrayList<String>(); - if(querylist.size()==4)final_answer_tmp=simpleWordnetIteration(querylist, query); + if(querylist.size()==4){ + + //final_answer_tmp=simpleLevinstheinIteration(querylist, query); + final_query_tmp = simpleWordnetIteration(querylist, query); + for(String i: final_query_tmp){ + + //do it unnice for first + boolean double_query=false; + for(String s: final_query ){ + + if(s.contains(i)){ + double_query=true; + + } + } + if(double_query==false){ + + final_query.add(i); + } + } + } + final_answer_tmp=simpleWordnetIteration(querylist, query); //if(querylist.size()>4)final_answer=complexWordnetIteration(querylist, query); //for a test only use: - if(querylist.size()>4)final_answer_tmp=newIteration(querylist,query); - - for(String i : final_answer_tmp){ - final_answer.add(i); + if(querylist.size()>4){ + final_query_tmp=newIteration(querylist,query); + for(String i: final_query_tmp){ + boolean double_query=false; + for(String s: final_query ){ + if(s.contains(i)){ + double_query=true; + } + } + if(double_query==false){ + final_query.add(i); + } + } } } } + + /* + * Send Query to Server and get answers + */ + + for(String anfrage : final_query){ + String answer_tmp; + answer_tmp=sendServerQuestionRequest(anfrage); + //System.out.println("Antwort vom Server: "+answer_tmp); + if(!final_answer.contains(anfrage)) + final_answer.add("Begin:\n"+anfrage +"\n"+answer_tmp+" \n End"); + //final_answer.add("Begin:\n"+anfrage +"\n"+answer_tmp+" \n End"); + } + + BufferedReader in = null; String tmp=""; @@ -317,7 +397,7 @@ } System.out.println(question); - out = out.replace("@en","").replace("\"",""); + out = out.replace("@en","").replace("\"","").replace("^^<http://www.w3.org/2001/XMLSchema#int> ", ""); System.out.println(out); BufferedWriter outfile = new BufferedWriter( @@ -326,6 +406,10 @@ outfile.write(tmp+"\n"+question+" :\n"+out); outfile.close(); + long stopIterationTime = System.currentTimeMillis(); + System.out.println("The Questionparsing took "+ (endParsingTime-startParsingTime)+ " ms"); + System.out.println("The Iteration took "+ (stopIterationTime-startIterationTime)+ " ms"); + System.out.println("All took "+ (stopIterationTime-startParsingTime)+ " ms"); } private ArrayList<String> newIteration(ArrayList<String> querylist, String query) throws SQLException, @@ -450,6 +534,7 @@ String resource=""; String property_to_compare_with=""; String sideOfProperty="LEFT"; + ArrayList<String> new_queries= new ArrayList<String>(); int tmpcounter=0; @@ -487,7 +572,7 @@ properties=property.getPropertys(getUriFromIndex(resource.toLowerCase(),0),sideOfProperty); if (properties==null){ - final_answer.add("Begin:\n"+query +"\nError in getting Properties \n End"); + System.out.println("Begin:\n"+query +"\nError in getting Properties \n End"); goOnAfterProperty=false; } @@ -495,18 +580,20 @@ } catch (IOException e) { - final_answer.add("Begin:\n"+query +"\nError in getting Properties \n End"); + System.out.println("Begin:\n"+query +"\nError in getting Properties \n End"); goOnAfterProperty=false; } if(goOnAfterProperty==true){ - ArrayList<String> new_queries= new ArrayList<String>(); + String bestQuery=""; + double highestNLD=0; //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(); + // System.out.println("Key "+ key +" and value "+value); //compare property gotten from the resource with the property from the original query double nld=Levenshtein.nld(property_to_compare_with.toLowerCase(), key); @@ -520,32 +607,46 @@ replacement=replacement.replace("ontology", "property"); } querynew=querynew.replace(replacement,value); - System.out.println("Simple Levensthein Query: "+ querynew); + if(nld>highestNLD){ + bestQuery=querynew; + highestNLD=nld; + } + /* System.out.println("Simple Levensthein Query: "+ querynew); + new_queries.add(querynew);*/ + //only add, if nld is greater than the already existing nld. + new_queries.add(querynew); } + } - + - + /*new_queries.add(bestQuery); + System.out.println("Best Query "+bestQuery);*/ //add original query for iteration + new_queries.add(query); + //iterate over all Queries and get answer from Server - for(String anfrage : new_queries){ + /* for(String anfrage : new_queries){ String answer_tmp; answer_tmp=sendServerQuestionRequest(anfrage); System.out.println("Antwort vom Server: "+answer_tmp); final_answer.add("Begin:\n"+anfrage +"\n"+answer_tmp+" \n End"); - } + }*/ } - - return final_answer; + + //test to returnqueries, put them together and than send them to the server. + return new_queries; + //return final_answer; } private ArrayList<String> complexeLevinstheinIteration(ArrayList<String> querylist, String query) throws SQLException, JWNLException { ArrayList<String> final_answer=new ArrayList<String>(); + ArrayList<String> new_queries= new ArrayList<String>(); String resourceOne=""; String property_to_compare_withOne=""; String resourceTwo=""; @@ -603,13 +704,13 @@ propertiesTwo=property.getPropertys(getUriFromIndex(resourceTwo.toLowerCase(),0),sideOfPropertyTwo); if (propertiesOne==null){ - final_answer.add("Begin:\n"+query +"\nError in getting Properties \n End"); + System.out.println("Begin:\n"+query +"\nError in getting Properties \n End"); goOnAfterProperty=false; } } catch (IOException e) { - final_answer.add("Begin:\n"+query +"\nError in getting Properties \n End"); + System.out.println("Begin:\n"+query +"\nError in getting Properties \n End"); goOnAfterProperty=false; } @@ -617,8 +718,8 @@ if(goOnAfterProperty==true){ - ArrayList<String> new_queries= new ArrayList<String>(); + //Iterate over property from resource one for (Entry<String, String> entryOne : propertiesOne.entrySet()) { @@ -672,17 +773,9 @@ //add original query for iteration new_queries.add(query); - //iterate over all Queries and get answer from Server - for(String anfrage : new_queries){ - String answer_tmp; - answer_tmp=sendServerQuestionRequest(anfrage); - System.out.println("Antwort vom Server: "+answer_tmp); - final_answer.add("Begin:\n"+anfrage +"\n"+answer_tmp+" \n End"); - } - } - return final_answer; + return new_queries; } @@ -769,7 +862,7 @@ System.out.println("tmp_semantics in Iteration: "+ tmp_semantics); if (tmp_semantics==null){ goOnAfterWordnet=false; - final_answer.add("Begin:\n"+query +"\n Error in searching Wordnet with word "+semantics+" \n End"); + System.out.println("Begin:\n"+query +"\n Error in searching Wordnet with word "+semantics+" \n End"); } else{ @@ -782,7 +875,7 @@ } catch (IOException e) { goOnAfterWordnet=false; - final_answer.add("Begin:\n"+query +"\n Error in searching Wordnet with word "+semantics+" \n End"); + System.out.println("Begin:\n"+query +"\n Error in searching Wordnet with word "+semantics+" \n End"); } @@ -832,6 +925,7 @@ private ArrayList<String> simpleWordnetIteration(ArrayList<String> querylist, String query) throws SQLException, JWNLException { ArrayList<String> final_answer=new ArrayList<String>(); + ArrayList<String> new_queries= new ArrayList<String>(); System.out.println("In simpleWordnetIteration"); @@ -882,7 +976,7 @@ } if(goOnAfterProperty==true){ - ArrayList<String> new_queries= new ArrayList<String>(); + System.out.println("Start Iterating Wordnet with "+property_to_compare_with+" and deept of "+explorationdepthwordnet); ArrayList<String> semantics=new ArrayList<String>(); @@ -901,7 +995,11 @@ semantics.add(_temp_); tmp_semantics=semantics; } - + if(property_to_compare_with.contains("_")){ + String[] array=property_to_compare_with.split("_"); + for(String i : array) tmp_semantics.add(i); + tmp_semantics.add(property_to_compare_with.replace("_"," ")); + } System.out.println("tmp_semantics: "+ tmp_semantics); Boolean goOnAfterWordnet = true; @@ -918,7 +1016,7 @@ System.out.println("tmp_semantics in Iteration: "+ tmp_semantics); if (tmp_semantics==null){ goOnAfterWordnet=false; - final_answer.add("Begin:\n"+query +"\n Error in searching Wordnet with word "+semantics+" \n End"); + System.out.println("Begin:\n"+query +"\n Error in searching Wordnet with word "+semantics+" \n End"); } else{ @@ -931,7 +1029,7 @@ } catch (IOException e) { goOnAfterWordnet=false; - final_answer.add("Begin:\n"+query +"\n Error in searching Wordnet with word "+semantics+" \n End"); + System.out.println("Begin:\n"+query +"\n Error in searching Wordnet with word "+semantics+" \n End"); } @@ -943,8 +1041,9 @@ String key = entry.getKey(); key=key.replace("\"",""); key=key.replace("@en",""); + String value = entry.getValue(); - // System.out.println("Key propery: "+ key); + System.out.println("Key "+ key +" and value "+value); // System.out.println("Value propery: "+ value); for(String b : semantics){ @@ -960,6 +1059,8 @@ test=test.replace("ontology", "property"); } query_tmp=query_tmp.replace(test,value); + System.out.println("\n"); + System.out.println("Original Query: "+ query); System.out.println("Simple Wordnet Query: "+ query_tmp); System.out.println("\n"); new_queries.add(query_tmp); @@ -969,25 +1070,20 @@ } //add original query for iteration - new_queries.add(query); - //iterate over all Queries and get answer from Server - 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"); - } + new_queries.add(query); + } } - return final_answer; + return new_queries; } private ArrayList<String> complexWordnetIteration(ArrayList<String> querylist, String query) throws SQLException, JWNLException { ArrayList<String> final_answer=new ArrayList<String>(); + ArrayList<String> new_queries= new ArrayList<String>(); String resourceOne=""; String property_to_compare_withOne=""; @@ -1062,7 +1158,6 @@ * #################################### Semantics One############################################# */ - ArrayList<String> new_queries= new ArrayList<String>(); //System.out.println("Start Iterating Wordnet with "+property_to_compare_withOne+" and deept of "+explorationdepthwordnet); ArrayList<String> semanticsOne=new ArrayList<String>(); @@ -1089,7 +1184,7 @@ tmp_semanticsOne=getSemantics(tmp_semanticsOne); if (tmp_semanticsOne==null){ goOnAfterWordnet=false; - final_answer.add("Begin:\n"+query +"\n Error in searching Wordnet with word "+semanticsOne+" \n End"); + System.out.println("Begin:\n"+query +"\n Error in searching Wordnet with word "+semanticsOne+" \n End"); } else{ @@ -1102,7 +1197,7 @@ } catch (IOException e) { goOnAfterWordnet=false; - final_answer.add("Begin:\n"+query +"\n Error in searching Wordnet with word "+semanticsOne+" \n End"); + System.out.println("Begin:\n"+query +"\n Error in searching Wordnet with word "+semanticsOne+" \n End"); } @@ -1136,7 +1231,7 @@ tmp_semanticsTwo=getSemantics(tmp_semanticsTwo); if (tmp_semanticsTwo==null){ goOnAfterWordnet=false; - final_answer.add("Begin:\n"+query +"\n Error in searching Wordnet with word "+semanticsTwo+" \n End"); + System.out.println("Begin:\n"+query +"\n Error in searching Wordnet with word "+semanticsTwo+" \n End"); } else{ @@ -1149,7 +1244,7 @@ } catch (IOException e) { goOnAfterWordnet=false; - final_answer.add("Begin:\n"+query +"\n Error in searching Wordnet with word "+semanticsTwo+" \n End"); + System.out.println("Begin:\n"+query +"\n Error in searching Wordnet with word "+semanticsTwo+" \n End"); } @@ -1214,17 +1309,11 @@ //add original query for iteration new_queries.add(query); - //iterate over all Queries and get answer from Server - 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; + return new_queries; } @@ -1623,19 +1712,19 @@ List<String> array_relatedNouns=null; List<String> array_bestsynonyms=null; - System.out.println("Wordnet Word: "+id); + //System.out.println("Wordnet Word: "+id); try{ array_relatedNouns =wordnet.getRelatedNouns(id); } catch(Exception e){ //array_relatedNouns.clear(); } - System.out.println("array_relatedNouns: "+ array_relatedNouns); + //System.out.println("array_relatedNouns: "+ array_relatedNouns); //System.out.println("after relatedNouns"); try{ array_bestsynonyms=wordnet.getBestSynonyms(POS.NOUN, id); - System.out.println("array_bestsynonyms: "+ array_bestsynonyms); + //System.out.println("array_bestsynonyms: "+ array_bestsynonyms); } catch(Exception e){ // @@ -1687,12 +1776,12 @@ catch(Exception e){ //array_relatedNouns.clear(); } - System.out.println("array_relatedNouns: "+ array_relatedNouns); + //System.out.println("array_relatedNouns: "+ array_relatedNouns); //System.out.println("after relatedNouns"); try{ array_bestsynonyms=wordnet.getBestSynonyms(POS.NOUN, advanced_id); - System.out.println("array_bestsynonyms: "+ array_bestsynonyms); + // System.out.println("array_bestsynonyms: "+ array_bestsynonyms); } catch(Exception e){ // @@ -1729,6 +1818,10 @@ + /* + * http://purpurtentacle.techfak.uni-bielefeld.de:8893/sparql new endpoint + */ + private String sendServerQuestionRequest(String query){ //SPARQL-Endpoint of Semantic Computing Group This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <seb...@us...> - 2012-01-02 09:38:03
|
Revision: 3519 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3519&view=rev Author: sebastianwtr Date: 2012-01-02 09:37:56 +0000 (Mon, 02 Jan 2012) Log Message: ----------- [tbsl exploration] started implementing the isA-Case for advanced Questions 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-30 10:26:03 UTC (rev 3518) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/GetRessourcePropertys.java 2012-01-02 09:37:56 UTC (rev 3519) @@ -51,10 +51,14 @@ String vergleichorig = vergleich; - - 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="; + /* + * change to dbpedia http://dbpedia.org/sparql + */ + //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="; + String tmp_left="http://dbpedia.org/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="; + String tmp_right="http://dbpedia.org/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="; String verarbeitungsurl=null; if(side.contains("RIGHT")) verarbeitungsurl=tmp_right; 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-30 10:26:03 UTC (rev 3518) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/SparqlObject.java 2012-01-02 09:37:56 UTC (rev 3519) @@ -58,11 +58,12 @@ //Konstruktor public SparqlObject() throws MalformedURLException, ClassNotFoundException, SQLException{ + wordnet = new WordNet(); System.out.println("Loading SPARQL Templator"); // btemplator = new BasicTemplator(); - btemplator.UNTAGGED_INPUT = false; + //btemplator.UNTAGGED_INPUT = false; //templator = new Templator(); System.out.println("Loading SPARQL Templator Done\n"); System.out.println("Start Indexing"); @@ -73,7 +74,7 @@ //normaly 1 setExplorationdepthwordnet(1); //eigentlich immer mit 0 initialisieren - setIterationdepth(1); + setIterationdepth(0); setNumberofanswers(1); } @@ -123,7 +124,7 @@ public void create_Sparql_query(String question) throws JWNLException, IOException, SQLException{ //create_Sparql_query_new(string); - ArrayList<ArrayList<String>> lstquery = new ArrayList<ArrayList<String>>(); + ArrayList<ArrayList<String>> lstquery = new ArrayList<ArrayList<String>>(); long startParsingTime = System.currentTimeMillis(); lstquery=getQuery(question); long endParsingTime = System.currentTimeMillis(); @@ -158,7 +159,7 @@ new InputStreamReader( new FileInputStream( "/tmp/testresult.txt" ) ) ); while( null != (s = in.readLine()) ) { - tmp+="\n"+s; + tmp=tmp.concat("\n".concat(s)); } } catch( FileNotFoundException ex ) { } catch( Exception ex ) { @@ -174,7 +175,7 @@ } String out=null; - if (query=="" || query==" "||query.length()==0) query="Could not parse"; + if (query.equals("") || query.equals(" ")||query.length()==0) query="Could not parse"; out=tmp + "\n" + question + ":\n"+query+"\n"; BufferedWriter outfile = new BufferedWriter( @@ -527,9 +528,27 @@ } + + /* + * if there are more than one condition and if there is an isA Case in it, use this function. + * TODO: Change other simple Case function, that they have a Resource to deal with, or change the get Query function + */ + private ArrayList<String> isAIteration(ArrayList<String> querylist, String query) throws SQLException, + JWNLException { + //only for special case, that the first condition has a resource + ArrayList<String> final_answer=new ArrayList<String>(); + + return final_answer; + + } + + + + private ArrayList<String> simpleLevinstheinIteration(ArrayList<String> querylist, String query) throws SQLException, JWNLException { - + + System.out.println("In Simpe levensthein case!!"); ArrayList<String> final_answer=new ArrayList<String>(); String resource=""; String property_to_compare_with=""; @@ -1043,7 +1062,7 @@ key=key.replace("@en",""); String value = entry.getValue(); - System.out.println("Key "+ key +" and value "+value); + //System.out.println("Key "+ key +" and value "+value); // System.out.println("Value propery: "+ value); for(String b : semantics){ @@ -1060,6 +1079,7 @@ } query_tmp=query_tmp.replace(test,value); System.out.println("\n"); + System.out.println("Match with "+ b); System.out.println("Original Query: "+ query); System.out.println("Simple Wordnet Query: "+ query_tmp); System.out.println("\n"); @@ -1375,6 +1395,9 @@ return lstquery; } + + //TODO: Plural Singual abfragen über die Wordnetdatei... + /** * Method gets a String and takes the information from the templator to creat a Sparql query. * @param question question in natural language @@ -1419,8 +1442,35 @@ filters=""; addQuery=false; } + + //if there is no order by, replace with "" + String orderdBy="ORDER BY "; + try{ + for(SPARQL_Term tmp : temp.getOrderBy()) { + System.out.println("Yeah"); + orderdBy=orderdBy+tmp+" "; + } + if((temp.getOrderBy()).size()==0)orderdBy=""; + } + catch(Exception e){ + orderdBy=""; + addQuery=false; + } + + //if limit == 0, then set limit as "" + String limit=""; + try{ + limit="LIMIT "+temp.getLimit(); + + if(temp.getLimit()==0)limit=""; + } + catch(Exception e){ + limit=""; + 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+"}"; + query="PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "+temp.getQt().toString()+" "+selTerms+" WHERE {"+ conditions.replace("--","") + filters+"}"+orderdBy +" "+limit; String conditions_new = ""; for(Path condition: temp.getConditions()){ @@ -1444,11 +1494,63 @@ 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 query_upside_down = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "+temp.getQt().toString()+" "+selTerms+" WHERE {"+ conditions_new.replace("--","") +filters+ "}" + orderdBy +" "+limit; String[] slots= null; + + + /* + * replace isA with rdf:type + */ + query_upside_down=query_upside_down.replace("isA", "rdf:type"); + query=query.replace("isA", "rdf:type"); + int slotcounter=1; + + /* + * the one after the isA, has to be an ontology Class!!! + * so first find out, which one is behind the isA + * best with regex or so.... take the condition, regex the thing between isA and . for End of Condition + * kind of regex=[*a IsA (\\?*a.)*a] + * Then put the isA thing in the variable isaComponent and mark it as later on as Resource! + */ + String isaComponent=""; + + Pattern p = Pattern.compile (".*isA (\\?.*)\\..*"); + + /* + * use conditions, because only in this case, there is the right resource right of the isA + */ + Matcher m = p.matcher (conditions.replace("--", "").replace(" ", " ")); + String result=""; + + System.out.println("################"); + while (m.find()) { + if(m.group(1)!=null) + //System.out.println(m.group(1)); + isaComponent=m.group(1); + } + System.out.println("isaComponent "+isaComponent); + + /* + * just in case, there is still a . in it... + * funzt + * + */ + if(isaComponent.contains(".")){ + String[] tmp_array=isaComponent.split("\\."); + for(String i: tmp_array) System.out.println("tmp_array "+i); + isaComponent=tmp_array[0]; + + System.out.println("new isaComponent "+isaComponent); + } + + if(isaComponent=="") isaComponent="No isa Component"; + System.out.println("isaComponent "+isaComponent); + System.out.println("################"); + for(Slot slot : temp.getSlots()){ + System.out.println("Slot: "+slot); //see below slotcounter=slotcounter+1; @@ -1469,8 +1571,56 @@ //query=query.replace(replace, "<"+hm_result+">"); - if(resource!=""){ + /*System.out.println("Recource "+resource); + System.out.println("Property "+property);*/ + + boolean skip=false; + if(resource.contains(isaComponent.replace("?", "")) || property.contains(isaComponent.replace("?", ""))){ + skip=true; + + /* + * now replace the variable with the value of OntologyClass + */ String replace=""; + String tmp=""; + + if(resource.contains(isaComponent.replace("?", ""))) tmp=resource; + if(property.contains(isaComponent.replace("?", ""))) tmp=property; + + String[] array = tmp.split(":"); + if(array[0].length()<2)replace = "?"+array[0]+" "; + else replace="?"+array[0]; + try{ + array[1]=array[1].replace(" ", ""); + } + catch(Exception e){ + + } + String hm_result=myindex.getontologyClassURI(array[1]); + if(hm_result==null)hm_result="http://dbpedia.org/ontology/"+Character.toUpperCase(array[1].charAt(0)) + array[1].substring(1, array[1].length()); + System.out.println(array[1]+" for getOntologyClass "+hm_result); + //System.out.print("URI for_ThingGettingURIfor: "+hm_result); + try + { + if(hm_result.contains("Category:")) hm_result=hm_result.replace("Category:",""); + } + catch ( Exception e ) + { + + } + + query=query.replace(replace, "<"+hm_result+">"); + //System.out.println("Query: "+query); + query_upside_down=query_upside_down.replace(replace, "<"+hm_result+">"); + //System.out.println("Query Up Side Down: "+query_upside_down); + + } + + + + + if(resource!=""&&skip==false){ + String replace=""; String[] array = resource.split(":"); if(array[0].length()<2)replace = "?"+array[0]+" "; else replace="?"+array[0]; @@ -1493,7 +1643,7 @@ } - if(property!=""){ + if(property!=""&&skip==false){ String replace=""; String[] array = property.split(":"); if(array[0].length()<2)replace = "?"+array[0]+" "; @@ -1512,6 +1662,8 @@ query_upside_down=query_upside_down.replace("><","> <").replace(">?", "> ?"); query=query.replace("><","> <").replace(">?", "> ?"); + query=query.replace("/__", "/"); + query_upside_down=query_upside_down.replace("/__", "/"); lstquerupsidedown.add(query_upside_down); lstquerynew.add(query); System.out.println("Query: "+query); @@ -1519,6 +1671,10 @@ + /* + * Add, that the one with the isa is an Resource!!! + * An ontology Resource! + */ 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 @@ -1601,6 +1757,10 @@ * @throws SQLException */ private String getUriFromIndex(String string, int fall) throws SQLException{ + + /* + * something like /dbpedia.org/property/__bla nicht erlauben, also /__ durch / ersetzen + */ String originalString=string; string=string.replace("_", " "); string=string.replace("-", " "); @@ -1646,7 +1806,7 @@ if(result==null) { if(fall==1)return "http://dbpedia.org/property/"+tmp; if(fall==0) { - String bla ="http://dbpedia.org/resource/"+tmp; + String hotfix ="http://dbpedia.org/resource/"+tmp; if(tmp.contains("_")){ String[] newarraytmp=tmp.split("_"); String tmpneu=""; @@ -1654,17 +1814,23 @@ tmpneu+= "_"+ Character.toUpperCase(s.charAt(0)) + s.substring(1); } tmpneu=tmpneu.replaceFirst("_", ""); - bla ="http://dbpedia.org/resource/"+tmpneu; - System.out.println("Hotfix: "+bla); + hotfix ="http://dbpedia.org/resource/"+tmpneu; + hotfix=hotfix.replace("/__", "/"); + System.out.println("Hotfix: "+hotfix); } - return bla; + hotfix=hotfix.replace("/__", "/"); + return hotfix; } else{ + result=result.replace("/__","/"); System.out.println("return result: "+result); return result; } } - else return result; + else { + result=result.replace("/__","/"); + return result; + } } @@ -1826,8 +1992,11 @@ //SPARQL-Endpoint of Semantic Computing Group //5171 - String tmp="http://greententacle.techfak.uni-bielefeld.de:5171/sparql?default-graph-uri=&query="+createServerRequest(query)+"&format=text%2Fhtml&debug=on&timeout="; - System.out.println(tmp); + /* + * change to dbpedia http://dbpedia.org/sparql + */ + //String tmp="http://greententacle.techfak.uni-bielefeld.de:5171/sparql?default-graph-uri=&query="+createServerRequest(query)+"&format=text%2Fhtml&debug=on&timeout="; + String tmp="http://dbpedia.org/sparql?default-graph-uri=&query="+createServerRequest(query)+"&format=text%2Fhtml&debug=on&timeout="; System.out.println(tmp); URL url; InputStream is; InputStreamReader isr; @@ -1861,7 +2030,12 @@ private ArrayList<String> sendServerQuestionRequestArray(String query){ //SPARQL-Endpoint of Semantic Computing Group - String tmp="http://greententacle.techfak.uni-bielefeld.de:5171/sparql?default-graph-uri=&query="+createServerRequest(query)+"&format=text%2Fhtml&debug=on&timeout="; + /* + * change to dbpedia http://dbpedia.org/sparql + */ + //String tmp="http://greententacle.techfak.uni-bielefeld.de:5171/sparql?default-graph-uri=&query="+createServerRequest(query)+"&format=text%2Fhtml&debug=on&timeout="; + String tmp="http://dbpedia.org/sparql?default-graph-uri=&query="+createServerRequest(query)+"&format=text%2Fhtml&debug=on&timeout="; + System.out.println(tmp); URL url; InputStream is; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <seb...@us...> - 2012-01-03 12:26:15
|
Revision: 3523 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3523&view=rev Author: sebastianwtr Date: 2012-01-03 12:26:07 +0000 (Tue, 03 Jan 2012) Log Message: ----------- [tbsl exploration] finished with IsA Case and started to refactor the Code 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 2012-01-03 08:50:54 UTC (rev 3522) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/GetRessourcePropertys.java 2012-01-03 12:26:07 UTC (rev 3523) @@ -64,7 +64,7 @@ if(side.contains("RIGHT")) verarbeitungsurl=tmp_right; if(side.contains("LEFT")) verarbeitungsurl=tmp_left; - System.out.println(verarbeitungsurl); + //System.out.println(verarbeitungsurl); //just in case..... if(!side.contains("LEFT") && !side.contains("RIGHT")) verarbeitungsurl=tmp_left; @@ -115,7 +115,7 @@ //System.out.println(tmp_array[i-1].toLowerCase() + " " +tmp_array[i]); } - System.out.println("created Properties: "+hm); + // System.out.println("created Properties: "+hm); return hm; } 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-01-03 08:50:54 UTC (rev 3522) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/SparqlObject.java 2012-01-03 12:26:07 UTC (rev 3523) @@ -55,6 +55,7 @@ BasicTemplator btemplator; Templator templator; private static mySQLDictionary myindex; + boolean only_best_levensthein_query; //Konstruktor public SparqlObject() throws MalformedURLException, ClassNotFoundException, SQLException{ @@ -74,8 +75,10 @@ //normaly 1 setExplorationdepthwordnet(1); //eigentlich immer mit 0 initialisieren - setIterationdepth(0); + setIterationdepth(1); setNumberofanswers(1); + + only_best_levensthein_query=true; } /* @@ -235,7 +238,8 @@ if(querylist.size()==4){ //System.out.println("YEAH!!!!!"); //final_answer_tmp=simpleLevinstheinIteration(querylist, query); - final_query_tmp=simpleLevinstheinIteration(querylist, query); + //final_query_tmp=simpleLevinstheinIteration(querylist, query); + final_query_tmp=simpleCase(querylist, query, "LEVENSTHEIN"); for(String i: final_query_tmp){ //do it unnice for first @@ -288,10 +292,54 @@ * Only Wordnet!!! */ if(getIterationdepth()==2&&startIterating==true||getIterationdepth()==9&&startIterating==true){ - ArrayList<String> final_answer_tmp = new ArrayList<String>(); ArrayList<String> final_query_tmp = new ArrayList<String>(); - + //isAIteration(querylist, query); + if(querylist.size()==4){ + + final_query_tmp=simpleCase(querylist, query, "WORDNET"); + for(String i: final_query_tmp){ + + //do it unnice for first + boolean double_query=false; + for(String s: final_query ){ + + if(s.contains(i)){ + double_query=true; + + } + } + if(double_query==false){ + + final_query.add(i); + } + } + } + + if(querylist.size()>4&&query.contains("rdf:type")){ + //System.out.println("YEAH!!!!!"); + //final_answer_tmp=simpleLevinstheinIteration(querylist, query); + final_query_tmp=isAIteration(querylist, query,"WORDNET"); + for(String i: final_query_tmp){ + + //do it unnice for first + boolean double_query=false; + for(String s: final_query ){ + + if(s.contains(i)){ + double_query=true; + + } + } + if(double_query==false){ + + final_query.add(i); + } + } + } + + + /* if(querylist.size()==4){ //final_answer_tmp=simpleLevinstheinIteration(querylist, query); final_query_tmp = simpleWordnetIteration(querylist, query); @@ -329,7 +377,7 @@ final_query.add(i); } } - } + }*/ } @@ -474,7 +522,7 @@ querylist_new.add("PROPERTY"+firstProperty); querylist_new.add(sideOfProperty+firstResource); if(answer_tmp.isEmpty()){ - answer_tmp=simpleWordnetIterationArray(querylist_new,firstquery); + answer_tmp=simpleCase(querylist_new,firstquery,"WORDNET"); } //if answer_tmp is still empty return null and exit function if(answer_tmp.isEmpty()){final_answer.add("new Iteration didnt work"); @@ -516,7 +564,7 @@ for(ArrayList as: secondquerylist){ ArrayList<String> answer_tmp_two=new ArrayList<String>(); //answer_tmp_two=sendServerQuestionRequestArray(s); - answer_tmp=simpleWordnetIterationArray(as,as.get(0).toString()); + answer_tmp=simpleCase(as,as.get(0).toString(),"WORDNET"); for(String t :answer_tmp_two){ final_answer.add(t); System.out.println("Answer from advanced Iteration: "+ t); @@ -529,27 +577,141 @@ } - /* - * if there are more than one condition and if there is an isA Case in it, use this function. - * TODO: Change other simple Case function, that they have a Resource to deal with, or change the get Query function - */ - private ArrayList<String> isAIteration(ArrayList<String> querylist, String query) throws SQLException, + + private ArrayList<String> isAIteration(ArrayList<String> querylist, String query, String fall) throws SQLException, JWNLException { - //only for special case, that the first condition has a resource - ArrayList<String> final_answer=new ArrayList<String>(); + ArrayList<String> new_queries= new ArrayList<String>(); + //TODO: in get Query change, that there will be a second query, but only with the part of the condition upsidedown, which doesnt contains an isA + System.out.println("\n In IsA Iteration \n"); - return final_answer; + /* Erster Schritt, alle x rausbekommen: + e.g: PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT ?x WHERE {?x rdf:type <http://dbpedia.org/ontology/Country>.} + + */ + //take query and use regex, to take only the part beween the {} + Pattern p = Pattern.compile (".*\\{(.*\\<http.*)\\}.*"); + Matcher m = p.matcher (query); + ArrayList<String> list_of_x=new ArrayList<String>(); + String query_for_x=null; + while(m.find()){ + String tmp=m.group(1); + + Pattern p2=Pattern.compile (".*(http://dbpedia.org/ontology/[A-Z].*)\\>\\W.*"); + Matcher m2 = p2.matcher (tmp); + //now we know, that we are in the part with the rdf type thing + while(m2.find()){ + query_for_x="PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT ?x WHERE { ?x rdf:type <"+m2.group(1)+">}"; + //System.out.println("Done: "+ query_for_x); + } + + } + + //now send query_for_x to the server, to get all x + System.out.println("IsA x-query: "+query_for_x); + if(query_for_x!=null)list_of_x=sendServerQuestionRequestArray(query_for_x); + + + /* + * Zweiter Schritt: + * alle Propertys z.B. der ersten 5 xe holen und in eine Liste packen + */ + + int number_of_x_used=5; + int tmpcounter=0; + + HashMap<String,String> list_of_properties=new HashMap<String,String>(); + + for(String s: list_of_x){ + /* + * First use left and also right Propertys + */ + tmpcounter++; + if(tmpcounter <=number_of_x_used){ + HashMap<String,String> propertiesleft = new HashMap<String, String>(); + HashMap<String,String> propertiesright = new HashMap<String, String>(); + GetRessourcePropertys property = new GetRessourcePropertys(); + + try { + propertiesleft=property.getPropertys(s,"LEFT"); + propertiesright=property.getPropertys(s,"RIGHT"); + } + catch (Exception e){ + + } + + /* + * now put the properties togehter + */ + list_of_properties.putAll(propertiesright); + list_of_properties.putAll(propertiesleft); + } + + } + //System.out.println(list_of_properties); + + /* + * get Property used in the original query + * + */ + + System.out.println("Original Query: "+query); + + //http://dbpedia.org/ontology/officialLanguage + + //look for property + Pattern p3=Pattern.compile (".*\\<(http://dbpedia.org/property/.*)\\>\\W\\?.*"); + Matcher m3 = p3.matcher(query); + while(m3.find()) System.out.println("Property in IsA: "+m3.group(1)); + + //look for ontology, which is NO class + //Pattern p4=Pattern.compile (".*\\<(http://dbpedia.org/ontology/^[A-Z].*)\\>\\W.*"); + Pattern p4=Pattern.compile (".*\\<(http://dbpedia.org/ontology/[a-z].*)\\>\\W\\?.*"); + Matcher m4 = p4.matcher(query); + String uri_property=null; + while(m4.find()) { + uri_property=m4.group(1); + System.out.println("Uri_Property: "+ uri_property); + } + + /* + * Nice, now i get http://dbpedia.org/ontology/officialLanguage + */ + if(uri_property!=null){ + String property=uri_property.replace("http://dbpedia.org/ontology/", "").replace("http://dbpedia.org/property/", ""); + String property_to_compare_with=""; + + + for(String s : querylist){ + if(s.contains("PROPERTY")){ + property_to_compare_with=s.replace("PROPERTY",""); + } + + } + + + /* + * Dritter Schritt: original Query nehmen und über die Liste mit den Propertys laufen und normal mit wordnet/levensthein vergleichen + */ + + //I think, i dont need here the last part of the uri but the original word from the query! + //new_queries=doWordnet(query,property,uri_property,list_of_properties); + + //if you want to use wordnet: + if(fall.contains("WORDNET")) new_queries=doWordnet(query,property_to_compare_with,uri_property,list_of_properties); + if(fall.contains("LEVENSTHEIN")) new_queries=doLevensthein(query,property_to_compare_with,uri_property,list_of_properties); + } + + return new_queries; + } - - - private ArrayList<String> simpleLevinstheinIteration(ArrayList<String> querylist, String query) throws SQLException, + + private ArrayList<String> simpleCase(ArrayList<String> querylist, String query, String fall) throws SQLException, JWNLException { - System.out.println("In Simpe levensthein case!!"); - ArrayList<String> final_answer=new ArrayList<String>(); + System.out.println("In Simpe levensthein case!!"); String resource=""; String property_to_compare_with=""; String sideOfProperty="LEFT"; @@ -582,6 +744,7 @@ HashMap<String,String> properties = new HashMap<String, String>(); GetRessourcePropertys property = new GetRessourcePropertys(); + Boolean goOnAfterProperty = true; System.out.println("URI from Resource "+ resource +": "+getUriFromIndex(resource.toLowerCase(),0)); @@ -604,56 +767,14 @@ } if(goOnAfterProperty==true){ - String bestQuery=""; - double highestNLD=0; - //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(); - // System.out.println("Key "+ key +" and value "+value); - - //compare property gotten from the resource with the property from the original query - double nld=Levenshtein.nld(property_to_compare_with.toLowerCase(), key); - - //check if nld is greater than Levensthein - if(nld>=LevenstheinMin){ - //if its so, replace old uri with the new one - String querynew=query; - String replacement = getUriFromIndex(property_to_compare_with.toLowerCase(),1); - if(!querynew.contains(replacement)){ - replacement=replacement.replace("ontology", "property"); - } - querynew=querynew.replace(replacement,value); - if(nld>highestNLD){ - bestQuery=querynew; - highestNLD=nld; - } - /* System.out.println("Simple Levensthein Query: "+ querynew); - new_queries.add(querynew);*/ - //only add, if nld is greater than the already existing nld. - - new_queries.add(querynew); - } - + if(fall.contains("WORDNET")) new_queries=doWordnet(query, property_to_compare_with,getUriFromIndex(property_to_compare_with.toLowerCase(),1),properties); + if(fall.contains("LEVENSTHEIN")) new_queries=doLevensthein(query, property_to_compare_with,getUriFromIndex(property_to_compare_with.toLowerCase(),1),properties); + //new_queries=doLevensthein(query, property_to_compare_with,getUriFromIndex(property_to_compare_with.toLowerCase(),1),properties); + + //add original query + new_queries.add(query); + } - - - /*new_queries.add(bestQuery); - System.out.println("Best Query "+bestQuery);*/ - //add original query for iteration - - new_queries.add(query); - - //iterate over all Queries and get answer from Server - /* for(String anfrage : new_queries){ - String answer_tmp; - answer_tmp=sendServerQuestionRequest(anfrage); - System.out.println("Antwort vom Server: "+answer_tmp); - final_answer.add("Begin:\n"+anfrage +"\n"+answer_tmp+" \n End"); - }*/ - } //test to returnqueries, put them together and than send them to the server. @@ -661,6 +782,47 @@ //return final_answer; } + + + + private ArrayList<String> doLevensthein(String query, String property_to_compare_with,String uri_of_property, HashMap<String, String> properties) + throws SQLException { + ArrayList<String> new_queries= new ArrayList<String>(); + String bestQuery=""; + double highestNLD=0; + //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); + + //check if nld is greater than Levensthein + if(nld>=LevenstheinMin){ + //if its so, replace old uri with the new one + String querynew=query; + //String replacement = getUriFromIndex(property_to_compare_with.toLowerCase(),1); + String replacement =uri_of_property ; + if(!querynew.contains(replacement)){ + replacement=replacement.replace("ontology", "property"); + } + querynew=querynew.replace(replacement,value); + if(nld>highestNLD){ + bestQuery=querynew; + highestNLD=nld; + } + if(only_best_levensthein_query==true) new_queries.add(bestQuery); + else new_queries.add(querynew); + } + + } + + return new_queries; + } + private ArrayList<String> complexeLevinstheinIteration(ArrayList<String> querylist, String query) throws SQLException, JWNLException { @@ -798,308 +960,90 @@ } -private ArrayList<String> simpleWordnetIterationArray(ArrayList<String> querylist, String query) throws SQLException, - JWNLException { - ArrayList<String> final_answer=new ArrayList<String>(); - - System.out.println("In simpleWordnetIteration"); - - String resource=""; - String property_to_compare_with=""; - String sideOfProperty="LEFT"; + +private ArrayList<String> doWordnet(String query, String property, String uri_of_property,HashMap<String,String> properties) throws SQLException, +JWNLException { + ArrayList<String> new_queries= new ArrayList<String>(); - - 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); + System.out.println("Start Iterating Wordnet with "+property+" 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); + System.out.println("Semantics: "+ semantics); + + //first check, if there is a singular form in the wordnet dictionary.. eg children -> child + String _temp_=myindex.getWordnetHelp(property); + if(_temp_==null){ + tmp_semantics=semantics; + } + else{ + semantics.clear(); + semantics.add(_temp_); + tmp_semantics=semantics; + } + + System.out.println("tmp_semantics: "+ tmp_semantics); + Boolean goOnAfterWordnet = true; - - HashMap<String,String> properties = new HashMap<String, String>(); - GetRessourcePropertys property = new GetRessourcePropertys(); - Boolean goOnAfterProperty = true; + for(int i=0;i<=explorationdepthwordnet;i++){ + try { - properties=property.getPropertys(getUriFromIndex(resource.toLowerCase(),0),sideOfProperty); - if (properties==null){ + tmp_semantics=getSemantics(tmp_semantics); + System.out.println("tmp_semantics in Iteration: "+ tmp_semantics); + if (tmp_semantics==null){ + goOnAfterWordnet=false; + System.out.println("Begin:\n"+query +"\n Error in searching Wordnet with word "+semantics+" \n End"); - final_answer.add("Begin:\n"+query +"\nError in getting Properties \n End"); - goOnAfterProperty=false; } + else{ + //each word only one time + for(String k : tmp_semantics){ + if(!semantics.contains(k)) semantics.add(k); + } + } } catch (IOException e) { - final_answer.add("Begin:\n"+query +"\nError in getting Properties \n End"); - goOnAfterProperty=false; + goOnAfterWordnet=false; + System.out.println("Begin:\n"+query +"\n Error in searching Wordnet with word "+semantics+" \n End"); } - if(goOnAfterProperty==true){ - - ArrayList<String> new_queries= new ArrayList<String>(); - - 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); - System.out.println("Semantics: "+ semantics); - - //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; - } - - System.out.println("tmp_semantics: "+ tmp_semantics); - Boolean goOnAfterWordnet = true; - - for(int i=0;i<=explorationdepthwordnet;i++){ - - try { - tmp_semantics=getSemantics(tmp_semantics); - System.out.println("tmp_semantics in Iteration: "+ tmp_semantics); - if (tmp_semantics==null){ - goOnAfterWordnet=false; - System.out.println("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) { - - goOnAfterWordnet=false; - System.out.println("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(); - key=key.replace("\"",""); - key=key.replace("@en",""); - - for(String b : semantics){ - if(key.contains(b.toLowerCase())){ - if(!result_SemanticsMatchProperties.contains(key)){ - result_SemanticsMatchProperties.add(key); - String query_tmp=query; - String replacement = getUriFromIndex(property_to_compare_with.toLowerCase(),1); - if(!query_tmp.contains(replacement)){ - replacement=replacement.replace("ontology", "property"); - } - query_tmp=query_tmp.replace(replacement,value); - System.out.println("Simple Wordnet Query: "+ query_tmp); - new_queries.add(query_tmp); - } - } - } - } - //add original query for iteration - new_queries.add(query); - //iterate over all Queries and get answer from Server - for(String bla : new_queries){ - ArrayList<String>answer_tmp=new ArrayList<String>(); - answer_tmp=sendServerQuestionRequestArray(bla); - for(String s: answer_tmp)final_answer.add(s); - //final_answer.add("Begin:\n"+bla +"\n"+answer_tmp+" \n End"); - } - } - } + } - - return final_answer; -} - - private ArrayList<String> simpleWordnetIteration(ArrayList<String> querylist, String query) throws SQLException, - JWNLException { - ArrayList<String> final_answer=new ArrayList<String>(); - ArrayList<String> new_queries= new ArrayList<String>(); - - System.out.println("In simpleWordnetIteration"); - - String resource=""; - String property_to_compare_with=""; - String sideOfProperty="LEFT"; - + if(goOnAfterWordnet==true){ - 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); - - - 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("Begin:\n"+query +"\nError in getting Properties \n End"); - goOnAfterProperty=false; - } - - } catch (IOException e) { - - final_answer.add("Begin:\n"+query +"\nError in getting Properties \n End"); - goOnAfterProperty=false; - - } - if(goOnAfterProperty==true){ - - - - 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); - System.out.println("Semantics: "+ semantics); - - //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; - } - if(property_to_compare_with.contains("_")){ - String[] array=property_to_compare_with.split("_"); - for(String i : array) tmp_semantics.add(i); - tmp_semantics.add(property_to_compare_with.replace("_"," ")); - } - System.out.println("tmp_semantics: "+ tmp_semantics); - Boolean goOnAfterWordnet = true; - - - System.out.println("##########################"); - System.out.println("properties for "+getUriFromIndex(resource.toLowerCase(),0)+": "+properties); - System.out.println("Property to compare with: "+property_to_compare_with); - System.out.println("Semantics: "+semantics); - System.out.println("##########################"); - for(int i=0;i<=explorationdepthwordnet;i++){ - - try { - tmp_semantics=getSemantics(tmp_semantics); - System.out.println("tmp_semantics in Iteration: "+ tmp_semantics); - if (tmp_semantics==null){ - goOnAfterWordnet=false; - System.out.println("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); + for (Entry<String, String> entry : properties.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + key=key.replace("\"",""); + key=key.replace("@en",""); + + for(String b : semantics){ + if(key.contains(b.toLowerCase())){ + if(!result_SemanticsMatchProperties.contains(key)){ + result_SemanticsMatchProperties.add(key); + String query_tmp=query; + //String replacement = getUriFromIndex(property_to_compare_with.toLowerCase(),1); + String replacement = uri_of_property; + if(!query_tmp.contains(replacement)){ + replacement=replacement.replace("ontology", "property"); } + query_tmp=query_tmp.replace(replacement,value); + System.out.println("Simple Wordnet Query: "+ query_tmp); + new_queries.add(query_tmp); } - - } catch (IOException e) { - - goOnAfterWordnet=false; - System.out.println("Begin:\n"+query +"\n Error in searching Wordnet with word "+semantics+" \n End"); - } - - } - - if(goOnAfterWordnet==true){ - System.out.println("in actual wordnet function"); - for (Entry<String, String> entry : properties.entrySet()) { - String key = entry.getKey(); - key=key.replace("\"",""); - key=key.replace("@en",""); - - String value = entry.getValue(); - //System.out.println("Key "+ key +" and value "+value); - // System.out.println("Value propery: "+ value); - - for(String b : semantics){ - if(key.contains(b.toLowerCase())){ - //to check, if no property is used twice... - if(!result_SemanticsMatchProperties.contains(key)){ - result_SemanticsMatchProperties.add(key); - String query_tmp=query; - String test = getUriFromIndex(property_to_compare_with.toLowerCase(),1); - - //could happen, that there is an ontology and not a property or upsidedown in the query - if(!query_tmp.contains(test)){ - test=test.replace("ontology", "property"); - } - query_tmp=query_tmp.replace(test,value); - System.out.println("\n"); - System.out.println("Match with "+ b); - System.out.println("Original Query: "+ query); - System.out.println("Simple Wordnet Query: "+ query_tmp); - System.out.println("\n"); - new_queries.add(query_tmp); - } - } - } - } - - //add original query for iteration - new_queries.add(query); - - } + } } + + } - - return new_queries; + return new_queries; } + - private ArrayList<String> complexWordnetIteration(ArrayList<String> querylist, String query) throws SQLException, JWNLException { ArrayList<String> final_answer=new ArrayList<String>(); @@ -1347,7 +1291,7 @@ /** - * Iterates thru the conditions and returns an array, where one can see, if the Property is left or right from the resource + * Iterates thrue the conditions and returns an array, where one can see, if the Property is left or right from the resource * @param query * @return returns an array, where one can see, if the Property is left or right from the resource */ @@ -1358,40 +1302,63 @@ ArrayList<String> lstquery = new ArrayList<String>(); while (m.find()) { String tmp= m.group(1); - tmp=tmp.replace("http://dbpedia.org/resource/","").replace("http://dbpedia.org/property/", "").replace("http://dbpedia.org/ontology/", ""); + //if there is an .../ontology/C, dann ist das eine Klasse und das ganze soll dann nicht ersetzt reingepackt werden, sondern so bleiben, wie es ist. + System.out.println("Before new pattern and checking "+tmp); + Pattern p1=Pattern.compile (".*(http://dbpedia.org/ontology/[A-Z].*)\\>\\W.*"); + Matcher m1 = p1.matcher (tmp); + String resourceTemp=""; - //split on . for sign for end of conditions - String[] firstArray=tmp.split("\\."); - for(String i : firstArray){ - - String[] secondArray=i.split(" "); - //always in three counts - int counter=0; - for(String j : secondArray){ - counter=counter+1; - //only one condition - if(secondArray.length%3==0){ - if(counter==1&&j.contains("<")){ - //position of Property is right - lstquery.add("RIGHT"+j.replace("<", "").replace(">","")); - } - else if(counter==3&&j.contains("<")){ - //position of Property is left - lstquery.add("RIGHT"+j.replace("<", "").replace(">","")); - } - else if(counter==2){ - lstquery.add("PROPERTY"+j.replace("<", "").replace(">","")); - } - - else if(j.contains("?")) lstquery.add("VARIABLE"); - } - if(counter==0)counter=0; - - - } + while(m1.find()){ + resourceTemp="RESOURCE"+m1.group(1); + tmp=tmp.replace("<"+m1.group(1)+">", "SKIP"); + System.out.println("New temp: "+tmp); } + /* if(m1.find()){ + System.out.println("YEAHHHHHHHHHHHHHHHHHHHH "+m1.group(1)); + } + else{*/ + + + tmp=tmp.replace("http://dbpedia.org/resource/","").replace("http://dbpedia.org/property/", "").replace("http://dbpedia.org/ontology/", ""); + + //split on . for sign for end of conditions + String[] firstArray=tmp.split("\\."); + for(String i : firstArray){ + + String[] secondArray=i.split(" "); + //always in three counts + int counter=0; + for(String j : secondArray){ + //System.out.println("j "+j); + counter=counter+1; + //only one condition + if(secondArray.length%3==0){ + if(counter==1&&j.contains("<")&&!j.contains("SKIP")&&!j.contains("rdf:type")){ + //position of Property is right + lstquery.add("RIGHT"+j.replace("<", "").replace(">","")); + } + else if(counter==3&&j.contains("<")&&!j.contains("SKIP")&&!j.contains("rdf:type")){ + //position of Property is left + //here was RIGHT before.... + lstquery.add("LEFT"+j.replace("<", "").replace(">","")); + } + else if(counter==2&&!j.contains("SKIP")&&!j.contains("rdf:type")){ + lstquery.add("PROPERTY"+j.replace("<", "").replace(">","")); + } + + else if(j.contains("?")) lstquery.add("VARIABLE"); + else if(j.contains("SKIP"))lstquery.add(resourceTemp); + else if(j.contains("rdf:type"))lstquery.add("IsA"); + } + if(counter==0)counter=0; + + + } + } + //} } + //System.out.println("lstquery "+lstquery); return lstquery; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <seb...@us...> - 2012-02-22 17:57:05
|
Revision: 3582 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3582&view=rev Author: sebastianwtr Date: 2012-02-22 17:56:55 +0000 (Wed, 22 Feb 2012) Log Message: ----------- [tbsl exploration] start new implementation Modified Paths: -------------- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/SparqlObject.java Added 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: 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 (rev 0) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/Hypothesis.java 2012-02-22 17:56:55 UTC (rev 3582) @@ -0,0 +1,31 @@ +package org.dllearner.algorithm.tbsl.exploration.Sparql; + +public class Hypothesis { +private String variable; +private String uri; +private float rank; +public String getUri() { + return uri; +} +public void setUri(String uri) { + this.uri = uri; +} +public String getVariable() { + return variable; +} +public void setVariable(String variable) { + this.variable = variable; +} +public float getRank() { + return rank; +} +public void setRank(float rank) { + this.rank = rank; +} + +public Hypothesis(String variable, String uri, float rank){ + setRank(rank); + setVariable(variable); + 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-02-14 15:44:21 UTC (rev 3581) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/SparqlObject.java 2012-02-22 17:56:55 UTC (rev 3582) @@ -2204,16 +2204,7 @@ } private String createAnswer(String string){ - - /*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); - } - */ Pattern p = Pattern.compile (".*\\<td\\>(.*)\\</td\\>.*"); string = string.replace("<table class=\"sparql\" border=\"1\">", "").replace("<tr>","").replace("</tr>", "").replace("</table>", ""); Matcher m = p.matcher (string); @@ -2235,18 +2226,7 @@ } private ArrayList<String> createAnswerArray(String string){ - //<td>Klaus Wowereit</td> - - //get with regex all between <td> </td> - - /*Pattern p = Pattern.compile (".*<td>(.*)</td>.*"); - Matcher m = p.matcher (string); - ArrayList<String> result=new ArrayList<String>(); - while (m.find()) { - if(m.group(1)!=null) - result.add(m.group(1)); - }*/ Pattern p = Pattern.compile (".*\\<td\\>(.*)\\</td\\>.*"); string = string.replace("<table class=\"sparql\" border=\"1\">", "").replace("<tr>","").replace("</tr>", "").replace("</table>", ""); Matcher m = p.matcher (string); @@ -2309,14 +2289,8 @@ return query; } - - - - } - - /* * Backup original Iteration function * Added: 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 (rev 0) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/Template.java 2012-02-22 17:56:55 UTC (rev 3582) @@ -0,0 +1,87 @@ +package org.dllearner.algorithm.tbsl.exploration.Sparql; + +import java.util.ArrayList; + +public class Template { + + private ArrayList<ArrayList<String>> condition = new ArrayList<ArrayList<String>>(); + private ArrayList<ArrayList<Hypothesis>> hypotesen = new ArrayList<ArrayList<Hypothesis>>(); + private String selectTerm; + private String having; + private String filter; + private String OrderBy; + private String limit; + + public String getHaving() { + return having; + } + public void setHaving(String having) { + this.having = having; + } + public String getFilter() { + return filter; + } + public void setFilter(String filter) { + this.filter = filter; + } + public String getOrderBy() { + return OrderBy; + } + public void setOrderBy(String orderBy) { + OrderBy = orderBy; + } + public String getLimit() { + return limit; + } + public void setLimit(String limit) { + this.limit = limit; + } + + public String getSelectTerm() { + return selectTerm; + } + public void setSelectTerm(String selectTerm) { + this.selectTerm = selectTerm; + } + + + public Template(ArrayList<ArrayList<String>>condition_new, String having_new, String filter_new, String SelectTerm_new, String OrderBy_new, String limit_new){ + setCondition(condition_new); + setHaving(having_new); + setFilter(filter_new); + setOrderBy(OrderBy_new); + setLimit(limit_new); + setSelectTerm(SelectTerm_new); + } + public ArrayList<ArrayList<String>> getCondition() { + return condition; + } + public void setCondition(ArrayList<ArrayList<String>> condition) { + this.condition = condition; + } + public ArrayList<ArrayList<Hypothesis>> getHypotesen() { + return hypotesen; + } + public void setHypotesen(ArrayList<ArrayList<Hypothesis>> hypotesen) { + this.hypotesen = hypotesen; + } + + public void addHypothese(ArrayList<Hypothesis> ht){ + this.hypotesen.add(ht); + } + + + public void printAll(){ + System.out.println("###### Template ######"); + System.out.println("condition: "+condition); + System.out.println("hypotesen: "+hypotesen); + System.out.println("selectTerm: "+selectTerm); + System.out.println("having: "+having); + System.out.println("filter: "+filter); + System.out.println("OrderBy: "+OrderBy); + System.out.println("limit: "+limit); + System.out.println("###### Template printed ######\n"); + } + +} + Added: 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 (rev 0) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/TemplateBuilder.java 2012-02-22 17:56:55 UTC (rev 3582) @@ -0,0 +1,121 @@ +package org.dllearner.algorithm.tbsl.exploration.Sparql; + +import java.net.MalformedURLException; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Set; + +import org.dllearner.algorithm.tbsl.sparql.BasicQueryTemplate; +import org.dllearner.algorithm.tbsl.sparql.Path; +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.templator.BasicTemplator; + +public class TemplateBuilder { + + static BasicTemplator btemplator; + + +public TemplateBuilder() throws MalformedURLException, ClassNotFoundException, SQLException{ + + TemplateBuilder.btemplator = new BasicTemplator(); + //btemplator.UNTAGGED_INPUT = false; + //Object_new.myindex = new mySQLDictionary(); + } + + + public ArrayList<Template> createTemplates(String question){ + ArrayList<Template> resultArrayList = new ArrayList<Template>(); + 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>>(); + String selectTerm = ""; + String having= ""; + String filter= ""; + String OrderBy= ""; + String limit= ""; + String condition_String = ""; + + boolean addTemplate=true; + try{ + for(SPARQL_Term terms :bqt.getSelTerms()) selectTerm=selectTerm+(terms.toString())+" "; + } + catch (Exception e){ + selectTerm=""; + addTemplate=false; + } + + 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(); + String[] tmp_array = conditions1.toString().split(" -- "); + for(String s: tmp_array){ + temp_array.add(s); + } + condition.add(temp_array); + } + + } + catch (Exception e){ + condition_String=""; + addTemplate=false; + } + + try{ + for(SPARQL_Filter tmp : bqt.getFilters()) filter=filter+tmp+" "; + } + catch(Exception e){ + filter=""; + addTemplate=false; + } + try{ + for(SPARQL_Having tmp : bqt.getHavings()) having=having+tmp+" "; + } + catch(Exception e){ + having=""; + addTemplate=false; + } + + //if there is no order by, replace with "" + OrderBy="ORDER BY "; + try{ + for(SPARQL_Term tmp : bqt.getOrderBy()) { + System.out.println("Yeah"); + OrderBy=OrderBy+tmp+" "; + } + if((bqt.getOrderBy()).size()==0)OrderBy=""; + } + catch(Exception e){ + OrderBy=""; + addTemplate=false; + } + + + try{ + limit="LIMIT "+bqt.getLimit(); + + if(bqt.getLimit()==0)limit=""; + } + catch(Exception e){ + limit=""; + 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); + } + for(Template temp : resultArrayList){ + temp.printAll(); + } + return resultArrayList; + } +} Added: 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 (rev 0) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/testClass_new.java 2012-02-22 17:56:55 UTC (rev 3582) @@ -0,0 +1,21 @@ +package org.dllearner.algorithm.tbsl.exploration.Sparql; + +import java.net.MalformedURLException; +import java.sql.SQLException; + +public class testClass_new { + + /** + * @param args + * @throws SQLException + * @throws ClassNotFoundException + * @throws MalformedURLException + */ + public static void main(String[] args) throws MalformedURLException, ClassNotFoundException, SQLException { + // TODO Auto-generated method stub + TemplateBuilder testobject = new TemplateBuilder(); + String question = "Which books are written by Daniele Steel?"; + testobject.createTemplates(question); + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
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. |
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. |