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):
|
def word_count(text):
|
||||||
words = []
|
pattern = r"\w+(?:'\w+)?"
|
||||||
for word in text.lower().split():
|
words = re.findall(pattern, text.lower())
|
||||||
cleaned_word = word.strip(string.punctuation)
|
|
||||||
if cleaned_word:
|
|
||||||
words.append(cleaned_word)
|
|
||||||
|
|
||||||
result = {}
|
return dict(Counter(words))
|
||||||
for word in words:
|
|
||||||
result[word] = result.get(word,0) +1
|
|
||||||
|
|
||||||
return result
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user