JapanesePhonetic.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using BeatLyrics.Tool.Models;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq.Expressions;
  6. using System.Reflection;
  7. using System.Text.RegularExpressions;
  8. namespace BeatLyrics.Tool.Utils
  9. {
  10. internal static class JapanesePhonetic
  11. {
  12. private static readonly Type ClassType;
  13. private static readonly Func<object, string> GetDisplayText;
  14. private static readonly Func<object, string> GetYomiText;
  15. private static readonly Regex RegexPassPattern1 = new Regex(@"^[0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,.:;!?"'`^~ ̄_&@#%+-*=<>()[]{}⦅⦆|¦/\$]+$", RegexOptions.Compiled);
  16. private static readonly Regex RegexPassPattern2 = new Regex(@"^[\p{IsKatakana}\p{IsHiragana}…]+$", RegexOptions.Compiled);
  17. private static readonly Regex RegexSplitPattern1 = new Regex(@"^(\p{IsCJKUnifiedIdeographs}+)(\p{IsHiragana}+)(\p{IsCJKUnifiedIdeographs}+)$", RegexOptions.Compiled);
  18. private static readonly Regex RegexSplitPattern2 = new Regex(@"^(\p{IsHiragana}+)(\p{IsCJKUnifiedIdeographs}+)$", RegexOptions.Compiled);
  19. private static readonly Regex RegexSplitPattern3 = new Regex(@"^(\p{IsCJKUnifiedIdeographs}+)(\p{IsHiragana}+)$", RegexOptions.Compiled);
  20. private static readonly Regex RegexSplitPattern4 = new Regex(@"^(\p{IsHiragana}+)(\p{IsCJKUnifiedIdeographs}+)(\p{IsHiragana}+)$", RegexOptions.Compiled);
  21. private static readonly Regex RegexSplitPattern5 = new Regex(@"^(\p{IsCJKUnifiedIdeographs}+)(\p{IsHiragana}+)(\p{IsCJKUnifiedIdeographs}+)(\p{IsHiragana}+)$", RegexOptions.Compiled);
  22. static JapanesePhonetic()
  23. {
  24. ClassType = Type.GetType("Windows.Globalization.JapanesePhoneticAnalyzer," +
  25. " Windows.Globalization, Version=255.255.255.255," +
  26. " Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime");
  27. var itemType = Type.GetType("Windows.Globalization.JapanesePhoneme," +
  28. " Windows.Globalization, Version=255.255.255.255," +
  29. " Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime");
  30. var pArg = Expression.Parameter(typeof(object));
  31. var pArgConv = Expression.Convert(pArg, itemType);
  32. GetDisplayText = Expression.Lambda<Func<object, string>>(Expression.Property(pArgConv, "DisplayText"), pArg).Compile();
  33. GetYomiText = Expression.Lambda<Func<object, string>>(Expression.Property(pArgConv, "YomiText"), pArg).Compile();
  34. }
  35. public static TextTuple[] GetWords(string text)
  36. {
  37. var ret = (IEnumerable)ClassType.InvokeMember("GetWords", BindingFlags.InvokeMethod, null, null,
  38. new object[] { text });
  39. var lst = new List<TextTuple>();
  40. foreach (var item in ret)
  41. {
  42. var displayText = GetDisplayText(item);
  43. if (displayText == " ")
  44. {
  45. lst.Add(new TextTuple { Text = " ", Ruby = " " });
  46. continue;
  47. }
  48. var yomiText = GetYomiText(item);
  49. {
  50. var match = RegexPassPattern1.Match(displayText);
  51. if (displayText == yomiText && match.Success)
  52. {
  53. var passed = displayText.Normalize(System.Text.NormalizationForm.FormKC);
  54. lst.Add(new TextTuple { Text = passed, Ruby = passed });
  55. continue;
  56. }
  57. }
  58. {
  59. if (RegexPassPattern2.IsMatch(displayText))
  60. {
  61. lst.Add(new TextTuple { Text = displayText, Ruby = displayText });
  62. continue;
  63. }
  64. }
  65. {
  66. var match = RegexSplitPattern1.Match(displayText);
  67. if (match.Success)
  68. {
  69. var middleHira = match.Groups[2].Value;
  70. var split = yomiText.Split(new[] { middleHira }, StringSplitOptions.RemoveEmptyEntries);
  71. lst.Add(new TextTuple { Text = match.Groups[1].Value, Ruby = split[0] });
  72. lst.Add(new TextTuple { Text = middleHira, Ruby = middleHira });
  73. lst.Add(new TextTuple { Text = match.Groups[3].Value, Ruby = split[1] });
  74. continue;
  75. }
  76. }
  77. {
  78. var match = RegexSplitPattern2.Match(displayText);
  79. if (match.Success)
  80. {
  81. var leftHira = match.Groups[1].Value;
  82. var split = yomiText.Split(new[] { leftHira }, StringSplitOptions.RemoveEmptyEntries);
  83. lst.Add(new TextTuple { Text = leftHira, Ruby = leftHira });
  84. lst.Add(new TextTuple { Text = match.Groups[2].Value, Ruby = split[0] });
  85. continue;
  86. }
  87. }
  88. {
  89. var match = RegexSplitPattern3.Match(displayText);
  90. if (match.Success)
  91. {
  92. var hira = match.Groups[2].Value;
  93. var split = yomiText.Split(new[] { hira }, StringSplitOptions.RemoveEmptyEntries);
  94. lst.Add(new TextTuple { Text = match.Groups[1].Value, Ruby = split[0] });
  95. lst.Add(new TextTuple { Text = hira, Ruby = hira });
  96. continue;
  97. }
  98. }
  99. {
  100. var match = RegexSplitPattern4.Match(displayText);
  101. if (match.Success)
  102. {
  103. var leadingHira = match.Groups[1].Value;
  104. var suffixHira = match.Groups[3].Value;
  105. var split = yomiText.Substring(leadingHira.Length);
  106. split = split.Substring(0, split.Length - suffixHira.Length);
  107. lst.Add(new TextTuple { Text = leadingHira, Ruby = leadingHira });
  108. lst.Add(new TextTuple { Text = match.Groups[2].Value, Ruby = split });
  109. lst.Add(new TextTuple { Text = suffixHira, Ruby = suffixHira });
  110. continue;
  111. }
  112. }
  113. {
  114. var match = RegexSplitPattern5.Match(displayText);
  115. if (match.Success)
  116. {
  117. var kan1 = match.Groups[1].Value;
  118. var hira1 = match.Groups[2].Value;
  119. var kan2 = match.Groups[3].Value;
  120. var hira2 = match.Groups[4].Value;
  121. var split = yomiText.Split(new[] { hira1, hira2 }, StringSplitOptions.RemoveEmptyEntries);
  122. lst.Add(new TextTuple { Text = kan1, Ruby = split[0] });
  123. lst.Add(new TextTuple { Text = hira1, Ruby = hira1 });
  124. lst.Add(new TextTuple { Text = kan2, Ruby = split[1] });
  125. lst.Add(new TextTuple { Text = hira2, Ruby = hira2 });
  126. continue;
  127. }
  128. }
  129. lst.Add(new TextTuple { Text = displayText, Ruby = yomiText });
  130. }
  131. var arr = lst.ToArray();
  132. return arr;
  133. }
  134. }
  135. }