字数计数器用于计算句子中的字数

问题描述 投票:0回答:1

我们将构建一个应用程序来计算句子/段落中的单词数。 该应用程序将有一个供用户输入的文本输入、一个提交按钮和一个用于显示结果的标签。 单击“提交”按钮后,应用程序应计算用户输入中的每个单词。这 必须使用循环。一旦您的代码完成单词计数,它应该显示在标签中 计算句子中出现的每个单词的出现次数 出现了。例如,如果用户输入句子“For a for loop count a count”,则 输出将是“for:2,a:2,循环:1,计数:2”。

我能够找出要使用的块,但无法获得结果。

app-inventor
1个回答
0
投票

您可以在 Python 中轻松完成此操作,不确定您使用的是什么语言。

from collections import Counter
from string import ascii_lowercase

sentence = "For a for loop count a count."
letters = {*(ascii_lowercase + " ")}  # you can modify this set depending on what limits you want on the words
fixed_sentence = "".join([c for c in sentence.lower() if c in letters])
print(Counter(fixed_sentence.split()))
© www.soinside.com 2019 - 2024. All rights reserved.