根据拼音首字母匹配汉字(电话本联系人查找)

版权声明:转载请注明出处:https://blog.csdn.net/zwjemperor https://blog.csdn.net/zwjemperor/article/details/7785195

国标码中汉字的分布是有一定规律的,拼音首字母相同的汉字在同一区域(但多音字有例外),分布如下:

int[] gbCode = new int[] { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, -1, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, -1, -1, 52980, 53689,54481, 55290 };
[gbCode[0],gbCode[1])区域是以a开关的汉字,.......
这样我们就可以先对需要查找的汉字字符串编码(用GB2312),然后判断即可。

private void init()
	{
		ArrayList<Integer> codeList;
		for (String name : names)
		{
			codeList = new ArrayList<Integer>();
			for (int k = 0; k < name.length(); k++)
			{
				codeList.add(getGBCode(name.charAt(k)));
			}
			gbCodeMap.put(name, codeList);
		}
		stack.push(gbCodeMap);
	}
private int getGBCode(char word)
	{
		byte[] array = new byte[2];
		int[] codes = new int[2];
		try
		{
			array = String.valueOf(word).getBytes("GB2312");
			for (int k = 0; k < array.length; k++)
				codes[k] = (array[k] + 256) % 256;
		}
		catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}
		int code = (codes[0] - '\0') * 256 + (codes[1] - '\0');
		return code;
	}


 
 

String[] names = { "罪过", "轻而易举", "因乐素", "团员2d", "团体" };//测试数据
Map<String, ArrayList<Integer>> gbCodeMap = new HashMap<String, ArrayList<Integer>>(names.length);//保存字符串编码
Stack<Map<String, ArrayList<Integer>>> stack = new Stack<Map<String, ArrayList<Integer>>>();//用栈存放已查找到的数据
int[] gbCode = new int[] { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, -1, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, -1, -1, 52980, 53689,54481, 55290 };





完整代码如下:(android代码,稍加改动即可移植到其它语言上)

public class SearchWord extends Activity
{
	EditText editText;
	TextView textView;
	String text = "";
	String[] names = { "罪过", "轻而易举", "因乐素", "团员2d", "团体" };
	Map<String, ArrayList<Integer>> gbCodeMap = new HashMap<String, ArrayList<Integer>>(names.length);
	Stack<Map<String, ArrayList<Integer>>> stack = new Stack<Map<String, ArrayList<Integer>>>();
	int[] gbCode = new int[] { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, -1, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, -1, -1, 52980, 53689,
			54481, 55290 };

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		ViewGroup layout = new LinearLayout(this);
		editText = new EditText(this);
		textView = new TextView(this);
		layout.addView(editText);
		layout.addView(textView);
		setContentView(layout);
		init();
		editText.addTextChangedListener(new TextWatcher()
		{

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count)
			{
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after)
			{
			}

			@Override
			public void afterTextChanged(Editable s)
			{
				Search();
			}
		});
	}

	private void Search()
	{
		int len = text.length();
		int currentLen = editText.getText().length();
		while (len > currentLen)
		{
			stack.pop();
			--len;
		}
		if (len == currentLen)
		{
			if (len != 0)
				setText(stack.peek());
		}
		else if (len < currentLen)
		{
			char word = editText.getText().toString().charAt(currentLen - 1);
			int index = getCharIndex(word);
			Map<String, ArrayList<Integer>> map = search(word, index, currentLen - 1);
			setText(map);
		}
		text = editText.getText().toString();
	}

	private void setText(Map<String, ArrayList<Integer>> map)
	{
		if (!map.isEmpty())
		{
			String string = "";
			for (String key : map.keySet())
				string += key + " ";
			textView.setText(string);
		}
		else
		{
			textView.setText("");
		}
	}

	private Map<String, ArrayList<Integer>> search(char word, int index, int currentLen)
	{
		Map<String, ArrayList<Integer>> map = new HashMap<String, ArrayList<Integer>>();
		Map<String, ArrayList<Integer>> lastMap = stack.peek();
		int gbCode = getGBCode(index);
		int lastGBCode = getGBCode(index - 1);
		for (String key : lastMap.keySet())
		{
			if (lastMap.get(key).size() <= currentLen)
				continue;
			int keyCode = lastMap.get(key).get(currentLen);
			if (keyCode >= lastGBCode && keyCode <= gbCode || word == key.charAt(currentLen))
			{
				map.put(key, lastMap.get(key));
				continue;
			}
		}
		stack.push(map);
		return map;
	}

	private void init()
	{
		ArrayList<Integer> codeList;
		for (String name : names)
		{
			codeList = new ArrayList<Integer>();
			for (int k = 0; k < name.length(); k++)
			{
				codeList.add(getGBCode(name.charAt(k)));
			}
			gbCodeMap.put(name, codeList);
		}
		stack.push(gbCodeMap);
	}

	private int getGBCode(char word)
	{
		byte[] array = new byte[2];
		int[] codes = new int[2];
		try
		{
			array = String.valueOf(word).getBytes("GB2312");
			for (int k = 0; k < array.length; k++)
				codes[k] = (array[k] + 256) % 256;
		}
		catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}
		int code = (codes[0] - '\0') * 256 + (codes[1] - '\0');
		return code;
	}

	private int getGBCode(int index)
	{
		if (index < 0 || index > 26)
			return -1;
		return gbCode[index];
	}

	private int getCharIndex(char c)
	{
		int index = 26 - ('z' - c);
		if (index < 0 || index > 26)
			index = 26 - ('Z' - c);
		if (index >= 0 && index <= 26)
			return index;
		else
			return -1;
	}
}



猜你喜欢

转载自blog.csdn.net/zwjemperor/article/details/7785195