Python中词频统计举一反三
时间: 2023-12-25 09:27:26 浏览: 34
Python中词频统计是一项非常常见的任务,可以通过以下步骤实现:
1.读取文本文件并将其存储为字符串。
```python
with open('filename.txt', 'r', encoding='utf-8') as f:
text = f.read()
```
2.使用分词工具(如jieba)将文本分割成单词列表。
```python
import jieba
words = jieba.lcut(text)
```
3.使用字典存储每个单词的出现次数。
```python
word_counts = {}
for word in words:
if len(word) == 1:
continue
else:
word_counts[word] = word_counts.get(word, 0) + 1
```
4.将字典转换为列表并按照出现次数排序。
```python
items = list(word_counts.items())
items.sort(key=lambda x: x[1], reverse=True)
```
5.输出前N个出现次数最多的单词及其出现次数。
```python
for i in range(N):
word, count = items[i]
print("{0:<10}{1:>5}".format(word, count))
```
举一反三,可以将上述方法应用于其他文本分析任务,例如情感分析、主题建模等。
相关问题
python中文词频统计_Python中文词频统计
在Python中进行中文词频统计,可以使用jieba库来进行中文分词。以下是一个简单的示例:
```python
import jieba
# 读取文本文件
with open("text.txt", "r", encoding="utf-8") as f:
text = f.read()
# 使用jieba分词
words = jieba.cut(text)
# 统计词频
freq = {}
for word in words:
if word in freq:
freq[word] += 1
else:
freq[word] = 1
# 输出结果
for word, count in freq.items():
print(word, count)
```
其中,先使用`jieba.cut()`方法对文本进行分词,然后使用一个字典来统计每个词出现的次数。最后遍历字典,输出结果。需要注意的是,在使用jieba库前,需要先安装和下载其所需要的字典文件。
python中文词频统计
可以使用Python中的Jieba库来实现中文词频统计。首先需要将文本文件读取并分词,然后对每个词语进行计数,最后输出结果即可。
以下是示例代码:
```python
import jieba
filename = 'example.txt' # 假设需要统计的文本文件名为example.txt
# 读取文件并分词
with open(filename, 'r', encoding='utf-8') as f:
text = f.read()
words = jieba.lcut(text)
# 统计词频
word_counts = {}
for word in words:
if len(word) < 2: # 过滤掉长度小于2的词语,可根据实际需求调整
continue
if word not in word_counts:
word_counts[word] = 1
else:
word_counts[word] += 1
# 按词频排序并输出
sorted_word_counts = sorted(word_counts.items(), key=lambda x: x[1], reverse=True)
for word, count in sorted_word_counts:
print(word, count)
```
这段代码可以对指定的文本文件进行中文词频统计,并按照词频从高到低输出每个词语及其出现次数。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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_column_c1.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)