Skip to content
This repository has been archived by the owner on Aug 17, 2023. It is now read-only.

Commit

Permalink
add furigana without kanji functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewchenk committed Jun 11, 2022
1 parent 0237deb commit b715a49
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ To add furigana to multiple cards in bulk,
open the Anki Browser and press "Edit" > "Bulk-add furigana".
* **Toolbar button.**
You can replace the content of any selected field with furigana by pressing `` on the toolbar.
You can do the same with furigana without kanji by pressing `` on the toolbar.
* **Note type**.
A new built-in note type designed for studying Japanese sentences.
You can find it by going to `Tools` > `Manage Note Types` > `Add` > `Japanese sentences`.
Expand Down
7 changes: 7 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"skip_numbers": true,
"generate_on_note_add": true,
"skip_words": "人,一,二",
"skip_kanji": false,
"toolbar": {
"furigana_button": {
"enable": true,
Expand All @@ -28,10 +29,16 @@
"enable": true,
"shortcut": "Alt+u",
"text": ""
},
"furigana_no_kanji_button": {
"enable": true,
"shortcut": "Alt+i",
"text": ""
}
},
"context_menu": {
"generate_furigana": true,
"generate_furigana_no_kanji": true,
"to_katakana": true,
"to_hiragana": true
}
Expand Down
4 changes: 4 additions & 0 deletions config.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ note type.
* `furigana_suffix`.
If a field called "abc" exists, and another field called "abc
(furigana)" exists, they will be used as source and destination fields.
* `skip_kanji`.
Whether to include the kanji and brackets when filling destination fields with readings.
* `skip_numbers`.
Whether to add furigana to numbers or not.
* `skip_words`.
Expand All @@ -40,6 +42,8 @@ Toggles context menu actions.
You can enable or disable the following actions:
* `generate_furigana`.
Paste furigana for selection.
* `generate_furigana_no_kanji`
Paste furigana without kanji for selection.
* `to_katakana`.
Convert selection to katakana.
* `to_hiragana`.
Expand Down
6 changes: 5 additions & 1 deletion context_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from .helpers import config
from .mecab_controller import to_katakana, to_hiragana
from .reading import reading
from .reading import reading, reading_no_kanji


class ContextMenuAction(abc.ABC):
Expand Down Expand Up @@ -54,6 +54,10 @@ class GenerateFurigana(ContextMenuAction):
label = "Furigana for selection"
action = staticmethod(reading)

class GenerateFuriganaNoKanji(ContextMenuAction):
key = "generate_furigana_no_kanji"
label = "Furigana without kanji for selection"
action = staticmethod(reading_no_kanji)

class ToKatakana(ContextMenuAction):
key = "to_katakana"
Expand Down
20 changes: 19 additions & 1 deletion reading.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ def fill_destination(note: Note, src_field: str, dst_field: str) -> bool:

# grab source text and update note
if src_text := mw.col.media.strip(note[src_field]).strip():
note[dst_field] = reading(src_text)
if config.get('skip_kanji'):
note[dst_field] = reading_no_kanji(src_text)
else:
note[dst_field] = reading(src_text)
return True

return False
Expand Down Expand Up @@ -125,6 +128,21 @@ def reading(src_text: str) -> str:

return ''.join(substrings).strip()

def reading_no_kanji(src_text: str) -> str:
skip_words = get_skip_words() + get_skip_numbers()
substrings = []
for token in tokenize(src_text):
if isinstance(token, ParseableToken):
for out in mecab.translate(token):
substrings.append(
out.hiragana_reading
if out.katakana_reading and out.word not in skip_words and out.headword not in skip_words
else out.word
)
else:
substrings.append(token)

return ''.join(substrings).strip()

mecab = MecabController(verbose=True)

Expand Down
7 changes: 6 additions & 1 deletion toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from aqt.editor import Editor

from .helpers import *
from .reading import reading
from .reading import reading, reading_no_kanji


@dataclass(frozen=True)
Expand Down Expand Up @@ -66,6 +66,11 @@ def init():
on_press=clean_furigana,
tip='Clean furigana in the field',
),
BtnCfg(
id='furigana_no_kanji_button',
on_press=lambda expr: reading_no_kanji(clean_furigana(expr)),
tip='Generate furigana without kanji in the field'
)
)
for cfg in buttons:
if cfg.enabled:
Expand Down

0 comments on commit b715a49

Please sign in to comment.