All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hymer.sensitivewords.TreeGenerator Maven / Gradle / Ivy

There is a newer version: 3.3.18-20230117
Show newest version
package org.hymer.sensitivewords;

import java.util.HashMap;
import java.util.Map;

/**
 * 
 * 敏感字树生成器
 * 
 * @author hymer
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class TreeGenerator {

	/**
	 * 将指定的词分成字构建到一棵树中。
	 * 
	 * @param tree
	 *            字树
	 * @param sensitiveWord
	 *            敏感词词
	 */
	public static void addWord2Tree(Map tree, String sensitiveWord) {
		addWord2Tree(tree, sensitiveWord, 0);
	}

	private static Map addWord2Tree(Map tree,
			String word, int index) {
		if (index == word.length()) {
			tree.put(Finder.TREE_END_KEY, generateWordMap(word));
			return tree;
		}
		String next = word.substring(index, index + 1);
		Map subTree = tree.get(next);
		if (subTree == null) {
			subTree = new HashMap();
		}
		tree.put(next, addWord2Tree(subTree, word, index + 1));
		return tree;
	}

	private static Map generateWordMap(String word) {
		Map wordMap = new HashMap();
		wordMap.put(Finder.WORD_VALUE, word);
		wordMap.put(Finder.WORD_LENGTH, word.length());
		return wordMap;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy