init commit

This commit is contained in:
路 范
2022-03-30 17:54:33 +08:00
parent df01841625
commit 904bdd16cd
500 changed files with 217251 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
namespace RoadFlow.Pinyin.format
{
/// <summary>
/// 大小写格式
/// </summary>
public enum CaseFormat
{
/// <summary>
/// 首字母大写,此选项对 a e o 几个独音无效
/// </summary>
CAPITALIZE_FIRST_LETTER,
/// <summary>
/// 全小写
/// </summary>
LOWERCASE,
/// <summary>
/// 全大写
/// </summary>
UPPERCASE
}
}

View File

@@ -0,0 +1,172 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using RoadFlow.Pinyin.exception;
namespace RoadFlow.Pinyin.format
{
/// <summary>
/// 拼音格式化
/// </summary>
public static class PinyinFormatter
{
/// <summary>
/// 将拼音格式化成指定的格式
/// </summary>
/// <param name="py">待格式化的拼音</param>
/// <param name="format">格式</param>
/// <see cref="ToneFormat"/>
/// <see cref="CaseFormat"/>
/// <see cref="VCharFormat"/>
/// <returns></returns>
public static string Format(string py, PinyinOutputFormat format)
{
// "v"或"u:"不能添加声调
if (ToneFormat.WITH_TONE_MARK == format.GetToneFormat &&
(
VCharFormat.WITH_V == format.GetVCharFormat
|| VCharFormat.WITH_U_AND_COLON == format.GetVCharFormat
)
)
{
throw new PinyinException("\"v\"或\"u:\"不能添加声调");
}
var pinyin = py;
switch (format.GetToneFormat)
{
case ToneFormat.WITHOUT_TONE:
// 不带声调
var reg = new Regex("[1-5]");
pinyin = reg.Replace(pinyin, "");
break;
case ToneFormat.WITH_TONE_MARK:
// 带声调标志
pinyin = pinyin.Replace("u:", "v");
pinyin = convertToneNumber2ToneMark(pinyin);
break;
}
switch (format.GetVCharFormat)
{
case VCharFormat.WITH_V:
// 输出v
pinyin = pinyin.Replace("u:", "v");
break;
case VCharFormat.WITH_U_UNICODE:
// 输出ü
pinyin = pinyin.Replace("u:", "ü");
break;
}
switch (format.GetCaseFormat)
{
case CaseFormat.UPPERCASE:
// 大写
pinyin = pinyin.ToUpper();
break;
case CaseFormat.LOWERCASE:
// 小写
pinyin = pinyin.ToLower();
break;
case CaseFormat.CAPITALIZE_FIRST_LETTER:
// 首字母大写
// 不处理单拼音 a e o
if (!new List<string> { "a", "e", "o"}.Contains(pinyin.ToLower()))
{
pinyin = pinyin.Substring(0, 1).ToUpper() + (pinyin.Length == 1 ? "" : pinyin.Substring(1));
}
break;
}
return pinyin;
}
/// <summary>
/// 将拼音的声调数字转换成字符
/// </summary>
/// <param name="pinyin"></param>
/// <returns></returns>
private static string convertToneNumber2ToneMark(string pinyin)
{
var lowerCasePinyinStr = pinyin.ToLower();
var reg = new Regex("[a-z]*[1-5]?");
if (!reg.IsMatch(lowerCasePinyinStr)) return lowerCasePinyinStr;
const char defautlCharValue = '$';
const int defautlIndexValue = -1;
var unmarkedVowel = defautlCharValue;
var indexOfUnmarkedVowel = defautlIndexValue;
const char charA = 'a';
const char charE = 'e';
const string ouStr = "ou";
const string allUnmarkedVowelStr = "aeiouv";
const string allMarkedVowelStr = "āáăàaēéĕèeīíĭìiōóŏòoūúŭùuǖǘǚǜü";
reg = new Regex("[a-z]*[1-5]");
if (!reg.IsMatch(lowerCasePinyinStr)) return lowerCasePinyinStr.Replace("v", "ü");
var tuneNumber = (int)char.GetNumericValue(lowerCasePinyinStr[lowerCasePinyinStr.Length - 1]);
var indexOfA = lowerCasePinyinStr.IndexOf(charA);
var indexOfE = lowerCasePinyinStr.IndexOf(charE);
var ouIndex = lowerCasePinyinStr.IndexOf(ouStr, StringComparison.Ordinal);
if (-1 != indexOfA)
{
indexOfUnmarkedVowel = indexOfA;
unmarkedVowel = charA;
}
else if (-1 != indexOfE)
{
indexOfUnmarkedVowel = indexOfE;
unmarkedVowel = charE;
}
else if (-1 != ouIndex)
{
indexOfUnmarkedVowel = ouIndex;
unmarkedVowel = ouStr[0];
}
else
{
reg = new Regex("[" + allUnmarkedVowelStr + "]");
for (var i = lowerCasePinyinStr.Length - 1; i >= 0; i--)
{
if (!reg.IsMatch(lowerCasePinyinStr[i].ToString())) continue;
indexOfUnmarkedVowel = i;
unmarkedVowel = lowerCasePinyinStr[i];
break;
}
}
if (defautlCharValue == unmarkedVowel || defautlIndexValue == indexOfUnmarkedVowel)
return lowerCasePinyinStr;
var rowIndex = allUnmarkedVowelStr.IndexOf(unmarkedVowel);
var columnIndex = tuneNumber - 1;
var vowelLocation = rowIndex * 5 + columnIndex;
var markedVowel = allMarkedVowelStr[vowelLocation];
var resultBuffer = new StringBuilder();
// 声母
resultBuffer.Append(lowerCasePinyinStr.Substring(0, indexOfUnmarkedVowel).Replace("v", "ü"));
// 有声调的部分
resultBuffer.Append(markedVowel);
// 剩下的部分
resultBuffer.Append(lowerCasePinyinStr.Substring(indexOfUnmarkedVowel + 1).Replace("v", "ü"));
var result = resultBuffer.ToString();
// 替换声调数字
result = new Regex("[0-9]").Replace(result, "");
return result;
// only replace v with ü (umlat) character
}
}
}

View File

@@ -0,0 +1,103 @@
using System;
namespace RoadFlow.Pinyin.format
{
/// <summary>
/// 拼音输出格式
/// </summary>
public class PinyinOutputFormat
{
// 声调格式
// 大小写格式
// 字符v的格式
/// <summary>
/// 获取声调格式
/// </summary>
public ToneFormat GetToneFormat { get; private set; }
/// <summary>
/// 获取大小写格式
/// </summary>
public CaseFormat GetCaseFormat { get; private set; }
/// <summary>
/// 获取字符v的格式
/// </summary>
public VCharFormat GetVCharFormat { get; private set; }
/// <summary>
/// 使用默认值初始化输出格式
/// ToneFormat.WITH_TONE_MARK,
/// CaseFormat.LOWERCASE,
/// VCharFormat.WITH_U_UNICODE
/// </summary>
public PinyinOutputFormat()
{
GetToneFormat = ToneFormat.WITH_TONE_MARK;
GetCaseFormat = CaseFormat.LOWERCASE;
GetVCharFormat = VCharFormat.WITH_U_UNICODE;
}
/// <summary>
/// 通过构造初始化输入格式
/// </summary>
/// <param name="toneFormat">声调格式</param>
/// <param name="caseFormat">大小写格式</param>
/// <param name="vCharFormat">字符V的格式</param>
public PinyinOutputFormat(ToneFormat toneFormat, CaseFormat caseFormat, VCharFormat vCharFormat)
{
SetFormat(toneFormat, caseFormat, vCharFormat);
}
/// <summary>
/// 通过构造初始化输入格式
/// </summary>
/// <param name="toneFormat">声调格式字符串</param>
/// <param name="caseFormat">大小写格式字符串</param>
/// <param name="vCharFormat">字符V的格式字符串</param>
/// <see cref="ToneFormat"/>
/// <see cref="CaseFormat"/>
/// <see cref="VCharFormat"/>
public PinyinOutputFormat(string toneFormat, string caseFormat, string vCharFormat)
{
SetFormat(toneFormat, caseFormat, vCharFormat);
}
/// <summary>
/// 设置输入格式
/// </summary>
/// <param name="toneFormat">声调格式</param>
/// <param name="caseFormat">大小写格式</param>
/// <param name="vCharFormat">字符V的格式</param>
public void SetFormat(ToneFormat toneFormat, CaseFormat caseFormat, VCharFormat vCharFormat)
{
GetToneFormat = toneFormat;
GetCaseFormat = caseFormat;
GetVCharFormat = vCharFormat;
}
/// <summary>
/// 设置输入格式
/// </summary>
/// <param name="toneFormat">声调格式字符串</param>
/// <param name="caseFormat">大小写格式字符串</param>
/// <param name="vCharFormat">字符V的格式字符串</param>
/// <see cref="ToneFormat"/>
/// <see cref="CaseFormat"/>
/// <see cref="VCharFormat"/>
public void SetFormat(string toneFormat, string caseFormat, string vCharFormat)
{
if (!string.IsNullOrEmpty(toneFormat))
{
GetToneFormat = (ToneFormat)Enum.Parse(typeof(ToneFormat), toneFormat);
}
if (!string.IsNullOrEmpty(caseFormat))
{
GetCaseFormat = (CaseFormat)Enum.Parse(typeof(CaseFormat), caseFormat);
}
if (!string.IsNullOrEmpty(vCharFormat))
{
GetVCharFormat = (VCharFormat)Enum.Parse(typeof(VCharFormat), vCharFormat);
}
}
}
}

View File

@@ -0,0 +1,21 @@
namespace RoadFlow.Pinyin.format
{
/// <summary>
/// 声调格式
/// </summary>
public enum ToneFormat
{
/// <summary>
/// 带声调标志
/// </summary>
WITH_TONE_MARK,
/// <summary>
/// 不带声调
/// </summary>
WITHOUT_TONE,
/// <summary>
/// 带声调数字值
/// </summary>
WITH_TONE_NUMBER
}
}

View File

@@ -0,0 +1,21 @@
namespace RoadFlow.Pinyin.format
{
/// <summary>
/// V(ü)字符格式
/// </summary>
public enum VCharFormat
{
/// <summary>
/// 将 ü 输出为 u:
/// </summary>
WITH_U_AND_COLON,
/// <summary>
/// 将 ü 输出为 v
/// </summary>
WITH_V,
/// <summary>
/// 将 ü 输出为ü
/// </summary>
WITH_U_UNICODE
}
}