de.tudarmstadt.ukp.dkpro.argumentation.annotations.Tools.java Source code

Java tutorial

Introduction

Here is the source code for de.tudarmstadt.ukp.dkpro.argumentation.annotations.Tools.java

Source

/*
 * Copyright 2014
 * 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.dkpro.argumentation.annotations;

import de.tudarmstadt.ukp.dkpro.core.api.metadata.type.DocumentMetaData;
import de.tudarmstadt.ukp.dkpro.core.io.xmi.XmiWriter;
import org.apache.commons.io.FileUtils;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.fit.component.CasDumpWriter;
import org.apache.uima.fit.factory.AnalysisEngineFactory;
import org.apache.uima.fit.pipeline.SimplePipeline;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;

import java.io.File;

/**
 * @author Ivan Habernal
 */
public class Tools {
    public static String dumpCas(JCas jCas) {
        try {
            File f = File.createTempFile("casdump", ".txt");
            SimplePipeline.runPipeline(jCas, AnalysisEngineFactory.createEngineDescription(CasDumpWriter.class,
                    CasDumpWriter.PARAM_OUTPUT_FILE, f));

            StringBuilder result = new StringBuilder(FileUtils.readFileToString(f));

            FileUtils.deleteQuietly(f);

            result.append("Id:").append(DocumentMetaData.get(jCas).getDocumentId());

            return result.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String getId(JCas jcas) {
        return DocumentMetaData.get(jcas).getDocumentId();
    }

    public static void saveJCas(JCas jcas, String outputDir) {
        try {
            SimplePipeline.runPipeline(jcas, AnalysisEngineFactory.createEngineDescription(XmiWriter.class,
                    XmiWriter.PARAM_TARGET_LOCATION, outputDir));
        } catch (AnalysisEngineProcessException | ResourceInitializationException e) {
            throw new RuntimeException(e);
        }
    }

}