Advertisement
UF6

2.5 Problem 3

UF6
Jul 4th, 2016
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.67 KB | None | 0 0
  1. path="/Users/rocke/Desktop/wonderland.txt"
  2. datafile=open(path,"r")
  3. outfile=open("/Users/rocke/Desktop/wonderland.txt","w")
  4. for line in datafile:
  5.     outfile.write(line)
  6. outfile.close()
  7.  
  8. datafile=open("/Users/rocke/Desktop/wonderland.txt","r")
  9.  
  10. letterFreqDic={}
  11. alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  12. for letter in alphabet:
  13.     letterFreqDic[letter]=0
  14. #print(letterFreqDic)
  15. n = 0    
  16. for i, line in enumerate(datafile):
  17.     if i< 60000:
  18.         line=line.strip()
  19.         if i <20:print(line)
  20.         for letter in line:
  21.             if letter.isalpha():
  22.                 letter = letter.upper()
  23.                 #print(letter)
  24.                 letterFreqDic[letter] = letterFreqDic[letter]+1
  25.                 n+=1
  26.                
  27. print(letterFreqDic)
  28. letterfrequency=[]
  29. for letter in alphabet:
  30.     c=letterFreqDic[letter]
  31.     letterfrequency.append((c/n,letter))
  32.  
  33. letterfrequency.sort()
  34. letterfrequency.reverse()
  35. for i in letterfrequency:
  36.     print(i)
  37.  
  38. #Project Gutenberg's Alice's Adventures in Wonderland, by Lewis Carroll
  39.  
  40. #This eBook is for the use of anyone anywhere at no cost and with
  41. #almost no restrictions whatsoever.  You may copy it, give it away or
  42. #re-use it under the terms of the Project Gutenberg License included
  43. #with this eBook or online at www.gutenberg.org
  44.  
  45.  
  46. #Title: Alice's Adventures in Wonderland
  47.  
  48. #Author: Lewis Carroll
  49.  
  50. #Posting Date: June 25, 2008 [EBook #11]
  51. #Release Date: March, 1994
  52. #[Last updated: December 20, 2011]
  53.  
  54. #Language: English
  55.  
  56.  
  57. #*** START OF THIS PROJECT GUTENBERG EBOOK ALICE'S ADVENTURES IN WONDERLAND ***
  58. #{'P': 1968, 'Z': 80, 'S': 7268, 'M': 2467, 'N': 8051, 'Q': 220, 'F': 2382, 'V': 963, 'W': 2952, 'C': 3000, 'T': 12200, 'B': 1746, 'K': #1290, 'E': 15395, 'A': 9802, 'Y': 2584, 'G': 2943, 'J': 235, 'L': 5211, 'I': 8633, 'R': 6610, 'X': 176, 'D': 5469, 'O': 9477, 'H': #7889, 'U': 3978}
  59. #(0.12517379603053932, 'E')
  60. #(0.09919586304466253, 'T')
  61. #(0.07969818439047395, 'A')
  62. #(0.0770556716454317, 'O')
  63. #(0.07019326931676817, 'I')
  64. #(0.06546113880103098, 'N')
  65. #(0.06414394783273301, 'H')
  66. #(0.05909471578759076, 'S')
  67. #(0.05374464382993601, 'R')
  68. #(0.04446739139272618, 'D')
  69. #(0.042369642813584955, 'L')
  70. #(0.03234435599931701, 'U')
  71. #(0.024392425338851442, 'C')
  72. #(0.02400214653342982, 'W')
  73. #(0.023928969257413266, 'G')
  74. #(0.021010009025197375, 'Y')
  75. #(0.02005870443698217, 'M')
  76. #(0.019367585719048046, 'F')
  77. #(0.016001431022286548, 'P')
  78. #(0.01419639154721154, 'B')
  79. #(0.01048874289570612, 'K')
  80. #(0.007829968533771313, 'V')
  81. #(0.0019107399848766963, 'J')
  82. #(0.001788777858182439, 'Q')
  83. #(0.0014310222865459514, 'X')
  84. #(0.0006504646757027052, 'Z')
  85.  
  86. #https://drive.google.com/file/d/0By4QogT9U6sTbm5pamhQdFRDcDQ/view?usp=sharing
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement