用Python进行中文文本数据去停词
时间: 2023-05-25 22:03:57 浏览: 49
以下是使用Python进行中文文本数据去停词的示例代码:
```python
import jieba
import codecs
# 读取停用词表
stopwords_path = 'stopwords.txt'
# 读取文本数据
text_path = 'text.txt'
with codecs.open(text_path, 'r', encoding='utf-8') as f:
text = f.read()
# 分词并去停用词
stopwords = set()
with codecs.open(stopwords_path, 'r', encoding='utf-8') as f:
for line in f:
stopwords.add(line.strip())
words = []
seg_list = jieba.cut(text, cut_all=False)
for word in seg_list:
if word not in stopwords:
words.append(word)
# 保存去停用词后的结果
output_path = 'output.txt'
with codecs.open(output_path, 'w', encoding='utf-8') as f:
for word in words:
f.write(word + ' ')
```
以上示例代码使用jieba库对文本数据进行分词,并使用停用词表进行去停用词处理。分词过程中可使用不同的模式,如全模式、精确模式等。本示例中使用默认的精确模式。去停用词的过程中,将每个词与停用词表中的词进行比较,如果不在停用词表中,则将其加入到分词结果中。最后将去停用词后的结果保存到文件中。请在示例代码中替换相关的文件路径和停用词表。