<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import pandas as pd
sys.path.append('./../../')

from pyogi.ki2converter import *
from pyogi.kifu import *


if __name__ == '__main__':

    res_table = []
    
    for n in range(0, 50000):

        n1 = (n // 10000)
        n2 = int(n < 10000)
    
        path = '~/data/shogi/2chkifu/{0}000{1}/{2:0>5}.KI2'.format(n1, n2, n)
        kifile = os.path.expanduser(path)

        if not os.path.exists(kifile):
            continue
            
        ki2converter = Ki2converter()
        ki2converter.from_path(kifile)

        csa = ki2converter.to_csa()

        if not csa:
            continue

        kifu = Kifu(csa)
        res = kifu.get_forking(['OU', 'HI'], display=False)
        if res[2] or res[3]:
            print(kifu.players)

        # Output
        # 1. sente forked | gote forked
        # 2. (sente won & sente forked) | (gote won & gote forked)
        res_table.append(
            [n,
             kifu.players[0],
             kifu.players[1],
             kifu.sente_win,
             kifu.is_sennichite,
             res[2] != [],
             res[3] != [],
             res[2] != [] or res[3] != [],
             (kifu.sente_win and res[2]!=[]) or 
             ((not kifu.sente_win) and res[3]!=[])])


        break

    columns = ['n', 'player0', 'player1', 'sente_win',
               'sennichite', 'sente_forking', 'gote_forking',
               'fork', 'fork&win']
    df = pd.DataFrame(res_table, columns=columns)

    pd.crosstab(df.loc[:, 'fork'], df.loc[:, 'fork&win'])<commit_msg>Fix sample code<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
プロ棋士の棋譜の中から王手飛車取りの場面を探して，
「プロの対局では王手飛車をかけたほうが負ける」が本当かどうかを確かめる
'''

import os
import sys
import pandas as pd
sys.path.append('./../../')

from pyogi.ki2converter import *
from pyogi.kifu import *


if __name__ == '__main__':

    res_table = []
    
    for n in range(0, 50000):

        n1 = (n // 10000)
        n2 = int(n < 10000)
    
        path = '~/data/shogi/2chkifu/{0}000{1}/{2:0>5}.KI2'.format(n1, n2, n)
        kifile = os.path.expanduser(path)

        if not os.path.exists(kifile):
            continue
            
        ki2converter = Ki2converter()
        ki2converter.from_path(kifile)

        csa = ki2converter.to_csa()

        if not csa:
            continue

        kifu = Kifu(csa)
        
        if not kifu.extracted:
            continue

        res = kifu.get_forking(['OU', 'HI'], display=False)
        if res[2] or res[3]:
            print(kifu.players)

        # Data
        #   fork: sente forked | gote forked
        #   forkandwin: (sente won & sente forked) | (gote won & gote forked)
        res_table.append(
            {
                'n': n,
                'player0': kifu.players[0],
                'player1': kifu.players[1],
                'sente_won': kifu.sente_win,
                'sennichite': kifu.is_sennichite,
                'sente_forking': res[2] != [],
                'gote_forking': res[3] != [],
                'fork': res[2] != [] or res[3] != [],
                'forkandwin': ((kifu.sente_win and res[2]!=[]) or 
                               (not kifu.sente_win and res[3]!=[]))
            }
        )


    # Output
    df = pd.DataFrame(res_table)
    print(pd.crosstab(df.loc[:, 'fork'], df.loc[:, 'forkandwin']))
{'fraction_non_alphanumeric': 0.15389150943396226, 'fraction_numerical': 0.028891509433962265, 'mean_word_length': 1.6843354430379747, '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}}
{'dir': 'git-commits-cleaned', 'id': '5434073', 'n_tokens_mistral': 1355, 'n_tokens_neox': 1266, 'n_words': 237}