From: <lor...@us...> - 2012-05-03 14:42:43
|
Revision: 3683 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3683&view=rev Author: lorenz_b Date: 2012-05-03 14:42:31 +0000 (Thu, 03 May 2012) Log Message: ----------- Some improvements in algorithms. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/algorithms/DisjointClassesLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/SimpleSubclassLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyDomainAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointDataPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointObjectPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentDataPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentObjectPropertyAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyDomainAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyRangeAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubDataPropertyOfAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubObjectPropertyOfAxiomLearner.java trunk/components-core/src/main/java/org/dllearner/core/AbstractAxiomLearningAlgorithm.java trunk/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.java Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/DisjointClassesLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/DisjointClassesLearner.java 2012-05-03 14:31:56 UTC (rev 3682) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/DisjointClassesLearner.java 2012-05-03 14:42:31 UTC (rev 3683) @@ -210,7 +210,7 @@ private void runSPARQL1_1_Mode(){ int limit = 1000; int offset = 0; - String queryTemplate = "SELECT ?type (COUNT(?s) AS ?count) WHERE {?s a ?type." + + String queryTemplate = "PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT ?type (COUNT(?s) AS ?count) WHERE {?s a ?type. ?type a owl:Class" + "{SELECT ?s WHERE {?s a <%s>.} LIMIT %d OFFSET %d} " + "} GROUP BY ?type"; String query; @@ -220,7 +220,7 @@ boolean repeat = true; while(!terminationCriteriaSatisfied() && repeat){ - query = String.format(queryTemplate, classToDescribe, limit, offset); + query = String.format(queryTemplate, classToDescribe, limit, offset);System.out.println(query); ResultSet rs = executeSelectQuery(query); QuerySolution qs; repeat = false; @@ -350,8 +350,9 @@ } //secondly, create disjoint classexpressions with score 1 - (#occurence/#all) + NamedClass cls; for (Entry<NamedClass, Integer> entry : sortByValues(class2Count)) { - NamedClass cls = entry.getKey(); + cls = entry.getKey(); // drop classes from OWL and RDF namespace if (cls.getName().startsWith(OWL2.getURI()) || cls.getName().startsWith(RDF.getURI())) continue; @@ -386,15 +387,6 @@ return evalDescs; } - private double accuracy(int total, int success){ - double[] confidenceInterval = Heuristics.getConfidenceInterval95Wald(total, success); - return (confidenceInterval[0] + confidenceInterval[1]) / 2; - } - - private double fMEasure(double precision, double recall){ - return 2 * precision * recall / (precision + recall); - } - private void keepMostGeneralClasses(Set<NamedClass> classes){ if(ks.isRemote()){ if(reasoner.isPrepared()){ Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/SimpleSubclassLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/SimpleSubclassLearner.java 2012-05-03 14:31:56 UTC (rev 3682) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/SimpleSubclassLearner.java 2012-05-03 14:42:31 UTC (rev 3683) @@ -170,7 +170,7 @@ boolean notEmpty = false; String query; if(ks.supportsSPARQL_1_1()){ - query = String.format("SELECT DISTINCT ?ind ?type WHERE {?ind a ?type. {SELECT ?ind {?ind a <%s>} LIMIT %d OFFSET %d}}", classToDescribe.getName(), limit, offset); + query = String.format("PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT DISTINCT ?ind ?type WHERE {?ind a ?type.?type a owl:Class. {SELECT ?ind {?ind a <%s>} LIMIT %d OFFSET %d}}", classToDescribe.getName(), limit, offset); } else { query = String.format("SELECT DISTINCT ?ind ?type WHERE {?ind a <%s>. ?ind a ?type} LIMIT %d OFFSET %d", classToDescribe.getName(), limit, offset); } Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyDomainAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyDomainAxiomLearner.java 2012-05-03 14:31:56 UTC (rev 3682) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DataPropertyDomainAxiomLearner.java 2012-05-03 14:42:31 UTC (rev 3683) @@ -138,7 +138,7 @@ } private int addIndividualsWithTypes(Map<Individual, SortedSet<Description>> ind2Types, int limit, int offset){ - String query = String.format("SELECT DISTINCT ?ind ?type WHERE {?ind <%s> ?o. ?ind a ?type} LIMIT %d OFFSET %d", propertyToDescribe.getName(), limit, offset); + String query = String.format("PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT DISTINCT ?ind ?type WHERE {?ind <%s> ?o. ?ind a ?type. ?type a owl:Class} LIMIT %d OFFSET %d", propertyToDescribe.getName(), limit, offset); // String query = String.format("SELECT DISTINCT ?ind ?type WHERE {?ind a ?type. {SELECT ?ind {?ind <%s> ?o.} LIMIT %d OFFSET %d}}", propertyToDescribe.getName(), limit, offset); Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointDataPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointDataPropertyAxiomLearner.java 2012-05-03 14:31:56 UTC (rev 3682) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointDataPropertyAxiomLearner.java 2012-05-03 14:42:31 UTC (rev 3683) @@ -25,6 +25,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import java.util.TreeSet; import org.dllearner.core.AbstractAxiomLearningAlgorithm; import org.dllearner.core.ComponentAnn; @@ -33,6 +34,7 @@ import org.dllearner.core.config.DataPropertyEditor; import org.dllearner.core.owl.DatatypeProperty; import org.dllearner.core.owl.DisjointDatatypePropertyAxiom; +import org.dllearner.kb.LocalModelBasedSparqlEndpointKS; import org.dllearner.kb.SparqlEndpointKS; import org.dllearner.kb.sparql.SPARQLTasks; import org.dllearner.kb.sparql.SparqlEndpoint; @@ -44,6 +46,7 @@ import com.hp.hpl.jena.query.ResultSet; import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.ModelFactory; +import com.hp.hpl.jena.rdf.model.RDFNode; @ComponentAnn(name="disjoint dataproperty axiom learner", shortName="dpldisjoint", version=0.1) public class DisjointDataPropertyAxiomLearner extends AbstractAxiomLearningAlgorithm { @@ -55,6 +58,10 @@ private Set<DatatypeProperty> allDataProperties; + private boolean usePropertyPopularity = true; + + private int popularity; + public DisjointDataPropertyAxiomLearner(SparqlEndpointKS ks){ this.ks = ks; } @@ -74,8 +81,14 @@ fetchedRows = 0; currentlyBestAxioms = new ArrayList<EvaluatedAxiom>(); - //TODO + // we return here if the class contains no instances + popularity = reasoner.getPopularity(propertyToDescribe); + if (popularity == 0) { + return; + } + //TODO detect existing axioms + //at first get all existing dataproperties in knowledgebase allDataProperties = new SPARQLTasks(ks.getEndpoint()).getAllDataProperties(); allDataProperties.remove(propertyToDescribe); @@ -132,7 +145,7 @@ //get properties and how often they occur int limit = 1000; int offset = 0; - String queryTemplate = "SELECT ?p (COUNT(?s) as ?count) WHERE {?s ?p ?o." + + String queryTemplate = "PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT ?p (COUNT(?s) as ?count) WHERE {?p a owl:DatatypeProperty. ?s ?p ?o." + "{SELECT ?s ?o WHERE {?s <%s> ?o.} LIMIT %d OFFSET %d}" + "}"; String query; @@ -178,19 +191,62 @@ Integer all = property2Count.get(propertyToDescribe); property2Count.remove(propertyToDescribe); + //get complete disjoint properties + Set<DatatypeProperty> completeDisjointProperties = new TreeSet<DatatypeProperty>(allProperties); + completeDisjointProperties.removeAll(property2Count.keySet()); + EvaluatedAxiom evalAxiom; //first create disjoint axioms with properties which not occur and give score of 1 - for(DatatypeProperty p : allProperties){ - evalAxiom = new EvaluatedAxiom(new DisjointDatatypePropertyAxiom(propertyToDescribe, p), - new AxiomScore(1)); + for(DatatypeProperty p : completeDisjointProperties){ + if(usePropertyPopularity){ + int overlap = 0; + int pop; + if(ks.isRemote()){ + pop = reasoner.getPopularity(p); + } else { + Model model = ((LocalModelBasedSparqlEndpointKS)ks).getModel(); + pop = model.listStatements(null, model.getProperty(p.getName()), (RDFNode)null).toSet().size(); + } + //we skip classes with no instances + if(pop == 0) continue; + + //we compute the estimated precision + double precision = accuracy(pop, overlap); + //we compute the estimated recall + double recall = accuracy(popularity, overlap); + //compute the overall score + double score = 1 - fMEasure(precision, recall); + + evalAxiom = new EvaluatedAxiom(new DisjointDatatypePropertyAxiom(propertyToDescribe, p), new AxiomScore(score)); + } else { + evalAxiom = new EvaluatedAxiom(new DisjointDatatypePropertyAxiom(propertyToDescribe, p), new AxiomScore(1)); + } axioms.add(evalAxiom); } //second create disjoint axioms with other properties and score 1 - (#occurence/#all) + DatatypeProperty p; for(Entry<DatatypeProperty, Integer> entry : sortByValues(property2Count)){ - evalAxiom = new EvaluatedAxiom(new DisjointDatatypePropertyAxiom(propertyToDescribe, entry.getKey()), - new AxiomScore(1 - (entry.getValue() / (double)all))); - axioms.add(evalAxiom); + p = entry.getKey(); + int overlap = entry.getValue(); + int pop; + if(ks.isRemote()){ + pop = reasoner.getPopularity(p); + } else { + Model model = ((LocalModelBasedSparqlEndpointKS)ks).getModel(); + pop = model.listStatements(null, model.getProperty(p.getName()), (RDFNode)null).toSet().size(); + } + //we skip classes with no instances + if(pop == 0) continue; + + //we compute the estimated precision + double precision = accuracy(pop, overlap); + //we compute the estimated recall + double recall = accuracy(popularity, overlap); + //compute the overall score + double score = 1 - fMEasure(precision, recall); + + evalAxiom = new EvaluatedAxiom(new DisjointDatatypePropertyAxiom(propertyToDescribe, p), new AxiomScore(score)); } property2Count.put(propertyToDescribe, all); @@ -202,6 +258,7 @@ l.setPropertyToDescribe(new DatatypeProperty("http://dbpedia.org/ontology/position")); l.setMaxExecutionTimeInSeconds(20); l.init(); + l.getReasoner().precomputeDataPropertyPopularity(); l.start(); System.out.println(l.getCurrentlyBestEvaluatedAxioms(5)); } Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointObjectPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointObjectPropertyAxiomLearner.java 2012-05-03 14:31:56 UTC (rev 3682) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/DisjointObjectPropertyAxiomLearner.java 2012-05-03 14:42:31 UTC (rev 3683) @@ -19,9 +19,7 @@ package org.dllearner.algorithms.properties; -import java.net.URL; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -32,17 +30,15 @@ import org.dllearner.core.AbstractAxiomLearningAlgorithm; import org.dllearner.core.ComponentAnn; import org.dllearner.core.EvaluatedAxiom; -import org.dllearner.core.EvaluatedDescription; import org.dllearner.core.config.ConfigOption; import org.dllearner.core.config.ObjectPropertyEditor; import org.dllearner.core.owl.DisjointObjectPropertyAxiom; -import org.dllearner.core.owl.FunctionalObjectPropertyAxiom; import org.dllearner.core.owl.ObjectProperty; +import org.dllearner.kb.LocalModelBasedSparqlEndpointKS; import org.dllearner.kb.SparqlEndpointKS; import org.dllearner.kb.sparql.SPARQLTasks; import org.dllearner.kb.sparql.SparqlEndpoint; import org.dllearner.learningproblems.AxiomScore; -import org.dllearner.learningproblems.Heuristics; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -50,6 +46,7 @@ import com.hp.hpl.jena.query.ResultSet; import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.ModelFactory; +import com.hp.hpl.jena.rdf.model.RDFNode; @ComponentAnn(name="disjoint objectproperty axiom learner", shortName="opldisjoint", version=0.1) public class DisjointObjectPropertyAxiomLearner extends AbstractAxiomLearningAlgorithm { @@ -63,6 +60,8 @@ private boolean usePropertyPopularity = true; + private int popularity; + public DisjointObjectPropertyAxiomLearner(SparqlEndpointKS ks){ this.ks = ks; } @@ -82,9 +81,16 @@ fetchedRows = 0; currentlyBestAxioms = new ArrayList<EvaluatedAxiom>(); + //we return here if the class contains no instances + popularity = reasoner.getPopularity(propertyToDescribe); + if(popularity == 0){ + return; + } + //TODO detect existing axioms - //at first get all existing objectproperties in knowledgebase + + //at first get all existing objectproperties in knowledge base allObjectProperties = new SPARQLTasks(ks.getEndpoint()).getAllObjectProperties(); allObjectProperties.remove(propertyToDescribe); @@ -139,7 +145,7 @@ private void runSPARQL1_1_Mode() { //get properties and how often they occur int offset = 0; - String queryTemplate = "SELECT ?p COUNT(?s) AS ?count WHERE {?s ?p ?o." + + String queryTemplate = "PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT ?p COUNT(?s) AS ?count WHERE {?p a owl:ObjectProperty. ?s ?p ?o." + "{SELECT ?s ?o WHERE {?s <%s> ?o.} LIMIT %d OFFSET %d}" + "}"; String query; @@ -187,12 +193,25 @@ //first create disjoint axioms with properties which not occur and give score of 1 for(ObjectProperty p : completeDisjointProperties){ if(usePropertyPopularity){ - int popularity = reasoner.getPropertyCount(p); - //skip if property is not used in kb - if(popularity == 0) continue; - double[] confidenceInterval = Heuristics.getConfidenceInterval95Wald(popularity, 0); - double accuracy = (confidenceInterval[0] + confidenceInterval[1]) / 2; - evalAxiom = new EvaluatedAxiom(new DisjointObjectPropertyAxiom(propertyToDescribe, p), new AxiomScore(1- accuracy)); + int overlap = 0; + int pop; + if(ks.isRemote()){ + pop = reasoner.getPopularity(p); + } else { + Model model = ((LocalModelBasedSparqlEndpointKS)ks).getModel(); + pop = model.listStatements(null, model.getProperty(p.getName()), (RDFNode)null).toSet().size(); + } + //we skip classes with no instances + if(pop == 0) continue; + + //we compute the estimated precision + double precision = accuracy(pop, overlap); + //we compute the estimated recall + double recall = accuracy(popularity, overlap); + //compute the overall score + double score = 1 - fMEasure(precision, recall); + + evalAxiom = new EvaluatedAxiom(new DisjointObjectPropertyAxiom(propertyToDescribe, p), new AxiomScore(score)); } else { evalAxiom = new EvaluatedAxiom(new DisjointObjectPropertyAxiom(propertyToDescribe, p), new AxiomScore(1)); } @@ -200,12 +219,28 @@ } //second create disjoint axioms with other properties and score 1 - (#occurence/#all) + ObjectProperty p; for(Entry<ObjectProperty, Integer> entry : sortByValues(property2Count)){ - double[] confidenceInterval = Heuristics.getConfidenceInterval95Wald(all, entry.getValue()); - double accuracy = (confidenceInterval[0] + confidenceInterval[1]) / 2;//System.out.println(entry + ": " + accuracy); - evalAxiom = new EvaluatedAxiom(new DisjointObjectPropertyAxiom(propertyToDescribe, entry.getKey()), - new AxiomScore(1 - accuracy)); - axioms.add(evalAxiom); + p = entry.getKey(); + int overlap = entry.getValue(); + int pop; + if(ks.isRemote()){ + pop = reasoner.getPopularity(p); + } else { + Model model = ((LocalModelBasedSparqlEndpointKS)ks).getModel(); + pop = model.listStatements(null, model.getProperty(p.getName()), (RDFNode)null).toSet().size(); + } + //we skip classes with no instances + if(pop == 0) continue; + + //we compute the estimated precision + double precision = accuracy(pop, overlap); + //we compute the estimated recall + double recall = accuracy(popularity, overlap); + //compute the overall score + double score = 1 - fMEasure(precision, recall); + + evalAxiom = new EvaluatedAxiom(new DisjointObjectPropertyAxiom(propertyToDescribe, p), new AxiomScore(score)); } property2Count.put(propertyToDescribe, all); @@ -216,9 +251,10 @@ SparqlEndpoint endpoint = SparqlEndpoint.getEndpointDBpedia(); // endpoint = new SparqlEndpoint(new URL("http://dbpedia.aksw.org:8902/sparql"), Collections.singletonList("http://dbpedia.org"), Collections.<String>emptyList())); DisjointObjectPropertyAxiomLearner l = new DisjointObjectPropertyAxiomLearner(new SparqlEndpointKS(endpoint));//.getEndpointDBpediaLiveAKSW())); - l.setPropertyToDescribe(new ObjectProperty("http://dbpedia.org/ontology/stateOfOrigin")); + l.setPropertyToDescribe(new ObjectProperty("http://dbpedia.org/ontology/birthPlace")); l.setMaxExecutionTimeInSeconds(10); l.init(); + l.getReasoner().precomputeObjectPropertyPopularity(); l.start(); for(EvaluatedAxiom ax : l.getCurrentlyBestEvaluatedAxioms(Integer.MAX_VALUE)){ System.out.println(ax); Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentDataPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentDataPropertyAxiomLearner.java 2012-05-03 14:31:56 UTC (rev 3682) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentDataPropertyAxiomLearner.java 2012-05-03 14:42:31 UTC (rev 3683) @@ -126,7 +126,7 @@ // get subjects with types int limit = 1000; int offset = 0; - String queryTemplate = "SELECT ?p COUNT(?s) AS ?count WHERE {?s ?p ?o." + String queryTemplate = "PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT ?p COUNT(?s) AS ?count WHERE {?s ?p ?o.?p a owl:DatatypeProperty." + "{SELECT ?s ?o WHERE {?s <%s> ?o.} LIMIT %d OFFSET %d}" + "}"; String query; Map<DatatypeProperty, Integer> result = new HashMap<DatatypeProperty, Integer>(); Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentObjectPropertyAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentObjectPropertyAxiomLearner.java 2012-05-03 14:31:56 UTC (rev 3682) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/EquivalentObjectPropertyAxiomLearner.java 2012-05-03 14:42:31 UTC (rev 3683) @@ -129,7 +129,7 @@ private void runSPARQL1_1_Mode() { //get subjects with types int offset = 0; - String queryTemplate = "SELECT ?p (COUNT(?s) AS ?count) WHERE {?s ?p ?o." + + String queryTemplate = "PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT ?p (COUNT(?s) AS ?count) WHERE {?s ?p ?o.?p a owl:ObjectProperty." + "{SELECT ?s ?o WHERE {?s <%s> ?o.} LIMIT %d OFFSET %d}" + "}"; String query; Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyDomainAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyDomainAxiomLearner.java 2012-05-03 14:31:56 UTC (rev 3682) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyDomainAxiomLearner.java 2012-05-03 14:42:31 UTC (rev 3683) @@ -139,7 +139,7 @@ } private int addIndividualsWithTypes(Map<Individual, SortedSet<Description>> ind2Types, int limit, int offset){ - String query = String.format("SELECT DISTINCT ?ind ?type WHERE {?ind <%s> ?o. ?ind a ?type} LIMIT %d OFFSET %d", propertyToDescribe.getName(), limit, offset); + String query = String.format("PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT DISTINCT ?ind ?type WHERE {?ind <%s> ?o. ?ind a ?type. ?type a owl:Class} LIMIT %d OFFSET %d", propertyToDescribe.getName(), limit, offset); // String query = String.format("SELECT DISTINCT ?ind ?type WHERE {?ind a ?type. {SELECT ?ind {?ind <%s> ?o.} LIMIT %d OFFSET %d}}", propertyToDescribe.getName(), limit, offset); @@ -176,7 +176,7 @@ } public static void main(String[] args) throws Exception{ - SparqlEndpointKS ks = new SparqlEndpointKS(new SparqlEndpoint(new URL("http://dbpedia.aksw.org:8902/sparql")));//.getEndpointDBpediaLiveAKSW())); + SparqlEndpointKS ks = new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpedia()); SPARQLReasoner reasoner = new SPARQLReasoner(ks); reasoner.prepareSubsumptionHierarchy(); @@ -184,7 +184,7 @@ ObjectPropertyDomainAxiomLearner l = new ObjectPropertyDomainAxiomLearner(ks); l.setReasoner(reasoner); - l.setPropertyToDescribe(new ObjectProperty("http://dbpedia.org/ontology/officialLanguage")); + l.setPropertyToDescribe(new ObjectProperty("http://dbpedia.org/ontology/Automobile/fuelCapacity")); l.setMaxExecutionTimeInSeconds(10); // l.setReturnOnlyNewAxioms(true); l.init(); Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyRangeAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyRangeAxiomLearner.java 2012-05-03 14:31:56 UTC (rev 3682) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/ObjectPropertyRangeAxiomLearner.java 2012-05-03 14:42:31 UTC (rev 3683) @@ -77,13 +77,13 @@ if(reasoner.isPrepared()){ //get existing ranges - Description existingDomain = reasoner.getRange(propertyToDescribe); - if(existingDomain != null){ - existingAxioms.add(new ObjectPropertyRangeAxiom(propertyToDescribe, existingDomain)); + Description existingRange = reasoner.getRange(propertyToDescribe); + if(existingRange != null){ + existingAxioms.add(new ObjectPropertyRangeAxiom(propertyToDescribe, existingRange)); if(reasoner.isPrepared()){ - if(reasoner.getClassHierarchy().contains(existingDomain)){ - for(Description sup : reasoner.getClassHierarchy().getSuperClasses(existingDomain)){ - existingAxioms.add(new ObjectPropertyRangeAxiom(propertyToDescribe, existingDomain)); + if(reasoner.getClassHierarchy().contains(existingRange)){ + for(Description sup : reasoner.getClassHierarchy().getSuperClasses(existingRange)){ + existingAxioms.add(new ObjectPropertyRangeAxiom(propertyToDescribe, existingRange)); logger.info("Existing range(inferred): " + sup); } } @@ -138,7 +138,7 @@ } private int addIndividualsWithTypes(Map<Individual, SortedSet<Description>> ind2Types, int limit, int offset){ - String query = String.format("SELECT DISTINCT ?ind ?type WHERE {?s <%s> ?ind. ?ind a ?type.} LIMIT %d OFFSET %d", propertyToDescribe.getName(), limit, offset); + String query = String.format("PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT DISTINCT ?ind ?type WHERE {?s <%s> ?ind. ?ind a ?type. ?type a owl:Class} LIMIT %d OFFSET %d", propertyToDescribe.getName(), limit, offset); // String query = String.format("SELECT DISTINCT ?ind ?type WHERE {?ind a ?type. {SELECT ?ind {?ind <%s> ?o.} LIMIT %d OFFSET %d}}", propertyToDescribe.getName(), limit, offset); @@ -179,9 +179,9 @@ ObjectPropertyRangeAxiomLearner l = new ObjectPropertyRangeAxiomLearner(ks); l.setReasoner(reasoner); - l.setPropertyToDescribe(new ObjectProperty("http://dbpedia.org/ontology/author")); + l.setPropertyToDescribe(new ObjectProperty("http://dbpedia.org/ontology/routeTypeAbbreviation")); l.setMaxExecutionTimeInSeconds(10); - l.setReturnOnlyNewAxioms(true); +// l.setReturnOnlyNewAxioms(true); l.init(); l.start(); Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubDataPropertyOfAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubDataPropertyOfAxiomLearner.java 2012-05-03 14:31:56 UTC (rev 3682) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubDataPropertyOfAxiomLearner.java 2012-05-03 14:42:31 UTC (rev 3683) @@ -125,7 +125,7 @@ //get subjects with types int limit = 1000; int offset = 0; - String queryTemplate = "SELECT ?p COUNT(?s) AS ?count WHERE {?s ?p ?o." + + String queryTemplate = "PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT ?p COUNT(?s) AS ?count WHERE {?s ?p ?o.?p a owl:DatatypeProperty." + "{SELECT ?s ?o WHERE {?s <%s> ?o.} LIMIT %d OFFSET %d}" + "}"; String query; Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubObjectPropertyOfAxiomLearner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubObjectPropertyOfAxiomLearner.java 2012-05-03 14:31:56 UTC (rev 3682) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/properties/SubObjectPropertyOfAxiomLearner.java 2012-05-03 14:42:31 UTC (rev 3683) @@ -125,7 +125,7 @@ //get subjects with types int limit = 1000; int offset = 0; - String queryTemplate = "SELECT ?p COUNT(?s) AS ?count WHERE {?s ?p ?o." + + String queryTemplate = "PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT ?p COUNT(?s) AS ?count WHERE {?s ?p ?o.?p a owl:ObjectProperty." + "{SELECT ?s ?o WHERE {?s <%s> ?o.} LIMIT %d OFFSET %d}" + "}"; String query; @@ -135,7 +135,7 @@ boolean repeat = true; while(!terminationCriteriaSatisfied() && repeat){ - query = String.format(queryTemplate, propertyToDescribe, limit, offset); + query = String.format(queryTemplate, propertyToDescribe, limit, offset);System.out.println(query); ResultSet rs = executeSelectQuery(query); QuerySolution qs; repeat = false; @@ -177,7 +177,7 @@ } public static void main(String[] args) throws Exception{ - SubObjectPropertyOfAxiomLearner l = new SubObjectPropertyOfAxiomLearner(new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpediaLiveOpenLink())); + SubObjectPropertyOfAxiomLearner l = new SubObjectPropertyOfAxiomLearner(new SparqlEndpointKS(SparqlEndpoint.getEndpointDBpedia())); l.setPropertyToDescribe(new ObjectProperty("http://dbpedia.org/ontology/writer")); l.setMaxExecutionTimeInSeconds(10); l.init(); Modified: trunk/components-core/src/main/java/org/dllearner/core/AbstractAxiomLearningAlgorithm.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/core/AbstractAxiomLearningAlgorithm.java 2012-05-03 14:31:56 UTC (rev 3682) +++ trunk/components-core/src/main/java/org/dllearner/core/AbstractAxiomLearningAlgorithm.java 2012-05-03 14:42:31 UTC (rev 3683) @@ -227,7 +227,7 @@ } protected Model executeConstructQuery(String query) { - logger.info("Sending query\n{} ...", query); + logger.debug("Sending query\n{} ...", query); if(ks.isRemote()){ SparqlEndpoint endpoint = ((SparqlEndpointKS) ks).getEndpoint(); QueryEngineHTTP queryExecution = new QueryEngineHTTP(endpoint.getURL().toString(), @@ -241,7 +241,7 @@ return model; } catch (QueryExceptionHTTP e) { if(e.getCause() instanceof SocketTimeoutException){ - logger.warn("Got timeout", e); + logger.warn("Got timeout"); } else { logger.error("Exception executing query", e); } @@ -254,7 +254,7 @@ } protected ResultSet executeSelectQuery(String query) { - logger.info("Sending query\n{} ...", query); + logger.debug("Sending query\n{} ...", query); if(ks.isRemote()){ SparqlEndpoint endpoint = ((SparqlEndpointKS) ks).getEndpoint(); QueryEngineHTTP queryExecution = new QueryEngineHTTP(endpoint.getURL().toString(), @@ -268,7 +268,7 @@ return rs; } catch (QueryExceptionHTTP e) { if(e.getCause() instanceof SocketTimeoutException){ - logger.warn("Got timeout", e); + logger.warn("Got timeout"); } else { logger.error("Exception executing query", e); } @@ -280,7 +280,7 @@ } protected ResultSet executeSelectQuery(String query, Model model) { - logger.info("Sending query on local model\n{} ...", query); + logger.debug("Sending query on local model\n{} ...", query); QueryExecution qexec = QueryExecutionFactory.create(query, model); ResultSet rs = qexec.execSelect();; @@ -366,6 +366,15 @@ return new AxiomScore(accuracy, confidence); } + protected double accuracy(int total, int success){ + double[] confidenceInterval = Heuristics.getConfidenceInterval95Wald(total, success); + return (confidenceInterval[0] + confidenceInterval[1]) / 2; + } + + protected double fMEasure(double precision, double recall){ + return 2 * precision * recall / (precision + recall); + } + class OWLFilter extends Filter<OntClass>{ @Override Modified: trunk/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.java 2012-05-03 14:31:56 UTC (rev 3682) +++ trunk/components-core/src/main/java/org/dllearner/reasoning/SPARQLReasoner.java 2012-05-03 14:42:31 UTC (rev 3683) @@ -94,6 +94,8 @@ private OntModel model; private Map<NamedClass, Integer> classPopularityMap; + private Map<ObjectProperty, Integer> objectPropertyPopularityMap; + private Map<DatatypeProperty, Integer> dataPropertyPopularityMap; public SPARQLReasoner(SparqlEndpointKS ks) { @@ -113,6 +115,12 @@ this.model = model; } + public void precomputePopularity(){ + precomputeClassPopularity(); + precomputeDataPropertyPopularity(); + precomputeObjectPropertyPopularity(); + } + public void precomputeClassPopularity(){ logger.info("Precomputing class popularity ..."); classPopularityMap = new HashMap<NamedClass, Integer>(); @@ -128,6 +136,36 @@ } } + public void precomputeObjectPropertyPopularity(){ + logger.info("Precomputing object property popularity ..."); + objectPropertyPopularityMap = new HashMap<ObjectProperty, Integer>(); + + Set<ObjectProperty> properties = new SPARQLTasks(ks.getEndpoint()).getAllObjectProperties(); + String queryTemplate = "SELECT (COUNT(*) AS ?cnt) WHERE {?s <%s> ?o}"; + + ResultSet rs; + for(ObjectProperty op : properties){ + rs = executeSelectQuery(String.format(queryTemplate, op.getName())); + int cnt = rs.next().getLiteral("cnt").getInt(); + objectPropertyPopularityMap.put(op, cnt); + } + } + + public void precomputeDataPropertyPopularity(){ + logger.info("Precomputing data property popularity ..."); + dataPropertyPopularityMap = new HashMap<DatatypeProperty, Integer>(); + + Set<DatatypeProperty> properties = new SPARQLTasks(ks.getEndpoint()).getAllDataProperties(); + String queryTemplate = "SELECT (COUNT(*) AS ?cnt) WHERE {?s <%s> ?o}"; + + ResultSet rs; + for(DatatypeProperty dp : properties){ + rs = executeSelectQuery(String.format(queryTemplate, dp.getName())); + int cnt = rs.next().getLiteral("cnt").getInt(); + dataPropertyPopularityMap.put(dp, cnt); + } + } + public int getPopularity(NamedClass nc){ if(classPopularityMap.containsKey(nc)){ return classPopularityMap.get(nc); @@ -143,6 +181,36 @@ } + public int getPopularity(ObjectProperty op){ + if(objectPropertyPopularityMap.containsKey(op)){ + return objectPropertyPopularityMap.get(op); + } else { + System.out.println("Cache miss: " + op); + String queryTemplate = "SELECT (COUNT(*) AS ?cnt) WHERE {?s <%s> ?o}"; + + ResultSet rs = executeSelectQuery(String.format(queryTemplate, op.getName())); + int cnt = rs.next().getLiteral("cnt").getInt(); + objectPropertyPopularityMap.put(op, cnt); + return cnt; + } + + } + + public int getPopularity(DatatypeProperty dp){ + if(dataPropertyPopularityMap.containsKey(dp)){ + return dataPropertyPopularityMap.get(dp); + } else { + System.out.println("Cache miss: " + dp); + String queryTemplate = "SELECT (COUNT(*) AS ?cnt) WHERE {?s <%s> ?o}"; + + ResultSet rs = executeSelectQuery(String.format(queryTemplate, dp.getName())); + int cnt = rs.next().getLiteral("cnt").getInt(); + dataPropertyPopularityMap.put(dp, cnt); + return cnt; + } + + } + public final ClassHierarchy prepareSubsumptionHierarchy() { logger.info("Preparing subsumption hierarchy ..."); long startTime = System.currentTimeMillis(); @@ -252,7 +320,7 @@ propertyCharacteristics.add(OWL2.AsymmetricProperty); for(Resource propChar : propertyCharacteristics){ - query = "CONSTRUCT {?s a <%s>} WHERE {?s a <%s>}".replaceAll("%s", propChar.getURI()); + query = "CONSTRUCT {?s a <%s>. ?s a <http://www.w3.org/2002/07/owl#ObjectProperty>} WHERE {?s a <%s>.}".replaceAll("%s", propChar.getURI()); model.add(loadIncrementally(query)); } //for functional properties we have to distinguish between data and object properties, @@ -914,7 +982,7 @@ } private ResultSet executeSelectQuery(String query){ - logger.info("Sending query \n {}", query); + logger.debug("Sending query \n {}", query); ResultSet rs = null; if(ks.isRemote()){ if(useCache){ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |