2023-05-08 03:55:02 +00:00
|
|
|
from beakers.recipe import Recipe
|
2023-07-11 23:49:23 +00:00
|
|
|
import pydantic
|
2023-05-08 03:55:02 +00:00
|
|
|
|
2023-07-11 23:49:23 +00:00
|
|
|
|
|
|
|
class Word(pydantic.BaseModel):
|
|
|
|
word: str
|
|
|
|
|
|
|
|
|
|
|
|
class ClassifiedWord(pydantic.BaseModel):
|
|
|
|
normalized_word: str
|
|
|
|
is_fruit: bool
|
|
|
|
|
|
|
|
|
|
|
|
class Sentence(pydantic.BaseModel):
|
|
|
|
sentence: list[str]
|
|
|
|
|
|
|
|
|
|
|
|
def word_classifier(item) -> ClassifiedWord:
|
|
|
|
return ClassifiedWord(
|
|
|
|
normalized_word=item.word.lower(),
|
|
|
|
is_fruit=item.word.lower()
|
|
|
|
in (
|
|
|
|
"apple",
|
|
|
|
"banana",
|
|
|
|
"fig",
|
|
|
|
"grape",
|
|
|
|
"lemon",
|
|
|
|
"mango",
|
|
|
|
"orange",
|
|
|
|
"pear",
|
|
|
|
"raspberry",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
recipe = Recipe("fruits-example")
|
|
|
|
recipe.add_beaker("word", Word)
|
|
|
|
recipe.add_beaker("classified_word", ClassifiedWord)
|
|
|
|
recipe.add_beaker("sentence", Sentence)
|
|
|
|
recipe.add_transform("word", "classified_word", word_classifier)
|
2023-05-08 03:55:02 +00:00
|
|
|
recipe.add_conditional(
|
2023-07-11 23:49:23 +00:00
|
|
|
"classified_word",
|
|
|
|
lambda cw: cw.is_fruit,
|
2023-05-08 03:55:02 +00:00
|
|
|
"fruits",
|
|
|
|
)
|
2023-07-13 21:54:13 +00:00
|
|
|
recipe.add_transform(
|
|
|
|
"fruits",
|
|
|
|
"sentence",
|
|
|
|
lambda x: Sentence(sentence=f"I love a fresh {x.normalized_word}".split()),
|
|
|
|
)
|
2023-07-11 23:49:23 +00:00
|
|
|
|
|
|
|
recipe.add_seed(
|
|
|
|
"word",
|
|
|
|
[
|
|
|
|
Word(word="apple"),
|
2023-07-13 21:54:13 +00:00
|
|
|
Word(word="bAnAnA"),
|
2023-07-11 23:49:23 +00:00
|
|
|
Word(word="hammer"),
|
|
|
|
Word(word="orange"),
|
2023-07-13 21:54:13 +00:00
|
|
|
Word(word="EGG"),
|
2023-07-11 23:49:23 +00:00
|
|
|
],
|
2023-05-08 03:55:02 +00:00
|
|
|
)
|