Skip to content

Commit

Permalink
#25 ListからArrayに変更&forからforeachに変更
Browse files Browse the repository at this point in the history
  • Loading branch information
aiueo-1234 committed Apr 29, 2024
1 parent 00f638a commit e16f6d5
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions KoeBook.Core/Services/ClaudeAnalyzerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public async ValueTask<BookScripts> LlmAnalyzeScriptLinesAsync(BookProperties bo
},
cancellationToken: cancellationToken
);
(var characterList, var characterId2Name) = ExtractCharacterList(message1.ToString(), scriptLines);
(var characters, var characterId2Name) = ExtractCharacterList(message1.ToString(), scriptLines);
progress.IncrementProgress();

var message2 = await _claudeService.Messages.CreateAsync(new()
Expand All @@ -45,7 +45,7 @@ public async ValueTask<BookScripts> LlmAnalyzeScriptLinesAsync(BookProperties bo
Messages = [new()
{
Role = "user",
Content = CreateVoiceTypeAnalyzePrompt(characterList)
Content = CreateVoiceTypeAnalyzePrompt(characters)
}]
},
cancellationToken: cancellationToken
Expand Down Expand Up @@ -122,7 +122,7 @@ [REVISE VOICE ID]
""";
}

private string CreateVoiceTypeAnalyzePrompt(List<Character> characterList)
private string CreateVoiceTypeAnalyzePrompt(Character[] characterList)
{
return $$"""
Assign the most fitting voice type to each character from the provided list, ensuring the chosen voice aligns with their role and attributes in the story. Only select from the available voice types.
Expand Down Expand Up @@ -150,10 +150,10 @@ private static string LineNumbering(List<ScriptLine> scriptLines)
return sb.ToString();
}

private static (List<Character>, Dictionary<string, string>) ExtractCharacterList(string response, List<ScriptLine> scriptLines)
private static (Character[], Dictionary<string, string>) ExtractCharacterList(string response, List<ScriptLine> scriptLines)
{
var lines = response.Split("\n");
var characterList = lines
var characters = lines
.SkipWhile(l => !l.StartsWith("[REVISE CHARACTER LIST]"))
.TakeWhile(l => !l.StartsWith("[REVISE VOICE ID]"))
.Where(l => l.StartsWith('c'))
Expand All @@ -162,26 +162,25 @@ private static (List<Character>, Dictionary<string, string>) ExtractCharacterLis
var dotIndex = l.IndexOf('.');
var colonIndex = l.IndexOf(':');
return new Character(l[1..dotIndex], l[(dotIndex + 2)..colonIndex], l[(colonIndex + 2)..]);
}).ToList();
}).ToArray();

var characterId2Name = characterList.Select(x => (x.Id, x.Name)).ToDictionary();
var characterId2Name = characters.Select(x => (x.Id, x.Name)).ToDictionary();
var voiceIdLines = lines.SkipWhile(l => !l.StartsWith("[REVISE VOICE ID]"))
.Where((x, i) => x.StartsWith(i.ToString())) //[REVISE VOICE ID]の分ズレる
.ToArray().AsSpan();
.Where((x, i) => x.StartsWith(i.ToString())); //[REVISE VOICE ID]の分ズレる

if (voiceIdLines.Length != scriptLines.Count)
if (voiceIdLines.Count() != scriptLines.Count)
throw new EbookException(ExceptionType.ClaudeTalkerAndStyleSettingFailed);
for (var i = 0; i < voiceIdLines.Length; i++)
foreach (var (voiceIdLine, scriptLine) in voiceIdLines.Zip(scriptLines))
{
var line = voiceIdLines[i].AsSpan();
var line = voiceIdLine.AsSpan();
line = line[(line.IndexOf(' ') + 2)..];//cまで無視
line = line[..line.IndexOf(' ')];// 二人以上話す時には先頭のものを使う
if (characterId2Name.TryGetValue(line.ToString(), out var characterName))
{
scriptLines[i].Character = characterName;
scriptLine.Character = characterName;
}
}
return (characterList, characterId2Name);
return (characters, characterId2Name);
}

private class Character
Expand Down

0 comments on commit e16f6d5

Please sign in to comment.