Browse Source

commit: clean and fix JapanesePhonetic process

HOME 4 years ago
parent
commit
0bd9f82e81

+ 1 - 0
BeatLyrics.Shared/Models/LyricDetail.cs

@@ -3,6 +3,7 @@
     public class LyricDetail
     {
         public string Text { get; set; }
+        public string TextWithRuby { get; set; }
         public int TimeMs { get; set; }
         public int DurationMs { get; set; }
     }

+ 4 - 4
BeatLyrics.Tool/DataProvider/OnlineLyric/Models/LyricSearchResultItem.cs

@@ -1,8 +1,7 @@
-using System;
-using System.Collections.Generic;
-using BeatLyrics.Common.Models;
-using BeatLyrics.Tool.Models;
+using BeatLyrics.Tool.Models;
 using BeatLyrics.Tool.Utils;
+using System;
+using System.Collections.Generic;
 
 namespace BeatLyrics.Tool.DataProvider.OnlineLyric.Models
 {
@@ -18,6 +17,7 @@ namespace BeatLyrics.Tool.DataProvider.OnlineLyric.Models
         public string Id { get; set; }
         public string Name { get; set; }
         public int Duration { get; set; }
+
         public string DurationText
         {
             get

+ 1 - 9
BeatLyrics.Tool/Dialogs/TextDialog.cs

@@ -1,12 +1,4 @@
-using System;
-using System.Collections.Generic;
-using System.ComponentModel;
-using System.Data;
-using System.Drawing;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using System.Windows.Forms;
+using System.Windows.Forms;
 
 namespace BeatLyrics.Tool.Dialogs
 {

+ 1 - 0
BeatLyrics.Tool/Dialogs/TextEditDialog.Designer.cs

@@ -73,6 +73,7 @@
             this.Controls.Add(this.EditorControl);
             this.Controls.Add(this.SaveTextBox);
             this.Controls.Add(this.SaveButton);
+            this.KeyPreview = true;
             this.MinimizeBox = false;
             this.Name = "TextEditForm";
             this.ShowInTaskbar = false;

+ 0 - 2
BeatLyrics.Tool/MainForm.cs

@@ -184,8 +184,6 @@ namespace BeatLyrics.Tool
 
         private void SearchTextBox_TextChanged(object sender, EventArgs e) => Search();
 
-        private void SearchVerifyCheckBox_CheckedChanged(object sender, EventArgs e) => Search();
-
         private void AllRadioButton_CheckedChanged(object sender, EventArgs e)
         {
             if (((RadioButton)sender).Checked) Search();

+ 32 - 1
BeatLyrics.Tool/Models/TextTuple.cs

@@ -8,7 +8,38 @@
 
         public override string ToString()
         {
-            return ToRubyTmpString();
+            return ToRubyHumString();
+        }
+
+        public string ToRubyHumString()
+        {
+            var left = 0;
+
+            var regexKata = new System.Text.RegularExpressions.Regex("^[\\p{IsKatakana}]+$");
+            if (regexKata.IsMatch(Text)) return Text;
+
+            if (Ruby == Text) return Text;
+
+            if (false == string.IsNullOrWhiteSpace(Ruby))
+            {
+                for (int i = 0; i < Ruby.Length; i++)
+                {
+                    if (Text[Text.Length - 1 - i] == Ruby[Ruby.Length - 1 - i]) continue;
+                    left = i;
+                    break;
+                }
+
+                var textLeft = Text.Substring(0, Text.Length - left);
+                var textRight = Text.Substring(Text.Length - left);
+
+                var midRuby = Ruby.Substring(0, Ruby.Length - left);
+
+                return $"<{textLeft}={midRuby}>{textRight}";
+            }
+
+            if (Text == Ruby || string.IsNullOrWhiteSpace(Ruby)) return Text;
+
+            return $"<{Text}={Ruby}>";
         }
 
         public string ToRubyTmpString()

+ 1 - 2
BeatLyrics.Tool/Properties/AssemblyInfo.cs

@@ -1,5 +1,4 @@
 using System.Reflection;
-using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 
 // 有关程序集的一般信息由以下
@@ -22,7 +21,7 @@ using System.Runtime.InteropServices;
 // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
 [assembly: Guid("e50656df-85a0-4a37-8f64-618d2dcac8d8")]
 
-// 程序集的版本信息由下列四个值组成: 
+// 程序集的版本信息由下列四个值组成:
 //
 //      主版本
 //      次版本

+ 0 - 1
BeatLyrics.Tool/UserControls/TextArrangeUserControl.Designer.cs

@@ -433,7 +433,6 @@
             this.Controls.Add(this.PlayPosTrackBar);
             this.Name = "TextArrangeUserControl";
             this.Size = new System.Drawing.Size(824, 426);
-            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TextArrangeUserControl_KeyDown);
             splitContainer1.Panel1.ResumeLayout(false);
             splitContainer1.Panel2.ResumeLayout(false);
             ((System.ComponentModel.ISupportInitialize)(splitContainer1)).EndInit();

+ 12 - 15
BeatLyrics.Tool/UserControls/TextArrangeUserControl.cs

@@ -13,9 +13,10 @@ namespace BeatLyrics.Tool.UserControls
 {
     internal partial class TextArrangeUserControl : UserControl
     {
-        private static readonly Regex _reg = new Regex("([(\\(]\\p{IsHiragana}+[\\))])", RegexOptions.Compiled);
-        private static readonly Regex _reg2 = new Regex("(<ruby=\\p{IsHiragana}+>)", RegexOptions.Compiled);
-        private static readonly Regex _reg3 = new Regex("(</ruby>)", RegexOptions.Compiled);
+        private static readonly Regex RegRemoveRuby1 = new Regex("([(\\(]\\p{IsHiragana}+[\\))])", RegexOptions.Compiled);
+        private static readonly Regex RegRemoveRuby2 = new Regex("(<ruby=\\p{IsHiragana}+>)", RegexOptions.Compiled);
+        private static readonly Regex RegRemoveRuby3 = new Regex("(</ruby>)", RegexOptions.Compiled);
+        private static readonly Regex RegRubyConvert = new Regex("(<(.+?)=(\\p{IsHiragana}+)>)", RegexOptions.Compiled);
 
         private LyricFileExt _file;
 
@@ -98,7 +99,7 @@ namespace BeatLyrics.Tool.UserControls
 
         protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
         {
-            if (false == ActiveControl is TextBox && keyData == Keys.Space)
+            if (false == ActiveControl is TextBox && (keyData == Keys.Space||keyData==Keys.ProcessKey))
             {
                 if (OggAudioPlayer.IsPlaying) OggAudioPlayer.Stop();
                 else OggAudioPlayer.Play();
@@ -107,13 +108,6 @@ namespace BeatLyrics.Tool.UserControls
             return base.ProcessCmdKey(ref msg, keyData);
         }
 
-        private void TextArrangeUserControl_KeyDown(object sender, KeyEventArgs e)
-        {
-            if (false == ActiveControl is TextBox && e.KeyCode == Keys.Space)
-            {
-            }
-        }
-
         private void PlayButton_Click(object sender, EventArgs e)
         {
             if (OggAudioPlayer.IsPlaying) OggAudioPlayer.Stop();
@@ -176,11 +170,13 @@ namespace BeatLyrics.Tool.UserControls
 
         private void RemoveRubyButton_Click(object sender, EventArgs e)
         {
+            //TODO: follow ruby format
             foreach (LyricDetailExt item in LeftLyricsObjectListView.SelectedObjects)
             {
-                item.Text = _reg.Replace(item.Text, "");
-                item.Text = _reg2.Replace(item.Text, "");
-                item.Text = _reg3.Replace(item.Text, "");
+                item.Text = RegRemoveRuby1.Replace(item.Text, "");
+                item.Text = RegRemoveRuby2.Replace(item.Text, "");
+                item.Text = RegRemoveRuby3.Replace(item.Text, "");
+                item.Text = RegRubyConvert.Replace(item.Text, match => match.Groups[2].Value);
                 LeftLyricsObjectListView.UpdateObject(item);
             }
         }
@@ -189,8 +185,9 @@ namespace BeatLyrics.Tool.UserControls
         {
             foreach (LyricDetailExt item in LeftLyricsObjectListView.SelectedObjects)
             {
-                var arr = Utils.JapanesePhonetic.GetWords(item.Text);
+                var arr = JapanesePhonetic.GetWords(item.Text);
                 item.Text = string.Join("", arr.Select(p => p.ToString()));
+                item.TextWithRuby = string.Join("", arr.Select(p => p.ToRubyTmpString()));
                 LeftLyricsObjectListView.UpdateObject(item);
             }
         }

+ 1 - 5
BeatLyrics.Tool/Utils/Hasher.cs

@@ -1,9 +1,5 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
+using System.Linq;
 using System.Security.Cryptography;
-using System.Text;
-using System.Threading.Tasks;
 
 namespace BeatLyrics.Tool.Utils
 {

+ 57 - 4
BeatLyrics.Tool/Utils/JapanesePhonetic.cs

@@ -1,10 +1,10 @@
-using BeatLyrics.Common.Models;
+using BeatLyrics.Tool.Models;
 using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq.Expressions;
 using System.Reflection;
-using BeatLyrics.Tool.Models;
+using System.Text.RegularExpressions;
 
 namespace BeatLyrics.Tool.Utils
 {
@@ -14,6 +14,12 @@ namespace BeatLyrics.Tool.Utils
         private static readonly Func<object, string> GetDisplayText;
         private static readonly Func<object, string> GetYomiText;
 
+        private static readonly Regex RegexPassPattern1 = new Regex(@"^[0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,.:;!?"'`^~ ̄_&@#%+-*=<>()[]{}⦅⦆|¦/\$]+$", RegexOptions.Compiled);
+        private static readonly Regex RegexPassPattern2 = new Regex(@"^[\p{IsKatakana}\p{IsHiragana}…]+$", RegexOptions.Compiled);
+
+        private static readonly Regex RegexSplitPattern1 = new Regex(@"^(\p{IsCJKUnifiedIdeographs}+)(\p{IsHiragana}+)(\p{IsCJKUnifiedIdeographs}+)$", RegexOptions.Compiled);
+        private static readonly Regex RegexSplitPattern2 = new Regex(@"^(\p{IsHiragana}+)(\p{IsCJKUnifiedIdeographs}+)$", RegexOptions.Compiled);
+
         static JapanesePhonetic()
         {
             ClassType = Type.GetType("Windows.Globalization.JapanesePhoneticAnalyzer," +
@@ -37,8 +43,55 @@ namespace BeatLyrics.Tool.Utils
 
             foreach (var item in ret)
             {
-                var x = new TextTuple { Text = GetDisplayText(item), Ruby = GetYomiText(item) };
-                lst.Add(x);
+                var displayText = GetDisplayText(item);
+                var yomiText = GetYomiText(item);
+
+                {
+                    var match = RegexPassPattern1.Match(displayText);
+                    if (displayText == yomiText && match.Success)
+                    {
+                        var passed = displayText.Normalize(System.Text.NormalizationForm.FormKC);
+                        lst.Add(new TextTuple { Text = passed, Ruby = passed });
+                        continue;
+                    }
+                }
+
+                {
+                    if (RegexPassPattern2.IsMatch(displayText))
+                    {
+                        lst.Add(new TextTuple { Text = displayText, Ruby = displayText });
+                        continue;
+                    }
+                }
+
+                {
+                    var match = RegexSplitPattern1.Match(displayText);
+                    if (match.Success)
+                    {
+                        var middleHira = match.Groups[2].Value;
+                        var split = yomiText.Split(new[] { middleHira }, StringSplitOptions.RemoveEmptyEntries);
+
+                        lst.Add(new TextTuple { Text = match.Groups[1].Value, Ruby = split[0] });
+                        lst.Add(new TextTuple { Text = middleHira, Ruby = middleHira });
+                        lst.Add(new TextTuple { Text = match.Groups[3].Value, Ruby = split[1] });
+                        continue;
+                    }
+                }
+
+                {
+                    var match = RegexSplitPattern2.Match(displayText);
+                    if (match.Success)
+                    {
+                        var leftHira = match.Groups[1].Value;
+                        var split = yomiText.Split(new[] { leftHira }, StringSplitOptions.RemoveEmptyEntries);
+
+                        lst.Add(new TextTuple { Text = leftHira, Ruby = leftHira });
+                        lst.Add(new TextTuple { Text = match.Groups[2].Value, Ruby = split[0] });
+                        continue;
+                    }
+                }
+
+                lst.Add(new TextTuple { Text = displayText, Ruby = yomiText });
             }
 
             var arr = lst.ToArray();

+ 2 - 5
BeatLyrics.Tool/Utils/LoadingWrap.cs

@@ -1,12 +1,9 @@
-using System;
-using System.Collections.Generic;
+using BeatLyrics.Tool.Properties;
+using System;
 using System.Drawing;
-using System.Linq;
-using System.Text;
 using System.Threading;
 using System.Threading.Tasks;
 using System.Windows.Forms;
-using BeatLyrics.Tool.Properties;
 
 namespace BeatLyrics.Tool.Utils
 {

+ 5 - 5
BeatLyrics.Tool/Utils/SpectrumAnalyzer.cs

@@ -43,8 +43,8 @@ namespace BeatLyrics.Tool.Utils
             {
                 //feed samples to fft
                 var timeSeries = samples[i];
-                var N = (uint)timeSeries.Length;
-                double[] wc = DSP.Window.Coefficients(windowType, N);
+                var n = (uint)timeSeries.Length;
+                double[] wc = DSP.Window.Coefficients(windowType, n);
 
                 double windowScaleFactor = DSP.Window.ScaleFactor.Signal(wc);
                 double[] windowedTimeSeries = DSP.Math.Multiply(timeSeries, wc);
@@ -55,14 +55,14 @@ namespace BeatLyrics.Tool.Utils
                 for (int pow = 1; pow <= 32; pow++)
                 {
                     var p = (uint)System.Math.Pow(2, pow);
-                    if (p >= N)
+                    if (p >= n)
                     {
-                        pad = p - N;
+                        pad = p - n;
                         break;
                     }
                 }
 
-                fft.Initialize(N, pad);
+                fft.Initialize(n, pad);
 
                 Complex[] cpxResult = fft.Execute(windowedTimeSeries);
                 double[] mag = DSP.ConvertComplex.ToMagnitude(cpxResult);

+ 2 - 2
BeatLyrics/LyricsComponent.cs

@@ -73,7 +73,7 @@ namespace BeatLyrics
             {
                 try
                 {
-                    _data.difficultyBeatmap.SetField("_noteJumpMovementSpeed", 17);
+                    _data.difficultyBeatmap.SetField("_noteJumpMovementSpeed", 18);
                 }
                 catch (Exception e)
                 {
@@ -142,7 +142,7 @@ namespace BeatLyrics
                 else
                 {
                     //Show LRC
-                    _text.UnditedText = _lyricFile.Main?.FirstOrDefault(p => p.TimeMs <= milliseconds && p.TimeMs + p.DurationMs >= milliseconds)?.Text
+                    _text.UnditedText = _lyricFile.Main?.Where(p => p.TimeMs <= milliseconds && p.TimeMs + p.DurationMs >= milliseconds).Select(p => p.TextWithRuby ?? p.Text).FirstOrDefault()
                                  + Environment.NewLine + _lyricFile.Subtitle?.FirstOrDefault(p => p.TimeMs <= milliseconds && p.TimeMs + p.DurationMs >= milliseconds)?.Text;
                 }
             }

+ 1 - 1
BeatLyrics/RubyTextMeshProUGUI.cs

@@ -25,7 +25,7 @@ namespace BeatLyrics
         [SerializeField] private string rubyVerticalOffset = "1em";
 
         [Tooltip("ruby scale. (1=100%)")]
-        [SerializeField] private float rubyScale = 0.5f;
+        [SerializeField] private float rubyScale = 0.7f;
 
         [Tooltip("ruby show type.")]
         [SerializeField] private RubyShowType rubyShowType = RubyShowType.RUBY_ALIGNMENT;