Last active
November 4, 2024 05:12
-
-
Save khajavi/a9ee53a1bd01140bd2f89095281ceaca to your computer and use it in GitHub Desktop.
Generate Anki Deck From Directory of Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- 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." | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}'.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[tool.poetry] | |
name = "anki" | |
version = "0.1.0" | |
description = "" | |
authors = ["Milad Khajavi <[email protected]>"] | |
readme = "README.md" | |
[tool.poetry.dependencies] | |
python = "^3.11" | |
genanki = "^0.13.1" | |
[build-system] | |
requires = ["poetry-core"] | |
build-backend = "poetry.core.masonry.api" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment