<jupyter_start><jupyter_text>solver simulation<jupyter_code>df_all_words = pd.read_pickle('data/df_all_words_entropy.pickle')
all_next_guess = pickle.load(open('data/next_guess_map.pickle', 'rb'))

df_answer = df_all_words[df_all_words['in_answers'] == True]
all_patterns = generate_all_patterns()
play(optimal_guess=False)<jupyter_output>注音dle: ㄌㄧㄡㄌㄧ 流利/流離

#1 guess:
ㄉ  ㄚ  ㄋ  ㄧ  ㄤ 大娘
⬛️ ⬛️ ⬛️ 🟨 ⬛️

#2 guess:
ㄒ  ㄧ  ㄥ  ㄨ  ㄣ 行文/刑問
⬛️ 🟩 ⬛️ ⬛️ ⬛️

#3 guess:
ㄐ  ㄧ  ㄢ  ㄌ  ㄧ 建立/見禮
⬛️ 🟩 ⬛️ 🟩 🟩

#4 guess:
ㄊ  ㄧ  ㄠ  ㄌ  ㄧ 條例/條理
⬛️ 🟩 ⬛️ 🟩 🟩

#5 guess:
ㄌ  ㄧ  ㄡ  ㄌ  ㄧ 流利/流離
🟩 🟩 🟩 🟩 🟩

solved! attempt = 5<jupyter_text>test all<jupyter_code>from joblib import Parallel, delayed
import multiprocessing

import collections
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams.update({'font.size': 14})
def play_all_answers(first_guess, to_print=False, show_zh=False):
    attempts = list()
    for answer in df_answer['zhuyin']:
        num_attempt = play(first_guess=first_guess, answer=answer,
                           to_print=to_print, show_zh=show_zh)
        attempts.append(num_attempt)
    return sum(attempts)/len(attempts), attempts


def play_all_possible_words_by_seq(to_print=False, show_zh=False):
    best = {'word': '', 'avg_attempt': 100}
    with tqdm(total=len(df_all_words)) as pbar:
        for word in df_all_words['zhuyin']:
            pbar.update(1)
            avg, dist = play_all_answers(
                first_guess=word, to_print=to_print, show_zh=show_zh)

            if avg < best['avg_attempt']:
                best['word'] = word
                best['avg_attempt'] = avg

            pbar.set_description(
                f"{word}-{avg:.3f}, {best['word']}-{best['avg_attempt']:.3f}")

    print(f"best first guess: {best['word']} {best['avg_attempt']}")


def play_all_possible_words(to_print=False, show_zh=False):
    def play_word(word):
        avg, dist = play_all_answers(first_guess=word)
        return avg, dist

    word_list = list(df_all_words['zhuyin'])
    results = Parallel(n_jobs=8, verbose=10)(
        delayed(play_word)(word) for word in word_list)

    return results
res = play_all_possible_words()
with open("play_all_result.pickle", "wb") as po:
    pickle.dump(res, po)<jupyter_output><empty_output><jupyter_text>view result<jupyter_code>with open("play_all_result.pickle", "rb") as po:
    guess_result = pickle.load(po)
len(guess_result)
avg_guess = np.ones(21721)
for i, res in enumerate(guess_result):
    avg_guess[i] = res[0]

print('average attempts:\t', sum(avg_guess)/len(avg_guess))
print('minimum attempts:\t', min(avg_guess))
print('index of min attempts:\t', np.argmin(avg_guess))
def top_k_best(k):
    ind_top = avg_guess.argsort()[:k]
    result = list()
    for ind in ind_top:
        result.append([ind, df_all_words.loc[ind, 'zhuyin'],
                       df_all_words.loc[ind, 'words'], avg_guess[ind]])
    df_top = pd.DataFrame(result, index=range(
        k+1)[1:], columns=['id', 'zhuyin', 'word', 'avg_guess'])
    return df_top
df_topk = top_k_best(10)
df_topk<jupyter_output><empty_output><jupyter_text>visualize result<jupyter_code>def draw_bar_chart(word, scores):
    
    print(f'\n注音dle: using "{word}" as the first guess')
    
    counts = collections.Counter(sorted(scores))

    fig, ax = plt.subplots()
    fig.set_size_inches(6, 4)
    tries = counts.keys()
    y_pos = np.arange(len(tries))
    times = counts.values()

    ax.barh(y_pos, times, align='center', color=[
            '#6AA964' for _ in range(5)]+['#C5B565' for _ in range(3)])
    ax.set_yticks(y_pos, labels=tries)
    ax.invert_yaxis()  # labels read top-to-bottom
    ax.set_xlabel('counts')
    ax.set_ylabel('number of attempts')
    ax.set_title(f'performance distribution')
    ax.bar_label(ax.containers[0])

    plt.show()
for i, row in df_topk.iterrows():
    print(f'#{i}')
    draw_bar_chart(row['zhuyin'], guess_result[row['id']][1])<jupyter_output>#1

注音dle: using "ㄉㄥㄌㄨㄣ" as the first guess
{'fraction_non_alphanumeric': 0.14522613065326634, 'fraction_numerical': 0.010552763819095477, 'mean_word_length': 3.8079710144927534, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 15, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '615049', 'license': 'MIT', 'n_tokens_mistral': 1754, 'n_tokens_neox': 1722, 'n_words': 307, 'path': 'simulation.ipynb', 'repo_name': 'DMC13/ZhuyinDle', 'size': '3980'}