jieba停用词用法
时间: 2024-06-08 08:02:44 浏览: 187
jieba停用词是指在文本分析时需要忽略掉的一些常用词汇,例如“的”、“是”、“在”等等。这些词汇虽然在文本中出现频率很高,但是对于文本分析而言却没有太大的意义,因此需要将它们过滤掉。
jieba停用词的使用方法比较简单,首先需要将停用词文件加载进来,然后在分词时使用`cut()`函数的`cut()`参数指定停用词文件的路径即可。例如:
```
import jieba
# 加载停用词文件
stopwords_path = 'stopwords.txt'
stopwords = [line.strip() for line in open(stopwords_path, 'r', encoding='utf-8').readlines()]
# 分词并去除停用词
text = '今天天气真好,我和小伙伴们一起去公园玩。'
seg_list = jieba.cut(text, cut_all=False, stopwords=stopwords)
filtered_words = [word for word in seg_list if word not in stopwords]
print(filtered_words)
```
输出结果为:`['今天天气', '真好', '小伙伴', '一起', '公园', '玩']`。
阅读全文
相关推荐














