de.tudarmstadt.ukp.experiments.argumentation.comments.pipeline.FullDebateContentReader.java Source code

Java tutorial

Introduction

Here is the source code for de.tudarmstadt.ukp.experiments.argumentation.comments.pipeline.FullDebateContentReader.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.comments.pipeline;

import de.tudarmstadt.ukp.experiments.web.comments.createdebate.Argument;
import de.tudarmstadt.ukp.experiments.web.comments.createdebate.Debate;
import de.tudarmstadt.ukp.experiments.web.comments.createdebate.DebateSerializer;
import de.tudarmstadt.ukp.dkpro.core.api.metadata.type.DocumentMetaData;
import org.apache.commons.io.FileUtils;
import org.apache.uima.UimaContext;
import org.apache.uima.collection.CollectionException;
import org.apache.uima.fit.component.JCasCollectionReader_ImplBase;
import org.apache.uima.fit.descriptor.ConfigurationParameter;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.util.Progress;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Queue;

/**
 * Treats the whole debate as a single piece of text
 *
 * @author Ivan Habernal
 */
public class FullDebateContentReader extends JCasCollectionReader_ImplBase {
    /**
     * Folder containing serialized XML files with {@link Debate} object in each file
     */
    public static final String PARAM_SOURCE_LOCATION = "sourceLocation";
    @ConfigurationParameter(name = PARAM_SOURCE_LOCATION, mandatory = true)
    File sourceLocation;

    Queue<File> files = new ArrayDeque<>();

    @Override
    public void initialize(UimaContext context) throws ResourceInitializationException {
        super.initialize(context);

        File[] fileArray = sourceLocation.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".xml");
            }
        });

        if (fileArray != null) {
            this.files.addAll(Arrays.asList(fileArray));
        }

    }

    @Override
    public void getNext(JCas jCas) throws IOException, CollectionException {
        File file = files.poll();

        Debate debate = DebateSerializer.deserializeFromXML(FileUtils.readFileToString(file, "utf-8"));

        // concatenate all texts together
        StringBuilder sb = new StringBuilder();
        sb.append(debate.getTitle()).append("\n").append(debate.getDescription());

        for (Argument argument : debate.getArgumentList()) {
            sb.append(argument.getText()).append("\n");
        }

        String debateText = sb.toString().trim();

        DocumentMetaData metaData = DocumentMetaData.create(jCas);
        metaData.setDocumentTitle(debate.getTitle());
        metaData.setDocumentId(debate.getUrl());

        jCas.setDocumentText(debateText);
        jCas.setDocumentLanguage("en");
    }

    @Override
    public boolean hasNext() throws IOException, CollectionException {
        return !files.isEmpty();
    }

    @Override
    public Progress[] getProgress() {
        return new Progress[0];
    }
}