<commit_before>package de.linkvt.bachelor.config;

import de.linkvt.bachelor.features.Feature;
import de.linkvt.bachelor.features.FeatureComparator;
import de.linkvt.bachelor.generator.OntologyGenerator;

import org.apache.commons.codec.binary.Hex;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLMutableOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.web.context.WebApplicationContext;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

/**
 * Creates beans for OWL API classes.
 */
@Configuration
public class OwlApiConfig {

  @Bean
  public OWLDataFactory dataFactory() {
    return OWLManager.getOWLDataFactory();
  }

  @Bean
  @Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.INTERFACES)
  public OWLMutableOntology owlOntology(HttpServletRequest request, OntologyGenerator generator) throws OWLOntologyCreationException {
    OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager();
    // Cast to a mutable ontology to pass OWLApi's strange checks
    return (OWLMutableOntology) ontologyManager.createOntology(IRI.create(createOntologyIri(request, generator)));
  }

  private String createOntologyIri(HttpServletRequest request, OntologyGenerator generator) {
    String url = request.getRequestURL().toString();

    // ignore urls of stored generations
    if (!url.matches(".*\\d+/$")) {
      SortedSet<Feature> features = new TreeSet<>(new FeatureComparator());
      features.addAll(generator.getFeatures());
      String featureString = features.stream().map(Feature::getToken).collect(Collectors.joining(","));

      try {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(featureString.getBytes());
        url += "tmp/" + String.valueOf(Hex.encodeHex(digest.digest())) + "/";<commit_msg>Add slash before appending the tmp path<commit_after>package de.linkvt.bachelor.config;

import de.linkvt.bachelor.features.Feature;
import de.linkvt.bachelor.features.FeatureComparator;
import de.linkvt.bachelor.generator.OntologyGenerator;

import org.apache.commons.codec.binary.Hex;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLMutableOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.web.context.WebApplicationContext;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

/**
 * Creates beans for OWL API classes.
 */
@Configuration
public class OwlApiConfig {

  @Bean
  public OWLDataFactory dataFactory() {
    return OWLManager.getOWLDataFactory();
  }

  @Bean
  @Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.INTERFACES)
  public OWLMutableOntology owlOntology(HttpServletRequest request, OntologyGenerator generator) throws OWLOntologyCreationException {
    OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager();
    // Cast to a mutable ontology to pass OWLApi's strange checks
    return (OWLMutableOntology) ontologyManager.createOntology(IRI.create(createOntologyIri(request, generator)));
  }

  private String createOntologyIri(HttpServletRequest request, OntologyGenerator generator) {
    String url = request.getRequestURL().toString();
    if (!url.endsWith("/")) {
      url += "/";
    }

    // ignore urls of stored generations
    if (!url.matches(".*\\d+/$")) {
      SortedSet<Feature> features = new TreeSet<>(new FeatureComparator());
      features.addAll(generator.getFeatures());
      String featureString = features.stream().map(Feature::getToken).collect(Collectors.joining(","));

      try {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(featureString.getBytes());
        url += "tmp/" + String.valueOf(Hex.encodeHex(digest.digest())) + "/";
{'fraction_non_alphanumeric': 0.10648148148148148, 'fraction_numerical': 0.00040257648953301127, 'mean_word_length': 5.581456953642384, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '11831', 'n_tokens_mistral': 1461, 'n_tokens_neox': 1431, 'n_words': 255}