robertvari

Eszter Haszon-Hegedüs - word stats

Apr 23rd, 2019
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. import collections
  2.  
  3. folderPath = "E:/_PythonSuli/0413/homeworks/"
  4. textfile = "The Adventures of Sherlock Holmes.txt"
  5. mydocument = folderPath + textfile
  6.  
  7. with open(mydocument, "r") as f:
  8.     mytext = str(f.read())
  9.  
  10.     if len(mytext) == 0:
  11.         print("file is empty")
  12.     else:
  13.  
  14.         numberOfWords = len(mytext.split(" ")) # ez így szebb!
  15.         character = (len(mytext.replace(" ", "")))     #karakterek száma
  16.  
  17.         mytext2 = mytext.replace("\n", " ")
  18.         listOfWords = [ word for word in mytext2.split(" ") if len(word)>3]
  19.         freq = collections.Counter(listOfWords).most_common(3)
  20.  
  21.     statfile = "Szavak száma: {}\nKarakterek száma (szóköz nélkül): {}\nA három leggyakoribb " \
  22.                "szó: {}, {}, {}".format(numberOfWords,character,freq[0][0],freq[1][0],freq[2][0])
  23.  
  24.     with open(folderPath + "statistics.txt", "w") as f:
  25.         f.write(statfile)
  26.  
  27.     print(statfile)
Advertisement
Add Comment
Please, Sign In to add comment