View Single Post
Old Yesterday, 05:35 PM   #18
DJUNGELSKOG
Groupie
DJUNGELSKOG ought to be getting tired of karma fortunes by now.DJUNGELSKOG ought to be getting tired of karma fortunes by now.DJUNGELSKOG ought to be getting tired of karma fortunes by now.DJUNGELSKOG ought to be getting tired of karma fortunes by now.DJUNGELSKOG ought to be getting tired of karma fortunes by now.DJUNGELSKOG ought to be getting tired of karma fortunes by now.DJUNGELSKOG ought to be getting tired of karma fortunes by now.DJUNGELSKOG ought to be getting tired of karma fortunes by now.DJUNGELSKOG ought to be getting tired of karma fortunes by now.DJUNGELSKOG ought to be getting tired of karma fortunes by now.DJUNGELSKOG ought to be getting tired of karma fortunes by now.
 
DJUNGELSKOG's Avatar
 
Posts: 168
Karma: 1162312
Join Date: Nov 2023
Location: Belgium / Vlaanderen
Device: Kobo Libra 2 + Vivlio Light HD + (ancient) iPad Pro
Python is not my main language so it’s probably a very ugly solution but it works for me

Code:
import sqlite3
import requests
from pystardict import Dictionary

DATABASE_PATH = 'vocabulary_builder.sqlite3'
STARDICT_PATH = 'your_dictionary_file_here’
ANKI_CONNECT_URL = 'http://localhost:8765'
NOT_FOUND_FILE = 'not_found.txt'

def get_translation(word):
    my_dict = Dictionary(STARDICT_PATH)
    for key in my_dict.idx.keys():
        if key == word:
            return my_dict.dict[key]

    return None


def add_note_to_anki(word, translation, prev_context, next_context):
    note = {
        "deckName": "your_deck_name",
        "modelName": "Basic",
        "fields": {
            "Front": word,
            "Back": f"{prev_context}[{word}]{next_context}<br><br>{translation}"
        },
        "tags": []
    }
    payload = {
        "action": "addNote",
        "version": 6,
        "params": {
            "note": note
        }
    }
    response = requests.post(ANKI_CONNECT_URL, json=payload)
    return response.json()

conn = sqlite3.connect(DATABASE_PATH)
cursor = conn.cursor()

cursor.execute("SELECT word, prev_context, next_context FROM vocabulary")
rows = cursor.fetchall()
not_found = []

for row in rows:
    word, prev_context, next_context = row
    translation = get_translation(word)
    print("TRANSLATION")
    print(translation)
    if translation:
        result = add_note_to_anki(word, translation, prev_context, next_context)
    else:
        print("not found")
        print(word)
        not_found.append(word)

with open(NOT_FOUND_FILE, 'w') as file:
    for word in not_found:
        file.write(f"{word}\n")

conn.close()
This exports words from koreader’s vocabulary builder database to anki, along with its context and translation.

You need to have Anki Desktop app running, otherwise it wouldn’t work. Or you can throw the anki part away and export it to .md instead
DJUNGELSKOG is offline   Reply With Quote