def top_ten_words_no_excludes(frequency, cnt): """接收词频字典,去除常见的冠词、代词、系动词和连接词后,输出出现次数最多的 cnt个单词及其出现次数,需排除的单词如下: excludes_words = ['a', 'an', 'the', 'i', 'he', 'she', 'his', 'my', 'we', 'or', 'is', 'was', 'do', 'and', 'at', 'to', 'of', 'it', 'on', 'that', 'her', 'c','in', 'you', 'had','s', 'with', 'for', 't', 'but', 'as', 'not', 'they', 'be', 'were', 'so', 'our','all', 'would', 'if', 'him', 'from', 'no', 'me', 'could', 'when', 'there','them', 'about', 'this', 'their', 'up', 'been', 'by', 'out', 'did', 'have'] """
时间: 2023-08-06 07:26:48 浏览: 37
这也是一个Python函数,其功能是对一个词频字典进行处理,去除常见的冠词、代词、系动词和连接词后,输出出现次数最多的cnt个单词及其出现次数。
下面是函数的具体实现:
```python
def top_ten_words_no_excludes(frequency, cnt):
# 定义需排除的单词列表
excludes_words = ['a', 'an', 'the', 'i', 'he', 'she', 'his', 'my', 'we',
'or', 'is', 'was', 'do', 'and', 'at', 'to', 'of', 'it',
'on', 'that', 'her', 'c', 'in', 'you', 'had', 's', 'with',
'for', 't', 'but', 'as', 'not', 'they', 'be', 'were', 'so',
'our', 'all', 'would', 'if', 'him', 'from', 'no', 'me',
'could', 'when', 'there', 'them', 'about', 'this', 'their',
'up', 'been', 'by', 'out', 'did', 'have']
# 去除需排除的单词后,统计单词出现的次数
freq = {k: v for k, v in frequency.items() if k.lower() not in excludes_words}
# 按照出现次数从大到小排序
sorted_freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
# 输出出现次数最多的cnt个单词及其出现次数
top_words = sorted_freq[:cnt]
# 返回结果
return top_words
```
函数首先定义需排除的单词列表`excludes_words`。然后,使用字典推导式去除需排除的单词并统计单词出现的次数,并将结果存储在字典`freq`中。接着,使用`sorted()`函数对字典`freq`按照出现次数从大到小排序。然后,取出前cnt个单词及其出现次数,存储在列表`top_words`中并返回。