Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. import string
  2.  
  3. # List of "unimportant" words (feel free to add more)
  4. skip_words = ['a', 'about', 'all', 'an', 'another', 'any', 'around', 'at',
  5. 'bad', 'beautiful', 'been', 'better', 'big', 'can', 'every', 'for',
  6. 'from', 'good', 'have', 'her', 'here', 'hers', 'his', 'how',
  7. 'i', 'if', 'in', 'into', 'is', 'it', 'its', 'large', 'later',
  8. 'like', 'little', 'main', 'me', 'mine', 'more', 'my', 'now',
  9. 'of', 'off', 'oh', 'on', 'please', 'small', 'some', 'soon',
  10. 'that', 'the', 'then', 'this', 'those', 'through', 'till', 'to',
  11. 'towards', 'until', 'us', 'want', 'we', 'what', 'when', 'why',
  12. 'wish', 'with', 'would']
  13.  
  14.  
  15. def filter_words(words, skip_words):
  16. """This function takes a list of words and returns a copy of the list from
  17. which all words provided in the list skip_words have been removed.
  18. For example:
  19.  
  20. >>> filter_words(["help", "me", "please"], ["me", "please"])
  21. ['help']
  22.  
  23. >>> filter_words(["go", "south"], skip_words)
  24. ['go', 'south']
  25.  
  26. >>> filter_words(['how', 'about', 'i', 'go', 'through', 'that', 'little',
  27. 'passage', 'to', 'the', 'south'], skip_words)
  28. ['go', 'passage', 'south']
  29.  
  30. """
  31.  
  32.  
  33. def remove_punct(text):
  34. """This function is used to remove all punctuation
  35. marks from a string. Spaces do not count as punctuation and should
  36. not be removed. The funcion takes a string and returns a new string
  37. which does not contain any puctuation. For example:
  38.  
  39. >>> remove_punct("Hello, World!")
  40. 'Hello World'
  41. >>> remove_punct("-- ...Hey! -- Yes?!...")
  42. ' Hey Yes'
  43. >>> remove_punct(",go!So.?uTh")
  44. 'goSouTh'
  45. """
  46. no_punct = ""
  47. for char in text:
  48. if not (char in string.punctuation):
  49. no_punct = no_punct + char
  50.  
  51. return no_punct
  52.  
  53.  
  54. def normalise_input(user_input):
  55. """This function removes all punctuation from the string and converts it to
  56. lower case. It then splits the string into a list of words (also removing
  57. any extra spaces between words) and further removes all "unimportant"
  58. words from the list of words using the filter_words() function. The
  59. resulting list of "important" words is returned. For example:
  60.  
  61. >>> normalise_input(" Go south! ")
  62. ['go', 'south']
  63. >>> normalise_input("!!! tAkE,. LAmp!?! ")
  64. ['take', 'lamp']
  65. >>> normalise_input("HELP!!!!!!!")
  66. ['help']
  67. >>> normalise_input("Now, drop the sword please.")
  68. ['drop', 'sword']
  69. >>> normalise_input("Kill ~ tHe :- gObLiN,. wiTH my SWORD!!!")
  70. ['kill', 'goblin', 'sword']
  71. >>> normalise_input("I would like to drop my laptop here.")
  72. ['drop', 'laptop']
  73. >>> normalise_input("I wish to take this large gem now!")
  74. ['take', 'gem']
  75. >>> normalise_input("How about I go through that little passage to the south...")
  76. ['go', 'passage', 'south']
  77.  
  78. """
  79.  
  80. no_punct = remove_punct((user_input).lower().split())
  81.  
  82. punctuation = '''!()-[]{};:'"\,<>./?@#$£%^&*_~'''
  83.  
  84. out_str = ""
  85.  
  86. for char in text:
  87. if char not in punctuations:
  88. out_str = out_str + char
  89. out_str2 = out_str.strip()
  90.  
  91. return out_str2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement