mirror of
https://github.com/denglihong2007/CRSim
synced 2026-08-05 17:05:52 +08:00
Merge pull request #306 from denglihong2007/dev
feat: 内置县级行政区划库,提高拼音处理能力
This commit is contained in:
12922
CRSim.Core/Assets/District.json
Normal file
12922
CRSim.Core/Assets/District.json
Normal file
File diff suppressed because it is too large
Load Diff
@@ -9,12 +9,22 @@
|
||||
<PackageId>CRSim.Core</PackageId>
|
||||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="Assets\district.json" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
|
||||
<PackageReference Include="Downloader" Version="4.0.3" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="10.0.1" />
|
||||
<PackageReference Include="PinYinConverterCore" Version="1.0.2" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="Assets\District.json" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
70
CRSim.Core/Converters/ChineseToPinyinConverter.cs
Normal file
70
CRSim.Core/Converters/ChineseToPinyinConverter.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using Microsoft.International.Converters.PinYinConverter;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace CRSim.Core.Converters
|
||||
{
|
||||
public static class ChineseToPinyinConverter
|
||||
{
|
||||
private static readonly Dictionary<string, string> ChineseToPinyinMap = JsonDocument.Parse(
|
||||
new StreamReader(typeof(ChineseToPinyinConverter).Assembly.GetManifestResourceStream("CRSim.Core.Assets.District.json")
|
||||
?? throw new InvalidOperationException())
|
||||
.ReadToEnd())
|
||||
.RootElement.EnumerateArray()
|
||||
.ToDictionary(x => x.GetProperty("name").GetString()!, x => x.GetProperty("pinyin").GetString()!);
|
||||
public static string ConvertToPinyin(string input)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(input)) return input;
|
||||
|
||||
StringBuilder result = new();
|
||||
int i = 0;
|
||||
|
||||
while (i < input.Length)
|
||||
{
|
||||
bool matched = false;
|
||||
|
||||
// 1. 尝试在字典中匹配连续字符组
|
||||
for (int len = Math.Min(input.Length - i, 10); len >= 2; len--)
|
||||
{
|
||||
string sub = input.Substring(i, len);
|
||||
if (ChineseToPinyinMap.TryGetValue(sub, out var pinyin))
|
||||
{
|
||||
result.Append(pinyin);
|
||||
i += len;
|
||||
matched = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 如果字典没匹配到,处理单个字符
|
||||
if (!matched)
|
||||
{
|
||||
char c = input[i];
|
||||
if (c == '南')
|
||||
{
|
||||
result.Append("nan");
|
||||
}
|
||||
else if (ChineseChar.IsValidChar(c))
|
||||
{
|
||||
// 使用 Microsoft PinYinConverter 获取第一个拼音
|
||||
ChineseChar chineseChar = new(c);
|
||||
if (chineseChar.Pinyins.Count > 0 && chineseChar.Pinyins[0] != null)
|
||||
{
|
||||
string rawPinyin = chineseChar.Pinyins[0].ToString();
|
||||
string purePinyin = new string([.. rawPinyin.Where(ch => !char.IsDigit(ch))]).ToLower();
|
||||
result.Append(purePinyin);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 非汉字字符(数字、字母、标点)原样保留
|
||||
result.Append(c);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
return result.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,6 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.1" />
|
||||
<PackageReference Include="PinYinConverterCore" Version="1.0.2" />
|
||||
<PackageReference Include="PP.Wpf" Version="1.0.8" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
using Microsoft.International.Converters.PinYinConverter;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using System.Globalization;
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace CRSim.ScreenSimulator.Converters
|
||||
@@ -10,34 +8,10 @@ namespace CRSim.ScreenSimulator.Converters
|
||||
public bool Upper { get; set; } = false;
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
if (value == null) return string.Empty;
|
||||
string chineseText = value.ToString();
|
||||
if (value is not string chineseText) return string.Empty;
|
||||
if (string.IsNullOrWhiteSpace(chineseText))
|
||||
return string.Empty;
|
||||
|
||||
var sb = new StringBuilder();
|
||||
foreach (char c in chineseText)
|
||||
{
|
||||
if (ChineseChar.IsValidChar(c))
|
||||
{
|
||||
var chineseChar = new ChineseChar(c);
|
||||
var pinyins = chineseChar.Pinyins;
|
||||
if (pinyins != null && pinyins.Count > 0)
|
||||
{
|
||||
var pinyin = pinyins[0][..^1];
|
||||
sb.Append(pinyin);
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.Append(c);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.Append(c);
|
||||
}
|
||||
}
|
||||
var result = sb.ToString().ToLower();
|
||||
var result = Core.Converters.ChineseToPinyinConverter.ConvertToPinyin(chineseText.Replace(" ", ""));
|
||||
return Upper ? result.ToUpper() : char.ToUpper(result[0], culture) + result[1..];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user