jieba分词 中文停用词
时间: 2023-08-29 09:11:19 浏览: 858
中文停用词是指在文本处理中被忽略的一些常见词语,因为这些词语通常不携带太多的语义信息,但在分词过程中却频繁出现。jieba分词是一种常用的中文分词工具,可以对中文文本进行分词处理。如果你想使用jieba分词并且过滤停用词,你可以按照以下步骤进行操作:
1. 安装jieba库:在Python环境中使用pip安装jieba库。
```python
pip install jieba
```
2. 导入jieba库并加载停用词表:在代码中导入jieba库,并加载停用词表。
```python
import jieba
# 加载停用词表
def load_stopwords(filepath):
stopwords = []
with open(filepath, 'r', encoding='utf-8') as f:
for line in f.readlines():
stopwords.append(line.strip())
return stopwords
stopwords = load_stopwords('stopwords.txt')
```
3. 分词并过滤停用词:使用jieba库进行分词,并过滤停用词。
```python
text = '我爱自然语言处理'
seg_list = jieba.cut(text)
filtered_words = []
for word in seg_list:
if word not in stopwords:
filtered_words.append(word)
print(filtered_words)
```
以上代码会输出分词结果,并过滤掉停用词。你需要提前准备一个停用词表,该表包含了需要忽略的常见词语,每个词语占一行,保存在文件`stopwords.txt`中或者其他你指定的文件中。
阅读全文
相关推荐













