词捚统计是文本分析中捘常使用到的一个功能,请编写一个函挻:统计一段话中出现捚率捘大的单词以及它出现的次挻。哨给捙内容为: Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense.
时间: 2024-03-15 13:46:31 浏览: 93
下面是一个Python函数,可以统计一段话中出现频率最高的单词以及它出现的次数:
```python
def count_words(text):
# 将文本转换为小写并分割成单词
words = text.lower().split()
# 统计每个单词出现的次数
word_counts = {}
for word in words:
if word in word_counts:
word_counts[word] += 1
else:
word_counts[word] = 1
# 找到出现次数最多的单词
max_count = 0
max_word = ''
for word, count in word_counts.items():
if count > max_count:
max_count = count
max_word = word
# 返回结果
return max_word, max_count
# 测试
text = 'Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense.'
word, count = count_words(text)
print(f'The most frequent word is "{word}" which appears {count} times.')
```
输出结果为:
```
The most frequent word is "better" which appears 6 times.
```
注意,这个函数只是一个简单的示例,实际上在文本分析中需要考虑很多其他因素,例如停用词、词干提取等等。
阅读全文