From: <lor...@us...> - 2012-06-29 12:18:50
|
Revision: 3767 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3767&view=rev Author: lorenz_b Date: 2012-06-29 12:18:39 +0000 (Fri, 29 Jun 2012) Log Message: ----------- Using popularity map as executing cache. Modified Paths: -------------- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/learning/SPARQLTemplateBasedLearner2.java trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/util/PopularityMap.java 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-28 13:44:49 UTC (rev 3766) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/learning/SPARQLTemplateBasedLearner2.java 2012-06-29 12:18:39 UTC (rev 3767) @@ -47,6 +47,7 @@ import org.dllearner.algorithm.tbsl.templator.Templator; import org.dllearner.algorithm.tbsl.util.Knowledgebase; import org.dllearner.algorithm.tbsl.util.PopularityMap; +import org.dllearner.algorithm.tbsl.util.PopularityMap.EntityType; import org.dllearner.algorithm.tbsl.util.Similarity; import org.dllearner.common.index.Index; import org.dllearner.common.index.IndexResultItem; @@ -782,9 +783,13 @@ private double getProminenceValue(String uri, SlotType type){ Integer popularity = null; if(popularityMap != null){ - if(type == SlotType.CLASS || type == SlotType.PROPERTY || type == SlotType.SYMPROPERTY + if(type == SlotType.CLASS){ + popularity = popularityMap.getPopularity(uri, EntityType.CLASS); + } else if(type == SlotType.PROPERTY || type == SlotType.SYMPROPERTY || type == SlotType.DATATYPEPROPERTY || type == SlotType.OBJECTPROPERTY){ - popularity = popularityMap.getPopularity(uri); + popularity = popularityMap.getPopularity(uri, EntityType.PROPERTY); + } else if(type == SlotType.RESOURCE || type == SlotType.UNSPEC){ + popularity = popularityMap.getPopularity(uri, EntityType.RESOURCE); } } if(popularity == null){ @@ -808,6 +813,9 @@ popularity = qs.get(projectionVar).asLiteral().getInt(); } } + if(popularity == null){ + popularity = Integer.valueOf(0); + } // if(cnt == 0){ Modified: trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/util/PopularityMap.java =================================================================== --- trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/util/PopularityMap.java 2012-06-28 13:44:49 UTC (rev 3766) +++ trunk/components-ext/src/main/java/org/dllearner/algorithm/tbsl/util/PopularityMap.java 2012-06-29 12:18:39 UTC (rev 3767) @@ -7,7 +7,9 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.dllearner.core.owl.DatatypeProperty; @@ -23,7 +25,7 @@ public class PopularityMap { - enum EntityType { + public enum EntityType { CLASS, PROPERTY, RESOURCE } @@ -48,22 +50,19 @@ // load popularity of classes for (NamedClass nc : new SPARQLTasks(endpoint).getAllClasses()) { System.out.println("Computing popularity for " + nc); - String query = String.format("SELECT COUNT(?s) WHERE {?s a <%s>}", nc.getName()); - int popularity = loadPopularity(query); + int popularity = loadPopularity(nc.getName(), EntityType.CLASS); class2Popularity.put(nc.getName(), Integer.valueOf(popularity)); } // load popularity of properties for (ObjectProperty op : new SPARQLTasks(endpoint).getAllObjectProperties()) { System.out.println("Computing popularity for " + op); - String query = String.format("SELECT COUNT(*) WHERE {?s <%s> ?o}", op.getName()); - int popularity = loadPopularity(query); - class2Popularity.put(op.getName(), Integer.valueOf(popularity)); + int popularity = loadPopularity(op.getName(), EntityType.PROPERTY); + property2Popularity.put(op.getName(), Integer.valueOf(popularity)); } for (DatatypeProperty dp : new SPARQLTasks(endpoint).getAllDataProperties()) { System.out.println("Computing popularity for " + dp); - String query = String.format("SELECT COUNT(*) WHERE {?s <%s> ?o}", dp.getName()); - int popularity = loadPopularity(query); - class2Popularity.put(dp.getName(), Integer.valueOf(popularity)); + int popularity = loadPopularity(dp.getName(), EntityType.PROPERTY); + property2Popularity.put(dp.getName(), Integer.valueOf(popularity)); } serialize(); } @@ -73,7 +72,11 @@ ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(new FileOutputStream(new File(file))); - oos.writeObject(class2Popularity); + List<Map<String, Integer>> mapList = new ArrayList<Map<String,Integer>>(); + mapList.add(class2Popularity); + mapList.add(property2Popularity); + mapList.add(resource2Popularity); + oos.writeObject(mapList); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -98,7 +101,10 @@ ObjectInputStream ois = null; try { ois = new ObjectInputStream(new FileInputStream(new File(file))); - class2Popularity = (Map<String, Integer>) ois.readObject(); + List<Map<String, Integer>> mapList = (List<Map<String, Integer>>) ois.readObject(); + class2Popularity = mapList.get(0); + property2Popularity = mapList.get(1); + resource2Popularity = mapList.get(2); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { @@ -115,12 +121,21 @@ } } + System.out.println("Loaded popularity map."); return true; } return false; } - private int loadPopularity(String query){ + private int loadPopularity(String uri, EntityType entityType){ + String query; + if(entityType == EntityType.CLASS){ + query = String.format("SELECT COUNT(?s) WHERE {?s a <%s>}", uri); + } else if(entityType == EntityType.PROPERTY){ + query = String.format("SELECT COUNT(*) WHERE {?s <%s> ?o}", uri); + } else { + query = String.format("SELECT COUNT(*) WHERE {?s ?p <%s>}", uri); + } int pop = 0; ResultSet rs = SparqlQuery.convertJSONtoResultSet(cache.executeSelectQuery(endpoint, query)); QuerySolution qs; @@ -137,10 +152,22 @@ Integer popularity; if(entityType == EntityType.CLASS){ popularity = class2Popularity.get(uri); + if(popularity == null){ + popularity = loadPopularity(uri, entityType); + class2Popularity.put(uri, popularity); + } } else if(entityType == EntityType.PROPERTY){ popularity = property2Popularity.get(uri); + if(popularity == null){ + popularity = loadPopularity(uri, entityType); + property2Popularity.put(uri, popularity); + } } else { popularity = resource2Popularity.get(uri); + if(popularity == null){ + popularity = loadPopularity(uri, entityType); + resource2Popularity.put(uri, popularity); + } } return popularity; } @@ -157,7 +184,9 @@ } public static void main(String[] args) { - new PopularityMap("dbpedia_popularity.map", SparqlEndpoint.getEndpointDBpedia(), new ExtractionDBCache("cache")).init(); + PopularityMap map = new PopularityMap("dbpedia_popularity.map", SparqlEndpoint.getEndpointDBpediaLiveAKSW(), new ExtractionDBCache("cache")); + map.init(); + System.out.println(map.getPopularity("http://dbpedia.org/ontology/Book")); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |