This commit is contained in:
owenchen 2026-05-29 13:03:00 +08:00
parent 597b88d075
commit 527205520b
12 changed files with 104 additions and 43 deletions

View File

@ -12,14 +12,17 @@
<Compile Remove="mp3\**" /> <Compile Remove="mp3\**" />
<Compile Remove="mp3_new1\**" /> <Compile Remove="mp3_new1\**" />
<Compile Remove="mp3_new\**" /> <Compile Remove="mp3_new\**" />
<Compile Remove="Task\Novel\**" />
<EmbeddedResource Remove="Lib\**" /> <EmbeddedResource Remove="Lib\**" />
<EmbeddedResource Remove="mp3\**" /> <EmbeddedResource Remove="mp3\**" />
<EmbeddedResource Remove="mp3_new1\**" /> <EmbeddedResource Remove="mp3_new1\**" />
<EmbeddedResource Remove="mp3_new\**" /> <EmbeddedResource Remove="mp3_new\**" />
<EmbeddedResource Remove="Task\Novel\**" />
<None Remove="Lib\**" /> <None Remove="Lib\**" />
<None Remove="mp3\**" /> <None Remove="mp3\**" />
<None Remove="mp3_new1\**" /> <None Remove="mp3_new1\**" />
<None Remove="mp3_new\**" /> <None Remove="mp3_new\**" />
<None Remove="Task\Novel\**" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -62,11 +65,11 @@
<PackageReference Include="pythonnet" Version="3.0.5" /> <PackageReference Include="pythonnet" Version="3.0.5" />
<PackageReference Include="org.k2fsa.sherpa.onnx" Version="1.13.2" /> <PackageReference Include="org.k2fsa.sherpa.onnx" Version="1.13.2" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Policy\" /> <Folder Include="Policy\" />
<Folder Include="Task\Novel\Process\" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

Binary file not shown.

Binary file not shown.

View File

@ -2,7 +2,7 @@
"profiles": { "profiles": {
"CloudBuilder.Topshelf": { "CloudBuilder.Topshelf": {
"commandName": "Project", "commandName": "Project",
"commandLineArgs": "task:TtsTask voice:Sherpa2 save_path:D:\\\\Net8\\\\FileServer\\\\Backup\\\\DmsFile\\\\voice\\\\20260528112739.mp3 text:二愣子睁大着双眼,直直望着茅草和烂泥糊成的黑屋顶,身上盖着的旧棉被,已呈深黄色,看不出原来的本来面目,还若有若无的散发着淡淡的霉味。" "commandLineArgs": "task:PythonRunTask book_id:B000008"
} }
} }
} }

View File

@ -92,8 +92,6 @@ namespace CloudBuilder.Topshelf.Python
} }
return null; return null;
} }
public List<HanlpConstituencyNode> GetHanlpCon(string text) public List<HanlpConstituencyNode> GetHanlpCon(string text)
{ {
@ -138,7 +136,6 @@ namespace CloudBuilder.Topshelf.Python
return null; return null;
} }
public List<string> GetTokCoarse(string text) public List<string> GetTokCoarse(string text)
{ {
try try

View File

@ -5,6 +5,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Xml.Linq;
namespace CloudBuilder.Topshelf.Python namespace CloudBuilder.Topshelf.Python
{ {
@ -179,11 +180,11 @@ namespace CloudBuilder.Topshelf.Python
/// <param name="nodes">所有节点</param> /// <param name="nodes">所有节点</param>
/// <param name="itemId">节点ID</param> /// <param name="itemId">节点ID</param>
/// <returns>节点对象如果未找到则返回null</returns> /// <returns>节点对象如果未找到则返回null</returns>
public static HanlpConstituencyNode GetNodeTopLabelById(List<HanlpConstituencyNode> nodes, int itemId, string label) public static HanlpConstituencyNode GetNodeTopLabelById(List<HanlpConstituencyNode> nodes, int itemId, string[] label)
{ {
HanlpConstituencyNode nd = GetNodeFatherById(nodes, itemId); HanlpConstituencyNode nd = GetNodeFatherById(nodes, itemId);
if (label == nd.Label && nd.Children > 0) return nd; if (label.Where(x => x == nd.Label).Any() && nd.Children > 0) return nd;
while (nd.FatherId > 0) while (nd.FatherId > 0)
{ {
@ -199,9 +200,9 @@ namespace CloudBuilder.Topshelf.Python
/// <param name="nodes">所有节点</param> /// <param name="nodes">所有节点</param>
/// <param name="itemId">节点ID</param> /// <param name="itemId">节点ID</param>
/// <returns>节点对象如果未找到则返回null</returns> /// <returns>节点对象如果未找到则返回null</returns>
public static string GetNodeBottomLabelById(List<HanlpConstituencyNode> nodes, int itemId, string label) public static string GetNodeBottomLabelById(List<HanlpConstituencyNode> nodes, int itemId, string[] label)
{ {
HanlpConstituencyNode[] nds = nodes?.Where(node => node.FatherId == itemId && node.Label == label && node.Children > 0).ToArray(); HanlpConstituencyNode[] nds = nodes?.Where(node => node.FatherId == itemId && label.Where(x => x == node.Label).Any() && node.Children > 0).ToArray();
if (nds == null || nds.Length == 0) return null; if (nds == null || nds.Length == 0) return null;
HanlpConstituencyNode father; HanlpConstituencyNode father;
List<string> list = new List<string>(); List<string> list = new List<string>();

View File

@ -79,6 +79,8 @@ namespace CloudBuilder.Topshelf.Python
// 2. 关闭 Python 运行时(关键!) // 2. 关闭 Python 运行时(关键!)
if (PythonEngine.IsInitialized) if (PythonEngine.IsInitialized)
{ {
AppContext.SetSwitch("System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization", true);
PythonEngine.Shutdown(); // 销毁 Python 解释器 PythonEngine.Shutdown(); // 销毁 Python 解释器
} }

View File

@ -2,12 +2,12 @@
using CloudBuilder.Core.DatabaseAccessor.Entity; using CloudBuilder.Core.DatabaseAccessor.Entity;
using CloudBuilder.Core.DependencyInjection.Task; using CloudBuilder.Core.DependencyInjection.Task;
using CloudBuilder.Topshelf.Python; using CloudBuilder.Topshelf.Python;
using CloudBuilder.Topshelf.Task.Novel.Process; using CloudBuilder.Topshelf.Utility;
using static System.Net.Mime.MediaTypeNames; using static System.Net.Mime.MediaTypeNames;
namespace CloudBuilder.Topshelf.Task namespace CloudBuilder.Topshelf.Task
{ {
//task:ChineseNameExtractorTask guid:B000006 //task:ChineseNameExtractorTask book_id:B000008
public class ChineseNameExtractorTask : IScheduleTask public class ChineseNameExtractorTask : IScheduleTask
{ {
private readonly IApplicationService service; private readonly IApplicationService service;
@ -19,16 +19,16 @@ namespace CloudBuilder.Topshelf.Task
public void Run(Dictionary<string, string> bodyDict) public void Run(Dictionary<string, string> bodyDict)
{ {
if (!bodyDict.ContainsKey("guid")) return; if (!bodyDict.ContainsKey("book_id")) return;
string guid = bodyDict["guid"]; string guid = bodyDict["book_id"];
PythonCommand python = new PythonCommand(service); PythonCommand python = new PythonCommand(service);
try try
{ {
Hanlp2 hanlp = new Hanlp2(python); Hanlp2 hanlp = new Hanlp2(python);
IRepository<AiSentenceDialogueViewEntity> repository = service.GetRepository<IRepository<AiSentenceDialogueViewEntity>>(); IRepository<AiSentenceViewEntity> repository = service.GetRepository<IRepository<AiSentenceViewEntity>>();
IRepository<AiSentenceEntity> repositoryAiSentenceEntity = service.GetRepository<IRepository<AiSentenceEntity>>(); IRepository<AiSentenceEntity> repositoryAiSentenceEntity = service.GetRepository<IRepository<AiSentenceEntity>>();
@ -42,10 +42,12 @@ namespace CloudBuilder.Topshelf.Task
SpeakerAnalysisHelper speakerFeatureLearner = new SpeakerAnalysisHelper(); SpeakerAnalysisHelper speakerFeatureLearner = new SpeakerAnalysisHelper();
AiSentenceDialogueViewEntity[] ents = repository.DetachedEntities.Where(x => x.BookGuid == guid).OrderBy(x => x.ChapterIndex).ThenBy(x => x.ParagraphIndex).ThenBy(x => x.DialogueIndc).ToArray(); AiSentenceViewEntity[] ents = repository.DetachedEntities.Where(x => x.BookId == guid).OrderBy(x => x.ChapterId).ThenBy(x => x.ParagraphId).ThenBy(x => x.SentenceIndex).ToArray();
if (ents == null || ents.Length == 0) return;
AiSentenceEntity aiSentenceEntity; AiSentenceEntity aiSentenceEntity;
string person = null; string person = null;
int paragraphIndex = 0; long paragraphIndex = 0;
int totalLines = ents.Length; int totalLines = ents.Length;
int processedLines = 0; // 已处理行数计数器 int processedLines = 0; // 已处理行数计数器
@ -54,23 +56,17 @@ namespace CloudBuilder.Topshelf.Task
{ {
processedLines++; processedLines++;
if (!string.IsNullOrEmpty(person) && paragraphIndex == ent.ParagraphIndex) person = speakerFeatureLearner.GetPerson(hanlp, ent.Content);
paragraphIndex = ent.ParagraphId;
if (!string.IsNullOrEmpty(person))
{ {
paragraphIndex = 0; aiSentenceEntity = repositoryAiSentenceEntity.Entities.Where(x => x.SentenceId == ent.SentenceId).FirstOrDefault();
aiSentenceEntity = repositoryAiSentenceEntity.Entities.Where(x => x.Guid == ent.Guid).FirstOrDefault();
aiSentenceEntity.PersonName = person; aiSentenceEntity.PersonName = person;
repositoryAiSentenceEntity.UpdateNow(aiSentenceEntity); repositoryAiSentenceEntity.UpdateNow(aiSentenceEntity);
continue;
} }
depRoot = hanlp.GetDepRoot(ent.Content);
ners = hanlp.GetHanlpNer(ent.Content);
srls = hanlp.GetHanlpSrl(ent.Content);
person = speakerFeatureLearner.GetPerson(depRoot, ners, srls, cons);
paragraphIndex = ent.ParagraphIndex;
UpdateProgress(processedLines, totalLines); UpdateProgress(processedLines, totalLines);
} }
@ -81,6 +77,7 @@ namespace CloudBuilder.Topshelf.Task
} }
finally finally
{ {
Console.WriteLine("");
python.Dispose(); python.Dispose();
} }
} }

View File

@ -1,7 +1,6 @@
using CloudBuilder.Core.DatabaseAccessor.Entity; using CloudBuilder.Core.DatabaseAccessor.Entity;
using CloudBuilder.Core.DependencyInjection.Task; using CloudBuilder.Core.DependencyInjection.Task;
using CloudBuilder.Topshelf.Python; using CloudBuilder.Topshelf.Python;
using CloudBuilder.Topshelf.Task.Novel.Process;
using CloudBuilder.Topshelf.Utility; using CloudBuilder.Topshelf.Utility;
namespace CloudBuilder.Topshelf.Task namespace CloudBuilder.Topshelf.Task
@ -29,22 +28,24 @@ namespace CloudBuilder.Topshelf.Task
text = "银发老者神色一肃的向金越禅师问道。"; text = "银发老者神色一肃的向金越禅师问道。";
//text = "鬼灵门门主一边吩咐着,一边冷冷打量着悬浮在身前的画轴,见其并没有任何异样发生,目光闪动几下。"; //text = "鬼灵门门主一边吩咐着,一边冷冷打量着悬浮在身前的画轴,见其并没有任何异样发生,目光闪动几下。";
text = "韩立闻言,眉头不经意的一皱,想了想后,还是摇摇头说道:"; text = "这是一个小城,说是小城其实只是一个大点的镇子,名字也叫青牛镇,只有那些住在附近山沟里、没啥见识的土人,才“青牛城”“青牛城”的叫个不停。这是干了十几年门丁的张二心里话。";
List<string> sentiments = hanlp.GetTokFine(text); //List<string> sentiments = hanlp.GetTokFine(text);
sentiments = hanlp.GetTokCoarse(text); //sentiments = hanlp.GetTokCoarse(text);
string depRoot = hanlp.GetDepRoot(text); //string depRoot = hanlp.GetDepRoot(text);
List<NamedEntity> ners = hanlp.GetHanlpNer(text); //List<NamedEntity> ners = hanlp.GetHanlpNer(text);
List<List<MeaningRepresentationParsingEntity>> srls = hanlp.GetHanlpSrl(text); //List<List<MeaningRepresentationParsingEntity>> srls = hanlp.GetHanlpSrl(text);
List<HanlpConstituencyNode> cons = hanlp.GetHanlpCon(text); //List<HanlpConstituencyNode> cons = hanlp.GetHanlpCon(text);
SpeakerAnalysisHelper speakerFeatureLearner = new SpeakerAnalysisHelper(); SpeakerAnalysisHelper speakerFeatureLearner = new SpeakerAnalysisHelper();
speakerFeatureLearner.GetPerson(depRoot, ners, srls, cons); var person = speakerFeatureLearner.GetPerson(hanlp, text);
text = "韩立闻言,眉头不经意的一皱,想了想后,还是摇摇头说道:";
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@ -12,7 +12,7 @@ using static System.Runtime.InteropServices.JavaScript.JSType;
namespace CloudBuilder.Topshelf.Task.TTS namespace CloudBuilder.Topshelf.Task.TTS
{ {
//task:TtsTask voice:Sherpa2 save_path:D:\MAFAI\cc.wav text:乌云忽然凝聚变形,瞬间化为了一个身高寸许的黑绿婴儿出来,并口吐人言的说道 //task:TtsTask voice:Sherpa2 save_path:D:\\Net8\\FileServer\\Backup\\DmsFile\\voice\\20260528112739.mp3 text:二愣子睁大着双眼,直直望着茅草和烂泥糊成的黑屋顶,身上盖着的旧棉被,已呈深黄色,看不出原来的本来面目,还若有若无的散发着淡淡的霉味
public class TtsTask : IScheduleTask public class TtsTask : IScheduleTask
{ {
private readonly IApplicationService service; private readonly IApplicationService service;

View File

@ -150,6 +150,8 @@ namespace CloudBuilder.Topshelf.Utility
break; break;
} }
string directoryName = Path.GetDirectoryName(saveFilePath);
if (!Directory.Exists(directoryName)) Directory.CreateDirectory(directoryName);
// --- 4. 保存音频 --- // --- 4. 保存音频 ---
audio.SaveToWaveFile(saveFilePath); audio.SaveToWaveFile(saveFilePath);
} }

View File

@ -2,16 +2,34 @@
using CloudBuilder.Topshelf.Python; using CloudBuilder.Topshelf.Python;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
using System.Linq; using System.Linq;
using System.Reflection.Metadata;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
namespace CloudBuilder.Topshelf.Task.Novel.Process namespace CloudBuilder.Topshelf.Utility
{ {
public class SpeakerAnalysisHelper public class SpeakerAnalysisHelper
{ {
public string GetPerson(string depRoot, List<NamedEntity> ners, List<List<MeaningRepresentationParsingEntity>> srls, List<HanlpConstituencyNode> cons) public string GetPerson(Hanlp2 hanlp, string text)
{
string depRoot = hanlp.GetDepRoot(text);
List<NamedEntity> ners = hanlp.GetHanlpNer(text);
List<List<MeaningRepresentationParsingEntity>> srls = hanlp.GetHanlpSrl(text);
List<HanlpConstituencyNode> cons = hanlp.GetHanlpCon(text);
SpeakerAnalysisHelper speakerFeatureLearner = new SpeakerAnalysisHelper();
return GetPerson(hanlp, depRoot, ners, srls, cons);
}
public string GetPerson(Hanlp2 hanlp, string depRoot, List<NamedEntity> ners, List<List<MeaningRepresentationParsingEntity>> srls, List<HanlpConstituencyNode> cons)
{ {
string arg0 = string.Empty; string arg0 = string.Empty;
@ -29,7 +47,14 @@ namespace CloudBuilder.Topshelf.Task.Novel.Process
ners.Where(x => mrp.Text.Contains(x.Text)).FirstOrDefault().Text.Length > 1) ners.Where(x => mrp.Text.Contains(x.Text)).FirstOrDefault().Text.Length > 1)
return ners.Where(x => mrp.Text.Contains(x.Text)).FirstOrDefault().Text; return ners.Where(x => mrp.Text.Contains(x.Text)).FirstOrDefault().Text;
return mrp.Text.Length > 6 ? mrp.Text.Substring(mrp.Text.Length - 6, 6) : mrp.Text; if (mrp.Text.Length > 4)
{
arg0 = GetPerson(hanlp, mrp.Text);
}
else
{
return mrp.Text;
}
} }
} }
@ -41,13 +66,46 @@ namespace CloudBuilder.Topshelf.Task.Novel.Process
} }
} }
if (ners != null && ners.Count() > 0) if (ners != null && ners.Where(x => x.Type.Contains("PERSON")).Count() > 0)
return ners.FirstOrDefault().Text; {
List<string> ps = new List<string>();
foreach (string namedEntity in ners.Where(x => x.Type.Contains("PERSON")).Select(x=>x.Text).Distinct().ToArray())
{
if (namedEntity.Length > 1)
{
if (!ps.Contains(namedEntity))
ps.Add(namedEntity);
}
else
{
HanlpConstituencyNode[] nodes;
HanlpConstituencyNode nodef;
string feature = null;
nodes = HanlpResultHelper.GetNodeByLabel(cons, namedEntity);
if (nodes == null || nodes.Length == 0) continue;
string[] nps = new string[] { "NP", "DNP" };
//如果存在找到父节点上的NP
foreach (HanlpConstituencyNode node in nodes)
{
nodef = HanlpResultHelper.GetNodeTopLabelById(cons, node.FatherId, nps);
if (nodef == null) continue;
feature = HanlpResultHelper.GetNodeBottomLabelById(cons, nodef.ItemId, nps);
if (string.IsNullOrEmpty(feature)) continue;
if (feature.Contains(namedEntity) && !ps.Contains(namedEntity)) ps.Add(feature);
}
}
}
return QuotedString.ToDelimiteredList(ps.ToArray());
}
return arg0; return arg0;
} }
public AiParagraphEntity[] AiParagraphs { get; set; } //public AiSentenceEntity[] AiParagraphs { get; set; }
public AiSentenceEntity[] AiSentences { get; set; } public AiSentenceEntity[] AiSentences { get; set; }