python中stopwords中文怎么用
时间: 2023-12-23 11:02:25 浏览: 126
在Python中,可以使用第三方库jieba和中文停用词表(stopwords)来实现中文文本的分词和去除停用词的功能。具体操作步骤如下:
1. 安装jieba库和中文停用词表(stopwords)
```python
!pip install jieba
```
下载中文停用词表(stopwords):https://github.com/goto456/stopwords/blob/master/%E4%B8%AD%E6%96%87%E5%81%9C%E7%94%A8%E8%AF%8D%E8%A1%A8.txt
2. 导入jieba库和打开中文停用词表(stopwords)
```python
import jieba
# 打开中文停用词表(stopwords)
stopwords_path = 'stopwords.txt'
stopwords = [line.strip() for line in open(stopwords_path, 'r', encoding='utf-8').readlines()]
```
3. 对中文文本进行分词和去除停用词
```python
# 分词
text = '我喜欢看电影,尤其是科幻电影。'
seg_list = jieba.cut(text, cut_all=False)
# 去除停用词
seg_result = []
for word in seg_list:
if word not in stopwords:
seg_result.append(word)
print(seg_result)
```
输出结果:
```
['我', '喜欢', '看', '电影', ',', '尤其', '科幻电影', '。']
```
这样,就完成了中文分词和去除停用词的操作。
阅读全文
相关推荐













