51 lines
1.4 KiB
C#
51 lines
1.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace WinFormsApp3
|
|
{
|
|
internal class Class1
|
|
{
|
|
}
|
|
}/*
|
|
// 这是正确、最简、不会报错的 NER 辅助类
|
|
public class NerInput
|
|
{
|
|
public string Text { get; set; }
|
|
}
|
|
|
|
public class NerOutput
|
|
{
|
|
// 不需要 Label!不会报错!
|
|
public string[] Words { get; set; }
|
|
}
|
|
|
|
var mlContext = new MLContext();
|
|
|
|
// ============================
|
|
// 关键:只做 分词 + 特征转换
|
|
// 绝对不加分类器,就不会要 Label
|
|
// ============================
|
|
var pipeline = mlContext.Transforms.Text.TokenizeIntoWords("Words", nameof(NerInput.Text));
|
|
|
|
// 空数据也不会报错
|
|
var emptyData = mlContext.Data.LoadFromEnumerable(new List<NerInput>());
|
|
var model = pipeline.Fit(emptyData);
|
|
|
|
// 预测引擎
|
|
var engine = mlContext.Model.CreatePredictionEngine<NerInput, NerOutput>(model);
|
|
|
|
// 测试
|
|
var result = engine.Predict(new NerInput
|
|
{
|
|
Text = "我和陈明今天去上海见张伟"
|
|
});
|
|
|
|
Console.WriteLine("分词结果:");
|
|
foreach (var word in result.Words)
|
|
{
|
|
Console.WriteLine("- " + word);
|
|
}
|
|
*/ |