-
Notifications
You must be signed in to change notification settings - Fork 0
/
ingredient.py
44 lines (34 loc) · 1.34 KB
/
ingredient.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
import editdistance
class Ingredient:
"""
Class representing an ingredient.
"""
COUNTABLE_MEASUREMENT = 'COUNTS'
NO_QUANTITY = -1
def __init__(self, name, quantity, measurement, descriptors):
self.name = name
self.quantity = quantity
self.measurement = measurement
self.descriptors = descriptors
def is_similar(self, str):
"""
Checks if this ingredient's name is similar to given string using edit distance.
Args:
str: String to check similarity with.
Returns:
True, if similar. False otherwise.
"""
contains_str = str in self.name
return editdistance.eval(str, self.name) < 2 or contains_str
def is_countable(self):
return self.measurement == Ingredient.COUNTABLE_MEASUREMENT
def has_no_quantity(self):
return self.quantity == Ingredient.NO_QUANTITY
def __repr__(self):
descriptors_str = ', ' + ' and '.join(
self.descriptors) if self.descriptors else ''
if self.quantity == Ingredient.NO_QUANTITY:
return f'{self.name}{descriptors_str}'
if self.measurement == Ingredient.COUNTABLE_MEASUREMENT:
return f'{self.quantity} {self.name}{descriptors_str}'
return f'{self.quantity} {self.measurement} of {self.name}{descriptors_str}'