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; } }
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; } }
private List <string> GetFirst(string name) { List <string> source = PinYinHelper.ZhToPinYinList(name); return((from pinyin in source select(from p in pinyin.Split('\'') where p != "" select p).Aggregate("", (string current, string py) => current + py[0].ToString())).ToList()); }
private List <PinYinResult> GetPinYin(string typeString, string hanziName) { List <string> list = PinYinHelper.ZhToPinYinList(hanziName); List <PinYinResult> list2 = new List <PinYinResult>(); foreach (string item in list) { for (int j = 0; j < PinYinListAll[typeString].Count; j++) { CacheFirstPinYin cacheFirstPinYin; try { cacheFirstPinYin = PinYinListAll[typeString][j]; } catch { continue; } if (cacheFirstPinYin != null && cacheFirstPinYin.SearchList != null && cacheFirstPinYin.SearchList.Any((string s) => s == item)) { list2.Add(new PinYinResult { ID = cacheFirstPinYin.ID, Name = cacheFirstPinYin.Name, Contrast = 35, Debug = "拼音相等" + 35 }); } } string[] nameArray = (from p in item.Split('\'') where p != "" select p).ToArray(); string[] array = nameArray; foreach (string i in array) { for (int l = 0; l < PinYinListAll[typeString].Count; l++) { CacheFirstPinYin cacheFirstPinYin2; try { cacheFirstPinYin2 = PinYinListAll[typeString][l]; } catch { continue; } if (cacheFirstPinYin2 != null && cacheFirstPinYin2.SearchList != null && cacheFirstPinYin2.SearchList.Any((string s) => s.Split('\'').Count((string c) => c == i) == nameArray.Count((string c) => c == i))) { list2.Add(new PinYinResult { ID = cacheFirstPinYin2.ID, Name = cacheFirstPinYin2.Name, Contrast = 35, Debug = "拼音包含字符" + 35 }); int num = cacheFirstPinYin2.SearchList.Min((string s) => s.IndexOf(i)); list2.Add(new PinYinResult { ID = cacheFirstPinYin2.ID, Name = cacheFirstPinYin2.Name, Contrast = num * -1, Debug = "拼音位置" + num * -1 }); for (int m = 0; m < nameArray.Length - 1; m++) { string lianxu = nameArray[m] + nameArray[m + 1]; if (cacheFirstPinYin2.SearchList.Any((string s) => s.Replace("'", "").Contains(lianxu))) { list2.Add(new PinYinResult { ID = cacheFirstPinYin2.ID, Name = cacheFirstPinYin2.Name, Contrast = 15, Debug = "拼音连续" + 15 }); } } } } } } return(list2); }