From: <ku...@us...> - 2008-09-25 16:20:57
|
Revision: 1258 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=1258&view=rev Author: kurzum Date: 2008-09-25 16:20:47 +0000 (Thu, 25 Sep 2008) Log Message: ----------- server commit Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/scripts/DumbLPFinder.java Modified: trunk/src/dl-learner/org/dllearner/scripts/DumbLPFinder.java =================================================================== --- trunk/src/dl-learner/org/dllearner/scripts/DumbLPFinder.java 2008-09-25 15:49:07 UTC (rev 1257) +++ trunk/src/dl-learner/org/dllearner/scripts/DumbLPFinder.java 2008-09-25 16:20:47 UTC (rev 1258) @@ -20,6 +20,9 @@ package org.dllearner.scripts; import java.io.File; +import java.net.URI; +import java.util.HashSet; +import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; @@ -28,21 +31,19 @@ import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.apache.log4j.SimpleLayout; +import org.dllearner.algorithms.refexamples.ExampleBasedROLComponent; import org.dllearner.core.ComponentManager; import org.dllearner.core.EvaluatedDescription; -import org.dllearner.core.LearningAlgorithm; -import org.dllearner.core.ReasonerComponent; +import org.dllearner.core.KnowledgeSource; import org.dllearner.core.ReasoningService; +import org.dllearner.core.configurators.ComponentFactory; import org.dllearner.core.owl.Individual; -import org.dllearner.reasoning.FastInstanceChecker; +import org.dllearner.kb.sparql.SparqlKnowledgeSource; +import org.dllearner.learningproblems.PosNegDefinitionLP; import org.dllearner.reasoning.OWLAPIReasoner; import org.dllearner.utilities.Files; import org.dllearner.utilities.datastructures.SetManipulation; import org.dllearner.utilities.learn.ConfWriter; -import org.dllearner.utilities.learn.LearnOWLFile; -import org.dllearner.utilities.learn.LearnOWLFileConfiguration; -import org.dllearner.utilities.learn.LearnSPARQLConfiguration; -import org.dllearner.utilities.learn.LearnSparql; import org.dllearner.utilities.owl.ReasoningServiceFactory; import org.dllearner.utilities.owl.ReasoningServiceFactory.AvailableReasoners; @@ -51,289 +52,204 @@ private static Logger logger = Logger.getRootLogger(); private static ReasoningService reasoningService; - - public static String ontologyPath = "examples/semantic_bible/NTNcombined.owl"; - - //private static Class usedReasoner = FastInstanceChecker.class; - private static Class<? extends ReasonerComponent> usedReasoner = OWLAPIReasoner.class; - + + private static String ontologyPath = "examples/semantic_bible/NTNcombined.owl"; + private static int numberOfLearningProblems = 100; - + + private static String baseDir = "sembib/"; + + private static String baseDirSparql = baseDir + "sparql/"; + + private static String baseDirNormal = baseDir + "normal/"; + private static boolean allOrExists = true; + private static boolean tenORthirty = true; - - private static boolean sparql = true; - + private static boolean DEBUG = false; - //private static boolean allOrExists = true; + // private static boolean allOrExists = true; + /** * @param args */ public static void main(String[] args) { initLogger(); logger.info("started"); - //String fileURL = new File(ontologyFile).toURI().toString(); - + // String fileURL = new File(ontologyFile).toURI().toString(); + reasoningService = ReasoningServiceFactory.getReasoningService( ontologyPath, AvailableReasoners.OWLAPIREASONERPELLET); - - String baseDir = "sembib/"; + Files.mkdir(baseDir); - String baseDirSparql = baseDir+ "sparql/"; Files.mkdir(baseDirSparql); - String baseDirNormal = baseDir+"normal/"; Files.mkdir(baseDirNormal); - + SortedSet<Individual> allIndividuals = new TreeSet<Individual>(); - allIndividuals.addAll( reasoningService.getIndividuals()); - + allIndividuals.addAll(reasoningService.getIndividuals()); + reasoningService = null; ComponentManager.getInstance().freeAllComponents(); - + int count = 0; - while(count<numberOfLearningProblems){ - - /*if((count%5)==0){ - //System.out.println(count+" "+allOrExists+"::"+tenORthirty); - - if(allOrExists && !tenORthirty){ - allOrExists = true; - tenORthirty = true; - }else if(!allOrExists && tenORthirty){ - allOrExists = true; - tenORthirty = false; - }else if(!allOrExists && !tenORthirty){ - allOrExists = false; - tenORthirty = true; - }else if(allOrExists && tenORthirty){ - allOrExists = false; - tenORthirty = false; + while (count < numberOfLearningProblems) { + + int exampleSize = (tenORthirty) ? 10 : 30; + int half = (tenORthirty) ? 5 : 15; + + String filename = ""; + filename += (allOrExists) ? "all_" : "exists_"; + filename += (tenORthirty) ? "ten_" : "thirty_"; + + try { + + SortedSet<Individual> tmp = SetManipulation.fuzzyShrinkInd( + allIndividuals, exampleSize); + + SortedSet<Individual> positiveEx = new TreeSet<Individual>(); + SortedSet<Individual> negativeEx = new TreeSet<Individual>(); + + for (Individual individual : tmp) { + if (positiveEx.size() < half) { + positiveEx.add(individual); + } else { + negativeEx.add(individual); + } + } - //System.out.println(count+" "+allOrExists+"::"+tenORthirty); - - }*/ - - int exampleSize = (tenORthirty)?10:30; - int half = (tenORthirty)?5:15; - - String filename =""; - filename += (allOrExists)?"all_":"exists_"; - filename += (tenORthirty)?"ten_":"thirty_"; - - - try{ - - SortedSet<Individual> tmp = SetManipulation.fuzzyShrinkInd(allIndividuals, exampleSize); - - SortedSet<Individual> positiveEx = new TreeSet<Individual>(); - SortedSet<Individual> negativeEx = new TreeSet<Individual>(); - - for (Individual individual : tmp) { - if(positiveEx.size()< half){ - positiveEx.add(individual); - }else{ - negativeEx.add(individual); - } - - } - - EvaluatedDescription d; - if(sparql){ - d = learnSPARQL( positiveEx, negativeEx); - }else { - d = learnOriginal( positiveEx, negativeEx); - } - - - - String div = (System.currentTimeMillis()%10000)+""; - if(d.getAccuracy()>=0.99){ - filename +="99+"; - }else if(d.getAccuracy()>=0.90){ - filename +="90+"; - }else if(d.getAccuracy()>=0.80){ - filename +="80+"; - }else if(d.getAccuracy()>=0.70){ - filename +="70+"; - }else if(d.getAccuracy()>0.50){ - filename +="50+"; - }else { - filename +="50-"; - } - filename+="_"; - filename+=(d.getDescriptionLength()<10)?"0"+d.getDescriptionLength():d.getDescriptionLength()+""; - filename += "_"+div+".conf"; - - String content = fileString(true, d, positiveEx, negativeEx); - Files.createFile(new File(baseDirSparql+filename), content); - content = fileString(false, d, positiveEx, negativeEx); - Files.createFile(new File(baseDirNormal+filename), content); - - - - }catch (Exception e) { + + EvaluatedDescription d; + + d = learnSPARQL(positiveEx, negativeEx); + + writeFiles(filename, d, positiveEx, negativeEx); + + } catch (Exception e) { e.printStackTrace(); - }finally{ + } finally { ComponentManager.getInstance().freeAllComponents(); } - //System.out.println(count); + // System.out.println(count); count++; } + } + private static void writeFiles(String filename, EvaluatedDescription d, + SortedSet<Individual> positiveEx, SortedSet<Individual> negativeEx) { + String div = (System.currentTimeMillis() % 10000) + ""; + if (d.getAccuracy() >= 0.99) { + filename += "99+"; + } else if (d.getAccuracy() >= 0.90) { + filename += "90+"; + } else if (d.getAccuracy() >= 0.80) { + filename += "80+"; + } else if (d.getAccuracy() >= 0.70) { + filename += "70+"; + } else if (d.getAccuracy() > 0.50) { + filename += "50+"; + } else { + filename += "50-"; + } + filename += "_"; + filename += (d.getDescriptionLength() < 10) ? "0" + + d.getDescriptionLength() : d.getDescriptionLength() + ""; + filename += "_" + div + ".conf"; + + String content = fileString(true, d, positiveEx, negativeEx); + Files.createFile(new File(baseDirSparql + filename), content); + content = fileString(false, d, positiveEx, negativeEx); + Files.createFile(new File(baseDirNormal + filename), content); } - public static String accString(EvaluatedDescription d){ - - String acc = (d.getAccuracy())+""; - try { acc = acc.substring(2,6); - acc= acc.substring(0,2)+"."+acc.substring(3)+"%";}catch (Exception e) { } - + @SuppressWarnings("unused") + private static String accString(EvaluatedDescription d) { + + String acc = (d.getAccuracy()) + ""; + try { + acc = acc.substring(2, 6); + acc = acc.substring(0, 2) + "." + acc.substring(3) + "%"; + } catch (Exception e) { + } + return acc; } - - public static String fileString(boolean sparql, EvaluatedDescription d, SortedSet<Individual> p, SortedSet<Individual> n){ - - String str = "/**\n" + - d.getDescription().toKBSyntaxString() + "\n"+ - d + "\n"+ - "\n"+ - "**/\n"+ - "\n\n"; - if(sparql){ - str+="sparql.instances = {\n"; + + private static String fileString(boolean sparql, EvaluatedDescription d, + SortedSet<Individual> p, SortedSet<Individual> n) { + + String str = "/**\n" + d.getDescription().toKBSyntaxString() + "\n" + d + + "\n" + "\n" + "**/\n" + "\n\n"; + if (sparql) { + str += "sparql.instances = {\n"; for (Individual individual : p) { - str+="\""+individual+"\",\n"; + str += "\"" + individual + "\",\n"; } for (Individual individual : n) { - str+="\""+individual+"\",\n"; + str += "\"" + individual + "\",\n"; } - str = str.substring(0, str.length()-2); - str+="};\n"; - + str = str.substring(0, str.length() - 2); + str += "};\n"; + } - - str+="\n"+ - "/**EXAMPLES**/\n"+ - ConfWriter.listExamples(true, p)+"\n"+ - ConfWriter.listExamples(false, n)+"\n"; - + + str += "\n" + "/**EXAMPLES**/\n" + ConfWriter.listExamples(true, p) + + "\n" + ConfWriter.listExamples(false, n) + "\n"; + return str; } - - private static EvaluatedDescription learnOriginal( SortedSet<Individual> posExamples, SortedSet<Individual> negExamples) { - - LearnOWLFile learner = new LearnOWLFile(getConfForOriginal()); - LearningAlgorithm la = null; - try{ - la = learner.learn( - SetManipulation.indToString(posExamples), - SetManipulation.indToString(negExamples), - usedReasoner); - la.start(); - }catch (Exception e) { - System.out.println("ignoring the error "+e.toString()); - - } - - EvaluatedDescription d = la.getCurrentlyBestEvaluatedDescription(); - - return d; - - - } - - private static EvaluatedDescription learnSPARQL( SortedSet<Individual> posExamples, SortedSet<Individual> negExamples) { - - LearnSparql learner = new LearnSparql(getConfForSparql()); - LearningAlgorithm la = null; - try{ - la = learner.learn( - SetManipulation.indToString(posExamples), - SetManipulation.indToString(negExamples), - usedReasoner); - la.start(); - }catch (Exception e) { - System.out.println("ignoring the error "+e.toString()); - - } - - EvaluatedDescription d = la.getCurrentlyBestEvaluatedDescription(); - - return d; - - - } - + private static EvaluatedDescription learnSPARQL( + SortedSet<Individual> posExamples, SortedSet<Individual> negExamples) { - private static LearnOWLFileConfiguration getConfForOriginal() { - LearnOWLFileConfiguration lc = new LearnOWLFileConfiguration(); - - - lc.setOWLFileURL(ontologyPath); - - lc.noisePercentage = 0; - lc.guaranteeXgoodDescriptions = 1; - if(allOrExists){ - lc.useAllConstructor = true; - lc.useCardinalityRestrictions = true; - lc.useExistsConstructor =true; - lc.useNegation = true; - }else { - lc.useAllConstructor = false; - lc.useCardinalityRestrictions = false; - lc.useExistsConstructor =true; - lc.useNegation = false; - } - - - Class<? extends ReasonerComponent> tmp = FastInstanceChecker.class; - if(usedReasoner.equals(tmp)){ - lc.maxExecutionTimeInSeconds = 30; - }else{ - lc.maxExecutionTimeInSeconds = 200; - } - return lc; + ExampleBasedROLComponent la = null; - } - - private static LearnSPARQLConfiguration getConfForSparql() { - LearnSPARQLConfiguration lc = new LearnSPARQLConfiguration(); - // lsc.sparqlEndpoint = sparqlTasks.getSparqlEndpoint(); + try { - - lc.recursiondepth = 2; - lc.closeAfterRecursion = true; - lc.useLits = true; - lc.predefinedEndpoint = "LOCALJOSEKIBIBLE"; - - lc.noisePercentage = 0; - lc.guaranteeXgoodDescriptions = 1; - lc.maxExecutionTimeInSeconds = 30; - - - - if(allOrExists){ - lc.useAllConstructor = true; - lc.useCardinalityRestrictions = true; - lc.useExistsConstructor =true; - lc.useNegation = true; - }else { - lc.useAllConstructor = false; - lc.useCardinalityRestrictions = false; - lc.useExistsConstructor =true; - lc.useNegation = false; + SortedSet<Individual> instances = new TreeSet<Individual>(); + instances.addAll(posExamples); + instances.addAll(negExamples); + + SparqlKnowledgeSource ks = ComponentFactory + .getSparqlKnowledgeSource(URI.create( + "http://www.blabla.com").toURL(), SetManipulation + .indToString(instances)); + + ks.getConfigurator().setCloseAfterRecursion(true); + ks.getConfigurator().setRecursionDepth(2); + ks.getConfigurator().setPredefinedEndpoint("LOCALJOSEKIBIBLE"); + ks.getConfigurator().setUseLits(true); + + Set<KnowledgeSource> tmp = new HashSet<KnowledgeSource>(); + tmp.add(ks); + // reasoner + OWLAPIReasoner f = ComponentFactory + .getOWLAPIReasoner(tmp); + ReasoningService rs = ComponentManager.getInstance() + .reasoningService(f); + + // learning problem + PosNegDefinitionLP lp = ComponentFactory.getPosNegDefinitionLP(rs, + SetManipulation.indToString(posExamples), SetManipulation + .indToString(negExamples)); + + // learning algorithm + la = ComponentFactory.getExampleBasedROLComponent(lp, rs); + la.getConfigurator().setNoisePercentage(0.0); + la.getConfigurator().setGuaranteeXgoodDescriptions(1); + la.getConfigurator().setMaxExecutionTimeInSeconds(30); + + la.start(); + } catch (Exception e) { + // System.out.println("ignoring the error "+e.toString()); + } - - // lsc.searchTreeFile = "log/WikipediaCleaner.txt"; - return lc; + return la.getCurrentlyBestEvaluatedDescription(); } - - + private static void initLogger() { SimpleLayout layout = new SimpleLayout(); @@ -347,18 +263,15 @@ e.printStackTrace(); } - logger.removeAllAppenders(); - if(DEBUG){ + if (DEBUG) { logger.setLevel(Level.DEBUG); ConsoleAppender consoleAppender = new ConsoleAppender(layout); logger.addAppender(consoleAppender); - }else{ + } else { logger.setLevel(Level.INFO); } logger.addAppender(fileAppender); - - } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |