/// <summary> ///二分查找关键字key是否在查找表arr中存在,若存在,返回索引号,否则返回-1; /// </summary> /// <param name="arr"></param> /// <param name="key"></param> /// <returns></returns> public static int FindIndex(List <string> list, int key) { int low = 0; int high = list.Count - 1; int mid = 0; while (low <= high) { mid = (low + high) / 2; if (key == CommonFun.GetCount(list[mid])) { return(mid); } else if (key < CommonFun.GetCount(list[mid])) { high = mid - 1; } else { low = mid + 1; } } return(low == list.Count ? list.Count : low); }
/// <summary> ///二分查找关键字key是否在查找表arr中存在,若存在,返回索引号,否则返回-1; /// </summary> /// <param name="arr"></param> /// <param name="key"></param> /// <returns></returns> public static int FindIndex(string[] arr, int key) { int low = 0; int high = arr.Length - 1; int mid = 0; while (low <= high) { mid = (low + high) / 2; if (key == CommonFun.GetCount(arr[mid])) { return(mid); } else if (key < CommonFun.GetCount(arr[mid])) { high = mid - 1; } else { low = mid + 1; } } return(low == arr.Length ? arr.Length : low); }
/// <summary> /// 输入接受字符 /// </summary> /// <param name="keyCode"></param> private void SetInputKey(int keyCode) { string inputStr = Convert.ToChar(keyCode).ToString().ToLower();//输入字母 string inputStrNumber = string.Empty; //输入法输入切换 if (keyCode == (int)Keys.F1 || keyCode == (int)Keys.F2 || keyCode == (int)Keys.F3 || keyCode == (int)Keys.F4 || keyCode == (int)Keys.F5) { keyStack.Clear();//清空原先输入内容 keyNumberList.Clear(); InitVariable(); } else if (keyCode == (int)Keys.F6)//小数点 { if (inputType == InputMethodType.InputType.CN9 || inputType == InputMethodType.InputType.CN) { SendResult("。"); } else { SendResult("."); } } else if (keyCode == (int)Keys.Enter) { if (ListResult.Count == 0)//发送回车键 { KeybdEvent(Keys.Enter); } } else if (keyCode == (int)Keys.Space) { if (ListResult.Count == 0)//发送空格 { KeybdEvent(Keys.Space); } } else if (keyCode > 97 && keyCode <= 105 && inputType == InputMethodType.InputType.CN9) //CN9输入方式 { inputStrNumber = Convert.ToChar(keyCode - 48).ToString().ToLower(); //获得键盘NO SetGroupPinyin(Convert.ToInt32(inputStrNumber)); textBoxInput.Text = GetGroupPinyin(); keyNumberList.Add(Convert.ToInt32(inputStrNumber)); } else if (keyCode > 97 && keyCode <= 105 && (inputType == InputMethodType.InputType.EN || inputType == InputMethodType.InputType.ENUpper) && keyNumberList.Count == 0) //英文,每次只允许录入一个 { inputStrNumber = Convert.ToChar(keyCode - 48).ToString().ToLower(); //获得键盘NO StringBuilder sb = new StringBuilder(); int count = 1; foreach (string item in KeyDic[Convert.ToInt32(inputStrNumber)]) { Word w = new Word(); if (this.inputType == InputMethodType.InputType.ENUpper) { w.Words = item.ToUpper(); } else { w.Words = item.ToLower(); } w.Counts = 0; ListResult.Add(w); sb.Append(count + "." + w.Words + " "); count++; } listBoxResult.Items.Add(sb.ToString()); endIndex = count - 1; keyNumberList.Add(Convert.ToInt32(inputStrNumber)); } else if (keyCode >= 96 && keyCode <= 105 && inputType == InputMethodType.InputType.NO) //数字 { inputStrNumber = Convert.ToChar(keyCode - 48).ToString().ToLower(); //获得键盘NO _resultStr = inputStrNumber; KeybdEvent((Keys)keyCode); } else if (keyCode == 96 && inputType != InputMethodType.InputType.NO)//空格按键 { KeybdEvent(Keys.Space); } else if (inputType == InputMethodType.InputType.CN)//全键盘 { if (keyCode != (int)Keys.Back && keyCode != (int)Keys.Space && keyCode != (int)Keys.Enter) { textBoxInput.Text = CommonFun.GetEnterPinyin(inputStr, textBoxInput.Text); } FindString(new string[] { textBoxInput.Text }); } else { // KeybdEvent((Keys)keyCode); SendResult(inputStr); } }
/// <summary> /// 查询字库 /// </summary> /// <param name="value"></param> /// <param name="isEcho"></param> private void SearchDict(string[] value, int seaType) { string[] tem1 = null; string[] tem2 = null; int tempStartIndex = 0; int tempEndIndex = 0; string tempStartStr = string.Empty;//声母 // 生成的时候去过滤不必要的拼音组合 int pingyinCount = -1; //记录组合拼音的最短字数 int tempPingyinCount = -1; //记录组合拼音的最短字数 foreach (string item in value) { if (string.IsNullOrEmpty(item)) { continue; } tempPingyinCount = CommonFun.GetCount(item); if (tempPingyinCount > pingyinCount && pingyinCount > -1) { break; } pingyinCount = tempPingyinCount; tempStartStr = item.Substring(0, 1); tem2 = CommonFun.StringSplitToArr(item); //输入内容 foreach (KeyValuePair <string, List <DicIndex> > kv in ListDicIndex) //循环字典 { foreach (DicIndex itemIndex in kv.Value) { if (itemIndex.StartStr == tempStartStr)//获得索引段 { tempStartIndex = itemIndex.StartIndex; tempEndIndex = itemIndex.EndIndex; break; } } for (int i = tempStartIndex; i < tempEndIndex; i++)//ListDict.Count { //if (i > tempEndIndex) //查询字典匹配 // break; //ListDict[i] 为2个元素,全CN,汉字 tem1 = CommonFun.StringSplitToArr(ListDict[i][0]);//字典 if (seaType == 1) { #region 相等查找 if (ListDict[i].Length == 2 && tem1.Length == tem2.Length && (CommonFun.IsEque(tem1, tem2) || CommonFun.IsStartEque(tem1, tem2)) )//全字相同匹或者首字母相同2个条件合并到一起搜索不用搜索2次 2016-1-22优化 { Word w = AddNewWord(i); if (!IsInWord(w.Words)) { ListResult.Add(w); } } #endregion } else if (seaType == 2) { #region 小于查找 if (ListDict[i].Length == 2 && tem1.Length <= tem2.Length && CommonFun.IsStartEque(tem1, tem2)) { Word w = AddNewWord(i); if (!IsInWord(w.Words)) { ListResult.Add(w); } } #endregion } else if (seaType == 3) { #region 小于查找 if (ListDict[i].Length == 2 && tem1.Length <= tem2.Length && CommonFun.IsLess(tem1, tem2)) { Word w = AddNewWord(i); if (!IsInWord(w.Words)) { ListResult.Add(w); } } #endregion } } //匹配字典 } //循环字典 } //音节循环查询 }
/// <summary> /// 获取CN9 /// </summary> /// <param name="keyIndex"></param> private void SetGroupPinyin(int keyIndex) { int tempCount = 0; string result = string.Empty; listBoxGroup.Items.Clear(); List <string> tempList = new List <string>(); if (keyStack.Count <= 0)//正常入栈 { for (int i = KeyDic[keyIndex].Length - 1; i >= 0; i--) { if (KeyDic[keyIndex][i] != "u" && KeyDic[keyIndex][i] != "i" && KeyDic[keyIndex][i] != "v") { keyStack.Push(KeyDic[keyIndex][i]); if (IsDebug) { listBoxGroup.Items.Add(KeyDic[keyIndex][i]); } } } } else { string currentStr = string.Empty; do { string strall = keyStack.Pop(); foreach (string inputStr in KeyDic[keyIndex]) { int index = 0; currentStr = CommonFun.GetEnterPinyin(inputStr, strall); tempCount = CommonFun.GetCount(currentStr); if (!tempList.Contains(currentStr)) { //按照拼音个数降序 #region 排序算法,比较慢 //for (int i = 0; i < tempList.Count; i++) //{ // if (CommonFun.GetCount(tempList[i]) >= tempCount) // index = i; //} //if (index > 0 && index == tempList.Count - 1) // tempList.Add(currentStr); //else // tempList.Insert(index, currentStr); #endregion //修改为二分法 index = CommonFun.FindIndex(tempList, tempCount); tempList.Insert(index, currentStr); } } } while (keyStack.Count > 0); for (int i = tempList.Count - 1; i >= 0; i--) { string[] tempArr = CommonFun.StringSplitToArr(tempList[i]); if (tempArr.Length > 2 && (tempArr[tempArr.Length - 1].Length <= 1 && tempArr[tempArr.Length - 2].Length <= 1)) { continue; } keyStack.Push(tempList[i]); if (IsDebug) { listBoxGroup.Items.Add(tempList[i]); } } #region 过滤不必要的组合 之前逻辑干掉 // List<string> tempList = new List<string>(); //foreach (string sResult in tempKeyStack) //{ // tempCount = CommonFun.GetCount(sResult); // string[] tempArr = CommonFun.StringSplitToArr(sResult); // //单个词并且,不能多音节组成词语或者最后2位都是单个字 // if (tempCount > 0 && tempArr.Length > 1 && (tempArr[0].Length <= 1 // || (tempArr[tempArr.Length - 1].Length <= 1 && tempArr[tempArr.Length - 2].Length <= 1) // )) // { // continue; // } // //2个以上切分,并且最后一位单独--去除 // if (tempArr.Length > 2 && tempArr[tempArr.Length - 1].Length <= 1) // { // continue; // } // //单个完整字往前排 // if (!tempList.Contains(sResult)) // { // int index = CommonFun.GetListIndex(tempList, sResult); // if (index == tempList.Count - 1) // { // tempList.Add(sResult); // } // else // { // tempList.Insert(index, sResult); // } // } //} //foreach (string item in tempList) //{ // keyStack.Push(item); // listBoxGroup.Items.Add(item); //} #endregion } }
/// <summary> /// 接受输入音节进行切分 /// </summary> /// <param name="inputStr">输入字符</param> /// <param name="allstr">CN字符串</param> /// <returns></returns> public static string GetEnterPinyin(string inputStr, string allstr) { string[] temp = CommonFun.StringSplitToArr(allstr); string value = temp.Length > 0 ? temp[temp.Length - 1] : ""; //CN分割最后一组 bool tempSheMu = CommonFun.IsShenMu(inputStr); //当前输入声母 bool oldSheMu = CommonFun.IsShenMu(value); //最后组声母 int tempYunMuZi = CommonFun.IsyYunMuZi(value + inputStr); //单字韵母 if (tempYunMuZi == 1) //韵母制定汉字包含 { allstr += inputStr; } else if (tempYunMuZi == 2)//韵母制定汉字匹配 { allstr += inputStr; } else if (tempSheMu && oldSheMu)//多个声母组合 { allstr += "'" + inputStr; } else { string last = value.Length > 0 ? value.Substring(value.Length - 1) : ""; if (last == "g" || last == "r" || last == "n") { string moveLast = value.Substring(0, value.Length - 1); //特殊符前面的音节有效果,并且与最后一位能够组成有效音节 if (!string.IsNullOrEmpty(moveLast) && CommonFun.IsTrueYinJieContains(moveLast) && CommonFun.IsTrueYinJieContains(last + inputStr)) { allstr = allstr.Insert(allstr.Length - 1, "'"); allstr += inputStr; return(allstr); } } bool isYinJie = CommonFun.IsTrueYinJie(value, inputStr); if (isYinJie) { allstr += "'" + inputStr; } else { allstr += inputStr; } } return(allstr); }