foiaghost/examples/fruits.py

58 lines
1.2 KiB
Python
Raw Normal View History

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-11 23:49:23 +00:00
recipe.add_transform("fruits", "sentence", lambda x: f"I love a fresh {x['word']}")
recipe.add_seed(
"word",
[
Word(word="apple"),
Word(word="banana"),
Word(word="hammer"),
Word(word="orange"),
Word(word="egg"),
],
2023-05-08 03:55:02 +00:00
)