文の中で速く単語を探します

2022 ワード

public class WordsFinder {

	/**
	 *  word text 
	 * @Date: 2013-6-14 05:57:19
	 * @Description: int
	 * @param word
	 * @param text
	 * @return
	 */
	public static int count(String word,String text){
		char[]w = word.toCharArray();
		char[]t = text.toCharArray();
		int size = w.length;
//		List<String> list = new ArrayList<String>();
		int sum = 0;
		for (int i = 0; i < t.length; i++) {
			if(w[0]==t[i]){
				if(i+size-1<t.length){
					boolean bool = true;
					for (int j = 1; j < size; j++) {
						bool&=(w[j]==t[i+j]);
						if(!bool){
							break;
						}
					}
					if(bool){
						sum++;
					}
				}
			}
		}
		return sum;
	}
	/**
	 *  text list , 
	 * @Date: 2013-6-27 02:57:28
	 * @Description: Set<String>
	 * @param list
	 * @param text
	 * @return
	 */
	public static Set<String> seek(List<String> list,String text){
		Set<String> words = new HashSet<String>(list);
		Set<String> set = new HashSet<String>();
		for (String word : words) {
			int sum = count(word, text);
			if(sum>0){
				set.add(word);
			}
		}
		return set;
	}
	/**
	 *  text list , 
	 * @Date: 2013-6-27 02:57:33
	 * @Description: Map<String,Integer>
	 * @param list
	 * @param text
	 * @return
	 */
	public static Map<String,Integer> seekCount(List<String> list,String text){
		Set<String> words = new HashSet<String>(list);
		Map<String,Integer> map = new HashMap<String, Integer>();
		for (String word : words) {
			int sum = count(word, text);
			if(sum>0){
				map.put(word, sum);
			}
		}
		return map;
	}
}