/*******************************************************************************
 * Copyright 2014
 * FG Language Technology
 * Technische Universität 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.tu.darmstadt.lt.ner.writer;

import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence;
import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token;
import de.tudarmstadt.ukp.dkpro.core.opennlp.OpenNlpSegmenter;
import org.apache.commons.io.IOUtils;
import org.apache.uima.analysis_engine.AnalysisEngine;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.fit.component.JCasConsumer_ImplBase;
import org.apache.uima.fit.descriptor.ConfigurationParameter;
import org.apache.uima.fit.util.JCasUtil;
import org.apache.uima.jcas.JCas;

import java.io.FileOutputStream;
import java.util.*;

import static org.apache.uima.fit.factory.AnalysisEngineFactory.createEngine;

/**
 * This is a helper Class, can be used from NoD. If you use a DKPro tokenizer during training, this
 * method use the same tokenizer available in DKPro,
 */
public class SentenceToCRFTestFileWriter
        extends JCasConsumer_ImplBase
{
    public static final String SENTENCE_ITERATOR = "iterator";
    @ConfigurationParameter(name = SENTENCE_ITERATOR, mandatory = true)
    private List<String> sentences = null;

    public static final String CRF_TEST_FILE_NAME = "crfFileName";
    @ConfigurationParameter(name = CRF_TEST_FILE_NAME, mandatory = true)
    private String crfFileName = null;

    public static final String CRF_TEST_FILE_LANG = "crfTestFileLanguage";
    @ConfigurationParameter(name = CRF_TEST_FILE_LANG, mandatory = false)
    private String crfTestFileLanguage = "de";

    public static final String LF = System.getProperty("line.separator");



    @Override
    public void process(JCas jcas) throws AnalysisEngineProcessException {
        try {
            StringBuilder sb = new StringBuilder();
            int index  = 0;

            for (String l: sentences) {
                Sentence sentence = new Sentence(jcas, index, l.length() + index);
                sentence.addToIndexes();
                index  = index + l.length() + 1;
                sb.append(l + "\n");
            }

            jcas.setDocumentText(sb.toString().trim());

            AnalysisEngine pipeline = createEngine(OpenNlpSegmenter.class,
                    OpenNlpSegmenter.PARAM_LANGUAGE, crfTestFileLanguage, OpenNlpSegmenter.PARAM_CREATE_SENTENCES,
                    false);
            pipeline.process(jcas);

            // get the token from jcas and convert it to CRF test file format. one token per line,
            // with
            // out gold.
            StringBuilder sbCRF = new StringBuilder();

            Map<Sentence, Collection<Token>> sentencesTokens = JCasUtil.indexCovered(jcas,
                    Sentence.class, Token.class);
            List<Sentence> sentences = new ArrayList<Sentence>(sentencesTokens.keySet());
            // sort sentences by sentence
            Collections.sort(sentences, new Comparator<Sentence>()
            {
                @Override
                public int compare(Sentence arg0, Sentence arg1)
                {
                    return arg0.getBegin() - arg1.getBegin();
                }
            });

            for (Sentence sentence : sentences) {
                for (Token token : sentencesTokens.get(sentence)) {
                    sbCRF.append(token.getCoveredText() + LF);
                }
                sbCRF.append(LF);
            }

            IOUtils.write(sbCRF.toString(), new FileOutputStream(crfFileName), "UTF-8");
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
@@ -841,199 +841,275 @@
 ort 
-de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentenc
+static org.apache.uima.fit.factory.AnalysisEngineFactory.createEngin
 e;%0A
+%0A
 import 
-de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token;%0Aimport de.tudarmstadt.ukp.dkpro.core.opennlp.OpenNlpSegmenter
+java.io.FileOutputStream;%0Aimport java.util.ArrayList;%0Aimport java.util.Collection;%0Aimport java.util.Collections;%0Aimport java.util.Comparator;%0Aimport java.util.List;%0Aimport java.util.Map
 ;%0A
+%0A
 impo
@@ -1478,131 +1478,197 @@
 ort 
-java.io.FileOutputStream;%0Aimport java.util.*;%0A%0Aimport static org.apache.uima.fit.factory.AnalysisEngineFactory
+de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence;%0Aimport de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token;%0Aimport de.tudarmstadt.ukp.dkpro
 .c
+o
 re
-ateEngine
+.opennlp.OpenNlpSegmenter
 ;%0A%0A/
@@ -3223,12 +3223,11 @@
 RAM_
-CREA
+WRI
 TE_S
@@ -3233,17 +3233,16 @@
 SENTENCE
-S
 ,%0A      

upgrade to dkpro 1.7