package com.aif.common;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import com.aif.language.sentence.AIF2NLPSentenceSplitter;
import com.aif.language.sentence.OpenNLPSentenceSplitter;
import com.aif.language.sentence.StanfordNLPSentenceSplitter;
import org.testng.annotations.Test;
import static junit.framework.Assert.assertTrue;

/**
 * @Test
 * 
 *       This class gets results made by
 *       AIF2NLPSentenceSplitter,
 *       OpenNLPSentenceSplitter,
 *       StanfordNLPSentenceSplitter classes and
 *       compares it with a standard text body. The standard must to be 100%
 *       correct and has the exact number of sentences. And if we compare our
 *       result with the standard, we will get quality output result.
 * 
 *       author oakslist
 * 
 */

public class SentenceSplitterQualityTest {

	private static final String STANDARD_TEXT_PATH = "/com/aif/language/"
			+ "sentence/for_sentence_split_test.txt";


    @Test(groups = "functional-slow")
    public void russianSentenceSplittersTest() {
        final int ethalonSentenceCount = 3015;
        final String textPath = "/unitTestData/TestData/RU/RU_alice_in_the_wonderland.txt";

        try(final InputStream resourceFile =
                    ClassLoader.class.getResourceAsStream(textPath)) {

            final String textData = FileHelper.readAllText(resourceFile);

            int aifResult = getAifResult(textData);
            int openNlpResult = getOpenNlpResult(textData);
            int stanfordResult = getStanfordResult(textData);

            assertTrue(ethalonSentenceCount <= aifResult);

            System.out.println(String.format("Quality test result: \n" +
                    "AIF2 lib: %d sentences detected\n" +
                    "OpenNLP lib: %d sentences detecded\n" +
                    "StanfordNLP lib: %d sentences detected", aifResult, openNlpResult, stanfordResult));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

	// get the AIF's splitter result:
	private int getAifResult(String stringFile) {
		AIF2NLPSentenceSplitter aifSplitter = new AIF2NLPSentenceSplitter();
		List<List<String>> aifResult = aifSplitter.split(stringFile);
		return aifResult.size();
	}

	// get the OpenNLP's splitter result. Return -1 if we have some problems
	// with IOException when we create modelResource otherwise return number of
	// sentences.
	private int getOpenNlpResult(String stringFile) {
		final String MODEL_RESOURCE_PATH = "/opennlp-models/en-sent.bin";

		try(InputStream modelResource = ClassLoader.class.getResourceAsStream(MODEL_RESOURCE_PATH)) {
            OpenNLPSentenceSplitter openNlpSplitter = new OpenNLPSentenceSplitter(modelResource);
			List<String> openNlpResult = openNlpSplitter.split(stringFile);

			return openNlpResult.size();
		} catch (IOException e) {
			e.printStackTrace();
			System.err.println("Problem with openNLP splitter: " + e);
		}

		return -1;
	}

	// get the Stanford's splitter result:
	private int getStanfordResult(String stringFile) {
		StanfordNLPSentenceSplitter stanfordSplitter = new StanfordNLPSentenceSplitter();

		List<String> stanfordResult = stanfordSplitter.split(stringFile);

		return stanfordResult.size();
	}

}

@@ -272,24 +272,68 @@
 ceSplitter;%0A
+import org.testng.annotations.DataProvider;%0A
 import org.t
@@ -888,17 +888,20 @@
 Test %7B%0A%0A
-%09
+    
 private 
@@ -917,329 +917,582 @@
 nal 
-String STANDARD_TEXT_PATH = %22/com/aif/language/%22%0A%09%09%09+ %22sentence/for_sentence_split_test.txt%22;%0A%0A%0A    @Test(groups = %22functional-slow%22)%0A    public void russianS
+Integer ALLOWED_DEVIATION = 50;%0A%0A    @DataProvider(name = %22texts%22)%0A    public Object%5B%5D%5B%5D getTexts() %7B%0A        return new Object%5B%5D%5B%5D %7B%0A                %7B%22/unitTestData/TestData/RU/for_sentence_split_test_4939.txt%22, 4939%7D,%0A                %7B%22/unitTestData/TestData/ENG/for_s
 entence
-S
+_s
 plit
+_
 te
-rsTest() %7B%0A        final int ethalonSentenceCount = 3015;%0A        final String textPath = %22/unitTestData/TestData/RU/RU_alice_in_the_wonderland.txt%22;
+st_EN_1000.txt%22, 1000%7D,%0A                %7B%22/unitTestData/TestData/RU/for_sentence_split_test_opencorpora_RU_5000.txt%22, 5000%7D%0A        %7D;%0A    %7D%0A%0A%0A    @Test(groups = %22quality-test%22, dataProvider = %22texts%22)%0A    public void sentenceSplittersTest(String filePath, Integer ethalonSentenceCount) %7B
 %0A%0A  
@@ -1592,20 +1592,20 @@
 sStream(
-text
+file
 Path)) %7B
@@ -1879,16 +1879,29 @@
 ertTrue(
+aifResult %3E= 
 ethalonS
@@ -1916,21 +1916,105 @@
 ount
- %3C= aifResult
+-ALLOWED_DEVIATION);%0A            //assertTrue(aifResult %3C= ethalonSentenceCount+ALLOWED_DEVIATION
 );%0A%0A
@@ -2082,24 +2082,103 @@
 sult: %5Cn%22 +%0A
+                            %22File: %25s, ethalon sentence count: %25d%5Cn%22 +%0A        
             
@@ -2243,16 +2243,24 @@
         
+     
+   
 %22OpenNLP
@@ -2312,16 +2312,24 @@
         
+        
 %22Stanfor
@@ -2359,18 +2359,72 @@
 detected
-%22,
+%5Cn%22, filePath, ethalonSentenceCount,%0A                   
  aifResu

use DataProvider for test