<jupyter_start><jupyter_text>Build Dataset - Radicals by Stroke-Order 0x01 Instead of recalculating the kanji-radical dataset and reorder them by stroke order for each time the kanji-radiclas are requested, there could be a prepared dataset, which already contains this information. This prepared datasets are common in normal programming, because there is no use in requesting the same data over and over again, and expecting different results, when the underlying database has not been changed.<jupyter_code>import json
import os

from jamdict import Jamdict


jamdict = Jamdict()
radicals = list(jamdict.radk.keys())
radicals
radicals_info={}
for radical in radicals:
    result = jamdict.lookup(radical)
    # print(result.chars)
    
    # sometimes there are more chars given, we are only interested which is equal to the radical itself.
    for char in result.chars:
        if not char.literal==radical:
            continue
            
        # to json provides a dictionary... not a json string
        char_dict = char.to_json()
        
        # print(char_dict.keys())
        # dict_keys(['literal', 'codepoints', 'radicals', 'stroke_count', 'grade', 'stroke_miscounts', 
        #            'variants', 'freq', 'rad_names', 'jlpt', 'dic_refs', 'q_codes', 'rm', 'nanoris'])
        
        # print(char_dict['stroke_count'])
        # print(char_dict['literal'])
        # print(char_dict['freq'])
        
        radicals_info[char_dict['literal']]=char_dict['stroke_count']
nonstandard_strokes={
    'ノ':1, 
    'ヨ':3,
    '｜':1,
    'ハ':2,
    '⺅':2,
    '⻖':3,
    '⻏':3,
    '⺾':3,
    '⺌':3,
    '⺹':4,
    'マ':2,
    'ユ':2 }

def get_stroke_count(radical):
    global radicals_info
    global nonstandard_strokes
    
    if radical not in radicals_info:
        return nonstandard_strokes[radical]
    return radicals_info[radical]
        
radicals.sort(key=get_stroke_count)
print(radicals)
myresult = jamdict.lookup('｜')
myresult
tree = {}

for radical in radicals:
    strokes = get_stroke_count(radical)
    if not strokes in tree:
        tree[strokes]=list()
    tree[strokes].append(radical)
    # tree[strokes].sort()
    
# I want to sort the radicals by the number of kanji they produce... because the unknown kanji, 
# highly probably has often used element which are also easy to identify
for strokes in tree.keys():
    radicallist = tree[strokes]
    sorted_list = sorted(radicallist, key=lambda radical: len(jamdict.radk[radical]), reverse=True)
    tree[strokes] = sorted_list
index = list(tree.keys())
index.sort()

flattened = []
for i in index:
    flattened.extend(tree[i])

flattened

DATA_DIR = '../data'
RADICAL_STROKE_DATA = 'kanjiRadicalStrokeData.json'

with open(os.path.join(DATA_DIR,RADICAL_STROKE_DATA),'w') as outputFile:
    json.dump( {'tree': tree, 'list':flattened }, outputFile);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11447459986082116, 'fraction_numerical': 0.005219206680584551, 'mean_word_length': 3.506269592476489, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '139003', 'license': 'MIT', 'n_tokens_mistral': 954, 'n_tokens_neox': 923, 'n_words': 259, 'path': 'ipynb/BuildDataset_RadicalsByStrokeOrder_0x01.ipynb', 'repo_name': 'mindscan-de/OrangeMoon-Backend', 'size': '2874'}