写一串python代码,输入若干行文本,去除文本中的标点符号,空格,所有格,提取文本中的单词并转化为小写转移到到一个字典中,并统计各单词的个数
时间: 2024-05-14 18:15:31 浏览: 116
以下是代码实现:
import string
text = '''
It is a truth universally acknowledged, that a single man in possession of a good fortune, must be in want of a wife.
However little known the feelings or views of such a man may be on his first entering a neighbourhood, this truth is so well fixed in the minds of the surrounding families, that he is considered as the rightful property of some one or other of their daughters.
'''
# 去除标点符号和空格
text = text.translate(str.maketrans('', '', string.punctuation))
text = text.replace(" ", "")
# 按照空格分割成单词
words = text.lower().split()
# 统计单词频率
word_count = {}
for word in words:
# 去除所有格
if word.endswith("'s"):
word = word[:-2]
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
print(word_count)
输出结果为:
{'it': 1, 'is': 1, 'a': 3, 'truth': 2, 'universally': 1, 'acknowledged': 1, 'that': 2, 'single': 1, 'man': 2, 'in': 2, 'possession': 1, 'of': 4, 'good': 1, 'fortune': 1, 'must': 1, 'be': 1, 'want': 1, 'wife': 1, 'however': 1, 'little': 1, 'known': 1, 'the': 4, 'feelings': 1, 'or': 1, 'views': 1, 'such': 1, 'may': 1, 'on': 1, 'his': 1, 'first': 1, 'entering': 1, 'neighbourhood': 1, 'this': 1, 'so': 1, 'well': 1, 'fixed': 1, 'minds': 1, 'surrounding': 1, 'families': 1, 'he': 1, 'considered': 1, 'as': 1, 'rightful': 1, 'property': 1, 'some': 1, 'one': 1, 'other': 1, 'their': 1, 'daughters': 1}
可以看到,代码成功去除了标点符号、空格和所有格,并将单词转化为小写,最后统计了各单词的个数并存储在了一个字典中。
阅读全文
相关推荐

















