def __init__(self): self.user = User() self.hsk = HskVocab() self.cat = Category() self.hlp = HanziLevelProject() self.cedict = Cedict() self.sentence = SpoonFed()
def __init__(self): self.user = User() self.logger = Logger('anki.log.yaml') self.to_unsuspend = [] for sentence in self.user.load_user_sentence(): self.to_unsuspend.append(sentence) with Logger('anki.log.yaml') as log: log_dict = log.load() self.to_unsuspend = [item for item in self.to_unsuspend if item not in log_dict.get('to_unsuspend', [])] self.logger.save(to_unsuspend=self.to_unsuspend)
def __init__(self): self.user = User() self.ref = { 'hsk': HskVocab(), 'category': Category(), 'hlp': HanziLevelProject(), 'dict': Cedict(), 'sentence': SpoonFed() } self.vocabs = [] used_vocab = [] # for vocab in self.user.sentence_to_vocab(): for vocab in self.user.load_user_vocab(): # for vocab in set(chain(self.user.sentence_to_vocab(), self.user.load_user_vocab())): lookup = self.ref['dict'].get(vocab) simplified = lookup.get('simplified', '') vocab = simplified if simplified else vocab if vocab not in used_vocab: used_vocab.append(vocab) if re.search(r'[\u2E80-\u2FD5\u3400-\u4DBF\u4E00-\u9FCC]', vocab): self.vocabs.append((vocab, self.to_note(vocab))) else: print('Skipped', vocab) self.sentences = [] for sentence in self.user.load_user_sentence(): self.sentences.append((sentence, self.to_tags(sentence, 'sentence'))) hanzi_list = list(set(re.findall(r'[\u2E80-\u2FD5\u3400-\u4DBF\u4E00-\u9FCC]', ''.join([entry for entry, content in chain(self.vocabs, self.sentences)])))) self.hanzi = [(hanzi, self.to_tags(hanzi, 'hanzi')) for hanzi in hanzi_list] self.tags = { 'hanzi': [], 'vocab': [], 'sentence': [] } for _, note in self.vocabs: self.tags['vocab'].extend(note['tags']) for _, tags in self.sentences: self.tags['sentence'].extend(tags) for _, tags in self.hanzi: self.tags['hanzi'].extend(tags)
def __init__(self): self.user = User() self.hsk = HskVocab() self.cat = Category() self.hlp = HanziLevelProject() self.cedict = Cedict() self.sentence = SpoonFed() self.to_unsuspend = list() self.to_add = list() all_vocabs = chain(self.user.sentence_to_vocab(), self.user.load_user_vocab()) used_vocab = [] for i, vocab in enumerate(all_vocabs): lookup = self.cedict.get(vocab) simplified = lookup.get('simplified', '') vocab = simplified if simplified else vocab if vocab not in used_vocab: used_vocab.append(vocab) if re.search(r'[\u2E80-\u2FD5\u3400-\u4DBF\u4E00-\u9FCC]', vocab): if self.hsk.what_level(vocab): self.to_unsuspend.append(vocab) else: self.to_add.append(vocab) else: print("Skipped:", vocab) with Logger('anki.log.yaml') as log: log_dict = log.load() self.to_add = [item for item in self.to_add if item not in log_dict.get('to_add', [])] self.to_unsuspend = [item for item in self.to_unsuspend if item not in log_dict.get('to_unsuspend', [])] log.save(**{ 'to_add': self.to_add, 'to_unsuspend': self.to_unsuspend })
class Sentence: def __init__(self): self.user = User() self.logger = Logger('anki.log.yaml') self.to_unsuspend = [] for sentence in self.user.load_user_sentence(): self.to_unsuspend.append(sentence) with Logger('anki.log.yaml') as log: log_dict = log.load() self.to_unsuspend = [item for item in self.to_unsuspend if item not in log_dict.get('to_unsuspend', [])] self.logger.save(to_unsuspend=self.to_unsuspend) def unsuspend_to_query(self): for i in range(0, len(self.to_unsuspend), 50): try: print(' OR '.join(['Hanzi:{0}'.format(item) for item in self.to_unsuspend[i: i+50]])) except IndexError: print(' OR '.join(['Hanzi:{0}'.format(item) for item in self.to_unsuspend[i:]]))
class Vocab: def __init__(self): self.user = User() self.hsk = HskVocab() self.cat = Category() self.hlp = HanziLevelProject() self.cedict = Cedict() self.sentence = SpoonFed() self.to_unsuspend = list() self.to_add = list() all_vocabs = chain(self.user.sentence_to_vocab(), self.user.load_user_vocab()) used_vocab = [] for i, vocab in enumerate(all_vocabs): lookup = self.cedict.get(vocab) simplified = lookup.get('simplified', '') vocab = simplified if simplified else vocab if vocab not in used_vocab: used_vocab.append(vocab) if re.search(r'[\u2E80-\u2FD5\u3400-\u4DBF\u4E00-\u9FCC]', vocab): if self.hsk.what_level(vocab): self.to_unsuspend.append(vocab) else: self.to_add.append(vocab) else: print("Skipped:", vocab) with Logger('anki.log.yaml') as log: log_dict = log.load() self.to_add = [item for item in self.to_add if item not in log_dict.get('to_add', [])] self.to_unsuspend = [item for item in self.to_unsuspend if item not in log_dict.get('to_unsuspend', [])] log.save(**{ 'to_add': self.to_add, 'to_unsuspend': self.to_unsuspend }) def to_tsv(self, filename): ''' modelName: "Chinese Custom Vocab" ''' fields_names = ['Simplified', 'Traditional', 'Pinyin', 'English', 'Hanzi level', 'Note', 'tags'] with open(filename, 'w') as f: for vocab in self.to_add: tags = [] hanzi_level = '' level = self.hsk.what_level(vocab) if level: tags.append(level) lookup = self.cedict.get(vocab) simplified = lookup.get('simplified', '') traditional = lookup.get('traditional', '') vocab = simplified if simplified else vocab level = self.hsk.what_level(vocab) for category in self.cat.what_category(vocab, 'vocab'): tags.append(category) hanzi_level_and_category = self.hlp.vocab_to_level_and_category(vocab) if hanzi_level_and_category: tags.extend(hanzi_level_and_category) hanzi_level = re.findall(r'\d+', hanzi_level_and_category[0])[0] if level: tags.append(level) fields = [ vocab, traditional if simplified != traditional else '', lookup.get('reading', ''), lookup.get('english', ''), hanzi_level, self.sentence.formatted_lookup(vocab), ' '.join(tags) ] assert len(fields_names) == len(fields), "Please recheck if the number and the order of the fields" f.write('\t'.join(fields) + '\n') def unsuspend_to_query(self): for i in range(0, len(self.to_unsuspend), 50): try: print(' OR '.join(['Simplified:{0} OR Traditional:{0}'.format(item) for item in self.to_unsuspend[i: i+50]])) except IndexError: print(' OR '.join(['Simplified:{0} OR Traditional:{0}'.format(item) for item in self.to_unsuspend[i:]]))
class AnkiConnect(): def __init__(self): self.user = User() self.hsk = HskVocab() self.cat = Category() self.hlp = HanziLevelProject() self.cedict = Cedict() self.sentence = SpoonFed() def do_vocab(self): to_unsuspend = [] to_add = [] all_vocabs = set( chain(self.user.sentence_to_vocab(), self.user.load_user_vocab())) try: for i, vocab in enumerate(all_vocabs): print("Submitting entry: {} of {}".format( i + 1, len(all_vocabs))) if re.search(r'[\u2E80-\u2FD5\u3400-\u4DBF\u4E00-\u9FCC]', vocab): if self.hsk.what_level(vocab): print("To unsuspend", vocab) to_unsuspend.extend([ int(x) for x in ac.POST( 'findCards', params={ 'query': "(Simplified:{0} OR Traditional:{0})". format(vocab) })['result'] ]) else: print("Queuing:", vocab) to_add.append(self.to_note(vocab)) else: print("Skipped:", vocab) except KeyboardInterrupt: pass print('Unsuspend count:', len(to_unsuspend)) print('Unsuspending:', to_unsuspend) print(ac.POST('unsuspend', params={'cards': to_unsuspend})) print('Attempted add note count:', len(to_add)) print('Checking if can add notes.') can_add = [] for i, canAddNote in enumerate( ac.POST('canAddNotes', params={'notes': to_add})['result']): if canAddNote: can_add.append(to_add[i]) print('Addable note count:', len(can_add)) print('Adding notes.') print(ac.POST('addNotes', params={'notes': can_add})) print('Changing decks:') self.change_decks() def to_note(self, vocab): tags = [] deck_name = 'Default' hanzi_level = '' level = self.hsk.what_level(vocab) if level: tags.append(level) for category in self.cat.what_category(vocab): tags.append(category) hanzi_level_and_category = self.hlp.vocab_to_level_and_category(vocab) if hanzi_level_and_category: tags.extend(hanzi_level_and_category) hanzi_level = re.findall(r'\d+', hanzi_level_and_category[0])[0] lookup = self.cedict.get(vocab) return { 'deckName': deck_name, 'modelName': "Chinese Custom Vocab", 'fields': { 'Simplified': lookup.get('Simplified', '') if lookup.get('Simplified', '') else vocab, 'Traditional': lookup.get('traditional', ''), 'Pinyin': lookup.get('reading', ''), 'English': lookup.get('english', ''), 'Hanzi level': hanzi_level, 'Note': self.sentence.formatted_lookup(vocab) }, 'tags': tags, } def change_decks(self): cards = ['Simplified', 'Traditional', 'English'] for card, deck in cards: to_change = ac.POST( 'findCards', params={'query': "added:1 card:{}".format(card)})['result'] print( ac.POST('changeDeck', params={ 'cards': to_change, 'deck': 'Chinese::Vocab Extra::{}'.format(card) }))
class Analysis: def __init__(self): self.user = User() self.ref = { 'hsk': HskVocab(), 'category': Category(), 'hlp': HanziLevelProject(), 'dict': Cedict(), 'sentence': SpoonFed() } self.vocabs = [] used_vocab = [] # for vocab in self.user.sentence_to_vocab(): for vocab in self.user.load_user_vocab(): # for vocab in set(chain(self.user.sentence_to_vocab(), self.user.load_user_vocab())): lookup = self.ref['dict'].get(vocab) simplified = lookup.get('simplified', '') vocab = simplified if simplified else vocab if vocab not in used_vocab: used_vocab.append(vocab) if re.search(r'[\u2E80-\u2FD5\u3400-\u4DBF\u4E00-\u9FCC]', vocab): self.vocabs.append((vocab, self.to_note(vocab))) else: print('Skipped', vocab) self.sentences = [] for sentence in self.user.load_user_sentence(): self.sentences.append((sentence, self.to_tags(sentence, 'sentence'))) hanzi_list = list(set(re.findall(r'[\u2E80-\u2FD5\u3400-\u4DBF\u4E00-\u9FCC]', ''.join([entry for entry, content in chain(self.vocabs, self.sentences)])))) self.hanzi = [(hanzi, self.to_tags(hanzi, 'hanzi')) for hanzi in hanzi_list] self.tags = { 'hanzi': [], 'vocab': [], 'sentence': [] } for _, note in self.vocabs: self.tags['vocab'].extend(note['tags']) for _, tags in self.sentences: self.tags['sentence'].extend(tags) for _, tags in self.hanzi: self.tags['hanzi'].extend(tags) def to_note(self, vocab): hanzi_level = '' hanzi_level_and_category = self.ref['hlp'].vocab_to_level_and_category(vocab) if hanzi_level_and_category: hanzi_level = re.findall(r'\d+', hanzi_level_and_category[0])[0] lookup = self.ref['dict'].get(vocab) simp = lookup.get('Simplified', '') trad = lookup.get('traditional', '') return { 'fields': { 'Simplified': simp if simp else vocab, 'Traditional': trad if trad != simp else '', 'Pinyin': lookup.get('reading', ''), 'English': lookup.get('english', ''), 'Hanzi level': hanzi_level, 'Note': self.ref['sentence'].formatted_lookup(vocab) }, 'tags': self.to_tags(vocab, 'vocab'), } def to_tags(self, entry, type_of_entry): tags = [] level = self.ref['hsk'].what_level(entry) if level: tags.append(level) for category in self.ref['category'].what_category(entry, type_of_entry): tags.append(category) hanzi_level_and_category = self.ref['hlp'].vocab_to_level_and_category(entry) if hanzi_level_and_category: tags.extend(hanzi_level_and_category) return tags