簡體   English   中英

如何在文本中查找特定單詞並使用 python 計算它們?

[英]How to find specific words in a text and count them using python?

我想檢查某些單詞是否出現在輸入文本中,如果出現,出現了多少次。

這些是我的投入:

  • 單詞列表: keywords = ["apple", "banana", "orange", "lemon"]
  • 要掃描的text = "This apple is very tasty but the banana is not delicious at all."text = "This apple is very tasty but the banana is not delicious at all."

現在我想計算關鍵字列表中的單詞出現在輸入文本中的次數。

所以這個例子的輸出應該是這樣的:

`我找到了2個詞。

這是我到目前為止所得到的,但在這種情況下它輸出的是 0 而不是 2。

text = "This apple is very tasty but the banana is not delicious at all."

keywords = ["apple", "banana", "orange", "lemon"]

def dictionary_score(text):
    wordcount=0
    for line in text:
        line = line.strip()
        line = line.lower()
        words = line.split(" ")
        for word in words:
            if keywords in words:
                wordcount += 1
print(f"I found {wordcount} words") 

正確計數的問題在哪里?

問題在於if keywords in words: 它會檢查整個keywords列表是否在您的words列表中。

您可能想檢查每個word是否在keywords列表中:

if word in keywords:
  1. text是一個字符串,並且for line in text迭代字符串的字符。 可以替換for line in text.splitlines():for line in text.splitlines():

  2. 應該是if word in keywords:而不是if word in keywords:if keywords in words:

     text = "This apple is very tasty but the banana is not delicious at all." keywords = ["apple", "banana", "orange", "lemon"] def dictionary_score(text): wordcount=0 for line in text.splitlines(): print(line) line = line.strip() line = line.lower() words = line.split(" ") for word in words: if word in keywords: wordcount += 1 print(f"I found {wordcount} words") dictionary_score(text)```

輸出: I found 2 words

您的代碼有幾個錯誤:

text = "This apple is very tasty but the banana is not delicious at all."
keywords = ["apple", "banana", "orange", "lemon"]

def dictionary_score(text):
    wordcount=0
    for line in text: #Iterate over each string character
        line = line.strip()
        line = line.lower()
        words = line.split(" ") #Here the list will be empty, because you are operating on a character.
        for word in words: #You are iterating over a empty list
            if keywords in words: #Checking if the list keywords is in words(that is empty)
                wordcount += 1
print(f"I found {wordcount} words") 
  • for line in text:迭代字符串的每個字符,在獲取字符串后,降低並拆分它。

  • if keywords in words:這里您檢查關鍵字列表是否在詞列表中,因為前面的解釋是空的。

這里是固定代碼:

text = "This apple is very tasty but the banana is not delicious at all."
keywords = ["apple", "banana", "orange", "lemon"]

def dictionary_score(text):
    wordcount = 0
    words = text.strip().lower().split(" ") #split the string, after stripping and lowering it
    for word in words: # Iterate over the words
        if word in keywords: # If the word is in the keywords list increment the counter
            wordcount += 1
    print(f"I found {wordcount} words") 

dictionary_score(text)

輸出: I found 2 words

在集合中使用計數器

from collections import Counter
text = "This apple is very tasty but the banana is not delicious at all."
dict_words = Counter(text.split(" "))
dict_word.get("apple", 0 ) #Get the word count for apple

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM