-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_search.py
116 lines (94 loc) · 4.29 KB
/
new_search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import sys
import re
import glob
import os
import collections
def make_list(str):
skip = False
result = []
for i in range(len(str)):
if skip == True:
skip = False
continue
if str[i] == '+':
skip = True
result.append(str[i:i+2])
else:
result.append(str[i])
return result
def mochigoma_list_to_str(mochigoma_list):
if len(mochigoma_list) == 0:
return '-'
else:
mochigoma_dict = collections.Counter(mochigoma_list)
mochigoma_str = ''
for x in koma_kigo2:
if mochigoma_dict[x] == 1:
mochigoma_str += x
elif mochigoma_dict[x] > 1:
mochigoma_str += (str(mochigoma_dict[x]) + x)
return mochigoma_str
def mochigoma_str_to_list(mochigoma_str):
if mochigoma_str == '-':
return []
else:
mochigoma_list = []
for i in range(len(mochigoma_str)):
if mochigoma_str[i].isdigit():
for n in range(int(mochigoma_str[i])-1):
mochigoma_list.append(mochigoma_str[i+1])
else:
mochigoma_list.append(mochigoma_str[i])
return mochigoma_list
koma_kigo = ['r', 'b', 'g', 's', 'n', 'l', 'p']
koma_kigo2 = [x.swapcase() for x in koma_kigo] + koma_kigo
sfen_files = glob.glob('sfen/*.sfen')
while True:
input_text = input('sfenを入力して下さい、終了時はq\n')
if input_text == 'q':
break
else:
input_text = input_text.replace('sfen ', '')
print('\n')
text_list = input_text.split(' ')
if len(text_list) != 4:
print('データが不適です')
continue
banmen, teban, mochigoma, tesu = text_list
banmen_row_list = [make_list(x) for x in banmen.swapcase().split('/')]
r_banmen_row_list = [x[::-1] for x in banmen_row_list[::-1]]
r_banmen = '/'.join([''.join(x) for x in r_banmen_row_list])
r_mochigoma = mochigoma_list_to_str(mochigoma_str_to_list(mochigoma.swapcase()))
normal_pattern = r'^' + re.escape(banmen) + r'\s([w|b])\s' + re.escape(mochigoma) + r'\s' + r'(\d+)$'
rotate_pattern = r'^' + re.escape(r_banmen) + r'\s([w|b])\s' + re.escape(r_mochigoma) + r'\s' + r'(\d+)$'
for each_file in sfen_files:
with open(each_file, 'r') as f:
my_data = f.read()
normal_result = re.finditer(normal_pattern, my_data, flags = re.MULTILINE)
rotate_result = re.finditer(rotate_pattern, my_data, flags = re.MULTILINE)
normal_result_list = list(normal_result)
rotate_result_list = list(rotate_result)
if (len(normal_result_list) + len(rotate_result_list)) != 0:
print(os.path.basename(each_file))
sente = re.search(r'^先手:.*$', my_data, flags = re.MULTILINE).group()
gote = re.search(r'^後手:.*$', my_data, flags = re.MULTILINE).group()
if len(normal_result_list) != 0:
print('通常検索')
for mm in normal_result_list:
teban = mm.groups()[0]
teban_text = sente if teban == 'b' else gote
sashite_pattern = r'^' + re.escape(mm.groups()[1]) + r'\s(\S+)$'
sashite_search = re.search(sashite_pattern, my_data, flags = re.MULTILINE)
sashite = sashite_search.groups()[0]
print('%s手目 %s が %s を指しました\n'%(mm.groups()[1], teban_text, sashite))
if len(rotate_result_list) != 0:
print('反転検索')
for mm in rotate_result_list:
teban = mm.groups()[0]
teban_text = sente if teban == 'b' else gote
sashite_pattern = r'^' + re.escape(mm.groups()[1]) + r'\s(\S+)$'
sashite_search = re.search(sashite_pattern, my_data, flags = re.MULTILINE)
sashite = sashite_search.groups()[0]
sashite = ''.join([str(10-int(x)) if x.isdigit() else x for x in sashite])
print('%s手目 %s が %s を指しました\n'%(mm.groups()[1], teban_text, sashite))
sys.exit()