-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep.py
99 lines (76 loc) · 3.57 KB
/
step.py
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
import itertools
import re
from units import convert_celsius_to_fahrenheit, convert_fahrenheit_to_celsius, convert_imperial_to_metric, convert_metric_to_imperial, IMPERIAL_TO_METRIC, METRIC_TO_IMPERIAL
class Step:
"""
Class representing a step in the cookbook with annotations.
"""
def __init__(self, text, actions, main_action, ingredients, tools, parameters):
self.text = text
self.actions = actions
self.main_action = main_action
self.ingredients = ingredients
self.tools = tools
self.parameters = parameters
def get_actions(self):
return self.actions
def get_main_action(self):
return self.main_action
def get_ingredients(self):
return self.ingredients
def get_tools(self):
return self.tools
def get_time_parameters(self):
return self.parameters['time']
def get_temperature_parameters(self):
return self.parameters['temperature']
def convert_units(self, target_unit):
"""
Converts the units in this step to the target unit.
Args:
target_unit: Target unit. Valid units are 'METRIC' and 'IMPERIAL'.
"""
if target_unit != 'METRIC' and target_unit != 'IMPERIAL':
return
temperature_parameters = self.get_temperature_parameters()
to_match = '\d*\.?\d+°C' if target_unit == 'IMPERIAL' else '\d*\.?\d+°F'
f_temp = convert_celsius_to_fahrenheit if target_unit == 'IMPERIAL' else convert_fahrenheit_to_celsius
converted_parameters = []
for p in temperature_parameters:
while re.search(to_match, p):
temperature = float(re.search(to_match, p).group()[:-2])
converted, unit = f_temp(temperature)
p = re.sub(to_match, str(converted) + unit, p, 1)
converted_parameters.append(p)
self.parameters['temperature'] = converted_parameters
for original, converted in zip(temperature_parameters, converted_parameters):
self.text = self.text.replace(original, converted)
units = METRIC_TO_IMPERIAL if target_unit == 'IMPERIAL' else IMPERIAL_TO_METRIC
f = convert_metric_to_imperial if target_unit == 'IMPERIAL' else convert_imperial_to_metric
for unit in reversed(units.keys()):
expressions = [f' {unit}', unit]
for e in expressions:
matches = re.findall(f'\d*\.?\d+{e}', self.text)
for match in matches:
q_str = match.replace(e, '')
q = int(q_str) if q_str.isnumeric() else float(q_str)
converted_quantity, converted_measurement = f(q, unit)
self.text = self.text.replace(match, ' '.join(
[str(converted_quantity), converted_measurement]))
def translate_portion_size(self, ratio):
"""
Translates the ingredient portions in this step by a ratio.
Args:
ratio: Ratio to translate by.
"""
for unit in itertools.chain(reversed(METRIC_TO_IMPERIAL.keys()), reversed(IMPERIAL_TO_METRIC.keys())):
expressions = [f' {unit}', unit]
for e in expressions:
matches = re.findall(f'\d*\.?\d+{e}', self.text)
for match in matches:
q_str = match.replace(e, '')
q = int(q_str) if q_str.isnumeric() else float(q_str)
self.text = self.text.replace(match, ' '.join(
[str(q * ratio), unit]))
def __repr__(self):
return self.text