From: <jen...@us...> - 2011-08-17 19:24:02
|
Revision: 3063 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3063&view=rev Author: jenslehmann Date: 2011-08-17 18:38:28 +0000 (Wed, 17 Aug 2011) Log Message: ----------- implemented new simple negative example finder for SPARQL and integrated it in enrichment script Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/core/ComponentManager.java trunk/components-core/src/main/java/org/dllearner/kb/sparql/SPARQLTasks.java trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java Added Paths: ----------- trunk/components-core/src/main/java/org/dllearner/utilities/examples/AutomaticNegativeExampleFinderSPARQL2.java Modified: trunk/components-core/src/main/java/org/dllearner/core/ComponentManager.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/core/ComponentManager.java 2011-08-17 14:34:36 UTC (rev 3062) +++ trunk/components-core/src/main/java/org/dllearner/core/ComponentManager.java 2011-08-17 18:38:28 UTC (rev 3063) @@ -100,7 +100,7 @@ "org.dllearner.kb.KBFile", "org.dllearner.kb.sparql.SparqlKnowledgeSource", "org.dllearner.kb.OWLAPIOntology", - "org.dllearner.kb.SparqlEndpointKS", +// "org.dllearner.kb.SparqlEndpointKS", //reasoners "org.dllearner.reasoning.OWLAPIReasoner", "org.dllearner.reasoning.fuzzydll.FuzzyOWLAPIReasoner", // added by Josue Modified: trunk/components-core/src/main/java/org/dllearner/kb/sparql/SPARQLTasks.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/kb/sparql/SPARQLTasks.java 2011-08-17 14:34:36 UTC (rev 3062) +++ trunk/components-core/src/main/java/org/dllearner/kb/sparql/SPARQLTasks.java 2011-08-17 18:38:28 UTC (rev 3063) @@ -1,5 +1,5 @@ /** - * Copyright (C) 2007-2008, Jens Lehmann + * Copyright (C) 2007-2011, Jens Lehmann * * This file is part of DL-Learner. * @@ -38,12 +38,14 @@ import com.hp.hpl.jena.query.ResultSetFactory; import com.hp.hpl.jena.query.ResultSetFormatter; import com.hp.hpl.jena.query.ResultSetRewindable; -import com.hp.hpl.jena.sparql.core.ResultBinding; /** - * @author Sebastian Hellmann Convenience class for SPARQL queries initialized + * Convenience class for SPARQL queries initialized * with a SparqlEndpoint. A Cache can also be used to further improve * query time. Some methods allow basic reasoning + * + * @author Sebastian Hellmann + * @author Jens Lehmann */ public class SPARQLTasks { @@ -93,6 +95,16 @@ // TODO check for quotes in uris return getRecursiveSuperOrSubClasses(classURI, maxDepth, false); } + + public SortedSet<String> getParallelClasses(String classURI, int limit) { + String query = "SELECT ?sub WHERE { <" + classURI + "> <http://www.w3.org/2000/01/rdf-schema#subClassOf> ?super ."; + query += "?sub <http://www.w3.org/2000/01/rdf-schema#subClassOf> ?super ."; + query += "FILTER( ?sub != <" + classURI + ">) . } LIMIT " + limit; + return queryAsSet(query, "?sub"); +// SparqlQuery sq = new SparqlQuery(query, sparqlEndpoint); +// ResultSet rs = sq.send(); + + } /** * This is the underlying function to get Super and SubClasses. Added: trunk/components-core/src/main/java/org/dllearner/utilities/examples/AutomaticNegativeExampleFinderSPARQL2.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/utilities/examples/AutomaticNegativeExampleFinderSPARQL2.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/utilities/examples/AutomaticNegativeExampleFinderSPARQL2.java 2011-08-17 18:38:28 UTC (rev 3063) @@ -0,0 +1,85 @@ +/** + * Copyright (C) 2007-2011, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.utilities.examples; + +import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; + +import org.dllearner.core.owl.NamedClass; +import org.dllearner.kb.SparqlEndpointKS; +import org.dllearner.kb.sparql.SPARQLTasks; +import org.dllearner.kb.sparql.SparqlEndpoint; +import org.dllearner.reasoning.SPARQLReasoner; +import org.dllearner.utilities.datastructures.Datastructures; + +/** + * + * Utility class for automatically retrieving negative examples from a + * SPARQL endpoint given a set of positive examples. + * + * @author Jens Lehmann + * @author Sebastian Hellmann + * + */ +public class AutomaticNegativeExampleFinderSPARQL2 { + + private SparqlEndpoint se; + + // for re-using existing queries + private SPARQLReasoner sr; + private SPARQLTasks st; + + public AutomaticNegativeExampleFinderSPARQL2(SparqlEndpoint se) { + this.se = se; + SparqlEndpointKS ks = new SparqlEndpointKS(se); + sr = new SPARQLReasoner(ks); + st = new SPARQLTasks(se); + } + + /** + * Get negative examples when learning the description of a class, i.e. + * all positives are from some known class. + * + * Currently, the method implementation is preliminary and does not allow + * to configure internals. + * + * @param classURI The known class of all positive examples. + * @param positiveExamples The existing positive examples. + */ + public SortedSet<String> getNegativeExamples(String classURI, SortedSet<String> positiveExamples) { + // get some individuals from parallel classes (we perform one query per class to avoid + // only getting individuals from a single class) + Set<String> parallelClasses = st.getParallelClasses(classURI, 5); // TODO: limit could be configurable + SortedSet<String> negEx = new TreeSet<String>(); + for(String parallelClass : parallelClasses) { + Set<String> inds = Datastructures.individualSetToStringSet(sr.getIndividuals(new NamedClass(parallelClass), 10)); + negEx.addAll(inds); + if(negEx.size()>100) { + return negEx; + } + } + // add some random instances + String query = "SELECT ?inst { ?inst <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?x } LIMIT 20"; + negEx.addAll(st.queryAsSet(query, "?inst")); + return negEx; + } + +} Modified: trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java 2011-08-17 14:34:36 UTC (rev 3062) +++ trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java 2011-08-17 18:38:28 UTC (rev 3063) @@ -36,6 +36,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Map.Entry; +import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; @@ -99,8 +100,10 @@ import org.dllearner.utilities.EnrichmentVocabulary; import org.dllearner.utilities.Helper; import org.dllearner.utilities.datastructures.Datastructures; +import org.dllearner.utilities.datastructures.SetManipulation; import org.dllearner.utilities.datastructures.SortedSetTuple; import org.dllearner.utilities.examples.AutomaticNegativeExampleFinderSPARQL; +import org.dllearner.utilities.examples.AutomaticNegativeExampleFinderSPARQL2; import org.semanticweb.owlapi.apibinding.OWLManager; import org.semanticweb.owlapi.io.SystemOutDocumentTarget; import org.semanticweb.owlapi.model.IRI; @@ -185,7 +188,6 @@ classAlgorithms.add(CELOE.class); } - @SuppressWarnings("unchecked") public void start() throws ComponentInitException, IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, LearningProblemUnsupportedException, MalformedURLException { // sanity check that endpoint/graph returns at least one triple @@ -210,15 +212,18 @@ st.getAllObjectProperties(); } else { if(resource instanceof ObjectProperty) { + System.out.println(resource + " appears to be an object property. Running appropriate algorithms."); for (Class<? extends AxiomLearningAlgorithm> algorithmClass : objectPropertyAlgorithms) { applyLearningAlgorithm(algorithmClass, ks); } } else if(resource instanceof DatatypeProperty) { + System.out.println(resource + " appears to be a data property. Running appropriate algorithms."); for (Class<? extends AxiomLearningAlgorithm> algorithmClass : dataPropertyAlgorithms) { applyLearningAlgorithm(algorithmClass, ks); } } else if(resource instanceof NamedClass) { + System.out.println(resource + " appears to be a class. Running appropriate algorithms."); for (Class<? extends LearningAlgorithm> algorithmClass : classAlgorithms) { if(algorithmClass == CELOE.class) { applyCELOE(ks, false); @@ -234,24 +239,31 @@ } private List<EvaluatedAxiom> applyCELOE(SparqlEndpointKS ks, boolean equivalence) throws ComponentInitException, LearningProblemUnsupportedException, MalformedURLException { - SPARQLTasks st = new SPARQLTasks(se); +// SPARQLTasks st = new SPARQLTasks(se); // get instances of class as positive examples SPARQLReasoner sr = new SPARQLReasoner(ks); - SortedSet<Individual> posExamples = sr.getIndividuals((NamedClass)resource, 50); + SortedSet<Individual> posExamples = sr.getIndividuals((NamedClass)resource, 20); SortedSet<String> posExStr = Helper.getStringSet(posExamples); // get negative examples via various strategies - AutomaticNegativeExampleFinderSPARQL finder = new AutomaticNegativeExampleFinderSPARQL(posExStr, st, null); - finder.makeNegativeExamplesFromNearbyClasses(posExStr, 50); - finder.makeNegativeExamplesFromParallelClasses(posExStr, 50); - finder.makeNegativeExamplesFromRelatedInstances(posExStr, "http://dbpedia.org/resource/"); - finder.makeNegativeExamplesFromSuperClasses(resource.getName(), 50); +// AutomaticNegativeExampleFinderSPARQL finder = new AutomaticNegativeExampleFinderSPARQL(posExStr, st, null); +// finder.makeNegativeExamplesFromNearbyClasses(posExStr, 50); +// finder.makeNegativeExamplesFromParallelClasses(posExStr, 50); +// finder.makeNegativeExamplesFromRelatedInstances(posExStr, "http://dbpedia.org/resource/"); +// finder.makeNegativeExamplesFromSuperClasses(resource.getName(), 50); // finder.makeNegativeExamplesFromRandomInstances(); - SortedSet<String> negExStr = finder.getNegativeExamples(50, false); +// SortedSet<String> negExStr = finder.getNegativeExamples(50, false); + // use own implementation of negative example finder + System.out.print("finding negatives ... "); + AutomaticNegativeExampleFinderSPARQL2 finder = new AutomaticNegativeExampleFinderSPARQL2(ks.getEndpoint()); + SortedSet<String> negExStr = finder.getNegativeExamples(resource.getName(), posExStr); + negExStr = SetManipulation.fuzzyShrink(negExStr, 20); SortedSet<Individual> negExamples = Helper.getIndividualSet(negExStr); SortedSetTuple<Individual> examples = new SortedSetTuple<Individual>(posExamples, negExamples); + System.out.println("done (" + negExStr.size()+ ")"); + ComponentManager cm = ComponentManager.getInstance(); SparqlKnowledgeSource ks2 = cm.knowledgeSource(SparqlKnowledgeSource.class); @@ -263,7 +275,9 @@ ks2.getConfigurator().setRecursionDepth(1); ks2.getConfigurator().setCloseAfterRecursion(true); // ks2.getConfigurator().setSaveExtractedFragment(true); + System.out.println("getting fragment ... "); ks2.init(); + System.out.println("done"); AbstractReasonerComponent rc = cm.reasoner(FastInstanceChecker.class, ks2); rc.init(); @@ -276,15 +290,17 @@ lp.getConfigurator().setType("equivalence"); lp.getConfigurator().setAccuracyMethod("fmeasure"); lp.getConfigurator().setUseApproximations(false); + lp.getConfigurator().setMaxExecutionTimeInSeconds(10); lp.init(); - CELOE la = cm.learningAlgorithm(CELOE.class, lp, rc); CELOEConfigurator cc = la.getConfigurator(); cc.setMaxExecutionTimeInSeconds(100); cc.setNoisePercentage(20); la.init(); + System.out.print("running CELOE ... "); la.start(); + System.out.println("done"); // convert the result to axioms (to make it compatible with the other algorithms) TreeSet<? extends EvaluatedDescription> learnedDescriptions = la.getCurrentlyBestEvaluatedDescriptions(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |