Skip to content

Commit

Permalink
#36 AiStoryクラスの処理を修正
Browse files Browse the repository at this point in the history
  • Loading branch information
miyaji255 committed Apr 28, 2024
1 parent 7c74a35 commit 9ba2281
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 166 deletions.
54 changes: 0 additions & 54 deletions Epub/KoeBook.Epub/Models/ClaudeStory.cs

This file was deleted.

31 changes: 31 additions & 0 deletions Epub/KoeBook.Epub/Services/AiStoryAnalyzerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using KoeBook.Core.Utilities;
using KoeBook.Epub.Contracts.Services;
using KoeBook.Epub.Models;
using KoeBook.Models;

namespace KoeBook.Epub.Services;

public partial class AiStoryAnalyzerService(ISplitBraceService splitBraceService)
{
private readonly ISplitBraceService _splitBraceService = splitBraceService;


public EpubDocument CreateEpubDocument(AiStory aiStory, Guid id)
{
return new EpubDocument(aiStory.Title, "", "", id)
{
Chapters = [new Chapter() {
Sections = aiStory.Sections.Select(s => new Section("") {
Elements = s.Paragraphs.SelectMany(p =>
_splitBraceService.SplitBrace(p.GetText())
.Zip(_splitBraceService.SplitBrace(p.GetScript()))
.Select(Element (p) => new Paragraph {
Text = p.First,
ScriptLine = new(p.Second, "","")
})
).ToList(),
}).ToList(),
}]
};
}
}
39 changes: 28 additions & 11 deletions Epub/KoeBook.Epub/Services/AnalyzerService.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
using System.Text;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using KoeBook.Core;
using KoeBook.Core.Contracts.Services;
using KoeBook.Core.Models;
using KoeBook.Epub.Contracts.Services;
using KoeBook.Epub.Models;
using KoeBook.Models;

namespace KoeBook.Epub.Services;

public partial class AnalyzerService(IScraperSelectorService scrapingService, IEpubDocumentStoreService epubDocumentStoreService, ILlmAnalyzerService llmAnalyzerService) : IAnalyzerService
public partial class AnalyzerService(
IScraperSelectorService scrapingService,
IEpubDocumentStoreService epubDocumentStoreService,
ILlmAnalyzerService llmAnalyzerService,
AiStoryAnalyzerService aiStoryAnalyzerService) : IAnalyzerService
{
private readonly IScraperSelectorService _scrapingService = scrapingService;
private readonly IEpubDocumentStoreService _epubDocumentStoreService = epubDocumentStoreService;
private readonly ILlmAnalyzerService _llmAnalyzerService = llmAnalyzerService;
private readonly AiStoryAnalyzerService _aiStoryAnalyzerService = aiStoryAnalyzerService;

public async ValueTask<BookScripts> AnalyzeAsync(BookProperties bookProperties, string tempDirectory, CancellationToken cancellationToken)
{
Expand All @@ -22,26 +29,36 @@ public async ValueTask<BookScripts> AnalyzeAsync(BookProperties bookProperties,
await fs.WriteAsync(CoverFile.ToArray(), cancellationToken);
await fs.FlushAsync(cancellationToken);

EpubDocument? document;
var rubyReplaced = false;
EpubDocument document;
try
{
document = await _scrapingService.ScrapingAsync(bookProperties.Source, coverFilePath, tempDirectory, bookProperties.Id, cancellationToken);
}
catch (EbookException)
{
throw;
switch (bookProperties)
{
case { SourceType: SourceType.Url or SourceType.FilePath, Source: string uri }:
document = await _scrapingService.ScrapingAsync(uri, coverFilePath, tempDirectory, bookProperties.Id, cancellationToken);
break;
case { SourceType: SourceType.AiStory, Source: AiStory aiStory }:
document = _aiStoryAnalyzerService.CreateEpubDocument(aiStory, bookProperties.Id);
rubyReplaced = true;
break;
default:
throw new UnreachableException($"SourceType: {bookProperties.SourceType}, Source: {bookProperties.Source}");
}
}
catch (EbookException) { throw; }
catch (Exception ex)
{
EbookException.Throw(ExceptionType.WebScrapingFailed, innerException: ex);
return default;
throw new EbookException(ExceptionType.WebScrapingFailed, innerException: ex);
}
_epubDocumentStoreService.Register(document, cancellationToken);

var scriptLines = document.Chapters.SelectMany(c => c.Sections)
.SelectMany(s => s.Elements)
.OfType<Paragraph>()
.Select(p =>
.Select<Paragraph, ScriptLine>(rubyReplaced
? p => p.ScriptLine!
: p =>
{
// ルビを置換
var line = ReplaceBaseTextWithRuby(p.Text);
Expand Down
88 changes: 0 additions & 88 deletions Epub/KoeBook.Epub/Services/ScrapingClaudeStroyService.cs

This file was deleted.

44 changes: 44 additions & 0 deletions KoeBook.Core/Models/AiStory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Xml.Serialization;
using KoeBook.Core.Utilities;

namespace KoeBook.Models;

[XmlRoot("Book", IsNullable = false)]
public record AiStory(
[XmlElement("Title", typeof(string), IsNullable = false)] string Title,
[XmlArray("Content", IsNullable = false), XmlArrayItem("Section", IsNullable = false)] AiStory.Section[] Sections)
{
public record Section(
[XmlElement("Paragraph", IsNullable = false)] Paragraph[] Paragraphs);


public record Paragraph(
[XmlElement("Text", typeof(TextElement), IsNullable = false), XmlElement("Ruby", typeof(Ruby), IsNullable = false)] InlineElement[] Inlines)
{
public string GetText() => string.Concat(Inlines.Select(e => e.Text));

public string GetScript() => string.Concat(Inlines.Select(e => e.Script));
}

public abstract record class InlineElement
{
public abstract string Text { get; }
public abstract string Script { get; }
}

public record TextElement([XmlText] string InnerText) : InlineElement
{
public override string Text => InnerText;
public override string Script => InnerText;
}


public record Ruby(
[XmlElement("Rb", IsNullable = false)] string Rb,
[XmlElement("Rt", IsNullable = false)] string Rt) : InlineElement
{
public override string Text => Rb;
public override string Script => Rt;
}

}
30 changes: 25 additions & 5 deletions KoeBook.Core/Models/BookProperties.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
namespace KoeBook.Core.Models;
using System.Diagnostics;
using KoeBook.Models;

namespace KoeBook.Core.Models;

/// <summary>
/// 読み上げる本の情報
/// </summary>
public class BookProperties(Guid id, string source, SourceType sourceType)
public class BookProperties
{
public Guid Id { get; } = id;
public BookProperties(Guid id, string source, SourceType sourceType)
{
if (sourceType != SourceType.FilePath && sourceType != SourceType.Url)
throw new ArgumentException($"{nameof(sourceType)}{nameof(SourceType.FilePath)}{nameof(SourceType.Url)}である必要があります。");
Id = id;
Source = source;
SourceType = sourceType;
}

public BookProperties(Guid id, AiStory aiStory) {
Id = id;
Source = aiStory;
}

public Guid Id { get; }

public string Source { get; } = source;
/// <summary>
/// UriまたはAiStory
/// </summary>
public object Source { get; }

public SourceType SourceType { get; } = sourceType;
public SourceType SourceType { get; }
}
3 changes: 3 additions & 0 deletions KoeBook.Core/Models/SourceType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ public enum SourceType

[EnumMember(Value = "ローカルファイル")]
FilePath,

[EnumMember(Value = "AI生成")]
AiStory,
}
Loading

0 comments on commit 9ba2281

Please sign in to comment.