def __init__(self): if AnkiWrapper.__instance != None: raise Exception( "ERROR. Usa getInstance() para no crear otra instancia.") else: AnkiWrapper.__instance = self self.col = Collection() self.cards = self.col.cards self.notes = self.col.notes self.cardsRaw = ankipandas.raw.get_table(self.col.db, "cards") self.notesRaw = ankipandas.raw.get_table(self.col.db, "notes") self.rutaBase = self.obtenerRutaBase() pd.options.mode.chained_assignment = None ankipandas.util.log.set_log_level('critical')
def get_learned_words(): col = Collection() is_spanish = col.cards['cdeck'] == 'Spanish' spanish = col.cards[is_spanish] is_due = spanish['cqueue'] == 'due' spanish_learned = spanish[is_due] spanish_learned.merge_notes(inplace=True) learned_words = [] for index, row in spanish_learned.iterrows(): word = row['nflds'][0] word = word.partition(' ')[0] # Only keep the main word learned_words.append(word) return set(learned_words)
def clean_tags(): # read configuration, if not exist, write new one collection_path = load_config_value( config_name=ANKI_CONFIG, message= "No config value is saved for ANKI COLLECTION PATH. Please insert one ", default=HOME_DIRECTORY + os.sep + 'AppData' + os.sep + 'Roaming' + os.sep + 'Anki2', config_key1='collection_path') print(collection_path) user_name = load_config_value( config_name=ANKI_CONFIG, message="No config value is saved for ANKI USER. Please insert one ", default='Default', config_key1='user_name') print(user_name) # open collection col = Collection(collection_path, user=user_name) cards = col.cards # choose which deck you want to clean decks = cards.list_decks() selected_index_of_deck = select_from_list("Select your deck: ", decks) # note IDs gotten e.g. from a query on cards deck_notes_ids = col.cards[cards.cdeck == decks[selected_index_of_deck]].nid.unique() # get a DataFrame of rows for these note IDs relevant_notes = col.notes.loc[deck_notes_ids] sel1_count = clean_html_tags_for_basic_cards( col, selection(relevant_notes, MODEL_BASIC)) sel2_count = clean_html_tags_for_basic_cards( col, selection(relevant_notes, MODEL_BASIC_AND_REVERTED)) count = add_to_count(sel1_count) + add_to_count(sel2_count) final_message(key="anki_remove_tags", manual_per_item_seconds=SAVING_PER_CARD, hours_of_programming=PROGRAMMING_HOURS, number_of_items=count)
from ankipandas import Collection col = Collection() n = col.notes # Notes as AnkiDataFrame # col.cards # Cards as AnkiDataFrame # col.revs
from ankipandas import Collection import networkx as nx import matplotlib.pyplot as plt col = Collection("/home/stephen/.local/share/Anki2/", user="******") decks = {} for card in col.cards.to_numpy(): if card[0] in decks: continue try: if card[13] == "Miscellaneous::English": continue else: idx = card[13].rindex("::") + 2 decks[card[0]] = '-'.join(card[13][idx:].split(" ")).lower() except ValueError: decks[card[0]] = card[13].lower() color_code, subjects, subjects_rank = {}, {}, {} subjects_count = {} with open("color-code.txt", "r") as f: lines = f.readlines() color_code['default'] = lines[0].split(',')[1].strip() for line in lines[1:]: d = line.split(',') for tag in d[2:]: color_code[tag.strip()] = d[1] subjects[tag.strip()] = d[0] G = nx.DiGraph()
def extract_kanji(): """This function is used to generate a csv file from the user's Anki profile so that they may more easily create the necessary txt file""" from ankipandas import Collection col = Collection() col.notes.to_csv('vocab.csv', encoding='utf-8-sig')
def main(no_kanjigrid, user): try: import fugashi tagger = fugashi.Tagger() EXTRA = True except ModuleNotFoundError: EXTRA = False __loc__ = os.path.abspath("") __loc__ = os.path.dirname(os.path.realpath(__file__)) DISREGARD_OLD_KNOWN = False ADD_NX_SUP = False CREATE_KANJIGRID = not no_kanjigrid COUNT_NEW_LEECHES = True write_to_file_text = "" col = Collection(user=user) notes = col.cards.merge_notes() path = __loc__ + "\\resources" kw_path = path + "\\.known_words.txt" if os.path.isfile(path + "\\.previous.txt"): with open(path + "\\.previous.txt", "r", encoding="utf-8") as file: print("Previous known words:") print(file.read()) print("_" * 50 + "\n" * 2) print("Current known words:") with open(path + "\\anki_cards.txt", "r") as file: card_list = file.read().splitlines() words = [] for cards in card_list: card, field = cards.split(":") field = int(field) selection = notes.query( f"nmodel == '{card}' and cqueue == 'due' " f"or nmodel == '{card}' and cqueue == 'suspended'" ) sellist = selection["nflds"].tolist() if COUNT_NEW_LEECHES: mask = notes.ntags.apply(lambda x: "leech" in x) leech_sel = notes[mask] sel = leech_sel.query(f"nmodel == '{card}' and cqueue == 'new'") sellist.extend(sel["nflds"].tolist()) print(f"card model {card} found:") write_to_file_text = write_to_file_text + f"card model {card} found:" + "\n" print(len(sellist)) write_to_file_text = write_to_file_text + str(len(sellist)) + "\n" for w in sellist: if not kana.is_single_kana(w[field - 1]): words.append(w[field - 1]) uniq_w = set(words) # for a better reprensation of what i actually known # it would probably be better to do this right before any processing # and not now which just inflates the numbers # 21.01 still unsure about this if EXTRA: extra = set() for w in uniq_w: w = kana.markup_book_html(w) tags = [ word.feature.lemma if word.feature.lemma else word.surface for word in tagger(w) ] tags = [ kana.clean_lemma(token) for token in tags if not kana.is_single_kana(token) ] tags = kana.get_unique_token_words(tags) extra.update(tags) uniq_w.update(extra) if not DISREGARD_OLD_KNOWN: if os.path.isfile(kw_path): with open(kw_path, "r", encoding="utf-8") as file: previous_known = file.read().splitlines() previous_known = [ word for word in previous_known if not kana.is_single_kana(word) and word ] uniq_w.update(previous_known) if ADD_NX_SUP: nx_sup = [] for i in range(1, 6): if os.path.isfile("n" + str(i) + ".txt"): # print(i) with open("n" + str(i) + ".txt", "r", encoding="utf-8") as file: # print(sum(1 for _ in file)) nx_sup.extend(list(file.read().split("\n"))) uniq_w.update(nx_sup) muniq = {w for w in kana.markup_known_words("\n".join(uniq_w)) if w != ""} muniq = list(muniq) muniq.sort() uniqK = kana.get_unique_kanji(muniq) print(f"found a total of {len(muniq)} words") print(f"with a total of {len(uniqK)} unique kanji") write_to_file_text = ( write_to_file_text + f"found a total of {len(muniq)} words" + "\n" ) write_to_file_text = ( write_to_file_text + f"with a total of {len(uniqK)} unique kanji" + "\n" ) with open(kw_path, "w", encoding="utf-8") as wr: wr.write("\n".join(muniq)) with open(path + "\\.previous.txt", "w", encoding="utf-8") as wr: wr.write(write_to_file_text) add_data = [ { "Date": current_date, "Time": current_time, "Words": len(muniq), "Kanji": len(uniqK), } ] if os.path.isfile(path + "\\.progress.csv"): prog_df = pd.read_csv(path + "\\.progress.csv", index_col=0) prog_df = prog_df.append(add_data, ignore_index=True, sort=False) prog_df.to_csv(path + "\\.progress.csv", index_label="Index") else: prog_df = pd.DataFrame(add_data) prog_df.to_csv(path + "\\.progress.csv", index_label="Index") if CREATE_KANJIGRID: kj.main()