/// <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); }
/// <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 } }