package kr.co.shineware.ds.aho_corasick;

import org.junit.Before;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class AhoCorasickSpeedTest {
    private AhoCorasickDictionary<List<String>> ahoCorasickDictionary;
    private Map<String, List<String>> dictionaryMap;

    @Before
    public void setup() throws IOException {
        System.out.println("Load dictionary");
        this.ahoCorasickDictionary = new AhoCorasickDictionary<>();
        this.dictionaryMap = new HashMap<>();
        loadWordDictionary("dic.word");
        putWordDictionaryToAhocorasickDictionary();
        System.out.println("Load done");
        this.ahoCorasickDictionary.buildFailLink();
    }

    private void putWordDictionaryToAhocorasickDictionary() {
        for (String word : this.dictionaryMap.keySet()) {
            this.ahoCorasickDictionary.put(word, this.dictionaryMap.get(word));
        }
    }

    private Map<String, List<String>> loadWordDictionary(String filename) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(filename));

        String line;

        while ((line = br.readLine()) != null) {
            line = line.trim();
            if (line.split("\t").length != 2) {
                continue;
            }
            String[] tokenEntry = line.split("\t");

            String word = tokenEntry[0];
            String[] morphes = tokenEntry[1].split(" ");
            dictionaryMap.put(word, Arrays.asList(morphes));
        }

        br.close();
        return dictionaryMap;
    }

    @Test
    public void speedTest() throws Exception {
        Collection<String> wikiTitleWords = getWikiTitleEojeol();
        long begin = System.currentTimeMillis();
        int totalCount=10;
        Set<String> wordSet = this.dictionaryMap.keySet();

        for(int i=0;i<10;i++) {
            for (String word : wordSet) {
                Map<String, List<String>> morphList = this.ahoCorasickDictionary.get(word);
            }

            for (String word : wikiTitleWords) {
                Map<String, List<String>> morphList = this.ahoCorasickDictionary.get(word);
            }
        }

        long end = System.currentTimeMillis();
        System.out.println((end-begin)/totalCount + "ms");
    }

    private Collection<String> getWikiTitleEojeol() throws Exception {
        Collection<String> eojeolList = new HashSet<>();
        BufferedReader br = new BufferedReader(new FileReader("wiki.titles"));

        String line;

        while((line = br.readLine()) != null){
            eojeolList.addAll(Arrays.asList(line.split(" ")));
        }
        br.close();
        return eojeolList;
    }
}

@@ -1268,232 +1268,106 @@
     
-if (line.split(%22%5Ct%22).length != 2) %7B%0A                continue;%0A            %7D%0A            String%5B%5D tokenEntry = line.split(%22%5Ct%22);%0A%0A            String word = tokenEntry%5B0%5D;%0A            String%5B%5D morphes = tokenEntry%5B1%5D.split(%22 %22
+String%5B%5D tokenEntry = line.split(%22%5Ct%22);%0A%0A            String word = Arrays.asList(tokenEntry).get(0
 );%0A 
@@ -1419,15 +1419,48 @@
 ist(
-morphes
+tokenEntry).subList(1, tokenEntry.length
 ));%0A
@@ -1721,17 +1721,19 @@
 talCount
-=
+ = 
 10;%0A    
@@ -1803,22 +1803,29 @@
  for
+ 
 (int i
-=0;i%3C
+ = 0; i %3C 
 10;
+ 
 i++)
@@ -2224,16 +2224,20 @@
 (end
--
+ - 
 begin)
-/
+ / 
 tota
@@ -2502,16 +2502,17 @@
    while
+ 
 ((line =
@@ -2535,16 +2535,17 @@
 != null)
+ 
 %7B%0A      

test case 버그 수정