/// <summary> /// 转化关键字 /// </summary> /// <param name="sIn"></param> /// <returns></returns> public WordPinYin GetWord_Pinyin(string sIn) { sIn = FormatSearchKey(sIn); if (String.IsNullOrWhiteSpace(sIn)) { return(null); } WordPinYin objRslt = new WordPinYin(); string sInWithoutSpace = sIn.Replace(" ", "").ToLower(); objRslt.Word = sIn.Replace(" ", ""); objRslt.WordWithSpace = sIn; if (sInWithoutSpace.IsOnlyEnglishLetterOrDigital()) { if (sIn.Length > 30) { return(null); } objRslt.Word = ""; objRslt.PinYin = sInWithoutSpace; objRslt.PinYinWithSpace = sIn; objRslt.FirstLetter = sInWithoutSpace; objRslt.FirstLetterWithSpace = sIn; return(objRslt); } if (sIn.Length > 20) { return(null); } HanziStringPinyin objPinyin = HanziStringPinyinManager.Instance.GetPinyinFromHanziString(sIn, 1); if (objPinyin != null) { string sPinyin = objPinyin.Pinyin[0].Replace("eng", "en").Replace("ing", "in"); objRslt.FirstLetter = objPinyin.FirstLetter[0]; objRslt.PinYin = sPinyin.Replace(" ", ""); objRslt.PinYinWithSpace = sPinyin; } return(objRslt); }
public HanziStringPinyin GetPinyinFromHanziString(string sHanziString, int nNoOfPinYin) { nNoOfPinYin = Math.Max(1, nNoOfPinYin); nNoOfPinYin = Math.Min(32, nNoOfPinYin); sHanziString = sHanziString.DoTrim().ToLower(); if (String.IsNullOrWhiteSpace(sHanziString)) { throw new ArgumentException("Hanzi string is null"); } if (sHanziString.Length > 100) { return(null); } HanziStringPinyin objRslt = new HanziStringPinyin(); PinYin[] aPinYin = new PinYin[sHanziString.Length]; int[] aIndex = new int[aPinYin.Length]; int nTotalRsltCount = 1; for (int i = 0; i < sHanziString.Length; i++) { string sSingleWord = sHanziString[i].ToString(); if (!sSingleWord.IsChinese()) { aPinYin[i] = GetPinyinBySingleNotChineseWord(sSingleWord); aIndex[i] = nTotalRsltCount; continue; } aPinYin[i] = GetPinYinForCorrespondingHanzi(sSingleWord); if (aPinYin[i] == null) { aPinYin[i] = GetPinyinBySingleNotChineseWord(sSingleWord); } else { nTotalRsltCount = nTotalRsltCount * aPinYin[i].lstPinyin.Count; } aIndex[i] = nTotalRsltCount; } int nRsltCount = Math.Min(nNoOfPinYin, nTotalRsltCount); Dictionary <string, int> dicTmpFirstLetter = new Dictionary <string, int>(); Dictionary <string, int> dicTmpPinyin = new Dictionary <string, int>(); Dictionary <string, int> dicTmpSPinyin = new Dictionary <string, int>(); while (objRslt.Pinyin.Count < nRsltCount) { int nCandidate = objRslt.Pinyin.Count + 1; string sPinYinRslt = ""; string sSPinyinRslt = ""; string sFirstLetterRslt = ""; for (int i = 0; i < aPinYin.Length; i++) { string sTmp = ""; if (aPinYin[i].IsChinese) { sTmp = " "; } int nCrntIndex = 0; if (i == 0) { nCrntIndex = (nCandidate - 1) % aIndex[i]; } else { nCrntIndex = ((nCandidate - 1) / aIndex[i - 1]) % aPinYin[i].lstPinyin.Count; } sPinYinRslt += sTmp + aPinYin[i].lstPinyin[nCrntIndex] + sTmp; sSPinyinRslt += sTmp + aPinYin[i].lstSPinyin[nCrntIndex] + sTmp; sFirstLetterRslt += aPinYin[i].lstPinyin[nCrntIndex].Substring(0, 1).Replace(" ", ""); } sPinYinRslt = sPinYinRslt.FormatMultiSpace(" ").DoTrim(); sSPinyinRslt = sSPinyinRslt.FormatMultiSpace(" ").DoTrim(); if (dicTmpPinyin.ContainsKey(sPinYinRslt)) { Container.LogService.UnexpectedBehavorLogger.Error("duplicate Pinyin happen,Hanzi=" + sHanziString + ",NofoPinyin=" + nNoOfPinYin + ",duplicate pinyin=" + sPinYinRslt); } else { objRslt.Pinyin.Add(sPinYinRslt); dicTmpPinyin[sPinYinRslt] = 1; } if (dicTmpSPinyin.ContainsKey(sSPinyinRslt)) { Container.LogService.UnexpectedBehavorLogger.Error("duplicate SPinyin happen,Hanzi=" + sHanziString + ",NofoPinyin=" + nNoOfPinYin + ",duplicate SPinyin=" + sSPinyinRslt); } else { objRslt.SPinyin.Add(sSPinyinRslt); dicTmpPinyin[sSPinyinRslt] = 1; } if (!dicTmpFirstLetter.ContainsKey(sFirstLetterRslt)) { objRslt.FirstLetter.Add(sFirstLetterRslt); dicTmpFirstLetter[sFirstLetterRslt] = 1; } } return(objRslt); }