coding utf-8 from random import choice sample randint seed import pymo

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
# coding: utf-8
from random import choice, sample, randint, seed
import pymorphy2
from pytils.numeral import get_plural
import time
def load_list(fname):
return [x.strip() for x in open(fname).read().decode('utf-8').splitlines() if x.strip()]
COURSES = load_list('data/course.txt')
COMPONENTS = load_list('data/component.txt')
KITCHENS = load_list('data/kitchen.txt')
ACTIONS = (
u'Прожарить',
u'Отварить',
u'Растереть',
)
morph = pymorphy2.MorphAnalyzer()
def inflect(word, form):
items = word.split()
return ' '.join(morph.parse(x)[0].inflect(form).word for x in items)
def get_component(form):
return inflect(choice(COMPONENTS), form)
def random_amount():
num = randint(1, 12)
if num in (1, 2):
amount = randint(1, 5)
return u'%d шт.' % amount
elif num in (3, 4):
amount = randint(1, 3)
return get_plural(amount, u'стакан,стакана,стаканов')
else:
amount = randint(5, 70) * 10
return u'%d г.' % amount
def random_action():
return choice(ACTIONS)
def generate_step(com, alt_com=None):
if alt_com:
val = u'%s %s с %s' % (com.title(), random_action(), inflect(alt_com, {'ablt'}))
return val
else:
val = u'%s %s' % (random_action().title(), inflect(com, {'accs'}))
return val
def generate_course(key):
seed(key)
course_name = choice(COURSES)
components = sample(COMPONENTS, randint(5, 7))
content = []
for com in components:
content.append({
'name': com,
'amount': random_amount(),
})
name = u'%s из %s с %s' % (
course_name.title(),
inflect(components[0], {'gent'}),
inflect(components[1], {'gent'}),
)
algo = []
algo.append(generate_step(components[0], components[1]))
for com in components[2:]:
algo.append(generate_step(com))
return {
'name': name,
'kitchen': choice(KITCHENS),
'content': content,
'algo': algo
}
def main():
for x in xrange(3):
course = generate_course(str(time.time()))
print '---------- %s ----------' % course['name']
print course['kitchen']
print 'Состав:'
for item in course['content']:
print '%s - %s' % (item['name'], item['amount'])
print 'Рецепт:'
for count, step in enumerate(course['algo']):
print u'%d. %s' % (count + 1, step)
print
if __name__ == '__main__':
main()