From: <lor...@us...> - 2012-06-13 14:56:49
|
Revision: 3738 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3738&view=rev Author: lorenz_b Date: 2012-06-13 14:56:37 +0000 (Wed, 13 Jun 2012) Log Message: ----------- Integration of manual mappings. Modified Paths: -------------- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/cli/TestFrontend.java trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/learning/SPARQLTemplateBasedLearner2.java trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/sparql/Slot.java trunk/components-ext/src/main/java/org/dllearner/common/index/SPARQLClassesIndex.java trunk/components-ext/src/main/java/org/dllearner/common/index/SPARQLIndex.java trunk/components-ext/src/main/java/org/dllearner/common/index/SPARQLPropertiesIndex.java trunk/components-ext/src/main/resources/tbsl/lexicon/english_oxford.lex trunk/components-ext/src/test/java/org/dllearner/algorithm/tbsl/TBSLTest.java Added Paths: ----------- trunk/components-ext/src/main/java/org/dllearner/common/index/MappingBasedIndex.java trunk/components-ext/src/main/resources/tbsl/oxford_class_mappings.txt trunk/components-ext/src/main/resources/tbsl/oxford_dataproperty_mappings.txt trunk/components-ext/src/main/resources/tbsl/oxford_objectproperty_mappings.txt trunk/components-ext/src/main/resources/tbsl/oxford_resource_mappings.txt trunk/components-ext/src/test/java/org/dllearner/algorithm/tbsl/OxfordEvaluation.java Modified: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/cli/TestFrontend.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/cli/TestFrontend.java 2012-06-13 10:31:55 UTC (rev 3737) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/cli/TestFrontend.java 2012-06-13 14:56:37 UTC (rev 3738) @@ -14,7 +14,7 @@ public class TestFrontend { // MODE ::= BASIC | LEIPZIG - static String MODE = "BASIC"; + static String MODE = "LEIPZIG"; public static void main(String[] args) { Modified: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/learning/SPARQLTemplateBasedLearner2.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/learning/SPARQLTemplateBasedLearner2.java 2012-06-13 10:31:55 UTC (rev 3737) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/learning/SPARQLTemplateBasedLearner2.java 2012-06-13 14:56:37 UTC (rev 3738) @@ -41,6 +41,7 @@ import org.dllearner.common.index.Index; import org.dllearner.common.index.IndexResultItem; import org.dllearner.common.index.IndexResultSet; +import org.dllearner.common.index.MappingBasedIndex; import org.dllearner.common.index.SOLRIndex; import org.dllearner.common.index.SPARQLDatatypePropertiesIndex; import org.dllearner.common.index.SPARQLObjectPropertiesIndex; @@ -96,6 +97,8 @@ private Index datatypePropertiesIndex; private Index objectPropertiesIndex; + private MappingBasedIndex mappingIndex; + private Templator templateGenerator; private Lemmatizer lemmatizer; private PartOfSpeechTagger posTagger; @@ -192,6 +195,10 @@ lemmatizer = new LingPipeLemmatizer(); } + public void setMappingIndex(MappingBasedIndex mappingIndex) { + this.mappingIndex = mappingIndex; + } + /* * Only for Evaluation useful. */ @@ -369,6 +376,7 @@ } } }); + slot2Allocations = Collections.synchronizedMap(new HashMap<Slot, Set<Allocation>>()); Set<WeightedQuery> allQueries = new TreeSet<WeightedQuery>(); @@ -385,11 +393,13 @@ long startTime = System.currentTimeMillis(); for (Slot slot : t.getSlots()) { - if(!slot2Allocations.containsKey(slot)){ + if(!slot2Allocations.containsKey(slot)){//System.out.println(slot + ": " + slot.hashCode());System.out.println(slot2Allocations); Callable<Map<Slot, SortedSet<Allocation>>> worker = new SlotProcessor(slot); Future<Map<Slot, SortedSet<Allocation>>> submit = executor.submit(worker); list.add(submit); - } + } else { + System.out.println("CACHE HIT"); + } } for (Future<Map<Slot, SortedSet<Allocation>>> future : list) { @@ -840,27 +850,42 @@ IndexResultSet rs; for(String word : slot.getWords()){ + rs = new IndexResultSet(); + if(mappingIndex != null){ + SlotType type = slot.getSlotType(); + if(type == SlotType.CLASS){ + rs.add(mappingIndex.getClassesWithScores(word)); + } else if(type == SlotType.PROPERTY || type == SlotType.SYMPROPERTY){ + rs.add(mappingIndex.getPropertiesWithScores(word)); + } else if(type == SlotType.DATATYPEPROPERTY){ + rs.add(mappingIndex.getDatatypePropertiesWithScores(word)); + } else if(type == SlotType.OBJECTPROPERTY){ + rs.add(mappingIndex.getObjectPropertiesWithScores(word)); + } else if(type == SlotType.RESOURCE || type == SlotType.UNSPEC){ + rs.add(mappingIndex.getResourcesWithScores(word)); + } + } if(slot.getSlotType() == SlotType.RESOURCE){ - rs = index.getResourcesWithScores(word, 50); + rs.add(index.getResourcesWithScores(word, 50)); } else { if(slot.getSlotType() == SlotType.CLASS){ word = PlingStemmer.stem(word); } - rs = index.getResourcesWithScores(word, 20); + rs.add(index.getResourcesWithScores(word, 20)); } for(IndexResultItem item : rs.getItems()){ double similarity = Similarity.getSimilarity(word, item.getLabel()); - //get the labels of the redirects and compute the highest similarity - if(slot.getSlotType() == SlotType.RESOURCE){ - Set<String> labels = getRedirectLabels(item.getUri()); - for(String label : labels){ - double tmp = Similarity.getSimilarity(word, label); - if(tmp > similarity){ - similarity = tmp; - } - } - } +// //get the labels of the redirects and compute the highest similarity +// if(slot.getSlotType() == SlotType.RESOURCE){ +// Set<String> labels = getRedirectLabels(item.getUri()); +// for(String label : labels){ +// double tmp = Similarity.getSimilarity(word, label); +// if(tmp > similarity){ +// similarity = tmp; +// } +// } +// } double prominence = getProminenceValue(item.getUri(), slot.getSlotType()); allocations.add(new Allocation(item.getUri(), prominence, similarity)); } @@ -876,6 +901,11 @@ } + private boolean isDatatypePropeprty(String uri){ + String query = "ASK {<%s> a <http://www.w3.org/2002/07/owl#DatatypeProperty>}."; + return executeAskQuery(query); + } + /** * @param args * @throws NoTemplateFoundException Modified: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/sparql/Slot.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/sparql/Slot.java 2012-06-13 10:31:55 UTC (rev 3737) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/sparql/Slot.java 2012-06-13 14:56:37 UTC (rev 3738) @@ -103,7 +103,8 @@ return new Slot(anchor,type,newWords); } - @Override + + /*@Override public int hashCode() { final int prime = 31; int result = 1; @@ -143,7 +144,31 @@ } else if (!words.equals(other.words)) return false; return true; + }*/ + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Slot other = (Slot) obj; + if(other.type == type && other.token == token){ + return true; + } + return false; } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((token == null) ? 0 : token.hashCode()); + result = prime * result + ((type == null) ? 0 : type.hashCode()); + return result; + } + } Added: trunk/components-ext/src/main/java/org/dllearner/common/index/MappingBasedIndex.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/common/index/MappingBasedIndex.java (rev 0) +++ trunk/components-ext/src/main/java/org/dllearner/common/index/MappingBasedIndex.java 2012-06-13 14:56:37 UTC (rev 3738) @@ -0,0 +1,211 @@ +package org.dllearner.common.index; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +public class MappingBasedIndex { + + private Map<String, List<String>> classUri2TokensMap = new HashMap<String, List<String>>(); + private Map<String, List<String>> resourceUri2TokensMap = new HashMap<String, List<String>>(); + private Map<String, List<String>> datatypePropertyUri2TokensMap = new HashMap<String, List<String>>(); + private Map<String, List<String>> objectPropertyUri2TokensMap = new HashMap<String, List<String>>(); + + public MappingBasedIndex(String classMappingsFile, String resourceMappingsFile, + String dataPropertyMappingsFile, String objectPropertyMappingsFile) { + BufferedReader br = null; + String line = null; + try { + //load class mappings + if(classMappingsFile != null){ + br = new BufferedReader(new FileReader(new File(classMappingsFile))); + while((line = br.readLine()) != null){ + int split = line.indexOf("|"); + //get the URI + String uri = line.substring(0, split); + //get the list of tokens + List<String> tokens = new ArrayList<String>(); + String tokenString = line.substring(split + 1); + String[] tokenArray = tokenString.split(","); + for(String token : tokenArray){ + tokens.add(token.trim()); + } + + classUri2TokensMap.put(uri, tokens); + } + } + + //load resource mappings + if(resourceMappingsFile != null){ + br = new BufferedReader(new FileReader(new File(resourceMappingsFile))); + while((line = br.readLine()) != null){ + int split = line.indexOf("|"); + //get the URI + String uri = line.substring(0, split); + //get the list of tokens + List<String> tokens = new ArrayList<String>(); + String tokenString = line.substring(split + 1); + String[] tokenArray = tokenString.split(","); + for(String token : tokenArray){ + tokens.add(token.trim()); + } + + resourceUri2TokensMap.put(uri, tokens); + } + } + + //load object property mappings + if(objectPropertyMappingsFile != null){ + br = new BufferedReader(new FileReader(new File(objectPropertyMappingsFile))); + while((line = br.readLine()) != null){ + int split = line.indexOf("|"); + //get the URI + String uri = line.substring(0, split); + //get the list of tokens + List<String> tokens = new ArrayList<String>(); + String tokenString = line.substring(split + 1); + String[] tokenArray = tokenString.split(","); + for(String token : tokenArray){ + tokens.add(token.trim()); + } + + objectPropertyUri2TokensMap.put(uri, tokens); + } + } + + //load datatype property mappings + if(dataPropertyMappingsFile != null){ + br = new BufferedReader(new FileReader(new File(dataPropertyMappingsFile))); + while((line = br.readLine()) != null){ + int split = line.indexOf("|"); + //get the URI + String uri = line.substring(0, split); + //get the list of tokens + List<String> tokens = new ArrayList<String>(); + String tokenString = line.substring(split + 1); + String[] tokenArray = tokenString.split(","); + for(String token : tokenArray){ + tokens.add(token.trim()); + } + + datatypePropertyUri2TokensMap.put(uri, tokens); + } + } + + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public List<String> getClasses(String token){ + List<String> uris = new ArrayList<String>(); + for(Entry<String, List<String>> entry : classUri2TokensMap.entrySet()){ + if(entry.getValue().contains(token)){ + uris.add(entry.getKey()); + } + } + return uris; + } + + public List<String> getResources(String token){ + List<String> uris = new ArrayList<String>(); + for(Entry<String, List<String>> entry : resourceUri2TokensMap.entrySet()){ + if(entry.getValue().contains(token)){ + uris.add(entry.getKey()); + } + } + return uris; + } + + public List<String> getObjectProperties(String token){ + List<String> uris = new ArrayList<String>(); + for(Entry<String, List<String>> entry : objectPropertyUri2TokensMap.entrySet()){ + if(entry.getValue().contains(token)){ + uris.add(entry.getKey()); + } + } + return uris; + } + + public List<String> getDatatypeProperties(String token){ + List<String> uris = new ArrayList<String>(); + for(Entry<String, List<String>> entry : datatypePropertyUri2TokensMap.entrySet()){ + if(entry.getValue().contains(token)){ + uris.add(entry.getKey()); + } + } + return uris; + } + + public List<String> getProperties(String token){ + List<String> uris = new ArrayList<String>(); + uris.addAll(getObjectProperties(token)); + uris.addAll(getDatatypeProperties(token)); + return uris; + } + + public IndexResultSet getClassesWithScores(String token){ + IndexResultSet rs = new IndexResultSet(); + for(String uri : getClasses(token)){ + rs.addItem(new IndexResultItem(uri, token, 1f)); + } + return rs; + } + + public IndexResultSet getResourcesWithScores(String token){ + IndexResultSet rs = new IndexResultSet(); + for(String uri : getResources(token)){ + rs.addItem(new IndexResultItem(uri, token, 1f)); + } + return rs; + } + + public IndexResultSet getObjectPropertiesWithScores(String token){ + IndexResultSet rs = new IndexResultSet(); + for(String uri : getObjectProperties(token)){ + rs.addItem(new IndexResultItem(uri, token, 1f)); + } + return rs; + } + + public IndexResultSet getDatatypePropertiesWithScores(String token){ + IndexResultSet rs = new IndexResultSet(); + for(String uri : getDatatypeProperties(token)){ + rs.addItem(new IndexResultItem(uri, token, 1f)); + } + return rs; + } + + public IndexResultSet getPropertiesWithScores(String token){ + IndexResultSet rs = new IndexResultSet(); + for(String uri : getProperties(token)){ + rs.addItem(new IndexResultItem(uri, token, 1f)); + } + return rs; + } + + public Boolean isDataProperty(String uri){ + if(datatypePropertyUri2TokensMap.containsKey(uri)) { + return true; + } else if(objectPropertyUri2TokensMap.containsKey(uri)){ + return false; + } + return null; + } + + public static void main(String[] args) { + MappingBasedIndex index = new MappingBasedIndex(MappingBasedIndex.class.getClassLoader().getResource("tbsl/oxford_class_mappings.txt").getPath(), null, null, null); + System.out.println(index.getClasses("flat")); + } + +} Modified: trunk/components-ext/src/main/java/org/dllearner/common/index/SPARQLClassesIndex.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/common/index/SPARQLClassesIndex.java 2012-06-13 10:31:55 UTC (rev 3737) +++ trunk/components-ext/src/main/java/org/dllearner/common/index/SPARQLClassesIndex.java 2012-06-13 14:56:37 UTC (rev 3738) @@ -1,5 +1,6 @@ package org.dllearner.common.index; +import org.dllearner.kb.sparql.ExtractionDBCache; import org.dllearner.kb.sparql.SparqlEndpoint; import com.hp.hpl.jena.rdf.model.Model; @@ -8,23 +9,20 @@ public SPARQLClassesIndex(SparqlEndpoint endpoint) { super(endpoint); - - super.queryTemplate = "SELECT DISTINCT ?uri WHERE {\n" + - "?s a ?uri.\n" + - "?uri <http://www.w3.org/2000/01/rdf-schema#label> ?label.\n" + - "FILTER(REGEX(STR(?label), '%s'))}\n" + - "LIMIT %d OFFSET %d"; - - super.queryWithLabelTemplate = "SELECT DISTINCT * WHERE {\n" + - "?s a ?uri.\n" + - "?uri <http://www.w3.org/2000/01/rdf-schema#label> ?label\n" + - "FILTER(REGEX(STR(?label), '%s'))}\n" + - "LIMIT %d OFFSET %d"; + init(); } + public SPARQLClassesIndex(SparqlEndpoint endpoint, ExtractionDBCache cache) { + super(endpoint, cache); + init(); + } + public SPARQLClassesIndex(Model model) { super(model); - + init(); + } + + private void init(){ super.queryTemplate = "SELECT DISTINCT ?uri WHERE {\n" + "?s a ?uri.\n" + "?uri <http://www.w3.org/2000/01/rdf-schema#label> ?label\n" + Modified: trunk/components-ext/src/main/java/org/dllearner/common/index/SPARQLIndex.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/common/index/SPARQLIndex.java 2012-06-13 10:31:55 UTC (rev 3737) +++ trunk/components-ext/src/main/java/org/dllearner/common/index/SPARQLIndex.java 2012-06-13 14:56:37 UTC (rev 3738) @@ -60,6 +60,10 @@ this.cache = cache; } + public void setCache(ExtractionDBCache cache) { + this.cache = cache; + } + @Override public List<String> getResources(String searchTerm) { return getResources(searchTerm, DEFAULT_LIMIT); Modified: trunk/components-ext/src/main/java/org/dllearner/common/index/SPARQLPropertiesIndex.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/common/index/SPARQLPropertiesIndex.java 2012-06-13 10:31:55 UTC (rev 3737) +++ trunk/components-ext/src/main/java/org/dllearner/common/index/SPARQLPropertiesIndex.java 2012-06-13 14:56:37 UTC (rev 3738) @@ -1,5 +1,6 @@ package org.dllearner.common.index; +import org.dllearner.kb.sparql.ExtractionDBCache; import org.dllearner.kb.sparql.SparqlEndpoint; import com.hp.hpl.jena.rdf.model.Model; @@ -11,6 +12,11 @@ init(); } + public SPARQLPropertiesIndex(SparqlEndpoint endpoint, ExtractionDBCache cache) { + super(endpoint, cache); + init(); + } + public SPARQLPropertiesIndex(Model model) { super(model); init(); @@ -28,7 +34,7 @@ "FILTER(REGEX(STR(?label), '%s', 'i'))}\n" + "LIMIT %d OFFSET %d"; - super.queryWithLabelTemplate = "PREFIX owl:<http://www.w3.org/2002/07/owl#> SELECT DISTINCT * WHERE {\n" + + super.queryWithLabelTemplate = "PREFIX owl:<http://www.w3.org/2002/07/owl#> SELECT DISTINCT ?uri ?label WHERE {\n" + "?s ?uri ?o.\n" + // "{?uri a owl:DatatypeProperty.} UNION {?uri a owl:ObjectProperty.} " + "?uri <http://www.w3.org/2000/01/rdf-schema#label> ?label\n" + Modified: trunk/components-ext/src/main/resources/tbsl/lexicon/english_oxford.lex =================================================================== --- trunk/components-ext/src/main/resources/tbsl/lexicon/english_oxford.lex 2012-06-13 10:31:55 UTC (rev 3737) +++ trunk/components-ext/src/main/resources/tbsl/lexicon/english_oxford.lex 2012-06-13 14:56:37 UTC (rev 3738) @@ -2,7 +2,7 @@ // PREPOSITIONS close to || (NP NP* (PP P:'close' P:'to' DP[dp])) || <x,l1,<e,t>, [ l1:[ | SLOT_closeto(x,y) ] ], [ (l2,y,dp,<<e,t>,t>) ], [ l2=l1 ],[ SLOT_closeto/OBJECTPROPERTY/near ]> - in || (NP NP* (PP P:'in' DP[dp])) || <x,l1,<e,t>, [ l1:[ | SLOT_location(x,y) ] ], [ (l2,y,dp,<<e,t>,t>) ], [ l2=l1 ],[ SLOT_location/PROPERTY/location^city^postal_code ]> + in || (NP NP* (PP P:'in' DP[dp])) || <x,l1,<e,t>, [ l1:[ | SLOT_location(x,y) ] ], [ (l2,y,dp,<<e,t>,t>) ], [ l2=l1 ],[ SLOT_location/PROPERTY/location^city^postal_code^address ]> since || (NP NP* (PP P:'since' DP[dp])) || <x,l1,<e,t>, [ l1:[ | SLOT_since(x,y) ] ], [ (l2,y,dp,<<e,t>,t>) ], [ l2=l1 ],[ SLOT_since/PROPERTY/since ]> for . pounds || (NP NP* (PP P:'for' (NP NUM[num] N:'pounds'))) || <x,l1,<e,t>, [ l1:[ | SLOT_price(x,y) ] ], [ (l2,y,num,e) ], [ l2=l1 ],[ SLOT_price/DATATYPEPROPERTY/price ]> Added: trunk/components-ext/src/main/resources/tbsl/oxford_class_mappings.txt =================================================================== --- trunk/components-ext/src/main/resources/tbsl/oxford_class_mappings.txt (rev 0) +++ trunk/components-ext/src/main/resources/tbsl/oxford_class_mappings.txt 2012-06-13 14:56:37 UTC (rev 3738) @@ -0,0 +1 @@ +http://diadem.cs.ox.ac.uk/ontologies/real-estate#House|house, houses, flat, flats, appartement, appartements \ No newline at end of file Added: trunk/components-ext/src/main/resources/tbsl/oxford_dataproperty_mappings.txt =================================================================== --- trunk/components-ext/src/main/resources/tbsl/oxford_dataproperty_mappings.txt (rev 0) +++ trunk/components-ext/src/main/resources/tbsl/oxford_dataproperty_mappings.txt 2012-06-13 14:56:37 UTC (rev 3738) @@ -0,0 +1,2 @@ +http://www.w3.org/2006/vcard/ns#street-address|address, location, postal code +http://www.w3.org/2006/vcard/ns#locality|address, location \ No newline at end of file Added: trunk/components-ext/src/main/resources/tbsl/oxford_objectproperty_mappings.txt =================================================================== Added: trunk/components-ext/src/main/resources/tbsl/oxford_resource_mappings.txt =================================================================== Added: trunk/components-ext/src/test/java/org/dllearner/algorithm/tbsl/OxfordEvaluation.java =================================================================== --- trunk/components-ext/src/test/java/org/dllearner/algorithm/tbsl/OxfordEvaluation.java (rev 0) +++ trunk/components-ext/src/test/java/org/dllearner/algorithm/tbsl/OxfordEvaluation.java 2012-06-13 14:56:37 UTC (rev 3738) @@ -0,0 +1,78 @@ +package org.dllearner.algorithm.tbsl; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.net.URL; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + +import org.dllearner.algorithm.tbsl.learning.SPARQLTemplateBasedLearner2; +import org.dllearner.common.index.Index; +import org.dllearner.common.index.MappingBasedIndex; +import org.dllearner.common.index.SPARQLClassesIndex; +import org.dllearner.common.index.SPARQLIndex; +import org.dllearner.common.index.SPARQLPropertiesIndex; +import org.dllearner.kb.sparql.ExtractionDBCache; +import org.dllearner.kb.sparql.SparqlEndpoint; + +public class OxfordEvaluation { + + private static final String QUERIES_FILE = "/home/lorenz/evaluation.txt"; + + public static void main(String[] args) throws Exception{ + SparqlEndpoint endpoint = new SparqlEndpoint(new URL("http://lgd.aksw.org:8900/sparql"), Collections.singletonList("http://diadem.cs.ox.ac.uk"), Collections.<String>emptyList()); + ExtractionDBCache cache = new ExtractionDBCache("cache"); + + SPARQLIndex resourcesIndex = new SPARQLIndex(endpoint, cache); + SPARQLIndex classesIndex = new SPARQLClassesIndex(endpoint, cache); + SPARQLIndex propertiesIndex = new SPARQLPropertiesIndex(endpoint, cache); + MappingBasedIndex mappingIndex= new MappingBasedIndex( + OxfordEvaluation.class.getClassLoader().getResource("tbsl/oxford_class_mappings.txt").getPath(), + OxfordEvaluation.class.getClassLoader().getResource("tbsl/oxford_resource_mappings.txt").getPath(), + OxfordEvaluation.class.getClassLoader().getResource("tbsl/oxford_objectproperty_mappings.txt").getPath(), + OxfordEvaluation.class.getClassLoader().getResource("tbsl/oxford_dataproperty_mappings.txt").getPath()); + + SPARQLTemplateBasedLearner2 learner = new SPARQLTemplateBasedLearner2(endpoint, resourcesIndex, classesIndex, propertiesIndex); + learner.setMappingIndex(mappingIndex); + learner.init(); + + int learnedQuestions = 0; + Map<String, String> question2QueryMap = new HashMap<String, String>(); + + BufferedReader br = new BufferedReader(new FileReader(new File(QUERIES_FILE))); + + int questionNr = 0; + String question = null; + while((question = br.readLine()) != null){ + question = question.replace("question:", "").trim(); + if(question.isEmpty()) continue; + if(!question.toLowerCase().contains("Give me all") && Character.isLowerCase(question.charAt(0))){ + question = "Give me all " + question; + } + System.out.println("########################################################"); + questionNr++; + System.out.println(question); + try { + learner.setQuestion(question); + learner.learnSPARQLQueries(); + String learnedQuery = learner.getBestSPARQLQuery(); + if(learnedQuery != null){ + question2QueryMap.put(question, learnedQuery); + learnedQuestions++; + } + } catch (Exception e) { + e.printStackTrace(); + } + } + System.out.println("Generated SPARQL queries for " + learnedQuestions + " questions."); + for(Entry<String, String> entry : question2QueryMap.entrySet()){ + System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); + System.out.println(entry.getKey()); + System.out.println(entry.getValue()); + } + } + +} Modified: trunk/components-ext/src/test/java/org/dllearner/algorithm/tbsl/TBSLTest.java =================================================================== --- trunk/components-ext/src/test/java/org/dllearner/algorithm/tbsl/TBSLTest.java 2012-06-13 10:31:55 UTC (rev 3737) +++ trunk/components-ext/src/test/java/org/dllearner/algorithm/tbsl/TBSLTest.java 2012-06-13 14:56:37 UTC (rev 3738) @@ -44,6 +44,7 @@ model.read(new FileInputStream(f), null, "TURTLE"); } catch (Exception e) { System.err.println("Parsing failed."); + e.printStackTrace(); } } } @@ -83,7 +84,7 @@ SPARQLTemplateBasedLearner2 learner = new SPARQLTemplateBasedLearner2(model, resourcesIndex, classesIndex, propertiesIndex); learner.init(); - String question = "Give me all houses with more than 2 bedrooms."; + String question = "Give me all houses with more than 3 bathrooms and more than 2 bedrooms."; learner.setQuestion(question); learner.learnSPARQLQueries(); @@ -104,6 +105,7 @@ String question = "Give me all houses near a school."; question = "Give me all houses with more than 3 bathrooms and more than 2 bedrooms."; + question = "Give me all houses with large garden and equipped kitchen"; learner.setQuestion(question); learner.learnSPARQLQueries(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |