-
Notifications
You must be signed in to change notification settings - Fork 0
/
kanjisentences.py
144 lines (127 loc) · 5.58 KB
/
kanjisentences.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# -*- coding: utf-8 -*-
import unicodedata
from aqt import mw
from aqt.utils import showInfo
from aqt.qt import *
_ignore = set(
u"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" +
u"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
u"abcdefghijklmnopqrstuvwxyz" +
u"12345678901234567890" +
u"あいうゔえおぁぃぅぇぉかきくけこがぎぐげごさしすせそざじずぜぞ" +
u"たちつてとだぢづでどなにぬねのはひふへほばびぶべぼぱぴぷぺぽ" +
u"まみむめもやゃゆゅよょらりるれろわをんっ" +
u"アイウヴエオァィゥェォカキクケコガギグゲゴサシスセソザジズゼゾ" +
u"タチツテトダヂヅデドナニヌネノハヒフヘホバビブベボパピプペポ" +
u"マミムメモヤャユュヨョラリルレロワヲンッ" +
u"!\"$%&'()|=~-^@[;:],./`{+*}<>?\\_" +
u"@「;:」、。・‘{+*}<>?\_!”#$%&’()|=.〜~ー" +
u"☆★*○●◎〇◯“…『』#♪゙〉〈→》《π×")
# returns true is the card is not new
def isKnownCard(card):
return card.type > 0
# returns true if a given character is kanji
def isKanji(ch):
if ch in _ignore:
return False
try:
return unicodedata.name(ch).find('CJK UNIFIED IDEOGRAPH') >= 0
except ValueError:
return False
# returns true if the card belongs to a model with 'japanese' in the name
def isJapaneseDeckCard(card):
model = card.note().model()
model_name = model['name']
return 'japanese' in model_name.lower()
class KanjiSentences:
def __init__(self, mw):
if mw:
self.suspendAction = QAction(
"Suspend sentences with unknown kanji", mw
)
self.unsuspendAction = QAction(
"Unsuspend sentences with known kanji", mw
)
mw.connect(
self.suspendAction, SIGNAL("triggered()"), self.suspendUnknown
)
mw.connect(
self.unsuspendAction, SIGNAL("triggered()"), self.unsuspendKnown
)
mw.form.menuTools.addSeparator()
mw.form.menuTools.addAction(self.suspendAction)
mw.form.menuTools.addAction(self.unsuspendAction)
def suspendUnknown(self):
knownKanji = self.knownKanji()
idsToSuspend = list()
cardIds = mw.col.db.list("select id from cards")
for id, i in enumerate(cardIds):
card = mw.col.getCard(i)
if card.queue >= 0 and isJapaneseDeckCard(card):
keys = card.note().keys()
expressionField = None
for s, key in ((key.lower(), key) for key in keys):
if s.strip().lower() == "expression":
expressionField = card.note()[key]
break
if expressionField is not None:
for ch in expressionField:
if ch in _ignore or not isKanji(ch):
continue
if ch not in knownKanji:
idsToSuspend.append(i)
break
mw.col.sched.suspendCards(idsToSuspend)
mw.reset()
showInfo("Suspended {num} cards!".format(num=len(idsToSuspend)))
def unsuspendKnown(self):
knownKanji = self.knownKanji()
idsToUnsuspend = list()
cardIds = mw.col.db.list("select id from cards")
for id, i in enumerate(cardIds):
card = mw.col.getCard(i)
if card.queue == -1 and isJapaneseDeckCard(card):
keys = card.note().keys()
expressionField = None
for s, key in ((key.lower(), key) for key in keys):
if s.strip().lower() == "expression":
expressionField = card.note()[key]
break
if expressionField is not None:
unsuspend = True
for ch in expressionField:
if ch in _ignore or not isKanji(ch):
continue
if ch not in knownKanji:
unsuspend = False
break
if unsuspend is True:
idsToUnsuspend.append(i)
mw.col.sched.unsuspendCards(idsToUnsuspend)
mw.reset()
showInfo("Unsuspended {num} cards!".format(num=len(idsToUnsuspend)))
def knownKanji(self):
knownKanji = set()
cardIds = mw.col.db.list("select id from cards")
for id, i in enumerate(cardIds):
card = mw.col.getCard(i)
if isKnownCard(card):
keys = card.note().keys()
kanjiField = None
for s, key in ((key.lower(), key) for key in keys):
if s.strip().lower() == "kanji":
kanjiField = card.note()[key]
break
if kanjiField is not None:
for kanji in kanjiField:
knownKanji.add(kanji)
return knownKanji
if __name__ != "__main__":
# Save a reference to the toolkit onto the mw, preventing garbage collection
# of PyQT objects
if mw:
mw.kanjiSentences = KanjiSentences(mw)
else:
print("This is a plugin for the Anki Spaced Repetition learning system and"
"cannot be run directly.")
print("Please download Anki2 from <http://ankisrs.net/>")