From acdfb439cceec3d84091528c2519bec0273a7c74 Mon Sep 17 00:00:00 2001 From: miyaji255 <84168445+miyaji255@users.noreply.github.com> Date: Fri, 5 Apr 2024 08:44:48 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=AB=E3=83=93=E7=BD=AE=E6=8F=9B=E3=82=92?= =?UTF-8?q?=E7=B0=A1=E7=B4=A0=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Epub/KoeBook.Epub/Models/Paragraph.cs | 2 +- Epub/KoeBook.Epub/Services/AnalyzerService.cs | 49 ++++--------------- KoeBook.Test/Epub/AnalyzerServiceTest.cs | 32 ++++++++++++ KoeBook.Test/Epub/EpubDocumentTest.cs | 4 +- 4 files changed, 45 insertions(+), 42 deletions(-) create mode 100644 KoeBook.Test/Epub/AnalyzerServiceTest.cs diff --git a/Epub/KoeBook.Epub/Models/Paragraph.cs b/Epub/KoeBook.Epub/Models/Paragraph.cs index 9faffe9..a765f23 100644 --- a/Epub/KoeBook.Epub/Models/Paragraph.cs +++ b/Epub/KoeBook.Epub/Models/Paragraph.cs @@ -6,5 +6,5 @@ public sealed class Paragraph : Element { public ScriptLine? ScriptLine { get; set; } public Audio? Audio => ScriptLine?.Audio; - public string? Text { get; set; } + public string Text { get; set; } = ""; } diff --git a/Epub/KoeBook.Epub/Services/AnalyzerService.cs b/Epub/KoeBook.Epub/Services/AnalyzerService.cs index 429e6e1..9b36bcc 100644 --- a/Epub/KoeBook.Epub/Services/AnalyzerService.cs +++ b/Epub/KoeBook.Epub/Services/AnalyzerService.cs @@ -39,31 +39,16 @@ public async ValueTask AnalyzeAsync(BookProperties bookProperties, } _epubDocumentStoreService.Register(document, cancellationToken); - var scriptLines = new List(); - foreach (var chapter in document.Chapters) - { - foreach (var section in chapter.Sections) + var scriptLines = document.Chapters.SelectMany(c => c.Sections) + .SelectMany(s => s.Elements) + .OfType() + .Select(p => { - foreach (var element in section.Elements) - { - if (element is Paragraph paragraph) - { - var line = paragraph.Text; - // rubyタグがあればルビのdictionaryに登録 - var rubyDict = ExtractRuby(line).ToDictionary(); + // ルビを置換 + var line = ReplaceBaseTextWithRuby(p.Text); - foreach (var ruby in rubyDict) - _rubyReplacements.TryAdd(ruby.Key, ruby.Value); - // ルビを置換 - line = ReplaceBaseTextWithRuby(line, rubyDict); - - var scriptLine = new ScriptLine(line, "", ""); - paragraph.ScriptLine = scriptLine; - scriptLines.Add(scriptLine); - } - } - } - } + return p.ScriptLine = new ScriptLine(line, "", ""); + }).ToList(); // 800文字以上になったら1チャンクに分ける var chunks = new List(); @@ -85,24 +70,10 @@ public async ValueTask AnalyzeAsync(BookProperties bookProperties, return bookScripts; } - private static IEnumerable> ExtractRuby(string text) - { - return RubyRegex() - .Matches(text) - .Select(m => KeyValuePair.Create(m.Groups[1].Value, m.Groups[2].Value)); - } - - private static string ReplaceBaseTextWithRuby(string text, Dictionary rubyDict) + private static string ReplaceBaseTextWithRuby(string text) { // 元のテキストからルビタグをすべてルビテキストに置き換える - var resultText = text; - foreach (var pair in rubyDict) - { - var rubyTag = $"{pair.Key}{pair.Value}"; - resultText = resultText.Replace(rubyTag, pair.Value); - } - - return resultText; + return RubyRegex().Replace(text, m => m.Groups[2].Value); } [GeneratedRegex("(.*?)(.*?)")] diff --git a/KoeBook.Test/Epub/AnalyzerServiceTest.cs b/KoeBook.Test/Epub/AnalyzerServiceTest.cs new file mode 100644 index 0000000..14a4f36 --- /dev/null +++ b/KoeBook.Test/Epub/AnalyzerServiceTest.cs @@ -0,0 +1,32 @@ +using System.Runtime.CompilerServices; +using KoeBook.Epub.Services; + +namespace KoeBook.Test.Epub; + +public class AnalyzerServiceTest +{ + [Theory] + [InlineData("aa", "aa")] + [InlineData("漢字かんじ", "かんじ")] + [InlineData("ああ漢字かんじあああ", "ああかんじあああ")] + [InlineData(""" + ああ漢字かんじあああ + ああ漢字かんじあああ + ああ漢字1かんじ1あああ + """, "ああかんじあああ\nああかんじあああ\nああかんじ1あああ")] + [InlineData("佐久平さくだいら 啓介けいすけ", + "佐久平さくだいら 啓介けいすけ")] + [InlineData("漢字\nかんじ", "漢字\nかんじ")] + public void ReplaceBaseTextWithRuby(string input, string expected) + { + var result = AnalyzerServiceProxy.ReplaceBaseTextWithRuby(null, input); + + Assert.Equal(expected, result); + } +} + +file static class AnalyzerServiceProxy +{ + [UnsafeAccessor(UnsafeAccessorKind.StaticMethod)] + public static extern string ReplaceBaseTextWithRuby(AnalyzerService? _, string text); +} diff --git a/KoeBook.Test/Epub/EpubDocumentTest.cs b/KoeBook.Test/Epub/EpubDocumentTest.cs index 6ce0f15..c36a83b 100644 --- a/KoeBook.Test/Epub/EpubDocumentTest.cs +++ b/KoeBook.Test/Epub/EpubDocumentTest.cs @@ -89,7 +89,7 @@ public void EnsureParagraph() var element = Assert.Single(section.Elements); var paragraph = Assert.IsType(element); Assert.Null(paragraph.Audio); - Assert.Null(paragraph.Text); + Assert.Empty(paragraph.Text); Assert.Null(paragraph.ClassName); // 空でないときは無視 @@ -129,7 +129,7 @@ public void EnsureParagraph() element = Assert.Single(document.Chapters[0].Sections[1].Elements); paragraph = Assert.IsType(element); Assert.Null(paragraph.Audio); - Assert.Null(paragraph.Text); + Assert.Empty(paragraph.Text); Assert.Null(paragraph.ClassName); // インデックスは正しく指定する必要がある