From: <seb...@us...> - 2011-10-16 09:27:03
|
Revision: 3306 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3306&view=rev Author: sebastianwtr Date: 2011-10-16 09:26:57 +0000 (Sun, 16 Oct 2011) Log Message: ----------- [tbsl] added a lot of new functions Modified Paths: -------------- 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/exploration_main/exploration_main.java 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-10-16 09:21:24 UTC (rev 3305) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/Sparql/SparqlObject.java 2011-10-16 09:26:57 UTC (rev 3306) @@ -14,6 +14,10 @@ import org.dllearner.algorithm.tbsl.nlp.WordNet; import org.dllearner.algorithm.tbsl.sparql.BasicQueryTemplate; +import org.dllearner.algorithm.tbsl.sparql.Path; +import org.dllearner.algorithm.tbsl.sparql.SPARQL_Term; +import org.dllearner.algorithm.tbsl.sparql.Slot; +import org.dllearner.algorithm.tbsl.sparql.Template; import org.dllearner.algorithm.tbsl.templator.BasicTemplator; import org.dllearner.algorithm.tbsl.templator.Templator; @@ -27,6 +31,7 @@ static WordNet wordnet; BasicTemplator btemplator; Templator templator; + HashMap<String, String> hm; /*Set<BasicQueryTemplate> querytemps = btemplator.buildBasicQueries(line); for (BasicQueryTemplate temp : querytemps) { @@ -35,218 +40,220 @@ */ //Konstruktor - public SparqlObject() throws MalformedURLException{ + public SparqlObject(HashMap<String, String> hm_new) throws MalformedURLException{ wordnet = new WordNet(); + hm=hm_new; System.out.println("Loading SPARQL Templator"); btemplator = new BasicTemplator(); templator = new Templator(); System.out.println("Loading SPARQL Templator Done\n"); } + + private void doIteration(String string1, String string2) throws JWNLException{ + long startTime = System.currentTimeMillis(); - public void create_Sparql_query(String string,HashMap<String, String> hm) throws JWNLException{ - // string=string.replaceAll("?", ""); - String[] array= string.split(" "); - String teststring=""; - /*Set<BasicQueryTemplate> querytemps = btemplator.buildBasicQueries("Who is the Mayor of Berlin?"); - for (BasicQueryTemplate temp : querytemps) { - //System.out.println(temp.toString()); - teststring=teststring.concat(temp.toString()); - } - - System.out.println("##################"); - System.out.println(teststring);*/ - - /** - * Cluster function - */ - /* - int length=array.length; - int [] result_array= new int[length]; - for(int p =0;p<length;p++){ - result_array[p]=0; + String string2_uri; + string2_uri=hm.get(string2); + string2_uri=string2_uri.replace("Category:", ""); + string2_uri=string2_uri.replace("category:", ""); + System.out.println("Get Propertys of "+string2); + + //contains uri AND string, every second is the string + ArrayList<String> properties = new ArrayList<String>(); + GetRessourcePropertys property = new GetRessourcePropertys(); + try { + //using uri now, not the string + properties=property.getPropertys(hm.get(string2)); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + System.out.println("Start Iterating Wordnet with "+string1+" and deept of "+iteration_deept); + ArrayList<String> semantics=new ArrayList<String>(); + ArrayList<String> tmp_semantics=new ArrayList<String>(); + ArrayList<String> result_SemanticsMatchProperties=new ArrayList<String>(); + semantics.add(string1); + tmp_semantics=semantics; + for(int i=0;i<=iteration_deept;i++){ + + try { + tmp_semantics=getSemantics(tmp_semantics); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + + } + //each word only one time + for(String k : tmp_semantics){ + if(!semantics.contains(k)) semantics.add(k); } - int zaehler=1; - - //looking for max 3 word as one index - for(int z=length-1;z>=0;z=z-1){ - if(z-2>=0){ - String tmp1 = array[z]; - String tmp2 = array[z-1]; - String tmp3 = array[z-2]; - String tmpstring3=(((tmp3.concat(" ")).concat(tmp2)).concat(" ")).concat(tmp1); - String tmpstring2=(tmp2.concat(" ")).concat(tmp1); - String tmpstring1=tmp1; - - //always looking for the "biggest" match - if(hm.get(tmpstring3)!=null){ - result_array[z]=zaehler; - result_array[z-1]=zaehler; - result_array[z-2]=zaehler; - zaehler++; - } - else{ - if(hm.get(tmpstring2)!=null){ - result_array[z]=zaehler; - result_array[z-1]=zaehler; - zaehler++; - } - else{ - if(hm.get(tmpstring1)!=null){ - result_array[z]=zaehler; - zaehler++; - } - } - } - - } - else{ - if(z-1>=0){ - String tmp1 = array[z]; - String tmp2 = array[z-1]; - - String tmpstring2=(tmp2.concat(" ")).concat(tmp1); - String tmpstring1=tmp1; - - //always looking for the "biggest" match + } + long endTime = System.currentTimeMillis(); + System.out.println("Getting Properties and Semantics took "+(endTime-startTime) +" ms\n"); + + //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))) + result_SemanticsMatchProperties.add(properties.get(h)); + } + } + } + for(String b : result_SemanticsMatchProperties){ + string1=b.toLowerCase(); + String anfrage; + String string1_uri; + string1_uri=hm.get(string1); + if(string1_uri!=null){ + anfrage="PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>select ?x where { <"+string2_uri+"> <"+string1_uri+"> ?x.}"; + System.out.println("Answer with the property \" " + b + "\" :\n"+sendServerQuestionRequest(anfrage)); + } + } + long endTime2 = System.currentTimeMillis(); + System.out.println("Getting Properties, Semantics and Answer from server took "+(endTime2-startTime) +" ms"); + } + + - if(hm.get(tmpstring2)!=null){ - result_array[z]=zaehler; - result_array[z-1]=zaehler; - zaehler++; - } - else{ - if(hm.get(tmpstring1)!=null){ - result_array[z]=zaehler; - zaehler++; - } - } - } - if(z==0){ - if(hm.get(array[z])!=null){ - result_array[z]=zaehler; - zaehler++; - } - } - } - } - - System.out.println("###### Cluster ######"); - for(int p =0;p<length;p++){ - System.out.println(result_array[p]); + private void create_Sparql_query_new(String string) throws JWNLException{ + String[] array_new=new String[4]; + array_new=getyy0AndQuery(string); + if(!array_new[0].contains("error")){ + String result=null; + + //Version 1 + result=sendServerQuestionRequest(array_new[0]); + if(result!="noanswer"){ + // System.out.println("Version1"); + System.out.println(result); } - System.out.println("######"); - */ - - //look, if the first word is a who! - if(array[0].contains("who")){ - int position=0; - for(int i=0;i<array.length;i++){ - if (array[i].contains("of")){ - position=i; - break; - } + else{ + doIteration(array_new[3],array_new[2]); + } + //Version2 + /* else{ + result=sendServerQuestionRequest(array_new[1]); + if(result!="noanswer"){ + System.out.println("Version2"); + System.out.println(result); } - String vor_of=array[position-1]; - String nach_of=""; - //if there is only one element after of - if(array.length-position-1==1){ - nach_of=array[position+1]; - } - else{ - for(int i=position+1; i<array.length;i++){ - nach_of=(nach_of.concat(array[i])).concat(" "); - } - - //delete last emty space - nach_of = nach_of.substring(0, nach_of.length()-1); - } - String uri_vor_of=" "; - String uri_nach_of=" "; - - uri_vor_of=hm.get(vor_of); - uri_nach_of=hm.get(nach_of); - if(uri_vor_of!=null && uri_nach_of!=null){ - uri_nach_of=uri_nach_of.replace("Category:", ""); - uri_nach_of=uri_nach_of.replace("category:", ""); - - String anfrage; - anfrage="PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>select ?x where { <"+uri_nach_of+"> <"+uri_vor_of+"> ?x.}"; - - //if there is no answer from the server, start searching with wordnet - String result=""; - result=sendServerQuestionRequest(anfrage); - if(result!="noanswer"){ - System.out.println(result); - } - else{ - long startTime = System.currentTimeMillis(); - - System.out.println("Get Propertys of "+nach_of); - - //contains uri AND string, every second is the string - ArrayList<String> properties = new ArrayList<String>(); - GetRessourcePropertys property = new GetRessourcePropertys(); - try { - //using uri now, not the string - properties=property.getPropertys(hm.get(nach_of)); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - System.out.println("Start Iterating Wordnet with "+vor_of+" and deept of "+iteration_deept); - ArrayList<String> semantics=new ArrayList<String>(); - ArrayList<String> tmp_semantics=new ArrayList<String>(); - ArrayList<String> result_SemanticsMatchProperties=new ArrayList<String>(); - semantics.add(vor_of); - tmp_semantics=semantics; - for(int i=0;i<=iteration_deept;i++){ + }*/ + } - try { - tmp_semantics=getSemantics(tmp_semantics); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - - } - //each word only one time - for(String k : tmp_semantics){ - if(!semantics.contains(k)) semantics.add(k); - } - - } - long endTime = System.currentTimeMillis(); - System.out.println("Getting Properties and Semantics took "+(endTime-startTime) +" ms\n"); - - //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))) - result_SemanticsMatchProperties.add(properties.get(h)); - } - } - } - for(String b : result_SemanticsMatchProperties){ - vor_of=b.toLowerCase(); - uri_vor_of=hm.get(vor_of); - if(uri_vor_of!=null){ - anfrage="PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>select ?x where { <"+uri_nach_of+"> <"+uri_vor_of+"> ?x.}"; - System.out.println("Answer with the property \" " + b + "\" :\n"+sendServerQuestionRequest(anfrage)); - } - } - long endTime2 = System.currentTimeMillis(); - System.out.println("Getting Properties, Semantics and Answer from server took "+(endTime2-startTime) +" ms"); - } - } - - } + } + public void create_Sparql_query(String string) throws JWNLException{ + //create_Sparql_query_new(string); + + Set<BasicQueryTemplate> querytemps = btemplator.buildBasicQueries(string.toLowerCase()); + 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()); + + String query; + SPARQL_Term selTerms = null; + for(SPARQL_Term terms :temp.getSelTerms()) selTerms=terms; + + Path conditions = null; + for(Path condition: temp.getConditions()) conditions=condition; + + + + //System.out.println("\n"); + System.out.println("\n"); + query=temp.getQt().toString()+" "+selTerms.toString()+" { "+ conditions.toString().replace("--","") + "} "; + + String[] slots= null; + for(Slot slot : temp.getSlots()){ + + //hier muss dann noch die abfrage aus der hm raus, also das direkt die uri eingebettet wird. + String tmp= slot.toString(); + tmp= tmp.replace("UNSPEC",""); + tmp= tmp.replace("RESOURCE",""); + tmp= tmp.replace("{",""); + tmp= tmp.replace("}",""); + tmp=tmp.replace(" ",""); + //System.out.println(tmp); + //damit auch wirklich nur ?y und nicht ?y0 ersetzt wird, einfach nach "?y " suchen. + String[] array = tmp.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 + query=query.replace(replace, "<"+hm.get(array[1])+">"); + + } + System.out.println(query); + } + // string=string.replaceAll("?", ""); + //create_Sparql_query_old(string); } + + + + + private String[] getyy0AndQuery(String string) { + String teststring=""; + String[] return_array = new String[4]; + Set<BasicQueryTemplate> querytemps = btemplator.buildBasicQueries(string); + for (BasicQueryTemplate temp : querytemps) { + teststring=temp.toString(); + System.out.println(teststring); + } + teststring=teststring.replace("\n", ""); + String[] array_tmp=teststring.split("\\}"); + + //only for y and y0 + String sparqlquery=""; + String y=""; + String y0=""; + for(String i : array_tmp){ + i=i.replace(" ", ""); + String[] tmp=null; + if(i.contains("SELECT")) sparqlquery=i.concat("}"); + if(i.contains("y0")){ + y0=i; + tmp=y0.split("\\{"); + y0=tmp[1];} + if(i.contains("y")&& !i.contains("y0")){ + y=i; + tmp=y.split("\\{"); + y=tmp[1]; + } + } + String uri1=hm.get(y0); + String uri2=hm.get(y); + if(uri1!=null && uri2!=null){ + uri1=uri1.replace("Category:", ""); + uri1=uri1.replace("category:", ""); + uri2=uri2.replace("Category:", ""); + uri2=uri2.replace("category:", ""); + String anfrage1; + anfrage1="PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>select ?x where { <"+uri1+"> <"+uri2+"> ?x.}"; + String anfrage2; + anfrage2="PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>select ?x where { <"+uri2+"> <"+uri1+"> ?x.}"; + return_array[0]=anfrage1; + return_array[1]=anfrage2; + return_array[2]=y0; + return_array[3]=y; + } + else{ + return_array[0]="error"; + } + return return_array; + + } private static ArrayList<String> getSemantics (ArrayList<String> semantics) throws IOException, JWNLException { ArrayList<String> result = new ArrayList<String>(); @@ -361,4 +368,201 @@ // query=query.replaceAll("\v",""); return query; } + } + +/** + * Cluster function + */ +/* +int length=array.length; +int [] result_array= new int[length]; +for(int p =0;p<length;p++){ + result_array[p]=0; +} +int zaehler=1; + +//looking for max 3 word as one index +for(int z=length-1;z>=0;z=z-1){ + if(z-2>=0){ + String tmp1 = array[z]; + String tmp2 = array[z-1]; + String tmp3 = array[z-2]; + + String tmpstring3=(((tmp3.concat(" ")).concat(tmp2)).concat(" ")).concat(tmp1); + String tmpstring2=(tmp2.concat(" ")).concat(tmp1); + String tmpstring1=tmp1; + + //always looking for the "biggest" match + if(hm.get(tmpstring3)!=null){ + result_array[z]=zaehler; + result_array[z-1]=zaehler; + result_array[z-2]=zaehler; + zaehler++; + } + else{ + if(hm.get(tmpstring2)!=null){ + result_array[z]=zaehler; + result_array[z-1]=zaehler; + zaehler++; + } + else{ + if(hm.get(tmpstring1)!=null){ + result_array[z]=zaehler; + zaehler++; + } + } + } + + } + else{ + if(z-1>=0){ + String tmp1 = array[z]; + String tmp2 = array[z-1]; + + String tmpstring2=(tmp2.concat(" ")).concat(tmp1); + String tmpstring1=tmp1; + + //always looking for the "biggest" match + + if(hm.get(tmpstring2)!=null){ + result_array[z]=zaehler; + result_array[z-1]=zaehler; + zaehler++; + } + else{ + if(hm.get(tmpstring1)!=null){ + result_array[z]=zaehler; + zaehler++; + } + } + } + if(z==0){ + if(hm.get(array[z])!=null){ + result_array[z]=zaehler; + zaehler++; + } + } + } + } + +System.out.println("###### Cluster ######"); +for(int p =0;p<length;p++){ + System.out.println(result_array[p]); +} +System.out.println("######"); +*/ + +/* + * private void create_Sparql_query_old(String string) throws JWNLException { + String[] array= string.split(" "); + + + //look, if the first word is a who! + if(array[0].contains("who")){ + int position=0; + for(int i=0;i<array.length;i++){ + if (array[i].contains("of")){ + position=i; + break; + } + } + String vor_of=array[position-1]; + String nach_of=""; + //if there is only one element after of + if(array.length-position-1==1){ + nach_of=array[position+1]; + } + else{ + for(int i=position+1; i<array.length;i++){ + nach_of=(nach_of.concat(array[i])).concat(" "); + } + + //delete last emty space + nach_of = nach_of.substring(0, nach_of.length()-1); + } + String uri_vor_of=" "; + String uri_nach_of=" "; + + uri_vor_of=hm.get(vor_of); + uri_nach_of=hm.get(nach_of); + if(uri_vor_of!=null && uri_nach_of!=null){ + uri_nach_of=uri_nach_of.replace("Category:", ""); + uri_nach_of=uri_nach_of.replace("category:", ""); + + String anfrage; + anfrage="PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>select ?x where { <"+uri_nach_of+"> <"+uri_vor_of+"> ?x.}"; + + //if there is no answer from the server, start searching with wordnet + String result=""; + result=sendServerQuestionRequest(anfrage); + if(result!="noanswer"){ + System.out.println(result); + } + else{ + long startTime = System.currentTimeMillis(); + + System.out.println("Get Propertys of "+nach_of); + + //contains uri AND string, every second is the string + ArrayList<String> properties = new ArrayList<String>(); + GetRessourcePropertys property = new GetRessourcePropertys(); + try { + //using uri now, not the string + properties=property.getPropertys(hm.get(nach_of)); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + System.out.println("Start Iterating Wordnet with "+vor_of+" and deept of "+iteration_deept); + ArrayList<String> semantics=new ArrayList<String>(); + ArrayList<String> tmp_semantics=new ArrayList<String>(); + ArrayList<String> result_SemanticsMatchProperties=new ArrayList<String>(); + semantics.add(vor_of); + tmp_semantics=semantics; + for(int i=0;i<=iteration_deept;i++){ + + try { + tmp_semantics=getSemantics(tmp_semantics); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + + } + //each word only one time + for(String k : tmp_semantics){ + if(!semantics.contains(k)) semantics.add(k); + } + + } + long endTime = System.currentTimeMillis(); + System.out.println("Getting Properties and Semantics took "+(endTime-startTime) +" ms\n"); + + //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))) + result_SemanticsMatchProperties.add(properties.get(h)); + } + } + } + for(String b : result_SemanticsMatchProperties){ + vor_of=b.toLowerCase(); + uri_vor_of=hm.get(vor_of); + if(uri_vor_of!=null){ + anfrage="PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>select ?x where { <"+uri_nach_of+"> <"+uri_vor_of+"> ?x.}"; + System.out.println("Answer with the property \" " + b + "\" :\n"+sendServerQuestionRequest(anfrage)); + } + } + long endTime2 = System.currentTimeMillis(); + System.out.println("Getting Properties, Semantics and Answer from server took "+(endTime2-startTime) +" ms"); + } + } + + } + } +*/ Modified: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/exploration_main/exploration_main.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/exploration_main/exploration_main.java 2011-10-16 09:21:24 UTC (rev 3305) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/exploration/exploration_main/exploration_main.java 2011-10-16 09:26:57 UTC (rev 3306) @@ -63,7 +63,7 @@ System.out.println("Entity1 done");*/ System.out.println("Done with indexing\n"); System.out.println("Start generating Wordnet Dictionary"); - SparqlObject sparql = new SparqlObject(); + SparqlObject sparql = new SparqlObject(hm); System.out.println("Generating Wordnet Dictionary Done"); long stopInitTime = System.currentTimeMillis(); System.out.println("Time for Initialising "+(stopInitTime-startInitTime)+" ms"); @@ -97,7 +97,7 @@ anzahl++; //get each line and send it to the parser s=s.replace("?",""); - sparql.create_Sparql_query(s.toLowerCase(),hm); + sparql.create_Sparql_query(s.toLowerCase()); } long timeNow = System.currentTimeMillis(); long diff = timeNow-startTime; @@ -112,7 +112,7 @@ for (BasicQueryTemplate temp : querytemps) { System.out.println(temp.toString()); }*/ - sparql.create_Sparql_query(line.toLowerCase(),hm); + sparql.create_Sparql_query(line.toLowerCase()); long endTime= System.currentTimeMillis(); System.out.println("\n The complete answering of the Question took "+(endTime-startTime)+" ms"); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |