Last active
July 10, 2024 07:26
-
-
Save virtalas/e4e8a56bfe48ccb9c5ed13cc181e6ba3 to your computer and use it in GitHub Desktop.
Import a Yomiwa (iOS) word list into Anki.
This file contains hidden or 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
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| # Explanation: | |
| # Export a word list from Yomiwa iOS app to a file, convert to a tab-separated file, and import to Anki. | |
| # Installation: | |
| # 1. pip install jamdict | |
| # 2. Follow jamdict's github page instructions to install the dictionary database. | |
| # Usage: | |
| # In Yomiwa, add words to a list. | |
| # 1. Add example sentences by adding the word first, then by pressing analyze text and starring it. | |
| # 2. Name the list 'anki' and choose 'export file' and move it to your computer. | |
| # 3. Run this script in the same folder. | |
| # 4. Import import_to_anki.txt into anki with tab-separated values setting. | |
| # The card type has to have the following fields: | |
| # kanji, reading, meaning, example sentence, pitch accent, tag | |
| # 5. In the rare case that the word was not found in the dictionary but you added an example sentence, it will be added as a numeric ID. You can then manually check what the word is and fix it. | |
| # 6. You can generate pitch accent graphs to the 'pitch accent' fields with some Anki plugin. | |
| import re | |
| import json | |
| from jamdict import Jamdict | |
| jam = Jamdict() | |
| to_write = '' | |
| word_count = 0 | |
| unfound_word_row = '' | |
| tag = 'Yomiwa' | |
| def finishRow(sentence = ''): | |
| # Add fields: Example sentence, pitch accent, tags | |
| # Assume there is already a tag before 'sentence' field to be added next. | |
| if len(sentence) == 0: | |
| return '\t' + '\t' + tag + '\n' | |
| else: | |
| return sentence + '\t' + '\t' + tag + '\n' | |
| with open('anki_list.ymwlist', 'r') as f: | |
| data_str = f.read().replace('\n', '') | |
| data_str = re.sub('\n', ' ', data_str) | |
| # print(data_str) | |
| data = json.loads(data_str)['IDs'] | |
| # print(data) | |
| # Newer versions of Yomiwa reverse the order. | |
| # Change order to oldest first. | |
| data.reverse() | |
| for index, entry in enumerate(data): | |
| if not isinstance(entry, int): | |
| # Skip over a non integer entry (= sentence) since it was handled together with the word. | |
| continue | |
| # Handle word. | |
| unfound_word_row = '' | |
| word_id = entry | |
| word = jam.lookup('id#' + str(word_id)) | |
| word_count += 1 | |
| if len(word.entries) == 0: | |
| # No definition found. | |
| unfound_word_row = str(word_id) + '\t\t\t' | |
| else: | |
| # Fill in word info. | |
| kana_form = word.entries[0].kana_forms[0] | |
| kanji_form = kana_form | |
| if len(word.entries[0].kanji_forms) > 0: | |
| kanji_form = word.entries[0].kanji_forms[0] | |
| senses = [] | |
| # Strip out | |
| # - things like '((noun (common) (futsuumeishi)))' | |
| # - change '/' to ', ' | |
| for s in word.entries[0].senses: | |
| s = str(s).replace('/', ', ') | |
| senses.append(re.sub(' [((](.*)[))]', '', s)) | |
| meanings = ', '.join(senses) | |
| to_write += str(kanji_form) + '\t' + str(kana_form) + '\t' + meanings + '\t' | |
| # Handle example sentence. | |
| if index + 1 == len(data): | |
| # Last word had no example sentence. | |
| to_write += finishRow() | |
| continue | |
| sentence = data[index + 1] | |
| if not isinstance(sentence, int): | |
| # Example sentence | |
| if len(unfound_word_row) > 0: | |
| # Add the unfound word with a sentence, so the word can be manually added. | |
| # Spot by a number in the first field. | |
| to_write += unfound_word_row | |
| sentence = sentence.replace('\n', '') | |
| to_write += finishRow(sentence) | |
| continue | |
| else: | |
| # There was no sentence, just finish this row and read the entry as a word. | |
| to_write += finishRow() | |
| f = open("import_to_anki.txt", "w") | |
| f.write(to_write) | |
| f.close() | |
| print(str(word_count) + ' words exported to import_to_anki.txt') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment