Guest User

Untitled

a guest
Apr 21st, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. text = 'Привет! Мы {развлекаемся|отрываемся|веселимся}, а {ты|вы}?'
  2.  
  3. import random
  4. import re
  5.  
  6. text = 'Привет! Мы {развлекаемся|отрываемся|веселимся}, а {ты|вы}?'
  7.  
  8. text1 = ''.join(item if not item.startswith('{')
  9. else random.choice(item.strip('{}').split('|'))
  10. for item in re.split(r'({.*?})', text))
  11.  
  12. print(text1)
  13. # Привет! Мы отрываемся, а вы?
  14.  
  15. print(re.split(r'({.*?})', text))
  16. # ['Привет! Мы ', '{развлекаемся|отрываемся|веселимся}', ', а ', '{ты|вы}', '?']
  17.  
  18. for item in re.split(r'({.*?})', text):
  19. print(item if not item.startswith('{') else random.choice(item.strip('{}').split('|')))
  20.  
  21. # Привет! Мы
  22. # развлекаемся
  23. # , а
  24. # вы
  25. # ?
  26.  
  27. >>> import rstr # $ pip install rstr
  28. >>> regex = r'Привет! Мы (развлекаемся|отрываемся|веселимся), а (ты|вы)?'
  29. >>> rstr.xeger(regex)
  30. 'Привет! Мы развлекаемся, а вы?'
  31. >>> rstr.xeger(regex)
  32. 'Привет! Мы веселимся, а ты?'
  33.  
  34. from random import choice
  35.  
  36. a = ['развлекаемся', 'отрываемся', 'веселимся']
  37. b = ['ты', 'вы']
  38.  
  39. text = 'Привет! Мы {}, а {}?'.format(choice(a), choice(b))
  40.  
  41. text_message = 'Hi! I play {guitar|the accordion|violin}, and {he|she} or {one|two|tree}?'
  42.  
  43. def getRandomChoice(text):
  44.  
  45.  
  46. label_one = text.find('{')
  47. label_two = text.find('}')
  48.  
  49. if label_one != -1:
  50. list_one = text[label_one + 1:label_two].split('|')
  51. text = text[:label_one] + choice(list_one) + text[label_two + 1:]
  52.  
  53. label_tree = text.find('{')
  54. label_four = text.find('}')
  55.  
  56. if label_tree != -1:
  57. list_two = text[label_tree + 1:label_four].split('|')
  58. text = text[:label_tree] + choice(list_two) + text[label_four + 1:]
  59.  
  60. label_five = text.find('{')
  61. label_six = text.find('}')
  62.  
  63. if label_five != -1:
  64. list_tree = text[label_five + 1:label_six].split('|')
  65. text = text[:label_five] + choice(list_tree) + text[label_six + 1:]
  66.  
  67. return text
  68.  
  69.  
  70. print(getRandomChoice(text_message))
Add Comment
Please, Sign In to add comment