<commit_before>/*
 *    Copyright 2015 Textocat
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.textocat.textokit.eval;

import org.apache.commons.lang3.StringUtils;
import org.apache.uima.UIMAException;
import org.apache.uima.cas.CAS;
import org.apache.uima.cas.TypeSystem;
import org.apache.uima.resource.metadata.TypeSystemDescription;
import org.apache.uima.util.CasCreationUtils;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.PostConstruct;
import java.io.IOException;

import static java.util.Arrays.asList;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.uima.fit.factory.TypeSystemDescriptionFactory.createTypeSystemDescription;
import static org.apache.uima.fit.factory.TypeSystemDescriptionFactory.createTypeSystemDescriptionFromPath;
import static org.apache.uima.util.CasCreationUtils.mergeTypeSystems;

/**
 * TODO move to deeper package, make non-public
 *
 * @author <NAME>
 */
public class TypeSystemInitializer implements FactoryBean<TypeSystem> {

    @Value("${typeSystem.description.paths}")
    private String typeSystemDescPathsString;
    @Value("${typeSystem.description.names}")
    private String typeSystemDescNamesString;

    // derived
    private String[] typeSystemDescPaths;
    private String[] typeSystemDescNames;

    private TypeSystem ts;

    @SuppressWarnings("unused")
    @PostConstruct
    private void init() {
        if (!isBlank(typeSystemDescPathsString)) {
            typeSystemDescPaths = StringUtils.split(typeSystemDescPathsString, ";,");
        }
        if (!isBlank(typeSystemDescNamesString)) {
            typeSystemDescNames = StringUtils.split(typeSystemDescNamesString, ";,");
        }
    }

    private TypeSystem createTypeSystem() throws IOException, UIMAException {
        TypeSystemDescription tsDesc = null;
        if (typeSystemDescPaths != null && typeSystemDescPaths.length > 0) {
            tsDesc = createTypeSystemDescriptionFromPath(typeSystemDescPaths);
        }
        if (typeSystemDescNames != null && typeSystemDescNames.length > 0) {
            TypeSystemDescription tsDescFromNames = createTypeSystemDescription(
                    typeSystemDescNames);
            if (tsDesc != null) {
                tsDesc = mergeTypeSystems(asList(tsDesc, tsDescFromNames));
            } else {
                tsDesc = tsDescFromNames;
            }
        }
        if (tsDesc == null) {
            throw new IllegalStateException(
                    "Type system description paths or names were not specified!");
        }
        CAS dumbCas = CasCreationUtils.createCas(tsDesc, null, null);
        TypeSystem typeSystem = dumbCas.getTypeSystem();
        // printAllTypes();
        return typeSystem;
    }

    @Override
    public TypeSystem getObject() throws Exception {
        if (ts == null) {
            ts = createTypeSystem();
        }
        return ts;
    }

    @Override
    public Class<?> getObjectType() {
        return TypeSystem.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}<commit_msg>enable a fallback to UIMAFit TSD discovery in Eval's TypeSystemInitializer<commit_after>/*
 *    Copyright 2015 Textocat
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.textocat.textokit.eval;

import org.apache.commons.lang3.StringUtils;
import org.apache.uima.UIMAException;
import org.apache.uima.cas.CAS;
import org.apache.uima.cas.TypeSystem;
import org.apache.uima.fit.factory.TypeSystemDescriptionFactory;
import org.apache.uima.resource.metadata.TypeSystemDescription;
import org.apache.uima.util.CasCreationUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.PostConstruct;
import java.io.IOException;

import static java.util.Arrays.asList;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.uima.fit.factory.TypeSystemDescriptionFactory.createTypeSystemDescription;
import static org.apache.uima.fit.factory.TypeSystemDescriptionFactory.createTypeSystemDescriptionFromPath;
import static org.apache.uima.util.CasCreationUtils.mergeTypeSystems;

/**
 * TODO move to deeper package, make non-public
 *
 * @author <NAME>
 */
public class TypeSystemInitializer implements FactoryBean<TypeSystem> {

    private final Logger log = LoggerFactory.getLogger(getClass());

    @Value("${typeSystem.description.paths}")
    private String typeSystemDescPathsString;
    @Value("${typeSystem.description.names}")
    private String typeSystemDescNamesString;

    // derived
    private String[] typeSystemDescPaths;
    private String[] typeSystemDescNames;

    private TypeSystem ts;

    @SuppressWarnings("unused")
    @PostConstruct
    private void init() {
        if (!isBlank(typeSystemDescPathsString)) {
            typeSystemDescPaths = StringUtils.split(typeSystemDescPathsString, ";,");
        }
        if (!isBlank(typeSystemDescNamesString)) {
            typeSystemDescNames = StringUtils.split(typeSystemDescNamesString, ";,");
        }
    }

    private TypeSystem createTypeSystem() throws IOException, UIMAException {
        TypeSystemDescription tsDesc = null;
        if (typeSystemDescPaths != null && typeSystemDescPaths.length > 0) {
            tsDesc = createTypeSystemDescriptionFromPath(typeSystemDescPaths);
        }
        if (typeSystemDescNames != null && typeSystemDescNames.length > 0) {
            TypeSystemDescription tsDescFromNames = createTypeSystemDescription(
                    typeSystemDescNames);
            if (tsDesc != null) {
                tsDesc = mergeTypeSystems(asList(tsDesc, tsDescFromNames));
            } else {
                tsDesc = tsDescFromNames;
            }
        }
        if (tsDesc == null) {
            log.info("TypeSystemDescription will be created using the UIMAFit discovery");
            tsDesc = TypeSystemDescriptionFactory.createTypeSystemDescription();
        }
        CAS dumbCas = CasCreationUtils.createCas(tsDesc, null, null);
        TypeSystem typeSystem = dumbCas.getTypeSystem();
        // printAllTypes();
        return typeSystem;
    }

    @Override
    public TypeSystem getObject() throws Exception {
        if (ts == null) {
            ts = createTypeSystem();
        }
        return ts;
    }

    @Override
    public Class<?> getObjectType() {
        return TypeSystem.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
{'fraction_non_alphanumeric': 0.08857658586900465, 'fraction_numerical': 0.0033522434244455906, 'mean_word_length': 3.1839266450916934, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '144220', 'n_tokens_mistral': 2068, 'n_tokens_neox': 2040, 'n_words': 516}