Пример #1
0
        /// <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);
        }
Пример #2
0
        /// <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);
        }
Пример #3
0
        /// <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
                        }
                    } //匹配字典
                }     //循环字典
            }         //音节循环查询
        }
Пример #4
0
        /// <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
            }
        }