Skip to content
Yoshiki Schmitz edited this page Mar 31, 2014 · 15 revisions

About this feature

Learning Chinese is a long and personalized process, and there is no single way to do it right. In order to help learners make the best of Chinese Pinyin Add-on, you can customize the way notes are entered.

For example, the default behavior is to have a single transcription field. In order to compare multiple transcriptions, you may have created a "Bopomofo" field on your notes model. You can now ask Chinese Support Add-on to automatically fill it up as you input new cards. (Please note that you will also need to modify the card models for new fields to be displayed).

Note: this is and advanced customization feature not needed for most users. Even though great care has been taken to make it as straightforward and failsafe as possible, it is recommended that you have some prior programming experience before using it.

In case you get stuck, you can safely delete file addons/chinese/edit_behavior.py from your Anki directory, and a default version of this file will be re-created the next time you run Anki. Therefore, it is a wise idea to make backups of your working customizations.

How to modify the Edit Behavior

You can access the file directly from the Tools->Add-ons->Chinese support->Editor behavior.

Function update_fields is called each time you leave an input field in the note editor window. It can then be used to update other fields. Note that if you update a non-existent field, it will simply be ignored.

The following functions are provided for your convenience. If they are not sufficient, you can also write your own. Check out the "addons/chinese/edit_functions.py" file in your Anki directory to see all available functions.

Helper functions

get_any(fields_list, fields_dictionary):

Searches through a list of field names, and returns the contents of the 1st one that exists.

set_all(fields_list, fields_dictionary, to):

Searches through a list of field names, and set all the ones that exist.

has_field(dico, fields):

Check if one of the named fields exists in the field list

colorize(text, ruby_whole=True):

Add tone color info.
(can be seen in the card preview, but not the note edit view).
Works on transcription, hanzi or ruby.

In the case of ruby, it will colorize only the annotation by default.
If ruby_whole = True, then it will colorize the whole character.

Warning : it's not recommended to use this function on hanzi directly,
since it cannot choose the correct color in the case of 
多音字 (characters with multiple pronunciations).

no_color(text):

Remove tone color info and other HTML pollutions.

accentuate_pinyin(text, force=False):

Add accents to pinyin. 
Eg: ni2 becomes ní.

Does nothing if the default transcription is not Pinyin,
unless force=True.
Nota : also removes coloring. If you want color, please add it last.

no_accents(text):

Remove accents.
Eg: ní becomes ni2.

ruby(text, transcription=None, only_one=False, try_dict_first=True):

Convert hanzi to ruby notation, eg: '你' becomes '你[nǐ]'.
This can in turn be used with the {{Ruby:fieldname}} card template,
to generate beautiful ruby-annotated cards.

If not specified, use the transcription type set in the menubar (eg pinyin).

if try_dict_first, looks up sequences of characters in the selected words dictionary
to supply a better transcription.

If not specified, insert all possible pinyin words for characters not found
in words dictionary.

ruby_top(text):

Extract the top (pronunciation) part of a ruby string.

ruby_bottom(txt):

Extract the bottom part of a ruby string.

hide(text, hidden)

Add hidden keyword to string (typically Hanzi and toneless pinyin),
to make a note searchable in the 'browse' window

hide_ruby(text)

Append hidden hanzi and toneless pinyin to a ruby string,
to make a note searchable in the 'browse' window.

no_tone(text):

Removes tone information and coloring.
Eg: 'ni3' becomes 'ni', 'má' becomes 'ma'

hanzi(text):

Returns just the anzi from a Ruby notation. 
Eg: '你[nǐ][You]' becomes '你'.

transcribe(text, transcription=None, only_one=True, try_dict_first=True):

Converts to specified transcription.
Eg : 你 becomes nǐ (transcription="Pinyin", only_one=True)

if try_tict_first is set and transcription is Pinyin or Bopomofo, 
then first try to lookup word in the selected dictionary. 
If it fails, or if there were multiple possible transcriptions, then 
look up each character one by one. 

For possible transcription choices, please see the Anki drop-down menu.
Tools->Add-ons->Chinese support->Set transcription

If no transcription is specified, use the transcription set in the menu.

In the case of a 多音字 (characters with multiple pronunciations), 
gives all possible pronunciations, unless only_one is set to False.
Eg : '了' becomes 'le liǎo' (transcription="Pinyin", only_one=False).

translate(text, from_lang="zh", to_lang=None, max_nb_lines=None):

Translate to a diferent language. 
Eg: '你好' becomes 'Hello'
Only installed dictionaries can be used.

to_lang possible values : en (English), de (German), fr (French)
if to_lang is unspecified, the default language will be used.

colorize_fuse(hanzi, pinyin):

Gives color to a Hanzi phrase based on the tone info from a 
corresponding Pinyin phrase.

Eg: "你好" and "ni3 hao3" ->  你好 (both colorized as 3rd tone).

pinyin(text):

Convert to Pinyin

mean_word(text):

Mean Word generation : not available yet.

sound(text, source=None):

Returns sound tag for a given Hanzi string.

If the sound does not already exist in the media directory, then
attempt to obtain it from the specified source.
if the specified source is omitted, use the one selected in the
tools menu.
If it fails (eg: no network connexion while trying to retrieve 
speech from Google TTS), return nothing.

Does not work with pinyin or other transcriptions.

no_sound(text):

Remove the [sound:xxx.mp3] tag that's added by Anki when you record a sound into a field.
If you don't remove it before taking data from one field to another, it will likely be 
duplicated, and play twice in a row.

separate_pinyin(text, force=False):

Separate pinyin syllables with whitespaces.
Eg: "Yīlù píng'ān" becomes "Yī lù píng ān"

Does nothing if the default transcription is not pinyin, 
unless Force=True
Useful for people pasting Pinyin from Google Translate.
Clone this wiki locally