word
This commit is contained in:
parent
e6403d63c2
commit
30624828a4
@ -1,15 +1,11 @@
|
||||
import string
|
||||
import re
|
||||
from collections import Counter
|
||||
|
||||
|
||||
def word_count(text):
|
||||
words = []
|
||||
for word in text.lower().split():
|
||||
cleaned_word = word.strip(string.punctuation)
|
||||
if cleaned_word:
|
||||
words.append(cleaned_word)
|
||||
pattern = r"\w+(?:'\w+)?"
|
||||
words = re.findall(pattern, text.lower())
|
||||
|
||||
result = {}
|
||||
for word in words:
|
||||
result[word] = result.get(word,0) +1
|
||||
|
||||
return result
|
||||
return dict(Counter(words))
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user