<commit_before>/*Copyright (c) 2015 "hbz"

This file is part of etikett.

etikett is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program 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 Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package helper;

import java.net.URL;
import java.text.Normalizer;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;

import org.eclipse.rdf4j.model.BNode;
import org.eclipse.rdf4j.model.Literal;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
import org.eclipse.rdf4j.rio.RDFFormat;

/**
 * @author <NAME>
 *
 */
@SuppressWarnings("javadoc")
public class GndLabelResolver {

    public static Properties turtleResourceProp = new Properties();
    public static Properties turtleObjectProp = new Properties();

    final public static String id = "http://d-nb.info/gnd/";
    final public static String id2 = "https://d-nb.info/gnd/";

    public GndLabelResolver() {
        setProperties();
    }

    private void setProperties() {
        turtleResourceProp.setProperty("protocol", "https://");
        turtleResourceProp.setProperty("alternateProtocol", "http://");
        turtleResourceProp.setProperty("domain", "d-nb.info/gnd/");

        turtleObjectProp.setProperty("namespace", "d-nb.info/standards/elementset/gnd#");
        turtleObjectProp.setProperty("preferredName", "preferredName");
        turtleObjectProp.setProperty("preferredNameForTheConferenceOrEvent", "preferredNameForTheConferenceOrEvent");
        turtleObjectProp.setProperty("preferredNameForTheCorporateBody", "preferredNameForTheCorporateBody");
        turtleObjectProp.setProperty("preferredNameForThePerson", "preferredNameForThePerson");
        turtleObjectProp.setProperty("preferredNameForThePlaceOrGeographicName",
                "preferredNameForThePlaceOrGeographicName");
        turtleObjectProp.setProperty("preferredNameForTheSubjectHeading", "preferredNameForTheSubjectHeading");
        turtleObjectProp.setProperty("preferredNameForTheWork", "preferredNameForTheWork");

    }

    /**
     * @param uri
     *            analyes data from the url to find a proper label
     * @return a label
     */
    public static String lookup(String uri, String language) {
        try {
            play.Logger.info("Lookup Label from GND. Language selection is not supported yet! " + uri);

            Collection<Statement> statement = RdfUtils.readRdfToGraph(new URL(uri + "/about/lds"), RDFFormat.RDFXML,
                    "application/rdf+xml");

            Iterator<Statement> sit = statement.iterator();

            while (sit.hasNext()) {
                Statement s = sit.next();
                boolean isLiteral = s.getObject() instanceof Literal;
                if (!(s.getSubject() instanceof BNode)) {
                    if (isLiteral) {
                        ValueFactory v = SimpleValueFactory.getInstance();
                        Statement newS = v.createStatement(s.getSubject(), s.getPredicate(), v.createLiteral(
                                Normalizer.normalize(s.getObject().stringValue(), Normalizer.Form.NFKC)));
                        String label = findLabel(newS, uri);
                        play.Logger.debug("Gefundenes Label: " + label);
                        if (label != null) {
                            play.Logger.info("Found Label: " + label);
                            return label;
                        }
                    }
                }
            }

        } catch (Exception e) {
            play.Logger.error("Failed to find label for " + uri, e);
        }
        return null;
    }

    private static String findLabel(Statement s, String uri) {

        String namespace = turtleResourceProp.getProperty("protocol") + turtleResourceProp.getProperty("namespace");
        play.Logger.info("huhu: " + namespace);

        Enumeration<Object> keys = turtleObjectProp.keys();
        while (keys.hasMoreElements()) {
            play.Logger.info("huhu");
            String predicate = namespace + turtleObjectProp.getProperty(keys.nextElement().toString());
            play.Logger.debug("Vergleichsstring: " + predicate);
            play.Logger.debug("Vergleichsstring: " + s.getPredicate().stringValue());
            if (predicate.equals(s.getPredicate().stringValue())) {
                return s.getObject().stringValue();
            }
        }

        namespace = turtleResourceProp.getProperty("alternateProtocol") + turtleResourceProp.getProperty("namespace");
        keys = turtleObjectProp.keys();
        while (keys.hasMoreElements()) {
            play.Logger.info("huhu2");
            String predicate = namespace + turtleObjectProp.getProperty(keys.nextElement().toString());
            play.Logger.debug("Vergleichsstring: " + predicate);
            play.Logger.debug("Vergleichsstring: " + s.getPredicate().stringValue());
            if (predicate.equals(s.getPredicate().stringValue())) {
                return s.getObject().stringValue();
            }
        }
        return null;
    }
}<commit_msg>removed refactoring due to ultiple issues<commit_after>/*Copyright (c) 2015 "hbz"

This file is part of etikett.

etikett is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program 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 Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package helper;

import java.net.URL;
import java.text.Normalizer;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;

import org.eclipse.rdf4j.model.BNode;
import org.eclipse.rdf4j.model.Literal;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
import org.eclipse.rdf4j.rio.RDFFormat;

/**
 * @author <NAME>
 *
 */
@SuppressWarnings("javadoc")
public class GndLabelResolver {

    final public static String namespace = "https://d-nb.info/standards/elementset/gnd#";

    final public static String preferredName = namespace + "preferredName";
    final public static String preferredNameForTheConferenceOrEvent = namespace
            + "preferredNameForTheConferenceOrEvent";
    final public static String preferredNameForTheCorporateBody = namespace + "preferredNameForTheCorporateBody";
    final public static String preferredNameForThePerson = namespace + "preferredNameForThePerson";
    final public static String preferredNameForThePlaceOrGeographicName = namespace
            + "preferredNameForThePlaceOrGeographicName";
    final public static String preferredNameForTheSubjectHeading = namespace + "preferredNameForTheSubjectHeading";
    final public static String preferredNameForTheWork = namespace + "preferredNameForTheWork";

    final public static String id = "http://d-nb.info/gnd/";
    final public static String id2 = "https://d-nb.info/gnd/";

    /**
     * @param uri
     *            analyes data from the url to find a proper label
     * @return a label
     */
    public static String lookup(String uri, String language) {
        try {
            play.Logger.info("Lookup Label from GND. Language selection is not supported yet! " + uri);

            Collection<Statement> statement = RdfUtils.readRdfToGraph(new URL(uri + "/about/lds"), RDFFormat.RDFXML,
                    "application/rdf+xml");

            Iterator<Statement> sIt = statement.iterator();

            while (sit.hasNext()) {
                Statement s = sIt.next();
                boolean isLiteral = s.getObject() instanceof Literal;
                if (!(s.getSubject() instanceof BNode)) {
                    if (isLiteral) {
                        ValueFactory v = SimpleValueFactory.getInstance();
                        Statement newS = v.createStatement(s.getSubject(), s.getPredicate(), v.createLiteral(
                                Normalizer.normalize(s.getObject().stringValue(), Normalizer.Form.NFKC)));
                        String label = findLabel(newS, uri);
                        if (label != null) {
                            play.Logger.info("Found Label: " + label);
                            return label;
                        }
                    }
                }
            }

        } catch (Exception e) {
            play.Logger.error("Failed to find label for " + uri, e);
        }
        return null;
    }

    private static String findLabel(Statement s, String uri) {
        if (!uri.equals(s.getSubject().stringValue()))
            return null;
        if (preferredName.equals(s.getPredicate().stringValue())) {
            return s.getObject().stringValue();
        }
        if (preferredNameForThePerson.equals(s.getPredicate().stringValue())) {
            return s.getObject().stringValue();
        }
        if (preferredNameForTheConferenceOrEvent.equals(s.getPredicate().stringValue())) {
            return s.getObject().stringValue();
        }
        if (preferredNameForTheCorporateBody.equals(s.getPredicate().stringValue())) {
            return s.getObject().stringValue();
        }
        if (preferredNameForThePerson.equals(s.getPredicate().stringValue())) {
            return s.getObject().stringValue();
        }
        if (preferredNameForThePlaceOrGeographicName.equals(s.getPredicate().stringValue())) {
            return s.getObject().stringValue();
        }
        if (preferredNameForTheSubjectHeading.equals(s.getPredicate().stringValue())) {
            return s.getObject().stringValue();
        }
        if (preferredNameForTheWork.equals(s.getPredicate().stringValue())) {
            return s.getObject().stringValue();
        }
        return null;
    }
}
{'fraction_non_alphanumeric': 0.10410270722857941, 'fraction_numerical': 0.0023257977486277792, 'mean_word_length': 2.675213675213675, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '499434', 'n_tokens_mistral': 2902, 'n_tokens_neox': 2797, 'n_words': 699}