使用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進行分詞(處理中文和英文混合)
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);
// 統(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處理中文分詞。 - 通過正則表達式
([A-Za-z0-9#+]+)提取英文單詞和數(shù)字(如C#、Python3)。
- 使用
停用詞過濾
- 內(nèi)置中英文停用詞表(如 “的”、“and”),過濾無意義詞匯。
- 過濾長度小于2的字符(如單字詞)。
詞頻統(tǒng)計
- 統(tǒng)計關(guān)鍵詞出現(xiàn)次數(shù)并按頻率降序排列。
擴展建議
- 加載外部停用詞表
從文件加載更全面的停用詞(如stopwords.txt):
private void LoadStopWordsFromFile(string path)
{
stopWords = new HashSet<string>(File.ReadAllLines(path));
}
- 詞性過濾
使用Jieba.NET的詞性標注功能,僅保留名詞、動詞等關(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 進行中文分詞
安裝完成后,你就可以在你的 .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);
}
}
}在上面的示例中,我們首先創(chuàng)建了一個 JiebaSegmenter 實例,然后使用 Cut 方法對字符串 "我愛北京天安門" 進行分詞。分詞結(jié)果會以 IEnumerable的形式返回,我們可以遍歷這個結(jié)果并打印出每個詞語。
分詞模式選擇
Jieba.NET 提供了三種分詞模式:精確模式、全模式和搜索引擎模式。你可以根據(jù)需要選擇合適的模式。
精確模式:試圖將句子最精確地切開,適合文本分析。
全模式:把句子中所有的可以成詞的詞語都掃描出來,速度非???,但是不能解決歧義問題。
搜索引擎模式:在精確模式的基礎(chǔ)上,對長詞再次切分,提高召回率,適合用于搜索引擎分詞。
你可以通過 Cut 方法的重載版本來指定分詞模式,例如:
var words = segmenter.Cut(text, cutMode: CutMode.Full); // 使用全模式進行分詞
添加自定義詞典
Jieba.NET 還支持自定義詞典功能,你可以將特定的詞匯添加到詞典中,以確保它們能夠被正確地識別為一個詞。例如:
segmenter.AddWord("天安門廣場"); // 將“天安門廣場”添加到詞典中添加自定義詞典后,當你對包含這些詞匯的文本進行分詞時,Jieba.NET 會將它們作為一個整體進行切分。
以上就是使用C#和Jieba.NET實現(xiàn)中英文混合文本關(guān)鍵詞的提取功能的詳細內(nèi)容,更多關(guān)于C# Jieba.NET關(guān)鍵詞提取的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#并行編程之數(shù)據(jù)并行Tasks.Parallel類
這篇文章介紹了C#并行編程之數(shù)據(jù)并行Tasks.Parallel類,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
使用C#與設(shè)備接口進行無縫通信的實現(xiàn)技巧
隨著物聯(lián)網(wǎng)、智能設(shè)備和自動化技術(shù)的快速發(fā)展,越來越多的設(shè)備需要與計算機系統(tǒng)進行實時通信,而C#作為一門強大的編程語言,憑借其廣泛的庫支持和高效的開發(fā)效率,已成為與設(shè)備接口對接的理想選擇,在本篇文章中,我們將探討如何使用C#與設(shè)備進行無縫通信2025-01-01
C# 將透明圖片的非透明區(qū)域轉(zhuǎn)換成Region的實例代碼
以下代碼實現(xiàn)將一張帶透明度的png圖片的非透明部分轉(zhuǎn)換成Region輸出的方法,有需要的朋友可以參考一下2013-10-10

