Skip to content

Instantly share code, notes, and snippets.

@Bundi-py
Last active February 28, 2020 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bundi-py/92a557ea7e1d87a9950f7a39784563e5 to your computer and use it in GitHub Desktop.
Save Bundi-py/92a557ea7e1d87a9950f7a39784563e5 to your computer and use it in GitHub Desktop.
# 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