Experiments with Knuth's 5,757 five letter words. https://charlesreid1.com/wiki/Five_Letter_Words

stats.py 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. """
  2. stats.py
  3. Compute statistics of the 5-letter word collection from the Stanford GraphBase.
  4. """
  5. from pprint import pprint
  6. from get_words import get_words
  7. if __name__=="__main__":
  8. # Store frequencies of letters and bigrams
  9. letters = {}
  10. bigrams = {}
  11. words = get_words()
  12. # Loop through each letter of each word
  13. for word in words:
  14. for c in word:
  15. if c in letters.keys():
  16. letters[c] += 1
  17. else:
  18. letters[c] = 1
  19. for i in range(len(word)-1):
  20. bigram = str(word[i]+word[i+1])
  21. if bigram in bigrams.keys():
  22. bigrams[bigram] += 1
  23. else:
  24. bigrams[bigram] = 1
  25. # To print out the dict in sorted order,
  26. # convert to a list of tuples.
  27. # This creates a list of (key,value) tuples,
  28. # and sorts them by value (itemgettr(1))
  29. import operator
  30. sorted_letters = sorted(letters.items(), key=operator.itemgetter(1))
  31. sorted_bigrams = sorted(bigrams.items(), key=operator.itemgetter(1))
  32. print("\n\n")
  33. pprint(sorted_letters)
  34. print("\n\n")
  35. pprint(sorted_bigrams)