Repository navigation
Expand file tree
/
Copy pathPinyinConverter.cs
More file actions
138 lines (127 loc) · 4 KB
/
Copy pathPinyinConverter.cs
File metadata and controls
138 lines (127 loc) · 4 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Reflection;
namespace SlickMaster.Builder.Utility
{
/// <summary>
/// 汉字转拼音工具类
/// </summary>
public class PinyinConverter
{
static Random rd = new Random();
/// <summary>
/// 获取拼音首字母:
/// 天下=>tx
/// </summary>
/// <param name="inputStr"></param>
/// <returns></returns>
public static string ConvertFirst(string inputStr)
{
string result = ConvertInternalSingle(inputStr, true);
return result;
}
/// <summary>
/// 获取全拼
/// 天下=>tianxia
/// </summary>
/// <param name="inputStr"></param>
/// <returns></returns>
public static string ConvertWhole(string inputStr)
{
string result = ConvertInternalSingle(inputStr, false);
return result;
}
/// <summary>
/// 获取全拼,不分词
/// 馄饨=>hun dun
/// </summary>
/// <param name="inputStr"></param>
/// <param name="isFirstChar"></param>
/// <param name="surfix"></param>
/// <returns></returns>
private static string ConvertInternalSingle(string inputStr, bool isFirstChar, bool surfix = true)
{
StringBuilder outputStr = new StringBuilder(128);
char[] inputChar = inputStr.ToCharArray();
string pinyin = string.Empty;
foreach (char ch in inputChar)
{
if (CheckCNString(ch.ToString()) == true)
{
//单字取拼音
pinyin = ConvertHelper.GetFirstPinYinCount(ch);
if (isFirstChar == true)
{
pinyin = pinyin.Substring(0, 1);
}
pinyin = Regex.Replace(pinyin, @"\d", "");
outputStr.Append(pinyin);
}
else
{
outputStr.Append(ch);
}
}
//添加后缀
if (surfix == true)
{
//单字母后缀
outputStr.Append(CreateChar());
//两位数字后缀
Random rnd = new Random();
int mark = rnd.Next(10, 99);
outputStr.Append(mark.ToString());
}
return outputStr.ToString();
}
/// <summary>
/// 获取单个随机字母串
/// </summary>
/// <returns></returns>
private static char CreateChar()
{
const string allowedChars = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
char c = allowedChars[rd.Next(0, allowedChars.Length)];
return c;
}
/// <summary>
/// 判断是否为中文字串
/// </summary>
/// <param name="inputStr"></param>
/// <returns></returns>
private static bool CheckCNString(string inputStr)
{
//字符串不能为空
bool isCN = false;
if (string.IsNullOrEmpty(inputStr)) return isCN;
//中文匹配
string patternCN = @"^[\u4e00-\u9fa5\r\n]+$";
if (Regex.IsMatch(inputStr, patternCN))
{
isCN = true;
}
return isCN;
}
/// <summary>
/// 判断字符串是否为英文
/// </summary>
/// <param name="inputStr"></param>
/// <returns></returns>
private static bool CheckEnglishString(string inputStr)
{
bool isEn = false;
Regex rex = new Regex("[a-z0-9A-Z_]+");
Match ma = rex.Match(inputStr);
if (ma.Success)
{
isEn = true;
}
return isEn;
}
}
}