利用jieba库分析统计出三国演义中出现次数排前20的中文词语(长度为1的词不统计) 统计结果写入文本文件result.txt中
时间: 2024-02-27 21:52:13 浏览: 24
```python
import jieba
# 打开三国演义文本文件,读取内容
with open('three_kingdoms.txt', 'r', encoding='utf-8') as f:
text = f.read()
# 使用jieba分词,并统计词频
word_counts = {}
words = jieba.lcut(text)
for word in words:
if len(word) > 1: # 排除长度为1的词
if word in word_counts:
word_counts[word] += 1
else:
word_counts[word] = 1
# 按词频降序排列,并取前20个词
top20_words = sorted(word_counts.items(), key=lambda x: x[1], reverse=True)[:20]
# 将结果写入文本文件result.txt中
with open('result.txt', 'w', encoding='utf-8') as f:
for word, count in top20_words:
f.write("{}\t{}\n".format(word, count))
print("统计结果已保存到result.txt中。")
```
相关问题
Python编写程序利用jieba库进行分词,统计出三国演义中三国人物的出现次数
好的,首先需要安装jieba库,可以使用以下命令进行安装:
```
pip install jieba
```
然后,我们需要读取三国演义的文本文件。假设三国演义的文本文件名为“sgyy.txt”,可以使用以下代码进行读取:
```python
with open('sgyy.txt', 'r', encoding='utf-8') as f:
sgyy_text = f.read()
```
接下来,我们需要使用jieba库进行分词。可以使用以下代码:
```python
import jieba
# 分词
seg_list = jieba.cut(sgyy_text)
# 去除停用词
stop_words = [',', '。', '、', ':', ';', '\n', '之', '也', '而', '乃', '其', '则', '者', '其', '为', '以', '有', '是', '在', '不', '而', '此', '彼']
word_list = [word for word in seg_list if word not in stop_words]
```
上面的代码中,我们使用了jieba.cut()方法对文本进行分词,并使用列表推导式去除了一些无意义的停用词。
接下来,我们需要统计出三国演义中三国人物的出现次数。可以使用以下代码:
```python
# 统计人物出现次数
person_dict = {}
for i in range(len(word_list)-1):
if word_list[i] == '曹操' or word_list[i] == '刘备' or word_list[i] == '孙权':
if word_list[i+1] != ',' and word_list[i+1] != '。':
if word_list[i] not in person_dict:
person_dict[word_list[i]] = 1
else:
person_dict[word_list[i]] += 1
# 输出结果
for key, value in person_dict.items():
print(key, value)
```
上面的代码中,我们使用了一个字典来统计人物出现次数。首先,我们遍历分词后的列表,判断当前词是否是三国人物中的一个(曹操、刘备、孙权)。如果当前词是一个三国人物,我们再判断下一个词是否是标点符号(“,”和“。”),如果不是,则将当前词加入到字典中,并将其出现次数加1。最后,我们将字典中统计出来的结果输出即可。
完整代码如下:
```python
import jieba
# 读取文本
with open('sgyy.txt', 'r', encoding='utf-8') as f:
sgyy_text = f.read()
# 分词
seg_list = jieba.cut(sgyy_text)
# 去除停用词
stop_words = [',', '。', '、', ':', ';', '\n', '之', '也', '而', '乃', '其', '则', '者', '其', '为', '以', '有', '是', '在', '不', '而', '此', '彼']
word_list = [word for word in seg_list if word not in stop_words]
# 统计人物出现次数
person_dict = {}
for i in range(len(word_list)-1):
if word_list[i] == '曹操' or word_list[i] == '刘备' or word_list[i] == '孙权':
if word_list[i+1] != ',' and word_list[i+1] != '。':
if word_list[i] not in person_dict:
person_dict[word_list[i]] = 1
else:
person_dict[word_list[i]] += 1
# 输出结果
for key, value in person_dict.items():
print(key, value)
```
请编写程序,对"凉州词.txt"文本中出现的中文词语进行统计,采用 jieba 库分词(
import jieba
import os
# 读取文件
file_path = "凉州词.txt"
with open(file_path, "r", encoding="utf-8") as f:
content = f.read()
# 使用jieba进行分词
seg_list = jieba.cut(content, cut_all=False)
# 统计词语出现次数
word_count = {}
for word in seg_list:
if len(word) > 1: # 只统计长度大于1的词语
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
# 将结果写入文件
output_path = "词语统计结果.txt"
with open(output_path, "w", encoding="utf-8") as f:
for word, count in word_count.items():
f.write(f"{word}: {count}\n")
print("统计完成,结果已写入词语统计结果.txt文件中。")
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)