使用C#和Jieba.NET實現(xiàn)中英文混合文本關(guān)鍵詞的提取功能
實現(xiàn)步驟
創(chuàng)建Windows窗體應(yīng)用程序
添加以下控件:TextBox:輸入文本(支持多行)Button:觸發(fā)分詞ListBox:顯示關(guān)鍵詞及詞頻
安裝NuGet包
Install-Package jieba.NET
完整代碼實現(xiàn)
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進(jìn)行分詞(處理中文和英文混合)
var segmenter = new JiebaSegmenter();
var segments = segmenter.Cut(inputText);
// 提取英文單詞(通過正則表達(dá)式補(bǔ)充處理)
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);
// 統(tǒng)計詞頻并排序
var keywordCounts = filteredWords
.GroupBy(word => word)
.OrderByDescending(g => g.Count())
.Select(g => $"{g.Key} ({g.Count()})")
.ToList();
// 顯示結(jié)果
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 = "提取關(guān)鍵詞";
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處理中文分詞。 - 通過正則表達(dá)式
([A-Za-z0-9#+]+)提取英文單詞和數(shù)字(如C#、Python3)。
- 使用
停用詞過濾
- 內(nèi)置中英文停用詞表(如 “的”、“and”),過濾無意義詞匯。
- 過濾長度小于2的字符(如單字詞)。
詞頻統(tǒng)計
- 統(tǒng)計關(guān)鍵詞出現(xiàn)次數(shù)并按頻率降序排列。
擴(kuò)展建議
- 加載外部停用詞表
從文件加載更全面的停用詞(如stopwords.txt):
private void LoadStopWordsFromFile(string path)
{
stopWords = new HashSet<string>(File.ReadAllLines(path));
}
- 詞性過濾
使用Jieba.NET的詞性標(biāo)注功能,僅保留名詞、動詞等關(guān)鍵詞:
var posSegmenter = new PosSegmenter();
var posTags = posSegmenter.Cut(inputText);
var nouns = posTags.Where(tag => tag.Flag.StartsWith("n"));
- TF-IDF算法
實現(xiàn)更高級的關(guān)鍵詞權(quán)重計算(需引入TF-IDF庫)。
使用 Jieba.NET 進(jìn)行中文分詞
安裝完成后,你就可以在你的 .NET 項目中使用 Jieba.NET 進(jìn)行中文分詞了。以下是一個簡單的示例:
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);
}
}
}在上面的示例中,我們首先創(chuàng)建了一個 JiebaSegmenter 實例,然后使用 Cut 方法對字符串 "我愛北京天安門" 進(jìn)行分詞。分詞結(jié)果會以 IEnumerable的形式返回,我們可以遍歷這個結(jié)果并打印出每個詞語。
分詞模式選擇
Jieba.NET 提供了三種分詞模式:精確模式、全模式和搜索引擎模式。你可以根據(jù)需要選擇合適的模式。
精確模式:試圖將句子最精確地切開,適合文本分析。
全模式:把句子中所有的可以成詞的詞語都掃描出來,速度非??欤遣荒芙鉀Q歧義問題。
搜索引擎模式:在精確模式的基礎(chǔ)上,對長詞再次切分,提高召回率,適合用于搜索引擎分詞。
你可以通過 Cut 方法的重載版本來指定分詞模式,例如:
var words = segmenter.Cut(text, cutMode: CutMode.Full); // 使用全模式進(jìn)行分詞
添加自定義詞典
Jieba.NET 還支持自定義詞典功能,你可以將特定的詞匯添加到詞典中,以確保它們能夠被正確地識別為一個詞。例如:
segmenter.AddWord("天安門廣場"); // 將“天安門廣場”添加到詞典中添加自定義詞典后,當(dāng)你對包含這些詞匯的文本進(jìn)行分詞時,Jieba.NET 會將它們作為一個整體進(jìn)行切分。
以上就是使用C#和Jieba.NET實現(xiàn)中英文混合文本關(guān)鍵詞的提取功能的詳細(xì)內(nèi)容,更多關(guān)于C# Jieba.NET關(guān)鍵詞提取的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#?利用Autofac批量接口注入依賴的問題小結(jié)
這篇文章主要介紹了C#?利用Autofac批量接口注入依賴的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-12-12
C# 當(dāng)前系統(tǒng)時間獲取及時間格式詳解
這篇文章主要介紹了C# 當(dāng)前系統(tǒng)時間獲取及時間格式詳解的相關(guān)資料,這里提供代碼實例,幫助大家學(xué)習(xí)參考,需要的朋友可以參考下2016-12-12

