Browse Source

简拼

bjmzak
wxd 2 years ago
parent
commit
a58d7496b8
  1. 170
      src/Shentun.Utilities/LanguageConverter.cs
  2. 1
      src/Shentun.Utilities/Shentun.Utilities.csproj
  3. 28
      test/Shentun.Peis.Application.Tests/BaseDataHandleTest.cs

170
src/Shentun.Utilities/LanguageConverter.cs

@ -7,83 +7,141 @@ namespace Shentun.Utilities
{ {
public class LanguageConverter public class LanguageConverter
{ {
public static string GetPYSimpleCode(string data, int len = 50)
{
//public static string GetPYSimpleCode(string data, int len = 50)
//{
string rtnValue = "";
// string rtnValue = "";
if (string.IsNullOrWhiteSpace(data))
{
return "";
}
// if (string.IsNullOrWhiteSpace(data))
// {
// return "";
// }
foreach (char obj in data)
{
try
{
ChineseChar chineseChar = new ChineseChar(obj);
string t = chineseChar.Pinyins[0].ToString();
rtnValue += t.Substring(0, 1);
}
catch
{
rtnValue += obj.ToString();
}
}
if (len > rtnValue.Length)
{
len = rtnValue.Length;
}
rtnValue = rtnValue.Substring(0, len).ToUpper();
return rtnValue;
}
// foreach (char obj in data)
// {
// try
// {
// ChineseChar chineseChar = new ChineseChar(obj);
// string t = chineseChar.Pinyins[0].ToString();
// rtnValue += t.Substring(0, 1);
// }
// catch
// {
// rtnValue += obj.ToString();
// }
// }
// if (len > rtnValue.Length)
// {
// len = rtnValue.Length;
// }
// rtnValue = rtnValue.Substring(0, len).ToUpper();
// return rtnValue;
//}
public static string GetPYCode(string data, int len = 50)
{
//public static string GetPYCode(string data, int len = 50)
//{
string rtnValue = "";
// string rtnValue = "";
if (string.IsNullOrWhiteSpace(data))
{
return "";
}
// if (string.IsNullOrWhiteSpace(data))
// {
// return "";
// }
foreach (char obj in data)
// foreach (char obj in data)
// {
// try
// {
// ChineseChar chineseChar = new ChineseChar(obj);
// string t = chineseChar.Pinyins[0].TrimEnd('1', '2', '3', '4', '5').ToUpper();
// rtnValue += t;
// }
// catch
// {
// rtnValue += obj.ToString();
// }
// }
// if (len > rtnValue.Length)
// {
// len = rtnValue.Length;
// }
// rtnValue = rtnValue.Substring(0, len).ToUpper();
// return rtnValue;
//}
//public static string GetPYCodeWithSpace(string data)
//{
// if(string.IsNullOrWhiteSpace(data))
// {
// return "";
// }
// string rtnValue = "";
// for (int i = 0; i < data.Length; i++)
// {
// rtnValue += " " + LanguageConverter.GetPYCode(data.Substring(i, 1));
// }
// return rtnValue.Trim();
//}
#region 新版
/// <summary>
/// 汉字转首字母
/// </summary>
/// <param name="strChinese"></param>
/// <returns></returns>
public static string GetPYSimpleCode(string strChinese)
{
try
{ {
try
if (strChinese.Length != 0)
{ {
ChineseChar chineseChar = new ChineseChar(obj);
string t = chineseChar.Pinyins[0].TrimEnd('1', '2', '3', '4', '5').ToUpper();
rtnValue += t;
}
catch
{
rtnValue += obj.ToString();
StringBuilder fullSpell = new StringBuilder();
for (int i = 0; i < strChinese.Length; i++)
{
var chr = strChinese[i];
fullSpell.Append(GetSpell(chr)[0]);
}
return fullSpell.ToString().ToUpper();
} }
} }
if (len > rtnValue.Length)
catch (Exception e)
{ {
len = rtnValue.Length;
Console.WriteLine("首字母转化出错!" + e.Message);
} }
rtnValue = rtnValue.Substring(0, len).ToUpper();
return rtnValue;
return string.Empty;
} }
public static string GetPYCodeWithSpace(string data)
private static string GetSpell(char chr)
{ {
if(string.IsNullOrWhiteSpace(data))
{
return "";
}
string rtnValue = "";
for (int i = 0; i < data.Length; i++)
var coverchr = NPinyin.Pinyin.GetPinyin(chr);
bool isChineses = ChineseChar.IsValidChar(coverchr[0]);
if (isChineses)
{ {
rtnValue += " " + LanguageConverter.GetPYCode(data.Substring(i, 1));
ChineseChar chineseChar = new ChineseChar(coverchr[0]);
foreach (string value in chineseChar.Pinyins)
{
if (!string.IsNullOrEmpty(value))
{
return value.Remove(value.Length - 1, 1);
}
}
} }
return rtnValue.Trim();
return coverchr;
} }
#endregion
} }
} }

1
src/Shentun.Utilities/Shentun.Utilities.csproj

@ -13,6 +13,7 @@
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.0" /> <PackageReference Include="Microsoft.Extensions.Http" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" /> <PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="3.1.5" /> <PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="3.1.5" />
<PackageReference Include="NPinyin.Core" Version="3.0.0" />
<PackageReference Include="NPOI" Version="2.5.6" /> <PackageReference Include="NPOI" Version="2.5.6" />
<PackageReference Include="OnceMi.AspNetCore.IdGenerator" Version="1.0.6" /> <PackageReference Include="OnceMi.AspNetCore.IdGenerator" Version="1.0.6" />
<PackageReference Include="PinYinConverterCore" Version="1.0.2" /> <PackageReference Include="PinYinConverterCore" Version="1.0.2" />

28
test/Shentun.Peis.Application.Tests/BaseDataHandleTest.cs

@ -10,6 +10,8 @@ using Volo.Abp.Uow;
using Xunit.Abstractions; using Xunit.Abstractions;
using Xunit; using Xunit;
using Shentun.Peis.DataMigrations; using Shentun.Peis.DataMigrations;
using Microsoft.International.Converters.PinYinConverter;
using Shentun.Utilities;
namespace Shentun.Peis namespace Shentun.Peis
{ {
@ -21,11 +23,17 @@ namespace Shentun.Peis
private readonly ITestOutputHelper _output; private readonly ITestOutputHelper _output;
private readonly IUnitOfWorkManager _unitOfWorkManager; private readonly IUnitOfWorkManager _unitOfWorkManager;
private readonly BaseDataHandleAppService _appService; private readonly BaseDataHandleAppService _appService;
private readonly IRepository<Asbitem, Guid> _asbitemRepository;
private readonly IRepository<Item, Guid> _itemRepository;
private readonly IRepository<Diagnosis, Guid> _diagnosisRepository;
public BaseDataHandleTest(ITestOutputHelper testOutputHelper) public BaseDataHandleTest(ITestOutputHelper testOutputHelper)
{ {
_output = testOutputHelper; _output = testOutputHelper;
_unitOfWorkManager = GetRequiredService<IUnitOfWorkManager>(); _unitOfWorkManager = GetRequiredService<IUnitOfWorkManager>();
_appService = GetRequiredService<BaseDataHandleAppService>(); _appService = GetRequiredService<BaseDataHandleAppService>();
_asbitemRepository = GetRequiredService<IRepository<Asbitem, Guid>>();
_itemRepository = GetRequiredService<IRepository<Item, Guid>>();
_diagnosisRepository = GetRequiredService<IRepository<Diagnosis, Guid>>();
} }
//[Fact] //[Fact]
@ -48,6 +56,23 @@ namespace Shentun.Peis
// } // }
// } // }
// } // }
[Fact]
public async void GetPYSimpleCode()
{
using (var unitOfWork = _unitOfWorkManager.Begin(isTransactional: true))
{
var diagnosisList = await _diagnosisRepository.GetListAsync();
foreach (var diagnosis in diagnosisList)
{
diagnosis.SimpleCode = LanguageConverter.GetPYSimpleCode(diagnosis.DisplayName);
}
await _diagnosisRepository.UpdateManyAsync(diagnosisList);
await unitOfWork.CompleteAsync();
}
}
@ -74,7 +99,7 @@ namespace Shentun.Peis
// } // }
// } // }
// } // }
//} //}
//[Fact] //[Fact]
@ -103,4 +128,5 @@ namespace Shentun.Peis
//} //}
} }
} }
Loading…
Cancel
Save