<commit_before>package cz.zcu.kiv.eeg.owlimport.model.rules.export;

import cz.zcu.kiv.eeg.owlimport.RepositoryWrapper;
import cz.zcu.kiv.eeg.owlimport.model.rules.AbstractEmptyParamsRule;
import cz.zcu.kiv.eeg.owlimport.model.rules.RuleExportException;
import info.aduna.iteration.Iteration;
import info.aduna.iteration.IteratorIteration;
import info.aduna.iteration.UnionIteration;
import org.openrdf.model.Statement;
import org.openrdf.model.Value;
import org.openrdf.model.ValueFactory;
import org.openrdf.model.impl.GraphImpl;
import org.openrdf.query.QueryEvaluationException;

import java.util.HashMap;
import java.util.Map;

/**
 * @author <NAME> <<EMAIL>>
 */
public class TrustedSourceRule extends AbstractEmptyParamsRule {
	private static final String RDF_TYPE_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";
	private static final String TRUSTED_SOURCE_CLASS_URI = "http://proton.semanticweb.org/2006/05/protons#Trusted";

	private static final String SOURCE_BASE_URI = "http://kiv.zcu.cz/eeg/KIM";

	private static final String ENTITIES_QUERY =
			"CONSTRUCT {Entity} <http://proton.semanticweb.org/2006/05/protons#generatedBy> {_sourceUri} " +
			"FROM {Entity} rdf:type {Type}, {Type} sesame:directType {ClassType}" +
			"WHERE ClassType = owl:Class";

	private static final String SOURCE_URI_PARAM = "_sourceUri";

	public TrustedSourceRule(String ruleTitle) {
		super(ruleTitle);
	}

	@Override
	public Iteration<Statement, ? extends Exception> getStatements() throws RuleExportException {
		GraphImpl graph = new GraphImpl();
		ValueFactory graphValueFactory = graph.getValueFactory();

		String sourceUri = String.format("%s#%s", SOURCE_BASE_URI, createUriPart(getSource().getTitle()));
		graph.add(graphValueFactory.createURI(sourceUri), graphValueFactory.createURI(RDF_TYPE_URI), graphValueFactory.createURI(TRUSTED_SOURCE_CLASS_URI));

		Iteration<Statement, QueryEvaluationException> sourceDef =  new IteratorIteration<>(graph.iterator());

		RepositoryWrapper repository = getRepository();
		Map<String, Value> queryParams = new HashMap<String, Value>();
		queryParams.put(SOURCE_URI_PARAM, repository.createLiteral(sourceUri));

		Iteration<Statement, QueryEvaluationException> entities;
		try {
			entities = repository.executeGraphQuery(ENTITIES_QUERY, queryParams, true);
		} catch (Exception e) {
			throw new RuleExportException("Error while exporting entities from repository.", e);
		}

		return new UnionIteration<Statement, QueryEvaluationException>(sourceDef, entities);
	}


	private String createUriPart(String title) {
		return title;
	}
}<commit_msg>TrustedSourceRule: normalization of exported source URI.<commit_after>package cz.zcu.kiv.eeg.owlimport.model.rules.export;

import cz.zcu.kiv.eeg.owlimport.RepositoryWrapper;
import cz.zcu.kiv.eeg.owlimport.model.rules.AbstractEmptyParamsRule;
import cz.zcu.kiv.eeg.owlimport.model.rules.RuleExportException;
import info.aduna.iteration.Iteration;
import info.aduna.iteration.IteratorIteration;
import info.aduna.iteration.UnionIteration;
import org.openrdf.model.Statement;
import org.openrdf.model.Value;
import org.openrdf.model.ValueFactory;
import org.openrdf.model.impl.GraphImpl;
import org.openrdf.query.QueryEvaluationException;

import java.text.Normalizer;
import java.util.HashMap;
import java.util.Map;

/**
 * @author <NAME> <<EMAIL>>
 */
public class TrustedSourceRule extends AbstractEmptyParamsRule {
	private static final String RDF_TYPE_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";
	private static final String TRUSTED_SOURCE_CLASS_URI = "http://proton.semanticweb.org/2006/05/protons#Trusted";

	private static final String SOURCE_BASE_URI = "http://kiv.zcu.cz/eeg/KIM/sources";

	private static final String ENTITIES_QUERY =
			"CONSTRUCT {Entity} <http://proton.semanticweb.org/2006/05/protons#generatedBy> {_sourceUri} " +
			"FROM {Entity} rdf:type {Type}, {Type} sesame:directType {ClassType}" +
			"WHERE ClassType = owl:Class";

	private static final String SOURCE_URI_PARAM = "_sourceUri";

	public TrustedSourceRule(String ruleTitle) {
		super(ruleTitle);
	}

	@Override
	public Iteration<Statement, ? extends Exception> getStatements() throws RuleExportException {
		GraphImpl graph = new GraphImpl();
		ValueFactory graphValueFactory = graph.getValueFactory();

		String sourceUri = String.format("%s#%s", SOURCE_BASE_URI, createUriPart(getSource().getTitle()));
		graph.add(graphValueFactory.createURI(sourceUri), graphValueFactory.createURI(RDF_TYPE_URI), graphValueFactory.createURI(TRUSTED_SOURCE_CLASS_URI));

		Iteration<Statement, QueryEvaluationException> sourceDef =  new IteratorIteration<>(graph.iterator());

		RepositoryWrapper repository = getRepository();
		Map<String, Value> queryParams = new HashMap<String, Value>();
		queryParams.put(SOURCE_URI_PARAM, repository.createLiteral(sourceUri));

		Iteration<Statement, QueryEvaluationException> entities;
		try {
			entities = repository.executeGraphQuery(ENTITIES_QUERY, queryParams, true);
		} catch (Exception e) {
			throw new RuleExportException("Error while exporting entities from repository.", e);
		}

		return new UnionIteration<Statement, QueryEvaluationException>(sourceDef, entities);
	}


	private String createUriPart(String title) {
		String noAccents = removeAccents(title);
		return noAccents.replaceAll("[^A-Za-z0-9_\\.-]", "-").replaceAll("-{2,}", "-");
	}


	private String removeAccents(String str) {
		return Normalizer.normalize(str, Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
	}
}
{'fraction_non_alphanumeric': 0.11947301931059376, 'fraction_numerical': 0.008121277747698972, 'mean_word_length': 6.197402597402597, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '5630070', 'n_tokens_mistral': 1842, 'n_tokens_neox': 1729, 'n_words': 324}