Compare commits

..

1 Commits

Author SHA1 Message Date
gabriel becker 72c9dae6b4 Make available translation instead of providing answers. 2 years ago
  1. 5
      requirements.txt
  2. 8
      src/ankimaker/config/configuration.py
  3. 2
      src/ankimaker/generator/__init__.py
  4. 17
      src/ankimaker/generator/question_answer_generator.py
  5. 28
      src/ankimaker/generator/translator_generator.py
  6. 21
      src/ankimaker/tasks/basic_csv_to_anki.py

5
requirements.txt

@ -1,5 +1,6 @@
click
genanki
pandas
pandas==1.5.2
pyyaml
bullet
bullet
translate

8
src/ankimaker/config/configuration.py

@ -12,16 +12,20 @@ class AnkimakerConfig(yaml.YAMLObject):
question_column = None
answer_column = None
separators = ','
input_language = None,
output_language = None,
filters: List[List[FilterConfig]] = list()
def __init__(
self, separators=',', header=None, answer_column=None, question_column=None,
filters=tuple(), *args, **karhs
filters=tuple(), input_language=None, output_language=None, *args, **karhs
):
self.answer_column = answer_column
self.question_column = question_column
self.header = header
self.separators = separators
self.input_language = input_language
self.output_language = output_language
self.filters = _conditionally_create_new_filters(filters)
@staticmethod
@ -34,6 +38,8 @@ class AnkimakerConfig(yaml.YAMLObject):
AnkimakerConfig.question_column = content.question_column
AnkimakerConfig.answer_column = content.answer_column
AnkimakerConfig.separators = content.separators
AnkimakerConfig.input_language = content.input_language
AnkimakerConfig.output_language = content.output_language
AnkimakerConfig.filters = _conditionally_create_new_filters(content.filters)

2
src/ankimaker/generator/__init__.py

@ -3,3 +3,5 @@ from . import (
)
from .card import create_note
from .model import create_model
from .translator_generator import TranslatorGenerator
from .question_answer_generator import QuestionAnswerGenerator

17
src/ankimaker/generator/question_answer_generator.py

@ -0,0 +1,17 @@
import genanki
from typing import Collection, List
from ankimaker import generator
class QuestionAnswerGenerator:
def __init__(self):
self.__model = generator.create_model()
def get_cards(self, questions: Collection[str], answers: Collection[str]) -> List[genanki.Model]:
assert len(questions) == len(answers)
cards = list()
for content_fields in zip(questions, answers):
card = generator.create_note(self.__model, fields=content_fields)
cards.append(card)
return cards

28
src/ankimaker/generator/translator_generator.py

@ -0,0 +1,28 @@
import genanki
from translate import Translator
from typing import Collection, List
from ankimaker import generator
class TranslatorGenerator:
def __init__(self, original_language, destination_language):
"""
:param original_language: Language of the inserted text, following https://en.wikipedia.org/wiki/ISO_639-1
:param destination_language: Language you want to translate to, following https://en.wikipedia.org/wiki/ISO_639-1
"""
self.__translator = Translator(from_lang=original_language, to_lang=destination_language)
self.__model = generator.model.create_model()
def get_cards(self, content_collection: Collection[str]) -> List[genanki.Model]:
cards = list()
for content in content_collection:
card = self._create_card(content)
cards.append(card)
return cards
def _create_card(self, content):
translation = self.__translator.translate(content)
fields = (content, translation)
card = generator.create_note(self.__model, fields)
return card

21
src/ankimaker/tasks/basic_csv_to_anki.py

@ -18,14 +18,19 @@ def load_csv(path: str) -> pd.DataFrame:
def add_df_to_deck(df: pd.DataFrame, deck: genanki.Deck) -> genanki.Deck:
model = generator.create_model()
for entry in df.to_dict('records'):
question = entry[Config.question_column]
answer = entry[Config.answer_column]
content_fields = (question, answer)
note = generator.create_note(model, fields=content_fields)
deck.add_note(note)
questions = df[Config.question_column].to_list()
if Config.answer_column is None:
generator_engine = generator.TranslatorGenerator(
original_language=Config.input_language,
destination_language=Config.output_language,
)
cards = generator_engine.get_cards(questions)
else:
answers = df[Config.answer_column]
generator_engine = generator.QuestionAnswerGenerator()
cards = generator_engine.get_cards(questions, answers)
for card in cards:
deck.add_note(card)
return deck

Loading…
Cancel
Save