Exemple #1
0
        public List <PinYinResult> GetPinYinAndHanZiResult(string typeString, List <PinYinSource> pinyinSourceList, string searchText, int pageSize, out int count, int page = 1, bool isDebug = false)
        {
            try
            {
                #region 初始化数据构造
                string error = "";
                SetHanZi(typeString, pinyinSourceList, ref error);
                SetPinYin(typeString, pinyinSourceList, ref error);
                SetFirstPinYin(typeString, pinyinSourceList, ref error);
                #endregion

                if (page < 1)
                {
                    page = 1;
                }
                List <PinYinResult> list = new List <PinYinResult>();
                int.TryParse(searchText, out int _);
                if (searchText.Length < 2 && !Regex.IsMatch(searchText, "[\\u4e00-\\u9fa5]+$"))
                {
                    count = 0;
                    return(new List <PinYinResult>());
                }
                searchText = PinYinHelper.GetNumberPinYin(searchText);
                list.AddRange(GetHanzi(typeString, searchText));
                list.AddRange(GetPinYin(typeString, searchText));
                list.AddRange(GetFirstPinYin(typeString, searchText));
                List <PinYinResult> list2 = new List <PinYinResult>();
                for (int i = 0; i < list.Count; i++)
                {
                    PinYinResult yinSoResult  = list[i];
                    PinYinResult pinYinResult = list2.FirstOrDefault((PinYinResult e) => e.ID == yinSoResult.ID);
                    if (pinYinResult == null)
                    {
                        list2.Add(yinSoResult);
                    }
                    else
                    {
                        pinYinResult.Contrast += yinSoResult.Contrast;
                        if (isDebug)
                        {
                            PinYinResult pinYinResult2 = pinYinResult;
                            pinYinResult2.Debug = pinYinResult2.Debug + "; " + yinSoResult.Debug;
                        }
                        else
                        {
                            pinYinResult.Debug = "";
                        }
                    }
                }
                count = list2.Count;
                return((from u in list2
                        orderby u.Contrast descending, u.Name.Length, u.Name
                        select u).Skip((page - 1) * pageSize).Take(pageSize).ToList());
            }
            catch (Exception ex)
            {
                //var logStr= "拼音日志", "typestring=" + typeString + "<span style='color:red'>错误:获取拼音结果出现错误! HanZiList.Count=" + HanZiListAll[typeString].Count + "&PinYinList.Count=" + PinYinListAll[typeString].Count + "&FirstPinYinList.Count=" + FirstPinYinListAll[typeString].Count + "</span>";
                throw ex;
            }
        }
Exemple #2
0
 private void SetHanZi(string typeString, List <PinYinSource> pinyinList, ref string error)
 {
     try
     {
         HanZiListAll[typeString] = new List <CacheFirstPinYin>();
         for (int i = 0; i < pinyinList.Count; i++)
         {
             PinYinSource pinYinSource = pinyinList[i];
             string       name         = pinYinSource.Name;
             name = PinYinHelper.GetNumberPinYin(name);
             CacheFirstPinYin cacheFirstPinYin = new CacheFirstPinYin();
             cacheFirstPinYin.ID         = pinYinSource.ID;
             cacheFirstPinYin.Name       = pinYinSource.Name;
             cacheFirstPinYin.SearchList = new List <string>
             {
                 pinYinSource.Name
             };
             if (name != pinYinSource.Name)
             {
                 cacheFirstPinYin.SearchList.Add(name);
             }
             HanZiListAll[typeString].Add(cacheFirstPinYin);
         }
     }
     catch (Exception)
     {
         //LogHelper.WriteCustom("拼音日志", "typestring=" + typeString + "<span style='color:red'>错误:SetHanZi方法错误!count=" + HanZiListAll[typeString].Count + "</span>", 2, ex);
         error = error + "  SetHanZi方法错误!count=" + HanZiListAll[typeString].Count;
     }
 }