package jp.ac.shibaura_it.se.tslab.aaextractor.predicate.attributes;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class NgramCounter {
	Pattern pattern;
	// http://qiita.com/sifue/items/039846cf8415efdc5c92
	private static String convertToUnicode(String original)
	{
	    StringBuilder sb = new StringBuilder();
	    for (int i = 0; i < original.length(); i++) {
	        sb.append(String.format("\\u%04X", Character.codePointAt(original, i)));
	    }
	    return sb.toString();
	}

	private void init(List<String> keywords) {
		List<String> unicodeKeywords = keywords.stream().map(k->convertToUnicode(k)).collect(Collectors.toList());
		String rex = String.join("|", unicodeKeywords);
		this.pattern = Pattern.compile(rex);
	}

	public NgramCounter(BufferedReader br) throws IOException {
		ArrayList<String> ngrams = new ArrayList<String>();
		String line;
		while((line = br.readLine())!=null){
			ngrams.add(line);
		}
		br.close();
		init(ngrams);
	}

	public NgramCounter(List<String> ngrams) {
		init(ngrams);
	}
	
	public int count(String str) {
		int count=0;
		Matcher m = this.pattern.matcher(str);
		while(m.find()) {
			count++;
		}
		return count;
	}
}

@@ -323,337 +323,8 @@
 ern;
-%0A%09// http://qiita.com/sifue/items/039846cf8415efdc5c92%0A%09private static String convertToUnicode(String original)%0A%09%7B%0A%09    StringBuilder sb = new StringBuilder();%0A%09    for (int i = 0; i %3C original.length(); i++) %7B%0A%09        sb.append(String.format(%22%5C%5Cu%2504X%22, Character.codePointAt(original, i)));%0A%09    %7D%0A%09    return sb.toString();%0A%09%7D
 %0A%0A%09p
@@ -371,36 +371,18 @@
 %7B%0A%09%09
-List%3C
 String
-%3E unicodeKeywords
+ rex
  = k
@@ -409,23 +409,20 @@
 (k-%3E
-convertToUnicod
+Pattern.quot
 e(k)
@@ -446,66 +446,20 @@
 ors.
-toList());%0A%09%09String rex = String.join(%22%7C%22, unicodeKeywords
+joining(%22%7C%22)
 );%0A%09

Update NgramCounter.java