diff --git a/Lib/CloudBuilder.AI.Service.dll b/Lib/CloudBuilder.AI.Service.dll index b45d464..8af1dd1 100644 Binary files a/Lib/CloudBuilder.AI.Service.dll and b/Lib/CloudBuilder.AI.Service.dll differ diff --git a/Lib/CloudBuilder.AI.dll b/Lib/CloudBuilder.AI.dll index c9f7991..a19d798 100644 Binary files a/Lib/CloudBuilder.AI.dll and b/Lib/CloudBuilder.AI.dll differ diff --git a/Program.cs b/Program.cs index a41325b..199d0bd 100644 --- a/Program.cs +++ b/Program.cs @@ -29,11 +29,12 @@ public partial class Program // 设置控制台编码为UTF-8,解决中文乱码问题 Console.OutputEncoding = Encoding.UTF8; Console.InputEncoding = Encoding.UTF8; + Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory); string taskName = null; Dictionary bodyDict = new Dictionary(); - //File.AppendAllText("log.txt", "Main:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); + File.AppendAllText("log.txt", "Main:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n"); if (args != null && args.Length > 0) { diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json index 44ac99d..aa79893 100644 --- a/Properties/launchSettings.json +++ b/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "CloudBuilder.Topshelf": { "commandName": "Project", - "commandLineArgs": "task:TtsTask book_id:B000008 chapter_id:4 speed:1 voice:Sherpa2 actor:韩立 actor_voice:Sherpa1 save_path:D:\\\\Net8\\\\FileServer\\\\Backup\\\\DmsFile\\\\voice" + "commandLineArgs": "task:ChineseNameExtractorTask book_id:B000010 chapter_id:73" } } } \ No newline at end of file diff --git a/Python/PythonCommand.cs b/Python/PythonCommand.cs index 70f6b91..5cb391e 100644 --- a/Python/PythonCommand.cs +++ b/Python/PythonCommand.cs @@ -92,6 +92,11 @@ namespace CloudBuilder.Topshelf.Python import os import json import logging +import sys +# web api通過cmd調用,如果沒有加入下面三行,會出錯 +sys.stdout = open(os.devnull, 'w') +sys.stderr = open(os.devnull, 'w') +os.environ['TQDM_DISABLE'] = '1' # 配置日志 logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') @@ -655,9 +660,22 @@ class HanLPProcessor: # 直接返回提取的值 return value + + def get_surnames_json(self): + """返回姓氏词典数组的JSON格式字符串,供C#使用""" + try: + with open(self.surnames_path, 'r', encoding='utf-8') as f: + surnames = [line.strip() for line in f if line.strip()] + + return json.dumps(surnames, ensure_ascii=False) + except Exception as e: + logger.error(f"获取姓氏词典失败: {e}") + return json.dumps([]) processor=HanLPProcessor() +# 测试split函数 + # 测试split函数 def test_split_function(): # 测试用例1: 简单的逗号分隔 @@ -838,4 +856,5 @@ if __name__ == "__main__": # processor=HanLPProcessor() print("") + */ \ No newline at end of file diff --git a/Task/AI/ChineseNameExtractorTask.cs b/Task/AI/ChineseNameExtractorTask.cs index 237fb93..476487b 100644 --- a/Task/AI/ChineseNameExtractorTask.cs +++ b/Task/AI/ChineseNameExtractorTask.cs @@ -8,7 +8,8 @@ using static System.Net.Mime.MediaTypeNames; namespace CloudBuilder.Topshelf.Task { - //task:ChineseNameExtractorTask book_id:B000008 chapter_id:3 + //task:ChineseNameExtractorTask book_id:B000010 chapter_id:74 from_chapter_id:74 + //task:ChineseNameExtractorTask book_id:B000010 from_chapter_id:500 public class ChineseNameExtractorTask : IScheduleTask { private readonly IApplicationService service; @@ -20,8 +21,6 @@ namespace CloudBuilder.Topshelf.Task public void Run(Dictionary bodyDict) { - //File.AppendAllText("log.txt", "ChineseNameExtractorTask:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); - if (!bodyDict.ContainsKey("book_id")) return; string bookId = bodyDict["book_id"]; @@ -29,19 +28,22 @@ namespace CloudBuilder.Topshelf.Task if (bodyDict.ContainsKey("chapter_id")) chapterId = bodyDict["chapter_id"]; + string from_chapter_id = string.Empty; + if (bodyDict.ContainsKey("from_chapter_id")) + from_chapter_id = bodyDict["from_chapter_id"]; + PythonCommand python = new PythonCommand(service); try { Hanlp2 hanlp = new Hanlp2(python); commonSurnames = hanlp.GetSurnames(); - IRepository repository = service.GetRepository>(); IRepository repositoryAiSentenceEntity = service.GetRepository>(); IRepository repositoryAiChapterPersonEntity = service.GetRepository>(); - AiChapterPersonEntity[] dels = repositoryAiChapterPersonEntity.DetachedEntities.Where(x => x.BookId == bookId).ToArray(); + AiChapterPersonEntity[] dels = repositoryAiChapterPersonEntity.DetachedEntities.Where(x => x.BookId == bookId && (string.IsNullOrEmpty(chapterId) || x.ChapterId == Convert.ToInt32(chapterId)) && (string.IsNullOrEmpty(from_chapter_id) || x.ChapterId >= Convert.ToInt32(from_chapter_id))).ToArray(); if (dels != null && dels.Length > 0) repositoryAiChapterPersonEntity.DeleteNow(dels); string depRoot; @@ -54,7 +56,7 @@ namespace CloudBuilder.Topshelf.Task SpeakerAnalysisHelper speakerAnalysisHelper = new SpeakerAnalysisHelper(); - AiSentenceViewEntity[] ents = repository.DetachedEntities.Where(x => x.BookId == bookId && (string.IsNullOrEmpty(chapterId) || x.ChapterId == Convert.ToInt32(chapterId))).OrderBy(x => x.ChapterId).ThenBy(x => x.ParagraphId).ThenBy(x => x.SentenceIndex).ToArray(); + AiSentenceViewEntity[] ents = repository.DetachedEntities.Where(x => x.BookId == bookId && (string.IsNullOrEmpty(chapterId) || x.ChapterId == Convert.ToInt32(chapterId)) && (string.IsNullOrEmpty(from_chapter_id) || x.ChapterId >= Convert.ToInt32(from_chapter_id))).OrderBy(x => x.ChapterId).ThenBy(x => x.ParagraphId).ThenBy(x => x.SentenceIndex).ToArray(); if (ents == null || ents.Length == 0) return; AiSentenceEntity aiSentenceEntity; @@ -65,36 +67,45 @@ namespace CloudBuilder.Topshelf.Task int processedLines = 0; // 已处理行数计数器 AiChapterPersonEntity aiChapterPerson; List cps = new List(); + foreach (var ent in ents) { processedLines++; - - person = speakerAnalysisHelper.GetPerson(hanlp, ent.Content); - paragraphIndex = ent.ParagraphId; - - if (!string.IsNullOrEmpty(person)) + try { - string[] ps = person.Split(','); - int orderIndex = 1; - foreach (var p in ps) + person = speakerAnalysisHelper.GetPerson(hanlp, ent.Content); + paragraphIndex = ent.ParagraphId; + + if (!string.IsNullOrEmpty(person)) { - if (p.Length == 1) continue; - aiChapterPerson = new AiChapterPersonEntity(); - aiChapterPerson.PersonName = KeepChineseCharactersOnly(p); - aiChapterPerson.BookId = ent.BookId; - aiChapterPerson.ChapterId = ent.ChapterId; - aiChapterPerson.ParagraphIndex = ent.ParagraphId; - aiChapterPerson.SentenceIndex = ent.SentenceIndex; - aiChapterPerson.DialogueIndc = ent.DialogueIndc; - aiChapterPerson.OrderIndex = orderIndex++; - cps.Add(aiChapterPerson); - repositoryAiChapterPersonEntity.InsertNow(aiChapterPerson); + string[] ps = person.Split(','); + int orderIndex = 1; + foreach (var p in ps) + { + if (p.Length == 1) continue; + aiChapterPerson = new AiChapterPersonEntity(); + aiChapterPerson.PersonName = KeepChineseCharactersOnly(p); + aiChapterPerson.BookId = ent.BookId; + aiChapterPerson.ChapterId = ent.ChapterId; + aiChapterPerson.ParagraphIndex = ent.ParagraphId; + aiChapterPerson.SentenceIndex = ent.SentenceIndex; + aiChapterPerson.DialogueIndc = ent.DialogueIndc; + aiChapterPerson.OrderIndex = orderIndex++; + + repositoryAiChapterPersonEntity.InsertNow(aiChapterPerson); + cps.Add(aiChapterPerson); + } } } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } ConsoleOutput.UpdateProgress(processedLines, totalLines); } + speakerAnalysisHelper.AiChapterPersons = cps.ToArray(); speakerAnalysisHelper.CommonSurnames = commonSurnames; speakerAnalysisHelper.MatchPerson(ents, repositoryAiSentenceEntity); diff --git a/Task/TTS/TtsTask.cs b/Task/TTS/TtsTask.cs index f12929f..0c99e7f 100644 --- a/Task/TTS/TtsTask.cs +++ b/Task/TTS/TtsTask.cs @@ -110,7 +110,7 @@ namespace CloudBuilder.Topshelf.Task.TTS voice = personMapping[ent.PersonName]; else { - voice = AI.Policy.ChineseTtsVoiceRandom.GetRandom(80).ToString(); + voice = CloudBuilder.AI.Policy.ChineseTtsVoiceRandom.GetRandom(80).ToString(); personMapping.Add(ent.PersonName, voice); } diff --git a/Utility/SpeakerAnalysisHelper.cs b/Utility/SpeakerAnalysisHelper.cs index 21608a7..336f032 100644 --- a/Utility/SpeakerAnalysisHelper.cs +++ b/Utility/SpeakerAnalysisHelper.cs @@ -68,12 +68,15 @@ namespace CloudBuilder.Topshelf.Utility { HanlpConstituencyNode[] nodesSon; nodef = HanlpResultHelper.GetNodeById(cons, node.ItemId + 1); - if (nps.Contains(nodef.Label)) + if (nodef != null) { - nodesSon = HanlpResultHelper.GetNodeSonById(cons, nodef.ItemId); - if (nodesSon != null && nodesSon.Where(x => x.Children == 0).Any()) + if (nps.Contains(nodef.Label)) { - feature += string.Concat(nodesSon.Select(x => x.Label)); + nodesSon = HanlpResultHelper.GetNodeSonById(cons, nodef.ItemId); + if (nodesSon != null && nodesSon.Where(x => x.Children == 0).Any()) + { + feature += string.Concat(nodesSon.Select(x => x.Label)); + } } } } @@ -152,7 +155,7 @@ namespace CloudBuilder.Topshelf.Utility } } } - + /*暂不处理arg1 foreach (var mrp in srl) { if (mrp.Type.ToUpper() != "ARG0") continue; @@ -170,6 +173,7 @@ namespace CloudBuilder.Topshelf.Utility arg1 = mrp.Text; } } + */ } } @@ -208,7 +212,8 @@ namespace CloudBuilder.Topshelf.Utility if (aiSentenceView.PersonName.Contains("【")) { - aiSentenceView.PersonName = AiChapterPersons.Where(x => !x.PersonName.Contains("【") && x.PersonName.Contains(aiSentenceView.PersonName.Replace("【", "").Replace("】", ""))).FirstOrDefault()!.PersonName; + if (AiChapterPersons.Where(x => !x.PersonName.Contains("【") && x.PersonName.Contains(aiSentenceView.PersonName.Replace("【", "").Replace("】", ""))).Any()) + aiSentenceView.PersonName = AiChapterPersons.Where(x => !x.PersonName.Contains("【") && x.PersonName.Contains(aiSentenceView.PersonName.Replace("【", "").Replace("】", ""))).FirstOrDefault()!.PersonName; } } //说话者在前面