filtered automations to tokens present

This commit is contained in:
smilerz
2023-04-25 12:38:14 -05:00
parent 479cf1a042
commit 8411537f87

View File

@ -3,6 +3,7 @@ import string
import unicodedata import unicodedata
from django.core.cache import caches from django.core.cache import caches
from django.db.models import Q
from cookbook.models import Automation, Food, Ingredient, Unit from cookbook.models import Automation, Food, Ingredient, Unit
@ -59,7 +60,7 @@ class IngredientParser:
else: else:
i = 0 i = 0
for a in Automation.objects.filter(space=self.request.space, disabled=False, type=Automation.TRANSPOSE_WORDS).only('param_1', 'param_2').order_by('order').all(): for a in Automation.objects.filter(space=self.request.space, disabled=False, type=Automation.TRANSPOSE_WORDS).only('param_1', 'param_2').order_by('order').all():
self.never_unit[i] = [a.param_1, a.param_2] self.transpose_words[i] = [a.param_1, a.param_2]
i += 1 i += 1
caches['default'].set(TRANSPOSE_WORDS_CACHE_KEY, self.transpose_words, 30) caches['default'].set(TRANSPOSE_WORDS_CACHE_KEY, self.transpose_words, 30)
else: else:
@ -270,22 +271,21 @@ class IngredientParser:
return: new ingredient string return: new ingredient string
""" """
####################################################
####################################################
####################################################
####################################################
if self.ignore_rules: if self.ignore_rules:
return ingredient return ingredient
else: else:
tokens = ingredient.replace(',',' ').split()
if self.transpose_words: if self.transpose_words:
for rule in self.transpose_words: filtered_rules = {}
ingredient = re.sub(rf"\b({rule[0]}) ({rule[1]})\b", r"\2 \1", ingredient) for key, value in self.transpose_words.items():
if value[0] in tokens and value[1] in tokens:
filtered_rules[key] = value
for k, v in filtered_rules.items():
ingredient = re.sub(rf"\b({v[0]})\W*({v[1]})\b", r"\2 \1", ingredient)
else: else:
for rule in Automation.objects.filter(space=self.request.space, type=Automation.TRANSPOSE_WORDS, disabled=False).order_by('order'): for rule in Automation.objects.filter(space=self.request.space, type=Automation.TRANSPOSE_WORDS, disabled=False).filter(Q(Q(param_1__in=tokens) | Q(param_2__in=tokens))).order_by('order'):
ingredient = re.sub(rf"\b({rule.param_1}) ({rule.param_2})\b", r"\2 \1", ingredient) ingredient = re.sub(rf"\b({v[0]})\W*({v[1]})\b", r"\2 \1", ingredient)
return ingredient return ingredient
def parse(self, ingredient): def parse(self, ingredient):
@ -304,8 +304,6 @@ class IngredientParser:
if len(ingredient) == 0: if len(ingredient) == 0:
raise ValueError('string to parse cannot be empty') raise ValueError('string to parse cannot be empty')
ingredient = self.apply_transpose_words_automations(ingredient)
# some people/languages put amount and unit at the end of the ingredient string # some people/languages put amount and unit at the end of the ingredient string
# if something like this is detected move it to the beginning so the parser can handle it # if something like this is detected move it to the beginning so the parser can handle it
if len(ingredient) < 1000 and re.search(r'^([^\W\d_])+(.)*[1-9](\d)*\s*([^\W\d_])+', ingredient): if len(ingredient) < 1000 and re.search(r'^([^\W\d_])+(.)*[1-9](\d)*\s*([^\W\d_])+', ingredient):
@ -330,6 +328,8 @@ class IngredientParser:
if re.match('([0-9])+([A-z])+\s', ingredient): if re.match('([0-9])+([A-z])+\s', ingredient):
ingredient = re.sub(r'(?<=([a-z])|\d)(?=(?(1)\d|[a-z]))', ' ', ingredient) ingredient = re.sub(r'(?<=([a-z])|\d)(?=(?(1)\d|[a-z]))', ' ', ingredient)
ingredient = self.apply_transpose_words_automations(ingredient)
tokens = ingredient.split() # split at each space into tokens tokens = ingredient.split() # split at each space into tokens
if len(tokens) == 1: if len(tokens) == 1:
# there only is one argument, that must be the food # there only is one argument, that must be the food