使用C#和Jieba.NET实现中英文混合文本关键词的提取功能
作者:老胖闲聊
Jieba.NET 是一个在 C# 中实现的分词库,它基于 Java 的 jieba 分词库,并进行了 C# 语言的移植,Jieba 是一个高效的中文分词工具,能够处理全模式、精确模式以及搜索引擎模式,本文给大家介绍了如何使用C#和Jieba.NET实现中英文混合文本关键词的提取功能
实现步骤
创建Windows窗体应用程序
添加以下控件:TextBox:输入文本(支持多行)Button:触发分词ListBox:显示关键词及词频
安装NuGet包
Install-Package jieba.NET
完整代码实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using JiebaNet.Segmenter;
using System.Text.RegularExpressions;
public partial class MainForm : Form
{
private TextBox inputBox;
private Button analyzeButton;
private ListBox resultList;
private HashSet<string> stopWords;
public MainForm()
{
InitializeComponent();
InitializeStopWords(); // 初始化停用词表
}
private void InitializeStopWords()
{
// 中英文停用词表(示例)
stopWords = new HashSet<string>
{
// 中文停用词
"的", "了", "在", "是", "我", "和", "有", "就", "不", "人",
// 英文停用词
"a", "an", "the", "is", "are", "and", "in", "on", "at"
};
}
private void AnalyzeButton_Click(object sender, EventArgs e)
{
string inputText = inputBox.Text.Trim();
if (string.IsNullOrEmpty(inputText))
{
MessageBox.Show("请输入文本!");
return;
}
// 使用Jieba进行分词(处理中文和英文混合)
var segmenter = new JiebaSegmenter();
var segments = segmenter.Cut(inputText);
// 提取英文单词(通过正则表达式补充处理)
var allWords = new List<string>();
foreach (var seg in segments)
{
// 处理中英文混合词(如 "C#编程" -> ["C#", "编程"])
var words = Regex.Matches(seg, @"([A-Za-z0-9#+]+)|([\u4e00-\u9fa5]+)")
.Cast<Match>()
.Select(m => m.Value.ToLower());
allWords.AddRange(words);
}
// 过滤停用词和单字词
var filteredWords = allWords
.Where(word => !stopWords.Contains(word) && word.Length >= 2);
// 统计词频并排序
var keywordCounts = filteredWords
.GroupBy(word => word)
.OrderByDescending(g => g.Count())
.Select(g => $"{g.Key} ({g.Count()})")
.ToList();
// 显示结果
resultList.DataSource = keywordCounts;
}
// 初始化窗体控件
private void InitializeComponent()
{
this.inputBox = new TextBox();
this.analyzeButton = new Button();
this.resultList = new ListBox();
// 布局控件
this.inputBox.Multiline = true;
this.inputBox.Location = new System.Drawing.Point(20, 20);
this.inputBox.Size = new System.Drawing.Size(400, 150);
this.analyzeButton.Text = "提取关键词";
this.analyzeButton.Location = new System.Drawing.Point(20, 180);
this.analyzeButton.Click += AnalyzeButton_Click;
this.resultList.Location = new System.Drawing.Point(20, 220);
this.resultList.Size = new System.Drawing.Size(400, 200);
this.ClientSize = new System.Drawing.Size(440, 440);
this.Controls.Add(inputBox);
this.Controls.Add(analyzeButton);
this.Controls.Add(resultList);
}
}
功能说明
中英文混合分词
- 使用
Jieba.NET处理中文分词。 - 通过正则表达式
([A-Za-z0-9#+]+)提取英文单词和数字(如C#、Python3)。
- 使用
停用词过滤
- 内置中英文停用词表(如 “的”、“and”),过滤无意义词汇。
- 过滤长度小于2的字符(如单字词)。
词频统计
- 统计关键词出现次数并按频率降序排列。
扩展建议
- 加载外部停用词表
从文件加载更全面的停用词(如stopwords.txt):
private void LoadStopWordsFromFile(string path)
{
stopWords = new HashSet<string>(File.ReadAllLines(path));
}
- 词性过滤
使用Jieba.NET的词性标注功能,仅保留名词、动词等关键词:
var posSegmenter = new PosSegmenter();
var posTags = posSegmenter.Cut(inputText);
var nouns = posTags.Where(tag => tag.Flag.StartsWith("n"));
- TF-IDF算法
实现更高级的关键词权重计算(需引入TF-IDF库)。
使用 Jieba.NET 进行中文分词
安装完成后,你就可以在你的 .NET 项目中使用 Jieba.NET 进行中文分词了。以下是一个简单的示例:
using JiebaNet.Segmenter;
using System;
class Program
{
static void Main(string[] args)
{
var segmenter = new JiebaSegmenter();
string text = "我爱北京天安门";
var words = segmenter.Cut(text);
foreach (var word in words)
{
Console.WriteLine(word);
}
}
}在上面的示例中,我们首先创建了一个 JiebaSegmenter 实例,然后使用 Cut 方法对字符串 "我爱北京天安门" 进行分词。分词结果会以 IEnumerable的形式返回,我们可以遍历这个结果并打印出每个词语。
分词模式选择
Jieba.NET 提供了三种分词模式:精确模式、全模式和搜索引擎模式。你可以根据需要选择合适的模式。
精确模式:试图将句子最精确地切开,适合文本分析。
全模式:把句子中所有的可以成词的词语都扫描出来,速度非常快,但是不能解决歧义问题。
搜索引擎模式:在精确模式的基础上,对长词再次切分,提高召回率,适合用于搜索引擎分词。
你可以通过 Cut 方法的重载版本来指定分词模式,例如:
var words = segmenter.Cut(text, cutMode: CutMode.Full); // 使用全模式进行分词
添加自定义词典
Jieba.NET 还支持自定义词典功能,你可以将特定的词汇添加到词典中,以确保它们能够被正确地识别为一个词。例如:
segmenter.AddWord("天安门广场"); // 将“天安门广场”添加到词典中添加自定义词典后,当你对包含这些词汇的文本进行分词时,Jieba.NET 会将它们作为一个整体进行切分。
以上就是使用C#和Jieba.NET实现中英文混合文本关键词的提取功能的详细内容,更多关于C# Jieba.NET关键词提取的资料请关注脚本之家其它相关文章!
