python四级词频停用词
时间: 2023-11-26 13:48:24 浏览: 87
根据提供的引用内容,可以使用Python中的re库进行中英文文本的识别,同时可以使用collections库中的Counter函数进行词频统计。停用词是指在文本分析中,为了避免出现一些常见但是无实际意义的词语,需要将这些词语从文本中剔除。常见的停用词包括“的”、“是”、“在”等。以下是一个Python实现的四级词频停用词的例子:
```python
import re
import collections
def get_words(file):
with open(file, 'r', encoding='utf-8') as f:
words_box = []
for line in f:
# 使用正则表达式匹配中英文文本
words = re.findall(u'[\u4E00-\u9FA5|\w]+', line)
# 将匹配到的词语添加到列表中
words_box.extend(words)
# 读取停用词表
with open('stopwords.txt', 'r', encoding='utf-8') as f:
stopwords = f.read().splitlines()
# 使用Counter函数进行词频统计
words_count = collections.Counter(words_box)
# 剔除停用词
for word in stopwords:
words_count.pop(word, None)
return words_count
# 获取两个文本的词频统计结果并相加
result = get_words('file1.txt') + get_words('file2.txt')
# 输出前10个高频词
print(result.most_common(10))
```
在上述代码中,我们首先使用正则表达式匹配中英文文本,并将匹配到的词语添加到列表中。然后,我们读取停用词表,并使用Counter函数进行词频统计。最后,我们遍历停用词表,将其中的停用词从词频统计结果中剔除。最终,我们将两个文本的词频统计结果相加,并输出前10个高频词。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)