/// <summary> /// /// </summary> /// <param name="str"></param> /// <param name="charCodeSplit"></param> /// <returns></returns> public IList <string> GetCodeOfString(string str, string charCodeSplit = "") { if (IsPinyinCode && Is1Char1Code) { return(pinyinGenerater.GetCodeOfString(str, charCodeSplit)); } var list = new List <string>(); if (Is1Char1Code || str.Length == 1) { foreach (char c in str) { list.Add(GetDefaultCodeOfChar(c)); } } else //多个字一个编码 { var result = ""; var arr = MutiWordCodeFormat.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); Dictionary <string, string> format = new Dictionary <string, string>(); foreach (var line in arr) { var kv = line.Split('='); var key = kv[0].Substring(5); var value = kv[1]; format.Add(key, value); } var k = "e" + str.Length; if (format.ContainsKey(k)) //找到对应编码 { var f = format[k]; result = GetStringCode(str, f); } else //字符串很长 { var key = ""; for (var i = str.Length; i > 0; i--) { key = "a" + i; if (format.ContainsKey(key)) { break; } } var f = format[key]; result = GetStringCode(str, f); } list.Add(result); } return(list); }
/// <summary> /// 获得一个词的编码,如果MutiCode==True,那么返回的是一个词的多种编码方式 /// </summary> /// <param name="str"></param> /// <param name="charCodeSplit"></param> /// <returns></returns> public override Code GetCodeOfString(string str) { IList <IList <string> > codes = GetAllCodesOfString(str); if (Is1Char1Code) { return(new Code(codes)); } //一词一码,按组词规则生成 var list = new List <string>(); string result = ""; string[] arr = MutiWordCodeFormat.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); var format = new Dictionary <string, string>(); foreach (string line in arr) { string[] kv = line.Split('='); string key = kv[0].Substring(5); string value = kv[1]; format.Add(key, value); } string k = "e" + str.Length; if (format.ContainsKey(k)) //找到对应编码 { string f = format[k]; result = GetStringCode(str, f); } else //字符串很长 { string key = ""; for (int i = str.Length; i > 0; i--) { key = "a" + i; if (format.ContainsKey(key)) { break; } } string f = format[key]; result = GetStringCode(str, f); } list.Add(result); return(new Code(list, false)); }