예제 #1
0
        public IdentificationResults IsOfType(ChineseType expectedType, string filename)
        {
            var resxItems = XDocument
                            .Load(filename)
                            .Descendants()
                            .Where(x => x.Name == "data")
                            .Select(x => $"{ x.Attributes().First(a => a.Name == "name").Value} - {x.Value}")
                            .ToList();

            foreach (var item in resxItems)
            {
                var itemKvp = item.Split(new string[] { " - " }, StringSplitOptions.RemoveEmptyEntries);

                var itemValue = itemKvp[1].Replace("\n", string.Empty).Trim();

                var chineseType = identifier.Identify(itemValue);

                if (chineseType != expectedType)
                {
                    return(new IdentificationResults
                    {
                        WasMatch = false,
                        Message = $"Mismatch found in file: {filename}.\nExpected {expectedType.ToString("G")}, but found issue in resource {itemKvp[0]}."
                    });
                }
            }

            return(new IdentificationResults
            {
                WasMatch = true
            });
        }
예제 #2
0
        private static string Match(ChineseLexicon lexicon, ChineseType chineseType, string part)
        {
            var isMatch = chineseType == ChineseType.Traditional
                ? lexicon.Words.Any(x => x.Traditional == part)
                : lexicon.Words.Any(x => x.Simplified == part);

            return(isMatch ? part : null);
        }
예제 #3
0
        // eq \o\ac(○,圈) eq \o\ac(□,A) eq \o\ac(△,!) eq \o\ac(◇,壹)
        //eq \o(\s\up 8(合并),\s\do 3(字符))eq \o(\s\up 8(两 ),\s\do 3( 字))eq \o(\s\up 8(三个),\s\do 3(字))eq \o(\s\up 8(五个),\s\do 3(合并字))eq \o(\s\up 8(六个合),\s\do 3(并字符))

        /// <summary>
        /// 返回中文字符类型及值列表
        /// </summary>
        /// <param name="chars"></param>
        /// <returns></returns>
        public List <ChineseType> ChineseList(string chars)
        {
            if (chars == null)
            {
                return(null);
            }
            else
            {
                List <ChineseType> list = new List <ChineseType>();

                string temp = "";

                if (chars.Contains(@" eq \o\ac("))           //包含带圈字符样式
                {
                    temp = chars.Replace(@" eq \o\ac(", ""); //替换

                    temp = temp.Replace(")", ";");           //替换,以分割
                    string[] strs = temp.Split(';');         //分割字符串
                    foreach (string str in strs)
                    {
                        if (str == "")
                        {
                            break;
                        }
                        ChineseType chinessType = new ChineseType();
                        chinessType.type   = "带圈字符";
                        chinessType.str    = str.Substring(2);
                        chinessType.symbol = str.Substring(0, 1);

                        list.Add(chinessType);//添加带圈字符到列表
                    }

                    return(list);
                }
                else if (chars.Contains(@"eq \o(\s\up 8("))      //包含合并字符样式
                {
                    temp = chars.Replace(@"eq \o(\s\up 8(", ""); //替换
                    temp = temp.Replace(@"),\s\do 3(", "");
                    temp = temp.Replace("  ", "");
                    temp = temp.Replace("))", ";");
                    string[] strs = temp.Split(';');//分割字符串
                    foreach (string str in strs)
                    {
                        ChineseType chinessType = new ChineseType();
                        chinessType.type = "合并字符";
                        chinessType.str  = str;
                        list.Add(chinessType);//添加合并字符到列表
                    }
                    return(list);
                }
                else
                {
                    return(null);
                }
            }
        }
        private static void ConvertEmailChinese(Outlook.MailItem mailItem, ChineseType chineseType)
        {
            Debug.Assert(mailItem != null);

            switch (chineseType)
            {
            case ChineseType.SimplifiedChinese: mailItem.HTMLBody = ConvertToSimplifiedChinese(mailItem.HTMLBody); break;

            case ChineseType.TraditionalChinese: mailItem.HTMLBody = ConvertToTraditionalChinese(mailItem.HTMLBody); break;
            }

            mailItem.Save();
        }
예제 #5
0
        /// <summary>
        /// 中文转换核心代码
        /// </summary>
        /// <param name="text">源字符串</param>
        /// <param name="format">目标转换输出格式</param>
        /// <returns></returns>
        private static string ConvertCore(string text, ChineseType format = ChineseType.Simplified)
        {
            string STR_SOURCE;
            string STR_TARGET;

            if (format == ChineseType.Simplified)
            {
                STR_SOURCE = STR_TRAD;
                STR_TARGET = STR_SIMP;
            }
            else
            {
                STR_SOURCE = STR_SIMP;
                STR_TARGET = STR_TRAD;
            }
            if (text.Length > 0)
            {
                char[] arrText = text.ToCharArray();
                for (int i = 0; i < arrText.Length; i++)
                {
                    int intChar = Convert.ToInt32(arrText[i]);
                    if (intChar < 0x4e00 || intChar > 0x9fa5)
                    {
                        continue;
                    }
                    else
                    {
                        intChar = STR_SOURCE.IndexOf(arrText[i]);
                        if (intChar >= 0)
                        {
                            arrText[i] = STR_TARGET[intChar];
                        }
                        else
                        {
                            continue;
                        }
                    }
                }
                return(new string(arrText));
            }
            else
            {
                return(text);
            }
        }
예제 #6
0
        public static string[] SplitWords(string chinese, ChineseType chineseType = ChineseType.Simplified)
        {
            var lexicon = ChineseLexicon.Current;

            if (lexicon is null)
            {
                return(chinese.Select(ch => ch.ToString()).ToArray());
            }

            var list   = new LinkedList <string>();
            var length = chinese.Length;

            var maxOffset        = Math.Min(chinese.Length, lexicon.WordMaxLength);
            var ptext            = length - maxOffset;
            var maxLengthPerTurn = maxOffset;
            int matchLength;

            for (; ptext + maxLengthPerTurn >= 0; ptext -= matchLength)
            {
                matchLength = 1;
                for (var i = ptext > 0 ? 0 : -ptext; i < maxLengthPerTurn; i++)
                {
                    var part = chinese.Substring(ptext + i, maxLengthPerTurn - i);
                    if (part.Length == 1)
                    {
                        list.AddFirst(part);
                        break;
                    }
                    else
                    {
                        var match = Match(lexicon, chineseType, part);
                        if (match != null)
                        {
                            list.AddFirst(match);
                            matchLength = match.Length;
                            break;
                        }
                    }
                }
            }

            return(list.ToArray());
        }
예제 #7
0
파일: Pinyin.cs 프로젝트: vicobill/Chinese
        public static string[] GetSingle(char ch, PinyinFormat format = PinyinFormat.Default, ChineseType chineseType = ChineseType.Simplified)
        {
            var lexicon = ChineseLexicon.Current ?? ChineseLexicon.Default;
            var word    = ch.ToString();

            var chineseWord = chineseType == ChineseType.Traditional
                ? lexicon.Words.First(x => x.Traditional == word)
                : lexicon.Words.First(x => x.Simplified == word);

            var pinyins = chineseWord.Pinyins.Select(pinyin =>
            {
                return(format switch
                {
                    PinyinFormat.Default => pinyin,
                    PinyinFormat.WithoutTone => GetPinyinWithoutTone(pinyin),
                    PinyinFormat.Phonetic => GetPhoneticSymbol(pinyin),
                    PinyinFormat.Code => pinyin.First().ToString(),
                    _ => throw new NotImplementedException(),
                });
            }).ToArray();
예제 #8
0
        public static string GetString(string chinese, PinyinFormat format = PinyinFormat.Default, ChineseType chineseType = ChineseType.Simplified)
        {
            var lexicon = ChineseLexicon.Current;

            IEnumerable <int> GetDefaultSteps()
            {
                foreach (var ch in chinese)
                {
                    yield return(1);
                }
            }

            var steps = lexicon is null?GetDefaultSteps() : ChineseTokenizer.SplitWords(chinese, chineseType).Select(x => x.Length);

            if (!chinese.IsNullOrWhiteSpace())
            {
                var sb          = new StringBuilder();
                var insertSpace = false;
                var ptext       = 0;
                foreach (var step in steps)
                {
                    var word = chinese.Substring(ptext, step);
                    try
                    {
                        string pinyin;
                        if (word.Length == 1)
                        {
                            var chineseChar = new ChineseChar(word[0]);
                            pinyin = chineseChar.Pinyins[0].ToString().ToLower();
                        }
                        else
                        {
                            var chineseWord = chineseType == ChineseType.Traditional
                                ? lexicon.Words.First(x => x.Traditional == word)
                                : lexicon.Words.First(x => x.Simplified == word);
                            pinyin = chineseWord.Pinyin;
                        }

                        if (format != PinyinFormat.Code)
                        {
                            if (insertSpace)
                            {
                                sb.Append(" ");
                            }
                        }

                        switch (format)
                        {
                        case PinyinFormat.Default: sb.Append(pinyin); break;

                        case PinyinFormat.WithoutTone: sb.Append(GetPinyinWithoutTone(pinyin)); break;

                        case PinyinFormat.Phonetic: sb.Append(GetPhoneticSymbol(pinyin)); break;

                        case PinyinFormat.Code: sb.Append(pinyin.First()); break;
                        }
                        insertSpace = true;
                    }
                    catch
                    {
                        sb.Append(word);
                        insertSpace = false;
                    }

                    ptext += step;
                }

                return(sb.ToString());
            }
            return(chinese);
        }