Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- text = 'Привет! Мы {развлекаемся|отрываемся|веселимся}, а {ты|вы}?'
- from random import choice
- a = ['развлекаемся', 'отрываемся', 'веселимся']
- b = ['ты', 'вы']
- text = 'Привет! Мы {}, а {}?'.format(choice(a), choice(b))
- text_message = 'Hi! I play {guitar|the accordion|violin}, and {he|she} or {one|two|tree}?'
- def getRandomChoice(text):
- label_one = text.find('{')
- label_two = text.find('}')
- if label_one != -1:
- list_one = text[label_one + 1:label_two].split('|')
- text = text[:label_one] + choice(list_one) + text[label_two + 1:]
- label_tree = text.find('{')
- label_four = text.find('}')
- if label_tree != -1:
- list_two = text[label_tree + 1:label_four].split('|')
- text = text[:label_tree] + choice(list_two) + text[label_four + 1:]
- label_five = text.find('{')
- label_six = text.find('}')
- if label_five != -1:
- list_tree = text[label_five + 1:label_six].split('|')
- text = text[:label_five] + choice(list_tree) + text[label_six + 1:]
- return text
- print(getRandomChoice(text_message))
- import random
- import re
- text = 'Привет! Мы {развлекаемся|отрываемся|веселимся}, а {ты|вы}?'
- text1 = ''.join(item if not item.startswith('{')
- else random.choice(item.strip('{}').split('|'))
- for item in re.split(r'({.*?})', text))
- print(text1)
- # Привет! Мы отрываемся, а вы?
- print(re.split(r'({.*?})', text))
- # ['Привет! Мы ', '{развлекаемся|отрываемся|веселимся}', ', а ', '{ты|вы}', '?']
- for item in re.split(r'({.*?})', text):
- print(item if not item.startswith('{') else random.choice(item.strip('{}').split('|')))
- # Привет! Мы
- # развлекаемся
- # , а
- # вы
- # ?
- >>> import rstr # $ pip install rstr
- >>> regex = r'Привет! Мы (развлекаемся|отрываемся|веселимся), а (ты|вы)?'
- >>> rstr.xeger(regex)
- 'Привет! Мы развлекаемся, а вы?'
- >>> rstr.xeger(regex)
- 'Привет! Мы веселимся, а ты?'
Add Comment
Please, Sign In to add comment