读取文件 '水浒传.txt' 用jieba 对全文分词 统计分词得到的词汇出现的次数 把这些词汇按出现的次数逆序排序 打印 '宋江' 出现的次数
时间: 2024-05-14 13:11:46 浏览: 11
好的,没有问题。关于你的问题,我可以帮你解决。以下是代码示例:
```python
import jieba
# 读取文件并分词
with open('水浒传.txt', 'r', encoding='utf-8') as f:
text = f.read()
words = jieba.lcut(text)
# 统计词频
word_counts = {}
for word in words:
if len(word) <= 1: # 排除单个字符的词
continue
if word in word_counts:
word_counts[word] += 1
else:
word_counts[word] = 1
# 按词频逆序排序
sorted_words = sorted(word_counts.items(), key=lambda x: x[1], reverse=True)
# 打印“宋江”出现的次数
songjiang_count = 0
for item in sorted_words:
if item[0] == '宋江':
songjiang_count = item[1]
break
print('宋江出现的次数:', songjiang_count)
```
希望这可以帮到你!
相关问题
用jieba库对文件分词并统计出现次数最多的五个词
好的,首先需要安装jieba库,可以使用以下命令安装:
```
pip install jieba
```
然后,我们可以使用以下代码来对文件进行分词并统计出现次数最多的五个词:
```python
import jieba
# 打开文件并读取内容
with open('example.txt', 'r', encoding='utf-8') as f:
content = f.read()
# 对内容进行分词
words = jieba.lcut(content)
# 统计词频
word_counts = {}
for word in words:
if len(word) == 1 or word == '\r\n':
continue
else:
word_counts[word] = word_counts.get(word, 0) + 1
# 按词频排序并取前五个
top_five = sorted(word_counts.items(), key=lambda x: x[1], reverse=True)[:5]
# 输出结果
for word, count in top_five:
print(f'{word}: {count}')
```
其中,假设待分词的文件名为example.txt,代码中读取文件内容后使用jieba.lcut()函数对内容进行分词,然后使用一个字典word_counts记录每个词出现的次数。最后使用sorted()函数按词频从大到小排序,再取前五个即为出现次数最多的五个词。
新建“test4”Python文件:读取素材中的“test.txt”文件,使用jieba库进行分词,并将出现次数前5的词汇和出现次数信息写入到“output.txt”文件中。
好的,以下是代码实现:
```python
import jieba
# 读取文件
with open("test.txt", "r", encoding="utf-8") as f:
content = f.read()
# 使用jieba进行分词
words = jieba.cut(content)
word_count = {}
# 统计词频
for word in words:
if len(word) > 1:
word_count[word] = word_count.get(word, 0) + 1
# 按照词频从大到小排序
sorted_word_count = sorted(word_count.items(), key=lambda x: x[1], reverse=True)
# 写入文件
with open("output.txt", "w", encoding="utf-8") as f:
for i in range(5):
f.write(f"{sorted_word_count[i][0]}:{sorted_word_count[i][1]}\n")
```
代码中使用了jieba库进行中文分词,通过字典统计词频,并使用sorted函数按照词频从高到低进行排序。最后,将出现次数前5的词汇和出现次数信息写入到“output.txt”文件中。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)