<commit_before>package se.kb.libris.whelks.plugin

import groovy.util.logging.Slf4j as Log

import com.damnhandy.uri.template.UriTemplate

import se.kb.libris.whelks.Document


@Log
class LibrisURIMinter extends BasicPlugin implements URIMinter {

    static final char[] ALPHANUM = "0123456789abcdefghijklmnopqrstuvwxyz".chars
    static final char[] VOWELS = "auoeiy".chars
    static final char[] DEVOWELLED = ALPHANUM.findAll { !VOWELS.contains(it) } as char[]

    URI base
    String typeKey = '@type'
    String documentUriTemplate
    String thingUriTemplate
    String objectLink
    String epochDate
    char[] alphabet = ALPHANUM
    String randomVariable = null
    int maxRandom = 0
    String timestampVariable = null
    boolean timestampCaesarCipher = false
    String uuidVariable = null
    boolean slugCharInAlphabet = true
    String slugCase = "lower"
    String compoundSlugSeparator = ""
    Map<String, MintRuleSet> rulesByDataset
    int minSlugSize = 2
    int maxWordsInSlug = 6
    int shortWordSize = 3

    private long epochOffset
    private int checkForWordsMinSize = maxWordsInSlug * (shortWordSize + 1)

    LibrisURIMinter() {
        if (base != null) {
            this.setBase(base)
        }
        this.timestampCaesarCipher = timestampCaesarCipher
        this.setEpochDate(epochDate)
        this.alphabet = alphabet
    }

    void setBase(String uri) {
        this.base = new URI(uri)
    }

    void setEpochDate(String epochDate) {
        if (epochDate instanceof String) {
            epochOffset = Date.parse("yyyy-MM-dd", epochDate).getTime()
        }
        this.epochDate = epochDate
    }

    void setRulesByDataset(Map rules) {
        def map = [:]
        this.rulesByDataset = map
        rules.each { k, v ->
            map[k] = new MintRuleSet(v)
        }
    }

    int createRandom() {
        return new Random().nextInt(maxRandom)
    }

    long createTimestamp() {
        return System.currentTimeMillis() - epochOffset
    }

    String createUUID() {
        return UUID.randomUUID()
    }

    URI mint(Document doc) {
        return base.resolve(computePath(doc))
    }

    String computePath(Document doc) {
        computePath(doc.getDataAsMap())
    }

    Map computePaths(Map data, String dataset) {
        def results = [:]
        def object = data
        if (objectLink) {
            object = data[objectLink]
        }
        if (documentUriTemplate) {
            def thingUri = computePath(object, dataset)
            results['thing'] = thingUri
            results['document'] = UriTemplate.expand(documentUriTemplate,
                    [thing: thingUri])
        } else {
            def documentUri = computePath(object, dataset)
            results['document'] = documentUri
            if (thingUriTemplate) {
                results['thing'] = UriTemplate.expand(thingUriTemplate,
                        [document: documentUri])
            }
        }
        log.info "Computed ${results} for object in ${dataset}"
        return results
    }

    String computePath(Map data, String dataset) {
        def vars = [:]
        if (timestampVariable) {
            vars[timestampVariable] = baseEncode(createTimestamp(), timestampCaesarCipher)
        }
        if (randomVariable) {
            vars[randomVariable] = baseEncode(createRandom())
        }
        if (uuidVariable) {
            vars[uuidVariable] = createUUID()
        }

        def type = data[typeKey]<commit_msg>Lowered log output to debug<commit_after>package se.kb.libris.whelks.plugin

import groovy.util.logging.Slf4j as Log

import com.damnhandy.uri.template.UriTemplate

import se.kb.libris.whelks.Document


@Log
class LibrisURIMinter extends BasicPlugin implements URIMinter {

    static final char[] ALPHANUM = "0123456789abcdefghijklmnopqrstuvwxyz".chars
    static final char[] VOWELS = "auoeiy".chars
    static final char[] DEVOWELLED = ALPHANUM.findAll { !VOWELS.contains(it) } as char[]

    URI base
    String typeKey = '@type'
    String documentUriTemplate
    String thingUriTemplate
    String objectLink
    String epochDate
    char[] alphabet = ALPHANUM
    String randomVariable = null
    int maxRandom = 0
    String timestampVariable = null
    boolean timestampCaesarCipher = false
    String uuidVariable = null
    boolean slugCharInAlphabet = true
    String slugCase = "lower"
    String compoundSlugSeparator = ""
    Map<String, MintRuleSet> rulesByDataset
    int minSlugSize = 2
    int maxWordsInSlug = 6
    int shortWordSize = 3

    private long epochOffset
    private int checkForWordsMinSize = maxWordsInSlug * (shortWordSize + 1)

    LibrisURIMinter() {
        if (base != null) {
            this.setBase(base)
        }
        this.timestampCaesarCipher = timestampCaesarCipher
        this.setEpochDate(epochDate)
        this.alphabet = alphabet
    }

    void setBase(String uri) {
        this.base = new URI(uri)
    }

    void setEpochDate(String epochDate) {
        if (epochDate instanceof String) {
            epochOffset = Date.parse("yyyy-MM-dd", epochDate).getTime()
        }
        this.epochDate = epochDate
    }

    void setRulesByDataset(Map rules) {
        def map = [:]
        this.rulesByDataset = map
        rules.each { k, v ->
            map[k] = new MintRuleSet(v)
        }
    }

    int createRandom() {
        return new Random().nextInt(maxRandom)
    }

    long createTimestamp() {
        return System.currentTimeMillis() - epochOffset
    }

    String createUUID() {
        return UUID.randomUUID()
    }

    URI mint(Document doc) {
        return base.resolve(computePath(doc))
    }

    String computePath(Document doc) {
        computePath(doc.getDataAsMap())
    }

    Map computePaths(Map data, String dataset) {
        def results = [:]
        def object = data
        if (objectLink) {
            object = data[objectLink]
        }
        if (documentUriTemplate) {
            def thingUri = computePath(object, dataset)
            results['thing'] = thingUri
            results['document'] = UriTemplate.expand(documentUriTemplate,
                    [thing: thingUri])
        } else {
            def documentUri = computePath(object, dataset)
            results['document'] = documentUri
            if (thingUriTemplate) {
                results['thing'] = UriTemplate.expand(thingUriTemplate,
                        [document: documentUri])
            }
        }
        log.debug "Computed ${results} for object in ${dataset}"
        return results
    }

    String computePath(Map data, String dataset) {
        def vars = [:]
        if (timestampVariable) {
            vars[timestampVariable] = baseEncode(createTimestamp(), timestampCaesarCipher)
        }
        if (randomVariable) {
            vars[randomVariable] = baseEncode(createRandom())
        }
        if (uuidVariable) {
            vars[uuidVariable] = createUUID()
        }

        def type = data[typeKey]
{'fraction_non_alphanumeric': 0.08806290207290922, 'fraction_numerical': 0.0045746962115796996, 'mean_word_length': 2.2615384615384615, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3868876', 'n_tokens_mistral': 2051, 'n_tokens_neox': 1966, 'n_words': 463}