Java tutorial
/* * 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.argumentation.type.DebateArgumentMetaData; 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; /** * Reader for arguments from serialized {@link Debate}s in XML files * <p/> * * @author Ivan Habernal */ public class DebateArgumentReader 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; /** * Argument topic, if known */ public static final String PARAM_ARGUMENT_TOPIC = "argumentTopic"; @ConfigurationParameter(name = PARAM_ARGUMENT_TOPIC, mandatory = false) String argumentTopic; Queue<File> files = new ArrayDeque<>(); Queue<Argument> currentArguments = new ArrayDeque<>(); Debate currentDebate = null; @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)); } } protected void loadArgumentsFromNextFile() throws IOException { File file = files.poll(); Debate debate = DebateSerializer.deserializeFromXML(FileUtils.readFileToString(file, "utf-8")); currentArguments = new ArrayDeque<>(debate.getArgumentList()); // and set current debate currentDebate = debate; } @Override public void getNext(JCas jCas) throws IOException, CollectionException { if (currentArguments.peek() == null) { loadArgumentsFromNextFile(); } Argument argument = currentArguments.poll(); // create argument meta data DebateArgumentMetaData argumentMetaData = new DebateArgumentMetaData(jCas); argumentMetaData.addToIndexes(); argumentMetaData.setArgPoints(argument.getVoteUpCount()); argumentMetaData.setAuthor(argument.getAuthor()); argumentMetaData.setId(argument.getId()); argumentMetaData.setParentId(argument.getParentId()); argumentMetaData.setStance(argument.getStance()); // if we know the topic if (this.argumentTopic != null) { argumentMetaData.setArgumentTopic(argumentTopic); } // and info about the parent debate argumentMetaData.setDebateDescription(currentDebate.getDescription()); argumentMetaData.setDebateTitle(currentDebate.getTitle()); argumentMetaData.setDebateUrl(currentDebate.getUrl()); jCas.setDocumentLanguage("en"); jCas.setDocumentText(argument.getText()); DocumentMetaData metaData = DocumentMetaData.create(jCas); metaData.setDocumentId(argument.getId()); metaData.setDocumentTitle(argument.getStance()); } @Override public boolean hasNext() throws IOException, CollectionException { return !currentArguments.isEmpty() || !files.isEmpty(); } @Override public Progress[] getProgress() { return new Progress[0]; } }