-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCNPronunciationConverter.cs
100 lines (100 loc) · 3.46 KB
/
CNPronunciationConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections.Generic;
using Microsoft.International.Converters.PinYinConverter;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace TeamNMSL.yndcksh
{
/// <summary>
/// 首字母轉中文類
/// </summary>
public class CNPronunciationConverter
{
static List<string> UsefulCNWords = init();
List<string> WordLibrary=new List<string>();
private static List<string> init()
{
string json = System.Text.Encoding.UTF8.GetString(Resources.CNWORDS_FILE);
JArray WordJsonArray = (JArray)JsonConvert.DeserializeObject(json);
List<string> r = new List<string>();
foreach (var item in WordJsonArray)
{
r.Add(item.ToString());
}
return r;
}
/// <summary>
/// </summary>
/// <param name="CustomWordLibraryJSONArray">JsonArray形式的詞庫</param>
public CNPronunciationConverter(string CustomWordLibraryJSONArray)
{
try
{
JArray WordJsonArray=(JArray)JsonConvert.DeserializeObject(CustomWordLibraryJSONArray);
foreach (var item in WordJsonArray)
{
WordLibrary.Add(item.ToString());
}
}
catch (Exception e)
{
throw e;
}
}
private string GetPronunciation(string ChineseWord) {
string GetString(char cn_char){
string rr = "";
if (ChineseChar.IsValidChar(cn_char))
{
var a = new ChineseChar(cn_char).Pinyins;
foreach (var item in a)
{
rr += item;
}
return rr + " ";
}
else
{
return cn_char.ToString().ToUpper()+" ";
}
}
string ResultString = "";
char[] ChineseChars=ChineseWord.ToCharArray();
foreach (var cn_char in ChineseChars)
{
ResultString += GetString(cn_char);
}
return ResultString.TrimEnd();
}
private string GetPinyinAbbreviation(string ChinesePinyin) {
string ResultString = "";
foreach (var pinyin in ChinesePinyin.Split(" "))
{
ResultString += pinyin.ToCharArray()[0];
}
return ResultString;
}
/// <summary>
/// 將首字母轉換成你詞庫裏對應的中文字詞,如果詞庫内不包含這個字詞,會從默認的詞庫内匹配,若未匹配到,將返回大寫后的原文
/// </summary>
/// <param name="Abbreviation">拼音縮寫</param>
/// <returns></returns>
public string GetTextFromAbbreviation(string Abbreviation) {
foreach (var Word in WordLibrary)
{
if (GetPinyinAbbreviation(GetPronunciation(Word)).ToLower()==Abbreviation.ToLower())
{
return Word;
}
}
foreach (var Word in UsefulCNWords)
{
if (GetPinyinAbbreviation(GetPronunciation(Word)).ToLower() == Abbreviation.ToLower())
{
return Word;
}
}
return Abbreviation.ToUpper();
}
}
}