001/**
002 * Copyright (C) 2007 - 2016, Jens Lehmann
003 *
004 * This file is part of DL-Learner.
005 *
006 * DL-Learner is free software; you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation; either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * DL-Learner is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package org.dllearner.algorithms.qtl.filters;
020
021import java.util.Set;
022
023import uk.ac.shef.wit.simmetrics.similaritymetrics.AbstractStringMetric;
024import uk.ac.shef.wit.simmetrics.similaritymetrics.QGramsDistance;
025
026import org.apache.jena.rdf.model.Property;
027import org.apache.jena.rdf.model.RDFNode;
028import org.apache.jena.rdf.model.Resource;
029import org.apache.jena.rdf.model.Selector;
030import org.apache.jena.rdf.model.Statement;
031
032public class QuestionBasedStatementSelector implements Selector {
033        
034        private Set<String> questionWords;
035        private AbstractStringMetric metric;
036        private double threshold = 0.5;
037        int cnt = 0;
038        
039        public QuestionBasedStatementSelector(Set<String> questionWords){
040                this.questionWords = questionWords;
041                metric = new QGramsDistance();
042                
043        }
044
045        @Override
046        public boolean test(Statement s) {
047                String predicate = s.getPredicate().getURI().substring(s.getPredicate().getURI().lastIndexOf("/"));
048                String object = null;
049                if(s.getObject().isURIResource()){
050                        object = s.getObject().asResource().getURI();
051                        object = object.substring(object.lastIndexOf("/")+1);
052                } else if(s.getObject().isLiteral()){
053                        object = s.getObject().asLiteral().getLexicalForm();
054                }
055                return isSimiliar2QuestionWord(object) || isSimiliar2QuestionWord(predicate);
056
057        }
058        
059        private boolean isSimiliar2QuestionWord(String s){
060                for(String word : questionWords){
061                        if(areSimiliar(word, s)){
062                                return true;
063                        }
064                }
065                return false;
066        }
067        
068        private boolean areSimiliar(String s1, String s2){//cnt++;System.out.println(cnt);
069                float sim = metric.getSimilarity(s1, s2);
070                return sim >= threshold;
071        }
072
073        @Override
074        public boolean isSimple() {
075                return false;
076        }
077
078        @Override
079        public Resource getSubject() {
080                return null;
081        }
082
083        @Override
084        public Property getPredicate() {
085                return null;
086        }
087
088        @Override
089        public RDFNode getObject() {
090                return null;
091        }
092
093}