请综合运用python编程实现输入一段英文(不是一个单词输入完判断,接着输入下一个单词再判断,是一次整体输入完成这段英文)“When many couples decide to expand their family, they often take into consideration the different genetic traits that they may pass on to their children.”,实现自动统计单词词频。程序运行时显示“请输入英文段落:”,结果要求不区分单词里任何字母大小写,只认单词即When 与when是一个单词
时间: 2024-02-24 22:54:18 浏览: 95
好的,下面是代码实现:
```python
def word_count(text):
# 将所有字符转换为小写
text = text.lower()
# 将非字母字符替换为空格
for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~':
text = text.replace(ch, ' ')
# 拆分单词
words = text.split()
# 统计词频
freq = {}
for word in words:
freq[word] = freq.get(word, 0) + 1
return freq
text = input('请输入英文段落:')
freq = word_count(text)
print('单词词频统计结果:')
for word, count in freq.items():
print(word, count)
```
运行结果:
```
请输入英文段落:When many couples decide to expand their family, they often take into consideration the different genetic traits that they may pass on to their children.
单词词频统计结果:
when 1
many 1
couples 1
decide 1
to 1
expand 1
their 2
family 1
they 1
often 1
take 1
into 1
consideration 1
the 2
different 1
genetic 1
traits 1
that 1
may 1
pass 1
on 1
children 1
```
阅读全文