From: <ku...@us...> - 2008-08-30 01:29:42
|
Revision: 1151 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=1151&view=rev Author: kurzum Date: 2008-08-30 01:29:38 +0000 (Sat, 30 Aug 2008) Log Message: ----------- Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/utilities/Files.java trunk/src/dl-learner/org/dllearner/utilities/learn/LearnOWLFile.java Added Paths: ----------- trunk/src/dl-learner/org/dllearner/scripts/DumbLPFinder.java Added: trunk/src/dl-learner/org/dllearner/scripts/DumbLPFinder.java =================================================================== --- trunk/src/dl-learner/org/dllearner/scripts/DumbLPFinder.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/scripts/DumbLPFinder.java 2008-08-30 01:29:38 UTC (rev 1151) @@ -0,0 +1,281 @@ +/** + * Copyright (C) 2007-2008, 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; + +import java.io.File; +import java.util.SortedSet; +import java.util.TreeSet; + +import org.apache.log4j.FileAppender; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.apache.log4j.SimpleLayout; +import org.dllearner.core.ComponentManager; +import org.dllearner.core.EvaluatedDescription; +import org.dllearner.core.LearningAlgorithm; +import org.dllearner.core.ReasoningService; +import org.dllearner.core.owl.Individual; +import org.dllearner.reasoning.FastInstanceChecker; +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.owl.ReasoningServiceFactory; +import org.dllearner.utilities.owl.ReasoningServiceFactory.AvailableReasoners; + +public class DumbLPFinder { + + 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 usedReasoner = OWLAPIReasoner.class; + private static boolean allOrExists = true; + private static boolean tenORthirty = true; + //private static boolean allOrExists = true; + + /** + * @param args + */ + public static void main(String[] args) { + initLogger(); + //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()); + + reasoningService = null; + + int count = 1; + while(count<10000){ + + 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; + } + //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 = learnOriginal( positiveEx, negativeEx); + + + + String div = (System.currentTimeMillis()%10000)+""; + filename+=(d.getDescriptionLength()<10)?"0"+d.getDescriptionLength():d.getDescriptionLength()+""; + if(d.getAccuracy()>=0.99){ + filename +="_99+"; + }else if(d.getAccuracy()>=0.75){ + filename +="_75+"; + }else if(d.getAccuracy()>=0.50){ + filename +="_50+"; + }else { + filename +="_50-"; + } + 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) { + e.printStackTrace(); + }finally{ + ComponentManager.getInstance().freeAllComponents(); + } + //System.out.println(count); + count++; + } + + + } + + 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) { } + + 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"; + for (Individual individual : p) { + str+="\""+individual+"\",\n"; + } + for (Individual individual : n) { + str+="\""+individual+"\",\n"; + } + str = str.substring(0, str.length()-2); + str+="};\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 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 tmp = FastInstanceChecker.class; + if(usedReasoner.equals(tmp)){ + lc.maxExecutionTimeInSeconds = 30; + }else{ + lc.maxExecutionTimeInSeconds = 200; + } + return lc; + + } + + + private static void initLogger() { + + SimpleLayout layout = new SimpleLayout(); + // create logger (a simple logger which outputs + // its messages to the console) + FileAppender fileAppender = null; + try { + fileAppender = new FileAppender(layout, "log/semBibleLog.txt", + false); + } catch (Exception e) { + e.printStackTrace(); + } + + + logger.removeAllAppenders(); + //ConsoleAppender consoleAppender = new ConsoleAppender(layout); + //logger.addAppender(consoleAppender); + logger.addAppender(fileAppender); + logger.setLevel(Level.INFO); + + + } + +} Modified: trunk/src/dl-learner/org/dllearner/utilities/Files.java =================================================================== --- trunk/src/dl-learner/org/dllearner/utilities/Files.java 2008-08-29 21:54:39 UTC (rev 1150) +++ trunk/src/dl-learner/org/dllearner/utilities/Files.java 2008-08-30 01:29:38 UTC (rev 1151) @@ -103,5 +103,16 @@ e.printStackTrace(); } } + + public static void mkdir(String dir){ + if (!new File(dir).exists()) { + try{ + new File(dir).mkdir(); + }catch (Exception e) { + e.printStackTrace(); + // this should not be a show stopper + } + } + } } Modified: trunk/src/dl-learner/org/dllearner/utilities/learn/LearnOWLFile.java =================================================================== --- trunk/src/dl-learner/org/dllearner/utilities/learn/LearnOWLFile.java 2008-08-29 21:54:39 UTC (rev 1150) +++ trunk/src/dl-learner/org/dllearner/utilities/learn/LearnOWLFile.java 2008-08-30 01:29:38 UTC (rev 1151) @@ -45,6 +45,8 @@ public LearnOWLFileConfiguration configuration; + public ComponentManager cm ; + public LearnOWLFile (LearnOWLFileConfiguration configuration){ this.configuration = configuration; } @@ -60,7 +62,7 @@ // the component manager is the central object to create // and configure components - ComponentManager cm = ComponentManager.getInstance(); + cm = ComponentManager.getInstance(); // knowledge source KnowledgeSource ks = cm.knowledgeSource(OWLFile.class); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |