对300万字的文档进行分词,并统计一元词频,按降序输出到txt文档中,要求去除停用词
时间: 2023-12-27 20:02:25 浏览: 51
好的,你需要先准备一个停用词表,然后我可以使用Python中的分词库jieba来实现分词和统计词频,并将结果按照要求输出到txt文档中。
以下是示例代码:
```python
import jieba
from collections import Counter
# 读取停用词表
with open('stopwords.txt', 'r', encoding='utf-8') as f:
stopwords = set([line.strip() for line in f.readlines()])
# 读取文档内容
with open('document.txt', 'r', encoding='utf-8') as f:
content = f.read()
# 分词
words = jieba.cut(content)
# 统计词频
word_count = Counter(words)
# 去除停用词
for stopword in stopwords:
word_count.pop(stopword, None)
# 按降序输出到txt文档中
with open('result.txt', 'w', encoding='utf-8') as f:
for word, count in word_count.most_common():
f.write('{} {}\n'.format(word, count))
```
在代码中,我们首先读取了停用词表和文档内容,然后使用jieba分词库对文档内容进行分词,并统计词频。接着,我们去除了停用词,并按照词频的降序将结果输出到txt文档中。注意,需要将停用词表和文档内容的文件路径改成实际路径。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)