foiaghost/examples/fruits.py
2023-07-13 16:54:13 -05:00

62 lines
1.2 KiB
Python

from beakers.recipe import Recipe
import pydantic
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)
recipe.add_conditional(
"classified_word",
lambda cw: cw.is_fruit,
"fruits",
)
recipe.add_transform(
"fruits",
"sentence",
lambda x: Sentence(sentence=f"I love a fresh {x.normalized_word}".split()),
)
recipe.add_seed(
"word",
[
Word(word="apple"),
Word(word="bAnAnA"),
Word(word="hammer"),
Word(word="orange"),
Word(word="EGG"),
],
)