Advertisement
Guest User

Untitled

a guest
Jul 28th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. import sys
  2. import string
  3.  
  4. def word_count():
  5. # We are going to count the occurences of all the words that appear in the book
  6. # Alice in Wonderland.
  7. #
  8. # Thus, for this exercise, you need to write a program that will tally
  9. # the occurences of all the words that appears in Alice in Wonderland serially.
  10. #
  11. # The text in Alice in Wonderland will be fed into this program line by line.
  12. # And you need to write a program that will take each line and do the following:
  13. # 1) Tokenize a line of text into string tokens, by white space
  14. # Example: "Hello, World!" will be converted into "Hello," and "World!"
  15. #
  16. # 2) Remove all punctuations
  17. # Example: "Hello," and "World!" will be converted to "Hello" and "World"
  18. #
  19. # 3) Convert all words into lowercases
  20. # Example "Hello" and "World" will be converted to "hello" and "world"
  21. #
  22. # Store the the number of times that a word appears in Alice in Wonderland
  23. # in the word_counts dictionary
  24.  
  25.  
  26. word_counts = {}
  27.  
  28. for line in sys.stdin:
  29. data = line.strip().split(" ")
  30.  
  31. for each in data:
  32. x = each.translate(string.maketrans("",""), string.punctuation)
  33. x = x.lower()
  34. if x in word_counts:
  35. word_counts[x] += 1
  36. else:
  37. word_counts[x] = 1
  38.  
  39. print word_counts
  40.  
  41. word_count()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement