From: <jen...@us...> - 2009-05-15 11:55:24
|
Revision: 1751 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=1751&view=rev Author: jenslehmann Date: 2009-05-15 11:55:08 +0000 (Fri, 15 May 2009) Log Message: ----------- - prepared class matching - added support for ASK queries Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/kb/sparql/SPARQLTasks.java trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlEndpoint.java trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQuery.java trunk/src/dl-learner/org/dllearner/scripts/matching/Evaluation.java Added Paths: ----------- trunk/src/dl-learner/org/dllearner/scripts/matching/LearnOSMClasses.java trunk/src/dl-learner/org/dllearner/scripts/matching/Utility.java Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/SPARQLTasks.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/SPARQLTasks.java 2009-05-14 06:33:31 UTC (rev 1750) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/SPARQLTasks.java 2009-05-15 11:55:08 UTC (rev 1751) @@ -28,6 +28,7 @@ import org.dllearner.utilities.datastructures.StringTuple; import org.dllearner.utilities.owl.OWLVocabulary; +import com.hp.hpl.jena.query.QuerySolution; import com.hp.hpl.jena.query.ResultSet; import com.hp.hpl.jena.query.ResultSetFactory; import com.hp.hpl.jena.query.ResultSetFormatter; Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlEndpoint.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlEndpoint.java 2009-05-14 06:33:31 UTC (rev 1750) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlEndpoint.java 2009-05-15 11:55:08 UTC (rev 1751) @@ -85,6 +85,8 @@ return getEndpointLOCALDBpedia(); } else if (name.equals("LOCALGEONAMES")) { return getEndpointLOCALGeonames(); + } else if (name.equals("LOCALGEODATA")) { + return getEndpointLOCALGeoData(); } else if (name.equals("LOCALJOSECKI") || name.equals("LOCALJOSEKI") ) { return getEndpointlocalJoseki(); } else if (name.equals("LOCALJOSEKIBIBLE")||name.equals("LOCALJOSECKIBIBLE")) { Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQuery.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQuery.java 2009-05-14 06:33:31 UTC (rev 1750) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/SparqlQuery.java 2009-05-15 11:55:08 UTC (rev 1751) @@ -41,7 +41,7 @@ /** * Represents one SPARQL query. It includes support for stopping the SPARQL * query (which may be necessary if a timeout is reached) and is designed to be - * able to run a query in a separate thread. + * able to run a query in a separate thread. * * @author Jens Lehmann * @author Sebastian Hellmann @@ -142,6 +142,15 @@ return rs; } + public boolean sendAsk() { + isRunning = true; + String service = sparqlEndpoint.getURL().toString(); + queryExecution = new QueryEngineHTTP(service, sparqlQueryString); + boolean result = queryExecution.execAsk(); + isRunning = false; + return result; + } + /** * Stops the execution of the query. */ Modified: trunk/src/dl-learner/org/dllearner/scripts/matching/Evaluation.java =================================================================== --- trunk/src/dl-learner/org/dllearner/scripts/matching/Evaluation.java 2009-05-14 06:33:31 UTC (rev 1750) +++ trunk/src/dl-learner/org/dllearner/scripts/matching/Evaluation.java 2009-05-15 11:55:08 UTC (rev 1751) @@ -20,12 +20,14 @@ package org.dllearner.scripts.matching; import java.io.BufferedReader; +import java.io.File; import java.io.FileReader; import java.io.IOException; import java.net.URI; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; +import java.util.zip.DataFormatException; import org.apache.log4j.Level; import org.apache.log4j.Logger; @@ -140,25 +142,13 @@ return discarded; } - public static void main(String args[]) throws IOException { + public static void main(String args[]) throws IOException, DataFormatException { Logger.getRootLogger().setLevel(Level.TRACE); // test file - String testFile = "log/geodata/owlsameas_en.dat"; + File testFile = new File("log/geodata/owlsameas_en.dat"); // map for collecting matches - Map<URI,URI> matches = new HashMap<URI,URI>(); - // read file line by line to collect matches - BufferedReader br = new BufferedReader(new FileReader(testFile)); - String line; - while ((line = br.readLine()) != null) { - String[] tmp = line.split("\t"); -// System.out.println(line); -// for(String test : tmp) { -// System.out.println(test); -// } - - matches.put(URI.create(tmp[1]), URI.create(tmp[0] + "#id")); - } + Map<URI,URI> matches = Utility.getMatches(testFile); // perform evaluation and print results Evaluation eval = new Evaluation(matches); System.out.println(eval.getTests() + " points tested (" + eval.getDiscarded() + " discarded)"); Added: trunk/src/dl-learner/org/dllearner/scripts/matching/LearnOSMClasses.java =================================================================== --- trunk/src/dl-learner/org/dllearner/scripts/matching/LearnOSMClasses.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/scripts/matching/LearnOSMClasses.java 2009-05-15 11:55:08 UTC (rev 1751) @@ -0,0 +1,96 @@ +/** + * Copyright (C) 2007-2009, 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.scripts.matching; + +import java.io.File; +import java.io.IOException; +import java.net.URI; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; +import java.util.Map.Entry; +import java.util.zip.DataFormatException; + +import org.dllearner.algorithms.celoe.CELOE; +import org.dllearner.core.ComponentManager; +import org.dllearner.core.LearningProblemUnsupportedException; +import org.dllearner.core.ReasonerComponent; +import org.dllearner.kb.sparql.SparqlEndpoint; +import org.dllearner.kb.sparql.SparqlKnowledgeSource; +import org.dllearner.kb.sparql.SparqlQuery; +import org.dllearner.learningproblems.PosOnlyLP; +import org.dllearner.reasoning.FastInstanceChecker; + +/** + * Uses learning algorithms to learn class definitions for DBpedia + * classes in LinkedGeoData. As a final result, we can classify most + * objects in LinkedGeoData using the DBpedia ontology. + * + * @author Jens Lehmann + * + */ +public class LearnOSMClasses { + + private static SparqlEndpoint dbpediaEndpoint = SparqlEndpoint.getEndpointLOCALDBpedia(); + private static SparqlEndpoint geoDataEndpoint = SparqlEndpoint.getEndpointLOCALGeoData(); + + public static void main(String args[]) throws IOException, DataFormatException, LearningProblemUnsupportedException { + File matchesFile = new File("log/geodata/owlsameas_en.dat"); + Map<URI,URI> matches = Utility.getMatches(matchesFile); + +// SPARQLTasks st = new SPARQLTasks(dbpediaEndpoint); + Set<String> positives = new TreeSet<String>(); + Set<String> negatives = new TreeSet<String>(); + + // loop through all matches + for(Entry<URI,URI> match : matches.entrySet()) { + URI dbpediaURI = match.getKey(); + URI lgdURI = match.getValue(); + // test whether the dbpediaURI is a city + String query = "ASK {<"+dbpediaURI+"> a <http://dbpedia.org/ontology/City>}"; + SparqlQuery sq = new SparqlQuery(query, dbpediaEndpoint); + boolean isCity = sq.sendAsk(); + if(isCity) { + positives.add(lgdURI.toString()); + } else { + negatives.add(lgdURI.toString()); + } + } + + Set<String> instances = new TreeSet<String>(); + instances.addAll(positives); + instances.addAll(negatives); + + // plug together DL-Learner components + ComponentManager cm = ComponentManager.getInstance(); + SparqlKnowledgeSource ks = cm.knowledgeSource(SparqlKnowledgeSource.class); + ks.getConfigurator().setInstances(instances); + ks.getConfigurator().setPredefinedEndpoint("LOCALGEODATA"); + ReasonerComponent reasoner = cm.reasoner(FastInstanceChecker.class, ks); + PosOnlyLP lp = cm.learningProblem(PosOnlyLP.class, reasoner); + lp.getConfigurator().setPositiveExamples(positives); + CELOE algorithm = cm.learningAlgorithm(CELOE.class, lp, reasoner); + + // execute algorithm + algorithm.start(); +// Set<EvaluatedDescriptionPosOnly> solutions = (Set<EvaluatedDescriptionPosOnly>) algorithm.getCurrentlyBestEvaluatedDescriptions(); + } + +} Added: trunk/src/dl-learner/org/dllearner/scripts/matching/Utility.java =================================================================== --- trunk/src/dl-learner/org/dllearner/scripts/matching/Utility.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/scripts/matching/Utility.java 2009-05-15 11:55:08 UTC (rev 1751) @@ -0,0 +1,68 @@ +/** + * Copyright (C) 2007-2009, 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.scripts.matching; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.net.URI; +import java.util.HashMap; +import java.util.Map; +import java.util.zip.DataFormatException; + +/** + * Utility methods for creating/optimising matches between knowledge bases. + * + * @author Jens Lehmann + * + */ +public final class Utility { + + /** + * Reads in a file containing known matches between knowledge bases. + * + * Note: + * - Currently, it is assumed that the format is URI + tabulator + URI + newline. + * - Currently, #id is appended to the second URI on the line. + * - Currently, the order is reversed. + * + * @param file A file, which contains matches. + * @return A map containing the matches. + * @throws IOException If an error occurs during reading the file. + */ + public static Map<URI, URI> getMatches(File file) throws IOException, DataFormatException { + Map<URI,URI> matches = new HashMap<URI,URI>(); + // read file line by line to collect matches + BufferedReader br = new BufferedReader(new FileReader(file)); + String line; + int lineCount = 0; + while ((line = br.readLine()) != null) { + String[] tmp = line.split("\t"); + if(tmp.length != 2) { + throw new DataFormatException("Line " + lineCount + " incorrectly formatted."); + } + matches.put(URI.create(tmp[1]), URI.create(tmp[0] + "#id")); + lineCount++; + } + return matches; + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |