Python程序计算给定文本中单词的出现

给定一个文本(段落)和一个,其出现在文本/段落被发现,我们必须找到多少次的重复文本

示例

    Input:
    text = "this is a book, this is very popular"
    word = "this"

    Output:
    'this' found 2 times.

考虑下面的程序,该程序用于计数文本中仅一个单词的出现

程序:

# Python程序来计数发生 
# 文字中的一个词

# 段
text = """Lorem Ipsum is simply dummy text of the 
printing and typesetting industry. Lorem Ipsum has been 
the industry's standard dummy text ever since the 1500s"""

word = "text"
# 搜索词
count = 0
for w in text.split():
    if w == word:
        count = count + 1
# 打印结果
print("\'%s\' found %d times." %(word, count))

word = "is"
# 搜索词
count = 0
for w in text.split():
    if w == word:
        count = count + 1
# 打印结果
print("\'%s\' found %d times." %(word, count))

word = "Hello"
# 搜索词
count = 0
for w in text.split():
    if w == word:
        count = count + 1
# 打印结果
print("\'%s\' found %d times." %(word, count))

输出结果

'text' found 2 times.
'is' found 1 times.
'Hello' found 0 times.