Skip to content

Commit

Permalink
Add Spanish
Browse files Browse the repository at this point in the history
  • Loading branch information
kinto-b committed Dec 26, 2024
1 parent c9b59d3 commit 698c700
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 24 deletions.
4 changes: 2 additions & 2 deletions karten/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ def card_prompt(word: str, lang: str) -> str:
return CardPrompt[lang.upper()].value + f"\n\nWord: {word}"


def card_collect(word: str, model: genai.GenerativeModel) -> Card:
def card_collect(word: str, lang: str, model: genai.GenerativeModel) -> Card:
"""Creates a card using Google LLM"""
prompt = card_prompt(word, "de")
prompt = card_prompt(word, lang)
response = model.generate_content(prompt)
try:
return json.loads(response.text)
Expand Down
23 changes: 11 additions & 12 deletions karten/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,31 @@ def cli(): # pylint: disable=missing-docstring

@cli.command()
@click.argument("word")
@option_lang
@option_key
@option_model
def card(word, key, model):
def card(word, lang, key, model):
"""Fetch and display JSON data for WORD"""
if not key:
click.echo("Error: API key must be provided.")
return
model = initialise_model(key, model)
try:
card = card_collect(word, model) # pylint: disable=redefined-outer-name
card = card_collect(word, lang, model) # pylint: disable=redefined-outer-name
click.echo(json.dumps(card, indent=2, ensure_ascii=False))
except CardError as e:
click.echo(e)


@cli.command()
@click.argument("words", nargs=-1)
@option_lang
@option_file
@option_key
@option_model
def deck(words, file, key, model):
def deck(words, lang, file, key, model):
"""Creates a csv of cards ready for import into Anki (or equivalent)"""
_create_deck(words, file, key, model)
_create_deck(words, lang, file, key, model)


@cli.command()
Expand All @@ -63,15 +65,13 @@ def kindle_deck(kindle_dir, file, lang, date_from, key, model):
the vocabulary lookups in language LANG from the kindle at KINDLE_DIR.
Note that currently only LANG='de' is supported.
"""

if lang != "de":
click.echo(f"Error: LANG='{lang}' not supported")

words = kindle_read(kindle_dir, lang, date_from)
_create_deck(words, file, key, model)
_create_deck(words, lang, file, key, model)


def _create_deck(words: Iterable[str], file: str, key: str, model: str) -> None:
def _create_deck(
words: Iterable[str], lang: str, file: str, key: str, model: str
) -> None:
"""Adds cards for new words to the deck at OUTPUT"""
append = os.path.exists(file)

Expand All @@ -80,8 +80,7 @@ def _create_deck(words: Iterable[str], file: str, key: str, model: str) -> None:
with click.progressbar(words) as progress:
for word in progress:
try:
card = card_collect(word, model) # pylint: disable=redefined-outer-name
deck.append(card_format(card))
deck.append(card_format(card_collect(word, lang, model)))
except CardError:
err.append(word)

Expand Down
3 changes: 2 additions & 1 deletion karten/cli_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def option_lang(func):
"""Word language"""
return click.option(
"--lang",
type=click.Choice(["de", "es"], case_sensitive=False),
default="de",
help="The ISO 639 code of the target language. Currently only 'de' supported.",
help="The ISO 639 code of the target language. Currently only 'de' and 'es' supported.",
)(func)
2 changes: 1 addition & 1 deletion karten/kindle.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def kindle_read(kindle_dir: str, lang: str, date_from: datetime) -> list[str]:
FROM words \
WHERE lang=? AND timestamp>?\
ORDER BY timestamp DESC;",
(lang, timestamp),
(lang.lower(), timestamp),
)

words = []
Expand Down
3 changes: 2 additions & 1 deletion karten/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def _prompt(lang, forms, examples):
- category: noun, verb, adjective, etc.
- definition: the meaning in English.
- forms: {forms}
- example: a few complex sentences using the word in {lang}.
- example: a few sentences using the word in {lang}.
These should should the usage with the different prepositions, cases, and tenses.
Include at least one complex example.
- reverse: the example sentences translated into English.
Expand All @@ -38,3 +38,4 @@ class CardPrompt(Enum):
"""Prompt templates for generating cards in a variety of languages"""

DE = _prompt(CONFIG["de"]["lang"], CONFIG["de"]["forms"], CONFIG["de"]["examples"])
ES = _prompt(CONFIG["es"]["lang"], CONFIG["es"]["forms"], CONFIG["es"]["examples"])
55 changes: 51 additions & 4 deletions karten/prompt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ de:
definition: ["to pick up"]
forms: ["hat abgeholt", "holte ab"]
example: [
"Ich hole dich am Bahnhof ab.",
"Er hat gesagt, er habe das Paket bei der Post abgeholt, ohne sich auszuweisen."
"Wenn ich mich nicht um die Kinder hätte kümmern müssen, hätte ich dich vom Bahnhof abholen können.",
"Er hat gesagt, er habe das Paket bei der Post abgeholt, ohne sich auszuweisen.",
]
reverse: [
"I will pick you up at the train station.",
"If I hadn't needed to take care of the children, I could have picked you up from the station",
"He said he picked the package up from the post office without showing ID."
]
-
Expand All @@ -35,4 +35,51 @@ de:
"After the brawl in the bar, which was triggered by a dispute \
over a football match, the police had to intervene to separate \
the angry partygoers.",
]
]
es:
lang: Spanish
forms: |
one of
+ the plural if it's a noun, (e.g. 'perros')
+ the present tense conjugations if it's a verb, OR
+ the comparative and superlative if it's an adjective (e.g. ['más rápido', 'el más rápido']).
examples:
-
word: caminar
category: verb
definition: ["to walk"]
present: ["camino", "caminas", "camina", "caminamos", "camináis", "caminan"]
example: [
"Ayer caminé por el parque durante una hora.",
"Después de la cena, siempre caminan juntos para relajarse.",
]
reverse: [
"Yesterday I walked through the park for an hour.",
"After dinner, they always walk together to relax."
]
-
word: el perro
category: noun
definition: ["dog"]
forms: ["los perros"]
example: [
"El perro ladró toda la noche.",
"Los perros corren rápidamente hacia el parque cuando ven la pelota.",
]
reverse: [
"The dog barked all night.",
"The dogs run quickly toward the park when they see the ball."
]
-
word: rápido
category: adjective
definition: ["fast", "quick"]
forms: ["más rápido", "el más rápido"]
example: [
"Este coche es más rápido que el anterior.",
"Es el corredor más rápido del equipo.",
]
reverse: [
"This car is faster than the previous one.",
"He is the fastest runner on the team."
]
6 changes: 3 additions & 3 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from karten.deck import deck_read, deck_write


def mock_card_collect(word, model): # pylint: disable=unused-argument
def mock_card_collect(word, lang, model): # pylint: disable=unused-argument
"""Overwrite the function which serves cards using the LLM"""
if word == "fail":
raise CardError("Fail!")
Expand Down Expand Up @@ -50,7 +50,7 @@ def helper_deck(self, file, words, expected, mock_card):
self.assertEqual(result.exit_code, 0)
mock_card.assert_called()

expected = [card_format(mock_card_collect(e, None)) for e in expected]
expected = [card_format(mock_card_collect(e, None, None)) for e in expected]
output = deck_read(file)
for e, o in zip(expected, output):
self.assertDictEqual(e, o)
Expand Down Expand Up @@ -103,7 +103,7 @@ def test_deck_file_append(self, mock_card):
with NamedTemporaryFile("w+", delete=False) as file:
fp = file.name
# Add some content to the file
deck_write([card_format(mock_card_collect("first", None))], file)
deck_write([card_format(mock_card_collect("first", None, None))], file)

words = ["some", "words"]
expected = ["first", "some", "words"]
Expand Down

0 comments on commit 698c700

Please sign in to comment.