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 org.nlp2rdf.core.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 * @author Sebastian Hellmann
039 * This class implements the NIF Offset URI Scheme.
040 * http://nlp2rdf.org/nif-1-0#toc-nif-recipe-offset-based-uris
041 * calling init() is not necessary
042 */
043 public class OffsetBased extends AbstractURIGenerator implements URIGenerator {
044 private static Logger log = LoggerFactory.getLogger(OffsetBased.class);
045
046
047 public static final String identifier = "offset";
048
049 @Override
050 public String getRecipeUri() {
051 return "http://nlp2rdf.lod2.eu/schema/string/OffsetBasedString";
052 }
053
054 @Override
055 public void assignRecipeClass(String uri, OntModel model) {
056 OffsetBasedString.create(uri, model);
057 }
058
059 @Override
060 public String makeUri(String base, String text, Span span) {
061
062 StringBuilder sb = new StringBuilder();
063 sb.append(base);
064 sb.append(identifier);
065 sb.append("_");
066 sb.append(span.getStart());
067 sb.append("_");
068 sb.append(span.getEnd());
069 sb.append("_");
070 sb.append(URIGeneratorHelper.getFirstCharacters(span.getCoveredText(text).toString(), firstCharLength));
071 log.trace(sb.toString());
072 return sb.toString();
073 }
074
075 @Override
076 public Span getSpanFor(String prefix, String uri, String text) {
077 StringTokenizer st = new StringTokenizer(uri.substring(prefix.length()), "_");
078 if (!(st.nextToken().equalsIgnoreCase(identifier))) {
079 throw new InvalidParameterException("The span could not be recognized correctly: " + uri + " with prefix " + prefix);
080 }
081 return new Span(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
082 }
083 }