Last active
February 28, 2020 14:13
-
-
Save Bundi-py/92a557ea7e1d87a9950f7a39784563e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Napiši program koji će prikazati 5 reči koje se najčešće pojavljuju u datom tekstu. Posle učitavanja teksta, | |
# program će prvo ukloniti sve znakove interpuncije, a prilikom brojanja reči zanemariti mala i velika slova. | |
import re | |
with open('new1.txt', encoding='utf-8') as f: | |
text = f.read() | |
text = text.lower() | |
words = re.findall('\w+', text) | |
frequency = {} | |
for word in words: | |
count = frequency.get(word,0) | |
frequency[word] = count + 1 | |
tuple = sorted(frequency.items(), key=lambda x:x[1], reverse=True) | |
for x, y in tuple[:5]: | |
print(x, '=', y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment