Skip to content

Commit

Permalink
#25 ListからArrayに変更
Browse files Browse the repository at this point in the history
  • Loading branch information
aiueo-1234 committed Apr 29, 2024
1 parent acf2e35 commit 3d7c3d1
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Epub/KoeBook.Epub/Services/AnalyzerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public async ValueTask<BookScripts> AnalyzeAsync(BookProperties bookProperties,
var line = ReplaceBaseTextWithRuby(p.Text);

return p.ScriptLine = new ScriptLine(line, "", "");
}).ToList();
}).ToArray();

// LLMによる話者、スタイル解析
var bookScripts = await _llmAnalyzerService.LlmAnalyzeScriptLinesAsync(bookProperties, scriptLines, cancellationToken)!;
Expand Down
2 changes: 1 addition & 1 deletion KoeBook.Core/Contracts/Services/ILlmAnalyzerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ namespace KoeBook.Core.Contracts.Services;

public interface ILlmAnalyzerService
{
ValueTask<BookScripts> LlmAnalyzeScriptLinesAsync(BookProperties bookProperties, List<ScriptLine> scriptLines, CancellationToken cancellationToken);
ValueTask<BookScripts> LlmAnalyzeScriptLinesAsync(BookProperties bookProperties, ScriptLine[] scriptLines, CancellationToken cancellationToken);
}
6 changes: 4 additions & 2 deletions KoeBook.Core/Models/BookScripts.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace KoeBook.Core.Models;
using System.Collections.Immutable;

namespace KoeBook.Core.Models;

/// <summary>
/// 本の読み上げ情報
Expand All @@ -15,5 +17,5 @@ public class BookScripts(BookProperties bookProperties, BookOptions options)
/// <summary>
/// 読み上げテキストの配列
/// </summary>
public required IReadOnlyList<ScriptLine> ScriptLines { get; set; }
public required ImmutableArray<ScriptLine> ScriptLines { get; set; }
}
39 changes: 20 additions & 19 deletions KoeBook.Core/Services/ChatGptAnalyzerService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System.Collections.Immutable;
using System.Text;
using System.Text.RegularExpressions;
using KoeBook.Core.Contracts.Services;
using KoeBook.Core.Helpers;
Expand All @@ -13,7 +14,7 @@ public partial class ChatGptAnalyzerService(IOpenAIService openAIService, IDispl
private readonly IOpenAIService _openAiService = openAIService;
private readonly IDisplayStateChangeService _displayStateChangeService = displayStateChangeService;

public async ValueTask<BookScripts> LlmAnalyzeScriptLinesAsync(BookProperties bookProperties, List<ScriptLine> scriptLines, CancellationToken cancellationToken)
public async ValueTask<BookScripts> LlmAnalyzeScriptLinesAsync(BookProperties bookProperties, ScriptLine[] scriptLines, CancellationToken cancellationToken)
{
var chunks = new List<string>();
var chunk = new StringBuilder();
Expand Down Expand Up @@ -50,7 +51,7 @@ public async ValueTask<BookScripts> LlmAnalyzeScriptLinesAsync(BookProperties bo
summary1 = summaryList.Dequeue();
characters1 = characterList.Dequeue();
}
var Task2 = SummaryCharacterListAnalysisAsync(scriptLines, chunks, summary1, characters1, i, cancellationToken);
var Task2 = SummaryCharacterListAnalysisAsync(chunks, summary1, characters1, i, cancellationToken);
// WhenAllで非同期処理を待つ
await Task.WhenAll(Task1, Task2);
currentLineIndex += chunks[i].Split("\n").Length - 1;
Expand All @@ -72,12 +73,12 @@ public async ValueTask<BookScripts> LlmAnalyzeScriptLinesAsync(BookProperties bo
}
)
{
ScriptLines = scriptLines
ScriptLines = [.. scriptLines]
};
return bookScripts;
}

private async Task CharacterStyleAnalysisAsync(List<ScriptLine> scriptLines,
private async Task CharacterStyleAnalysisAsync(ScriptLine[] scriptLines,
List<string> chunks,
string summary,
string characterList,
Expand All @@ -89,8 +90,8 @@ private async Task CharacterStyleAnalysisAsync(List<ScriptLine> scriptLines,
RESTART:
var completionResult = await _openAiService.ChatCompletion.CreateCompletion(new ChatCompletionCreateRequest
{
Messages = new List<ChatMessage>
{
Messages =
[
ChatMessage.FromSystem($$"""
All Information
- Goal
Expand Down Expand Up @@ -156,15 +157,15 @@ 3. Target Sentence
```
"""
)
},
],
Model = OpenAI.ObjectModels.Models.Gpt_4_turbo_preview,
MaxTokens = 4000
}, cancellationToken: cancellationToken);
if (completionResult.Successful)
{
var result = completionResult.Choices.First().Message.Content;
// "#### Talker and Style Setting"以下の文章を改行区切りでリスト化
List<string> output = new List<string>();
var output = new List<string>();
var lines = result?.Split("\n");
var start = false;
for (var i = 0; i < lines?.Length; i++)
Expand Down Expand Up @@ -212,7 +213,7 @@ 3. Target Sentence
}
}

private async Task<(string summary, string characterList)> SummaryCharacterListAnalysisAsync(List<ScriptLine> scriptLines,
private async Task<(string summary, string characterList)> SummaryCharacterListAnalysisAsync(
List<string> chunks,
string summary,
string characterList,
Expand All @@ -222,8 +223,8 @@ 3. Target Sentence
var storyText = string.Join("\n", chunks.Skip(int.Max(0, idx - 4)).Take(4));
var completionResult = await _openAiService.ChatCompletion.CreateCompletion(new ChatCompletionCreateRequest
{
Messages = new List<ChatMessage>
{
Messages =
[
ChatMessage.FromSystem($$"""
All Information
- Goal
Expand Down Expand Up @@ -264,13 +265,13 @@ 3. Story
...
#### Summery of {{Math.Min(20,(idx+1)*5)}} points
#### Summery of {{Math.Min(20, (idx + 1) * 5)}} points
- {summary1}
- {summary2}
...
```
"""),
},
],
Model = OpenAI.ObjectModels.Models.Gpt_4_turbo_preview,
MaxTokens = 4000
}, cancellationToken: cancellationToken);
Expand Down Expand Up @@ -320,15 +321,15 @@ 3. Story
}
}

private async Task<Dictionary<string, string>> GetCharacterVoiceMappingAsync(List<ScriptLine> scriptLines, string characterDescription, CancellationToken cancellationToken)
private async Task<Dictionary<string, string>> GetCharacterVoiceMappingAsync(ScriptLine[] scriptLines, string characterDescription, CancellationToken cancellationToken)
{
// キャラクター名一覧の取得
var characterList = scriptLines.Select(x => "- " + x.Character).Distinct().ToList();
var characterListString = string.Join("\n", characterList);
var completionResult = await _openAiService.ChatCompletion.CreateCompletion(new ChatCompletionCreateRequest
{
Messages = new List<ChatMessage>
{
Messages =
[
ChatMessage.FromSystem($$"""
All Information
- Goal
Expand Down Expand Up @@ -365,15 +366,15 @@ Make a table of character names and voices
...
```
""")
},
],
Model = OpenAI.ObjectModels.Models.Gpt_4_turbo_preview,
MaxTokens = 4000
}, cancellationToken: cancellationToken);
if (completionResult.Successful)
{
var result = completionResult.Choices.First().Message.Content;
var lines = result?.Split("\n");
Dictionary<string, string> characterVoiceMapping = new();
Dictionary<string, string> characterVoiceMapping = [];
foreach (var match in from line in lines
let match = CharacterMappingRegex().Match(line)
select match)
Expand Down
11 changes: 6 additions & 5 deletions KoeBook.Core/Services/ClaudeAnalyzerService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Buffers;
using System.Collections.Immutable;
using System.Text;
using KoeBook.Core.Contracts.Services;
using KoeBook.Core.Helpers;
Expand All @@ -13,7 +14,7 @@ public partial class ClaudeAnalyzerService(IClaudeService claudeService, IDispla
private readonly ISoundGenerationSelectorService _soundGenerationSelectorService = soundGenerationSelectorService;
private static readonly SearchValues<char> _searchValues = SearchValues.Create(", ");

public async ValueTask<BookScripts> LlmAnalyzeScriptLinesAsync(BookProperties bookProperties, List<ScriptLine> scriptLines, CancellationToken cancellationToken)
public async ValueTask<BookScripts> LlmAnalyzeScriptLinesAsync(BookProperties bookProperties, ScriptLine[] scriptLines, CancellationToken cancellationToken)
{
var progress = _displayStateChangeService.ResetProgress(bookProperties, GenerationState.Analyzing, 2);
var lineNumberingText = LineNumbering(scriptLines);
Expand Down Expand Up @@ -53,7 +54,7 @@ public async ValueTask<BookScripts> LlmAnalyzeScriptLinesAsync(BookProperties bo
var characterVoiceMapping = ExtractCharacterVoiceMapping(message2.ToString(), characterId2Name);
progress.Finish();

return new(bookProperties, new(characterVoiceMapping)) { ScriptLines = scriptLines };
return new(bookProperties, new(characterVoiceMapping)) { ScriptLines = [.. scriptLines] };
}
catch (OperationCanceledException) { throw; }
catch (Exception e)
Expand Down Expand Up @@ -140,7 +141,7 @@ [Assign Voices]
""";
}

private static string LineNumbering(List<ScriptLine> scriptLines)
private static string LineNumbering(ScriptLine[] scriptLines)
{
var sb = new StringBuilder();
foreach (var (index, scriptLine) in scriptLines.Select((x, i) => (i, x)))
Expand All @@ -150,7 +151,7 @@ private static string LineNumbering(List<ScriptLine> scriptLines)
return sb.ToString();
}

private static (Character[], Dictionary<string, string>) ExtractCharacterList(string response, List<ScriptLine> scriptLines)
private static (Character[], Dictionary<string, string>) ExtractCharacterList(string response, ScriptLine[] scriptLines)
{
var lines = response.Split("\n");
var characters = lines
Expand Down Expand Up @@ -179,7 +180,7 @@ private static (Character[], Dictionary<string, string>) ExtractCharacterList(st
}
return 0;
}).Count();
if (voiceIdLinesCount != scriptLines.Count)
if (voiceIdLinesCount != scriptLines.Length)
throw new EbookException(ExceptionType.ClaudeTalkerAndStyleSettingFailed);
return (characters, characterId2Name);
}
Expand Down

0 comments on commit 3d7c3d1

Please sign in to comment.