CloudBuilder.Topshelf/Python/Hanlp2.cs
owenchen 871de41ba5 ow
2026-06-04 08:16:49 +08:00

186 lines
5.4 KiB
C#

using Python.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace CloudBuilder.Topshelf.Python
{
public class Hanlp2
{
public readonly PythonCommand python;
public Hanlp2(PythonCommand python)
{
this.python = python;
string file = python.ApplicationService.Configuration["Python:PythonFile"]!;
if (!File.Exists(file)) return;
string init = File.ReadAllText(file, Encoding.UTF8);
using (Py.GIL())
{
python.Exec(init);
}
}
public List<NamedEntity> GetHanlpNer(string text)
{
try
{
//必须定义方法的方式使用
using (Py.GIL())
{
string json = python.GetValueExec($"processor.get_ner_json('{text}')");
// 反序列化
if (json == "[]") return null;
return JsonSerializer.Deserialize<List<NamedEntity>>(json);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return null;
}
public List<List<MeaningRepresentationParsingEntity>> GetHanlpSrl(string text)
{
try
{
//必须定义方法的方式使用
using (Py.GIL())
{
string json = python.GetValueExec($"processor.get_srl_json('{text}')");
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true // 忽略属性名大小写
};
// 反序列化
return JsonSerializer.Deserialize<List<List<MeaningRepresentationParsingEntity>>>(json, options);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return null;
}
public string GetDepRoot(string text)
{
try
{
//必须定义方法的方式使用
using (Py.GIL())
{
string json = python.GetValueExec($"processor.get_root('{text}')");
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true // 忽略属性名大小写
};
// 反序列化
return json;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return null;
}
public List<HanlpConstituencyNode> GetHanlpCon(string text)
{
if (string.IsNullOrEmpty(text) || text.Length < 3) return null;
try
{
//必须定义方法的方式使用
using (Py.GIL())
{
string json = python.GetValueExec($"processor.get_con_json('{text}')");
if (json == "[]") return null;
return HanlpConstituencyNode.FromJsonString(json);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return null;
}
public List<string> GetTokFine(string text)
{
try
{
//必须定义方法的方式使用
using (Py.GIL())
{
string json = python.GetValueExec($"processor.get_tok_fine('{text}')");
// 反序列化
if (json == "[]") return null;
return JsonSerializer.Deserialize<List<string>>(json);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return null;
}
public List<string> GetTokCoarse(string text)
{
try
{
//必须定义方法的方式使用
using (Py.GIL())
{
string json = python.GetValueExec($"processor.get_tok_coarse('{text}')");
// 反序列化
if (json == "[]") return null;
return JsonSerializer.Deserialize<List<string>>(json);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return null;
}
public List<string> GetSurnames()
{
try
{
//必须定义方法的方式使用
using (Py.GIL())
{
string json = python.GetValueExec($"processor.get_surnames_json()");
// 反序列化
if (json == "[]") return null;
return JsonSerializer.Deserialize<List<string>>(json);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return null;
}
public string Get(string arg)
{
return python.GetValueExec(arg);
}
}
}