001    /***************************************************************************/
002    /*  Copyright (C) 2010-2011, Sebastian Hellmann                            */
003    /*  Note: If you need parts of NLP2RDF in another licence due to licence   */
004    /*  incompatibility, please mail hellmann@informatik.uni-leipzig.de        */
005    /*                                                                         */
006    /*  This file is part of NLP2RDF.                                          */
007    /*                                                                         */
008    /*  NLP2RDF is free software; you can redistribute it and/or modify        */
009    /*  it under the terms of the GNU General Public License as published by   */
010    /*  the Free Software Foundation; either version 3 of the License, or      */
011    /*  (at your option) any later version.                                    */
012    /*                                                                         */
013    /*  NLP2RDF is distributed in the hope that it will be useful,             */
014    /*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
015    /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the           */
016    /*  GNU General Public License for more details.                           */
017    /*                                                                         */
018    /*  You should have received a copy of the GNU General Public License      */
019    /*  along with this program. If not, see <http://www.gnu.org/licenses/>.   */
020    /***************************************************************************/
021    
022    package org.nlp2rdf.core.impl;
023    
024    import com.hp.hpl.jena.ontology.OntModel;
025    import eu.lod2.nlp2rdf.schema.str.*;
026    import opennlp.tools.util.Span;
027    import org.nlp2rdf.core.URIGenerator;
028    import org.nlp2rdf.core.util.URIGeneratorHelper;
029    import org.slf4j.Logger;
030    import org.slf4j.LoggerFactory;
031    
032    import java.lang.String;
033    import java.security.InvalidParameterException;
034    import java.util.Set;
035    import java.util.StringTokenizer;
036    
037    /**
038     * User: Sebastian Hellmann - http://bis.informatik.uni-leipzig.de/SebastianHellmann
039     */
040    public class OffsetBased extends AbstractURIGenerator implements URIGenerator {
041        private static Logger log = LoggerFactory.getLogger(OffsetBased.class);
042    
043        int firstCharLength = 20;
044        public static final String  identifier = "offset";
045    
046        @Override
047        public String getRecipeUri() {
048            return "http://nlp2rdf.lod2.eu/schema/string/OffsetBasedString";
049        }
050    
051        @Override
052        public void assignRecipeClass(String uri, OntModel model) {
053            OffsetBasedString.create(uri, model);
054        }
055    
056        @Override
057        public String makeUri(String base, String text, Span span) {
058    
059            StringBuilder sb = new StringBuilder();
060            sb.append(base);
061            sb.append(identifier);
062            sb.append("_");
063            sb.append(span.getStart());
064            sb.append("_");
065            sb.append(span.getEnd());
066            sb.append("_");
067            sb.append(URIGeneratorHelper.getFirstCharacters(span.getCoveredText(text).toString(), firstCharLength));
068            log.trace(sb.toString());
069            return sb.toString();
070        }
071    
072    
073        @Override
074        public Span getSpanFor(String prefix, String uri, String text, String anchoredPart) {
075            StringTokenizer st = new StringTokenizer(uri.substring(prefix.length()), "_");
076            if(! (st.nextToken().equalsIgnoreCase(identifier))){
077                throw new InvalidParameterException("The span could not be recognized correctly: "+uri+" with prefix "+prefix);
078            }
079           return new Span(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()));
080        }
081    
082         /*
083            @Override
084               public Span getSpanFor(String prefix, String uri, String text, String anchoredPart) {
085                   String rest = uri.substring(prefix.length());
086                   if(!rest.startsWith(identifier)){
087                       throw new InvalidParameterException("The could not be recognized correctly: "+uri);
088                   }
089                   return null;
090               }
091              */
092    
093    }