diff --git a/requirements.txt b/requirements.txt index 35c4c73..82358f4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ click genanki -pandas +pandas==1.5.2 pyyaml -bullet \ No newline at end of file +bullet +translate \ No newline at end of file diff --git a/src/ankimaker/config/configuration.py b/src/ankimaker/config/configuration.py index 16125cf..ed32230 100644 --- a/src/ankimaker/config/configuration.py +++ b/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) diff --git a/src/ankimaker/generator/__init__.py b/src/ankimaker/generator/__init__.py index a25c744..26889fc 100644 --- a/src/ankimaker/generator/__init__.py +++ b/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 diff --git a/src/ankimaker/generator/question_answer_generator.py b/src/ankimaker/generator/question_answer_generator.py new file mode 100644 index 0000000..996f70d --- /dev/null +++ b/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 diff --git a/src/ankimaker/generator/translator_generator.py b/src/ankimaker/generator/translator_generator.py new file mode 100644 index 0000000..9fa9cab --- /dev/null +++ b/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 diff --git a/src/ankimaker/tasks/basic_csv_to_anki.py b/src/ankimaker/tasks/basic_csv_to_anki.py index 8700669..cb629f6 100644 --- a/src/ankimaker/tasks/basic_csv_to_anki.py +++ b/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