Skip to content

Instantly share code, notes, and snippets.

@joeminicucci
Created December 6, 2014 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeminicucci/fd4c5234de3e206d8883 to your computer and use it in GitHub Desktop.
Save joeminicucci/fd4c5234de3e206d8883 to your computer and use it in GitHub Desktop.
autofill (chinese support)
# -*- coding: utf-8 -*-
#
# Copyright © 2013 Chris Hatch, <foonugget@gmail.com>
#
# License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html
#
from aqt.utils import showInfo
from anki.find import Finder
from edit_behavior_model import *
from edit_functions import *
from aqt import mw
from aqt.utils import showInfo
import re
from time import sleep
def no_html(txt):
return re.sub("<.*?>", "", txt)
def fill_sounds(collection, view_key):
if view_key == "deckBrowser":
return showInfo(u"First select one of your decks")
query_str = "deck:current"
d_scanned = 0
d_has_fields = 0
d_success = 0
d_failed = 0
notes = Finder(collection).findNotes(query_str)
mw.progress.start(immediate=True, min=0, max=len(notes))
for noteId in notes:
d_scanned += 1
note = collection.getNote(noteId)
note_dict = dict(note) # edit_function routines require a dict
if has_field(Transcription_fields, note_dict) and has_field(Hanzi_fields, note_dict):
d_has_fields += 1
msg_string = "<b>Processing:</b> %(hanzi)s<br><b>OK:</b> %(filled)d<br><b>Failed:</b> %(failed)d"% {"hanzi":cleanup(no_html(get_any(Hanzi_fields, note_dict))), "filled":d_success, "failed":d_failed}
mw.progress.update(label=msg_string, value=d_scanned)
#Update the transcription field
#If empty, transcribe from Hanzi
if get_any(Transcription_fields, note_dict) == "" :
t = colorize( transcribe( no_sound( get_any(Hanzi_fields, note_dict) ) ) )
#Hide the unaccented transcription in the field,
#to make searching easier
t = hide(t, no_tone(t))
set_all(Transcription_fields, note_dict, to = t )
#Otherwise colorize the pinyin
else:
t = colorize( accentuate_pinyin( separate_pinyin(no_color(get_any(Transcription_fields, note_dict) ) )))
t = hide(t, no_tone(t))
set_all(Transcription_fields, note_dict, to = t)
#Update Meaning field only if empty.
m = ""
if get_any(Meaning_fields, note_dict) == "" :
m = translate(get_any(Hanzi_fields, note_dict))
#If there's no mean word field, then add it here
if not has_field(Mean_Word_fields, note_dict):
_mw = get_mean_word(get_any(Hanzi_fields, note_dict))
if _mw:
m += "<br>Cl: "+_mw
#If there's no alt spelling field, then add it here
if not has_field(Alternate_fields, note_dict):
_mw = get_alternate_spellings(get_any(Hanzi_fields, note_dict))
if _mw:
m += "<br>Also written: "+_mw
set_all(Meaning_fields, note_dict, to = m)
d_success = d_success+1
# write back to note from dict and flush
for f in Transcription_fields:
if note_dict.has_key(f) and note_dict[f] <> note[f]:
note[f] = note_dict[f]
note.flush()
# write back to note from dict and flush
for f in Meaning_fields:
if note_dict.has_key(f) and note_dict[f] <> note[f]:
note[f] = note_dict[f]
note.flush()
mw.progress.finish()
msg_string = "Note: This is a hackaround. It is recommended you reinstall the Chinese Support Addon after use."
showInfo(msg_string)
def fill_all(collection, view_key):
# TODO: fill all - reading, pinyin, sounds, etc.
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment