Is there a clever way to count how many unique kanji appear in a deck?

I have over 16,000 cards. Many cards have sentences.

It seems like there should be some way to skim all the cards and get a total for the unique number of kanji that appear.

You might be able to do this with the Japanese Support add-on’s stats?

1 Like

Thanks

I checked it out, but it showed that I had 0 kanji in my deck of 16,000 cards.

Perhaps because they are all kanji compounds?

The add-on expects your note type to be in a standard format, eg to include ‘Japanese’ in the name, and to have text in a field called ‘Expression’.

1 Like

For anyone studying Japanese, you might like this. I asked CHAT GPT to rewrite the code. Now it outputs all the Kanji in the Anki deck, writes how many times that kanji appears, then lists them all in descending order.

import sys
import re

def main():
    # set input and output files
    input_file = "kanji.txt"
    output_file = "output.txt"
    
    # read input file
    with open(input_file, "r", encoding="utf-8") as f:
        text = f.read()

    # remove kanji within <div id=tag> tags
    text = re.sub(r'<div id=tag>.*?</div>', '', text, flags=re.DOTALL)

    # count kanji
    kanji_count = {}
    for char in text:
        if '\u4e00' <= char <= '\u9fff':
            if char in kanji_count:
                kanji_count[char] += 1
            else:
                kanji_count[char] = 1
    
    # sort kanji by frequency
    kanji_freq = [(kanji, freq) for kanji, freq in kanji_count.items()]
    kanji_freq.sort(key=lambda x: x[1], reverse=True)
    
    # write results to output file
    with open(output_file, "w", encoding="utf-8") as f:
        f.write(f"Total Unique Kanji: {len(kanji_freq)}\n\n")
        for kanji, freq in kanji_freq:
            f.write(f"{kanji}: {freq}\n")

if __name__ == "__main__":
    if len(sys.argv) == 1:
        main()
    else:
        print("Usage: python kanji.py")  

name the script kanji.py
name the exported list of cards kanji.txt
it outputs to output.txt

kanji appearing in tags are not included

if you dont know how to run the script, chat gpt taught me when i asked :slight_smile:

Apparently, ChatGPT hasn’t scraped this forum, yet, and doesn’t know how to use the debug console. :wink:
Here is more concise version you can execute in Anki directly without a Python installation:

text = ""
for nid in mw.col.find_notes('"deck:My Japanese deck"'):
    text += mw.col.get_note(nid).joined_fields()

kanji_count = {}
for char in text:
    if '\u4e00' <= char <= '\u9fff':
        kanji_count[char] = kanji_count.get(char, 0) + 1
    
kanji_freq = [(kanji, freq) for kanji, freq in kanji_count.items()]
kanji_freq.sort(key=lambda x: x[1], reverse=True)

print(f"Total Unique Kanji: {len(kanji_freq)}\n\n")
for kanji, freq in kanji_freq:
    print(f"{kanji}: {freq}")
4 Likes

ChatGPT is becoming Frank’s Red Hot sauce, I think. (If you don’t get the reference, their slogan is, “I put that s💩t on everything!”)

>>> text = ""
... for nid in mw.col.find_notes('"deck:日本語"'):
...     text += mw.col.get_note(nid).joined_fields()
... 
... kanji_count = {}
... for char in text:
...     if '\u4e00' <= char <= '\u9fff':
...         kanji_count[char] = kanji_count.get(char, 0) + 1
...     
... kanji_freq = [(kanji, freq) for kanji, freq in kanji_count.items()]
... kanji_freq.sort(key=lambda x: x[1], reverse=True)
... 
... print(f"Total Unique Kanji: {len(kanji_freq)}\n\n")
... for kanji, freq in kanji_freq:
... pp(    print(f"{kanji}: {freq}"))
Traceback (most recent call last):
  File "aqt.main", line 1774, in onDebugRet
  File "<string>", line 15
    pp(    print(f"{kanji}: {freq}"))
    ^
IndentationError: expected an indented block

If that is supposed to be a question, you seem to have pressed Ctrl+Shift+Enter instead of Ctrl+Enter which added pp() breaking the code.

1 Like

out of curiosity i asked chat gpt to make code that can run in ankis debug console, and it did. albeit not as concise as your code

I would be interested to see that code. When I tried it, ChatGPT told me very confidently, but also very incorrectly what to do.

you have to tell it that. then it will correct its mistakes. it rarely ever spits out corrrect code on the first go

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.