package ch.epfl.bbp.uima.ae;

import static ch.epfl.bbp.uima.BlueUima.PARAM_MODEL_FILE;
import static ch.epfl.bbp.uima.ae.PosTagAnnotator.PARAM_TAG_DICT;
import static com.google.common.base.Preconditions.checkArgument;
import static org.apache.uima.fit.factory.AnalysisEngineFactory.createEngineDescription;
import static org.apache.uima.fit.pipeline.SimplePipeline.runPipeline;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.uima.UIMAException;
import org.apache.uima.analysis_engine.AnalysisEngineDescription;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.epfl.bbp.shaded.opennlp.tools.lang.english.SentenceDetector;
import ch.epfl.bbp.uima.BlueUima;
import ch.epfl.bbp.uima.testutils.UimaTests;

@Deprecated(/* Use annotators directly instead */)
public class OpenNlpHelper {
    private static Logger LOG = LoggerFactory.getLogger(OpenNlpHelper.class);

    public final static String OPENNLP_ROOT = BlueUima.BLUE_UIMA_ROOT
            + "resources/opennlp/";

    @Deprecated(/* Use SentenceAnnotator directly with appropriate model */)
    public static AnalysisEngineDescription getSentenceSplitter()
            throws ResourceInitializationException {
        String modelFile = OPENNLP_ROOT + "/sentence/SentDetectPennBio.bin.gz";
        checkArgument(new File(modelFile).exists(), "no model file at "
                + modelFile);
        return createEngineDescription(SentenceAnnotator.class,
                BlueUima.PARAM_MODEL_FILE, modelFile);
    }

    @Deprecated(/* Use TokenAnnotator directly with appropriate model */)
    public static AnalysisEngineDescription getTokenizer()
            throws ResourceInitializationException {
        String modelFile = OPENNLP_ROOT + "/token/TokenizerGenia.bin.gz";
        checkArgument(new File(modelFile).exists(), "no model file at "
                + modelFile);
        return createEngineDescription(TokenAnnotator.class,
                PARAM_MODEL_FILE, modelFile);
    }

    @Deprecated(/* Use PosTagAnnotator directly with appropriate model */)
    public static AnalysisEngineDescription getPosTagger()
            throws ResourceInitializationException {
        String tagDict = OPENNLP_ROOT + "/postag/tagdict-genia";
        String modelFile = OPENNLP_ROOT + "/postag/Tagger_Genia.bin.gz";
        checkArgument(new File(tagDict).exists(), "no tag dict file at "
                + tagDict);
        return createEngineDescription(PosTagAnnotator.class,
                PARAM_TAG_DICT, tagDict, PARAM_MODEL_FILE, modelFile);
    }

    /**
     * Conv. method to use the SentenceDetector directly; create it with the
     * method below
     */
    public static List<String> splitSentence2(String text, SentenceDetector sd) {
        List<String> sentences = new ArrayList<String>();
        try {
            int sentenceOffsets[] = sd.sentPosDetect(text);

            int begin = 0;
            int end = 0;
            for (int i = 0; i < sentenceOffsets.length; i++) {
                end = begin
                        + (text.substring(begin, sentenceOffsets[i]).trim())
                                .length();
                sentences.add(text.substring(begin, end));
                begin = sentenceOffsets[i];
            }
        } catch (Exception e) {
            LOG.warn("failed to extract sentences from text '" + text + "'", e);
        }
        return sentences;
    }

    @Deprecated(/* Use SentenceDetector directly with appropriate model */)
    public static ch.epfl.bbp.shaded.opennlp.tools.lang.english.SentenceDetector getSentenceDetector()
            throws IOException {
        String modelFile = OPENNLP_ROOT + "/sentence/SentDetectPennBio.bin.gz";
        checkArgument(new File(modelFile).exists(), "no model file at "
                + modelFile);
        return new ch.epfl.bbp.shaded.opennlp.tools.lang.english.SentenceDetector(
                modelFile);
    }

    @Deprecated(/* Use ChunkAnnotator directly with appropriate model */)
    public static AnalysisEngineDescription getChunker()
            throws ResourceInitializationException {
        String modelFile = OPENNLP_ROOT + "/chunker/Chunker_Genia.bin.gz";
        return createEngineDescription(ChunkAnnotator.class,
                PARAM_MODEL_FILE, modelFile);
    }

    /**
     * @param text
     * @return a JCas that has been (naively) sentence splitted and tokenized
     */
    public static JCas getOpenNlpTokenizedTestCas(String text)
            throws UIMAException, IOException {
        JCas testCas = UimaTests.getTestCas(text);
        runPipeline(testCas, getSentenceSplitter(), getTokenizer());
        return testCas;
    }
}

@@ -1341,32 +1341,80 @@
 tionException %7B%0A
+        warnDeprecated(%22getSentenceSplitter%22);%0A%0A
         String m
@@ -1469,32 +1469,32 @@
 ennBio.bin.gz%22;%0A
-
         checkArg
@@ -1883,32 +1883,73 @@
 tionException %7B%0A
+        warnDeprecated(%22getTokenizer%22);%0A%0A
         String m
@@ -2401,32 +2401,73 @@
 tionException %7B%0A
+        warnDeprecated(%22getPosTagger%22);%0A%0A
         String t
@@ -3897,32 +3897,80 @@
 s IOException %7B%0A
+        warnDeprecated(%22getSentenceDetector%22);%0A%0A
         String m
@@ -4429,32 +4429,71 @@
 tionException %7B%0A
+        warnDeprecated(%22getChunker%22);%0A%0A
         String m
@@ -4770,32 +4770,109 @@
 kenized%0A     */%0A
+    @Deprecated(/* Use annotators directly with their appropriate models */)%0A
     public stati
@@ -4958,32 +4958,87 @@
 , IOException %7B%0A
+        warnDeprecated(%22getOpenNlpTokenizedTestCas%22);%0A%0A
         JCas tes
@@ -5137,24 +5137,24 @@
 kenizer());%0A
-
         retu
@@ -5170,11 +5170,143 @@
 s;%0A    %7D
+%0A%0A    private static void warnDeprecated(String name) %7B%0A        LOG.warn(%22%5BOpenNlpHelper%5D method %22 + name + %22 is deprecated%22);%0A    %7D
 %0A%7D%0A

add log warning when using OpenNlpHelper