de.tudarmstadt.ukp.experiments.argumentation.sequence.evaluation.JCasIOHelper.java Source code

Java tutorial

Introduction

Here is the source code for de.tudarmstadt.ukp.experiments.argumentation.sequence.evaluation.JCasIOHelper.java

Source

/*
 * Copyright 2016
 * Ubiquitous Knowledge Processing (UKP) Lab
 * Technische Universitt Darmstadt
 *
 * 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 de.tudarmstadt.ukp.experiments.argumentation.sequence.evaluation;

import org.apache.commons.io.IOUtils;
import org.apache.uima.cas.CAS;
import org.apache.uima.cas.impl.XmiCasDeserializer;
import org.apache.uima.fit.factory.TypeSystemDescriptionFactory;
import org.apache.uima.jcas.JCas;
import org.apache.uima.util.CasCreationUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;

/**
 * @author Ivan Habernal
 */
public class JCasIOHelper {
    public static final FilenameFilter XMI_FILTER = new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.matches("\\d+\\.xmi");
        }
    };

    /**
     * Loads JCas from a xmi file given the current typesystem on classpath (not lenient)
     *
     * @param file file
     * @return jcas
     * @throws Exception
     */
    public static JCas loadJCasFromFile(File file) throws Exception {
        CAS cas = CasCreationUtils.createCas(TypeSystemDescriptionFactory.createTypeSystemDescription(), null,
                null);
        FileInputStream in = new FileInputStream(file);
        XmiCasDeserializer.deserialize(in, cas, false);
        IOUtils.closeQuietly(in);

        return cas.getJCas();
    }
}