Skip to content

Instantly share code, notes, and snippets.

@khajavi
Last active November 4, 2024 05:12
Show Gist options
  • Select an option

  • Save khajavi/a9ee53a1bd01140bd2f89095281ceaca to your computer and use it in GitHub Desktop.

Select an option

Save khajavi/a9ee53a1bd01140bd2f89095281ceaca to your computer and use it in GitHub Desktop.

Revisions

  1. khajavi revised this gist Nov 4, 2024. No changes.
  2. khajavi revised this gist Nov 4, 2024. No changes.
  3. khajavi created this gist Nov 4, 2024.
    5 changes: 5 additions & 0 deletions example.yaml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    - sentence: "The weather is sort of chilly, but it's not too cold for a walk."
    explanation: "This sentence uses 'sort of' to mean it’s a bit chilly but not very cold."
    - sentence: "I sort of understand the concept, but I need more practice."
    explanation: "Here, 'sort of' implies partial understanding but a need for more clarity."

    55 changes: 55 additions & 0 deletions main.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    import genanki
    import yaml
    import glob
    import os

    # Define paths
    EXAMPLES_DIR = './examples/' # Directory where the example YAML files are stored
    OUTPUT_FILE = 'learn_english_by_example.apkg' # Output file name for the Anki package

    # Create a model (the format of the cards)
    model_id = 1607392320
    deck_name = "Learn English By Example"
    anki_model = genanki.Model(
    model_id,
    deck_name,
    fields=[
    {'name': 'Sentence'},
    {'name': 'Explanation'},
    ],
    templates=[
    {
    'name': 'Card 1',
    'qfmt': '{{Sentence}}',
    'afmt': '{{FrontSide}}<hr id="answer">{{Explanation}}',
    },
    ])

    # Create a new deck
    deck_id = 2059400111
    anki_deck = genanki.Deck(deck_id,deck_name)

    # Function to load examples from YAML files in the specified directory
    def load_examples_from_directory(directory):
    examples = []
    for filepath in glob.glob(os.path.join(directory, '*.yaml')):
    with open(filepath, 'r') as file:
    file_examples = yaml.safe_load(file)
    examples.extend(file_examples)
    return examples

    # Load all examples from files in the directory
    examples = load_examples_from_directory(EXAMPLES_DIR)

    # Add each example sentence and explanation as a card
    for example in examples:
    sentence, explanation = example['sentence'], example['explanation']
    note = genanki.Note(
    model=anki_model,
    fields=[sentence, explanation])
    anki_deck.add_note(note)

    # Create a package (this will save the deck as an .apkg file)
    genanki.Package(anki_deck).write_to_file(OUTPUT_FILE)

    print(f"Anki deck has been created and saved as '{OUTPUT_FILE}'.")
    15 changes: 15 additions & 0 deletions pyproject.toml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    [tool.poetry]
    name = "anki"
    version = "0.1.0"
    description = ""
    authors = ["Milad Khajavi <khajavi@gmail.com>"]
    readme = "README.md"

    [tool.poetry.dependencies]
    python = "^3.11"
    genanki = "^0.13.1"


    [build-system]
    requires = ["poetry-core"]
    build-backend = "poetry.core.masonry.api"