Guest User

Untitled

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