Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. import sys
  2.  
  3. def main():
  4. print('sys.argv', sys.argv)
  5. args = sys.argv[1:]
  6. if len(args) == 1:
  7. arg = args[0]
  8. words = word_array(arg)
  9. print(repr(words))
  10. else:
  11. sys.stderr.write("Expecting zero or one command-line argument. Got {!r}\n".format(args))
  12. raise sys.exit(1)
  13.  
  14. def word_array(line):
  15. words = []
  16. i = 0
  17. start_word_index = -1
  18. while True:
  19. if i >= len(line):
  20. break
  21. curr_is_space = line[i] == ' ' and (line[i-1] != '.') and (line[i-1] != ',') and (line[i-1] != '!') and (line[i-1] != '?')
  22. curr_is_letter = (line[i] != ' ') and (line[i] != '.') and (line[i] != ',') and (line[i] != '!') and (line[i] != '?')
  23. curr_is_puncuation = (line[i] == '.') or (line[i] == ',') or (line[i] == '!') or (line[i] == '?')
  24. if curr_is_letter and start_word_index == -1:
  25. start_word_index = i
  26. elif curr_is_space and start_word_index != -1:
  27. words.append(line[start_word_index:i])
  28. start_word_index = -1
  29. elif curr_is_puncuation and start_word_index != -1:
  30. words.append(line[start_word_index:(i)])
  31. start_word_index = -1
  32. i = i + 1
  33.  
  34. if start_word_index != -1:
  35. words.append(line[start_word_index:])
  36. return words
  37.  
  38. if __name__ == '__main__':
  39. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement