From: <jen...@us...> - 2011-09-02 07:50:34
|
Revision: 3221 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3221&view=rev Author: jenslehmann Date: 2011-09-02 07:50:27 +0000 (Fri, 02 Sep 2011) Log Message: ----------- fixed bug and improved formatting in enrichment script Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/utilities/CommonPrefixMap.java trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java Modified: trunk/components-core/src/main/java/org/dllearner/utilities/CommonPrefixMap.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/utilities/CommonPrefixMap.java 2011-09-02 07:22:35 UTC (rev 3220) +++ trunk/components-core/src/main/java/org/dllearner/utilities/CommonPrefixMap.java 2011-09-02 07:50:27 UTC (rev 3221) @@ -24,6 +24,8 @@ /** * Can be used as base for a prefix map. * + * TODO: We might implement a version of this class, which is synchronised with prefix.cc. + * * @author Jens Lehmann * */ @@ -32,8 +34,11 @@ private static final long serialVersionUID = 5434065917532534702L; public CommonPrefixMap() { + put("dbpedia","http://dbpedia.org/resource/"); put("dbp","http://dbpedia.org/property/"); put("dbo","http://dbpedia.org/ontology/"); + put("yago","http://dbpedia.org/class/yago/"); + put("gml","http://www.opengis.net/gml/"); } } Modified: trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java 2011-09-02 07:22:35 UTC (rev 3220) +++ trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java 2011-09-02 07:50:27 UTC (rev 3221) @@ -77,7 +77,6 @@ import org.dllearner.core.AxiomLearningAlgorithm; import org.dllearner.core.ComponentAnn; import org.dllearner.core.ComponentInitException; -import org.dllearner.core.ComponentManager; import org.dllearner.core.EvaluatedAxiom; import org.dllearner.core.EvaluatedDescription; import org.dllearner.core.LearningAlgorithm; @@ -128,7 +127,6 @@ 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.Statement; /** * Command Line Interface for Enrichment. @@ -200,6 +198,10 @@ private CommonPrefixMap prefixes = new CommonPrefixMap(); + // cache for SparqKnowledgeSource + SparqlKnowledgeSource ksCached; + AbstractReasonerComponent rcCached; + public Enrichment(SparqlEndpoint se, Entity resource, double threshold, boolean verbose) { this.se = se; this.resource = resource; @@ -303,8 +305,8 @@ // System.out.println("Running algorithms for class " + nc); for (Class<? extends LearningAlgorithm> algorithmClass : classAlgorithms) { if(algorithmClass == CELOE.class) { - applyCELOE(ks, nc, false); - applyCELOE(ks, nc, true); + applyCELOE(ks, nc, false, false); + applyCELOE(ks, nc, true, true); } else { applyLearningAlgorithm((Class<AxiomLearningAlgorithm>)algorithmClass, ks, nc); } @@ -325,73 +327,67 @@ } } - private List<EvaluatedAxiom> applyCELOE(SparqlEndpointKS ks, NamedClass nc, boolean equivalence) throws ComponentInitException { -// SPARQLTasks st = new SPARQLTasks(se); - + private List<EvaluatedAxiom> applyCELOE(SparqlEndpointKS ks, NamedClass nc, boolean equivalence, boolean reuseKnowledgeSource) throws ComponentInitException { + // get instances of class as positive examples SPARQLReasoner sr = new SPARQLReasoner(ks); SortedSet<Individual> posExamples = sr.getIndividuals(nc, 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); -// finder.makeNegativeExamplesFromRandomInstances(); -// SortedSet<String> negExStr = finder.getNegativeExamples(50, false); // use own implementation of negative example finder + long startTime = System.currentTimeMillis(); System.out.print("finding negatives ... "); AutomaticNegativeExampleFinderSPARQL2 finder = new AutomaticNegativeExampleFinderSPARQL2(ks.getEndpoint()); SortedSet<String> negExStr = finder.getNegativeExamples(nc.getName(), posExStr); negExStr = SetManipulation.fuzzyShrink(negExStr, 20); SortedSet<Individual> negExamples = Helper.getIndividualSet(negExStr); SortedSetTuple<Individual> examples = new SortedSetTuple<Individual>(posExamples, negExamples); + long runTime = System.currentTimeMillis() - startTime; + System.out.println("done (" + negExStr.size()+ " examples fround in " + runTime + " ms)"); - System.out.println("done (" + negExStr.size()+ ")"); - - SparqlKnowledgeSource ks2 = new SparqlKnowledgeSource(); - ks2.setInstances(Datastructures.individualSetToStringSet(examples.getCompleteSet())); - ks2.setUrl(ks.getEndpoint().getURL()); - ks2.setDefaultGraphURIs(new TreeSet<String>(ks.getEndpoint().getDefaultGraphURIs())); - ks2.setUseLits(false); - ks2.setUseCacheDatabase(true); - ks2.setRecursionDepth(2); - ks2.setCloseAfterRecursion(true); -// ks2.getConfigurator().setSaveExtractedFragment(true); - System.out.println("getting fragment ... "); - ks2.init(); - System.out.println("done"); + SparqlKnowledgeSource ks2; + AbstractReasonerComponent rc; + if(reuseKnowledgeSource) { + ks2 = ksCached; + rc = rcCached; + System.out.println("re-using previously generated knowledge base fragment"); + } else { + ks2 = new SparqlKnowledgeSource(); + ks2.setInstances(Datastructures.individualSetToStringSet(examples.getCompleteSet())); + ks2.setUrl(ks.getEndpoint().getURL()); + ks2.setDefaultGraphURIs(new TreeSet<String>(ks.getEndpoint().getDefaultGraphURIs())); + ks2.setUseLits(false); + ks2.setUseCacheDatabase(true); + ks2.setRecursionDepth(2); + ks2.setCloseAfterRecursion(true); + startTime = System.currentTimeMillis(); + System.out.print("getting knowledge base fragment ... "); + ks2.init(); + runTime = System.currentTimeMillis() - startTime; + System.out.println("done in " + runTime + " ms"); + rc = new FastInstanceChecker(ks2); + rc.init(); + ksCached = ks2; + rcCached = rc; + } - AbstractReasonerComponent rc = new FastInstanceChecker(ks2); - rc.init(); - - // TODO: super class learning ClassLearningProblem lp = new ClassLearningProblem(rc); -// lp.setPositiveExamples(posExamples); -// lp.setNegativeExamples(negExamples); -// try { - lp.setClassToDescribe(nc); -// } catch (MalformedURLException e1) { -// e1.printStackTrace(); -// } -// lp.setType("equivalence"); - lp.setEquivalence(true); -// lp.setAccuracyMethod("fmeasure"); + lp.setClassToDescribe(nc); + lp.setEquivalence(equivalence); lp.setHeuristic(HeuristicType.FMEASURE); lp.setUseApproximations(false); lp.setMaxExecutionTimeInSeconds(10); lp.init(); CELOE la = new CELOE(lp, rc); -// CELOEConfigurator cc = la.getConfigurator(); la.setMaxExecutionTimeInSeconds(10); la.setNoisePercentage(25); la.init(); - System.out.print("running CELOE (for " + (equivalence ? "EquivalentClasses" : "SubClasses)") + "... "); + startTime = System.currentTimeMillis(); + System.out.print("running CELOE (for " + (equivalence ? "equivalent classes" : "sub classes") + ") ... "); la.start(); - System.out.println("done"); + runTime = System.currentTimeMillis() - startTime; + System.out.println("done in " + runTime + " ms"); // convert the result to axioms (to make it compatible with the other algorithms) List<? extends EvaluatedDescription> learnedDescriptions = la.getCurrentlyBestEvaluatedDescriptions(threshold); @@ -440,7 +436,7 @@ } } long runtime = System.currentTimeMillis() - startTime; - System.out.println("done in " + runtime + "ms"); + System.out.println("done in " + runtime + " ms"); List<EvaluatedAxiom> learnedAxioms = learner .getCurrentlyBestEvaluatedAxioms(nrOfAxiomsToLearn, threshold); System.out.println(prettyPrint(learnedAxioms)); @@ -464,8 +460,8 @@ private String prettyPrint(EvaluatedAxiom axiom) { double acc = axiom.getScore().getAccuracy() * 100; String accs = df.format(acc); - if(acc<10d) { accs = " " + accs; } - if(acc<100d) { accs = " " + accs; } + if(accs.length()==3) { accs = " " + accs; } + if(accs.length()==4) { accs = " " + accs; } String str = accs + "%\t" + axiom.getAxiom().toManchesterSyntaxString(null, prefixes); return str; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |