C# Sdcb.PaddleInference 中文分词、词性标注
目录
效果
项目
代码
using Sdcb.PaddleNLP.Lac;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
namespace C__Sdcb.PaddleInference_中文分词_词性标注
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ChineseSegmenter segmenter;
private void button1_Click(object sender, EventArgs e)
{
string input = "我是中国人,我爱我的祖国。";
textBox1.Text = input;
string[] result = segmenter.Segment(input);
textBox2.Text = string.Join(",", result);
}
private void Form1_Load(object sender, EventArgs e)
{
segmenter = new ChineseSegmenter();
}
private void button2_Click(object sender, EventArgs e)
{
string input = "我爱北京天安门";
textBox1.Text = input;
textBox2.Text = "";
WordAndTag[] result = segmenter.Tagging(input);
string labels = string.Join(",", result.Select(x => x.Label));
string words = string.Join(",", result.Select(x => x.Word));
string tags = string.Join(",", result.Select(x => x.Tag));
textBox2.Text += "words:" + words + "\r\n";
textBox2.Text += "labels:" + labels + "\r\n";
textBox2.Text += "tags" + tags + "\r\n";
}
private void button3_Click(object sender, EventArgs e)
{
string input = "我爱北京天安门";
textBox1.Text = input;
textBox2.Text = "";
Dictionary<string, WordTag?> customizedWords = new Dictionary<string, WordTag?>();
customizedWords.Add("北京天安门", WordTag.LocationName);
LacOptions lacOptions = new LacOptions(customizedWords);
ChineseSegmenter segmenter_custom = new ChineseSegmenter(lacOptions);
WordAndTag[] result = segmenter_custom.Tagging(input);
string labels = string.Join(",", result.Select(x => x.Label));
string words = string.Join(",", result.Select(x => x.Word));
string tags = string.Join(",", result.Select(x => x.Tag));
textBox2.Text += "words:" + words + "\r\n";
textBox2.Text += "labels:" + labels + "\r\n";
textBox2.Text += "tags" + tags + "\r\n";
}
}
}
using Sdcb.PaddleNLP.Lac;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
namespace C__Sdcb.PaddleInference_中文分词_词性标注
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ChineseSegmenter segmenter;
private void button1_Click(object sender, EventArgs e)
{
string input = "我是中国人,我爱我的祖国。";
textBox1.Text = input;
string[] result = segmenter.Segment(input);
textBox2.Text = string.Join(",", result);
}
private void Form1_Load(object sender, EventArgs e)
{
segmenter = new ChineseSegmenter();
}
private void button2_Click(object sender, EventArgs e)
{
string input = "我爱北京天安门";
textBox1.Text = input;
textBox2.Text = "";
WordAndTag[] result = segmenter.Tagging(input);
string labels = string.Join(",", result.Select(x => x.Label));
string words = string.Join(",", result.Select(x => x.Word));
string tags = string.Join(",", result.Select(x => x.Tag));
textBox2.Text += "words:" + words + "\r\n";
textBox2.Text += "labels:" + labels + "\r\n";
textBox2.Text += "tags" + tags + "\r\n";
}
private void button3_Click(object sender, EventArgs e)
{
string input = "我爱北京天安门";
textBox1.Text = input;
textBox2.Text = "";
Dictionary<string, WordTag?> customizedWords = new Dictionary<string, WordTag?>();
customizedWords.Add("北京天安门", WordTag.LocationName);
LacOptions lacOptions = new LacOptions(customizedWords);
ChineseSegmenter segmenter_custom = new ChineseSegmenter(lacOptions);
WordAndTag[] result = segmenter_custom.Tagging(input);
string labels = string.Join(",", result.Select(x => x.Label));
string words = string.Join(",", result.Select(x => x.Word));
string tags = string.Join(",", result.Select(x => x.Tag));
textBox2.Text += "words:" + words + "\r\n";
textBox2.Text += "labels:" + labels + "\r\n";
textBox2.Text += "tags" + tags + "\r\n";
}
}
}
下载
参考
https://github.com/sdcb/PaddleSharp/blob/master/docs/paddlenlp-lac.md