-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathword-square-build-stages.py
More file actions
146 lines (130 loc) · 3.74 KB
/
word-square-build-stages.py
File metadata and controls
146 lines (130 loc) · 3.74 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import argparse
import glob
import json
import os
import re
import sqlite3
import sys
from typing import Iterable, List
ROW_RE = re.compile(r'^[A-Z]{7}$')
SOLUTION_RE = re.compile(r'^Solution\s+#\d+:$')
def iter_input_files(patterns: List[str]) -> Iterable[str]:
for pattern in patterns:
expanded = os.path.expanduser(pattern)
for path in sorted(glob.glob(expanded)):
yield path
def compute_cols(rows: List[str]) -> List[str]:
return [''.join(row[i] for row in rows) for i in range(7)]
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description='Build word-square stages SQLite database.')
parser.add_argument(
'--input',
nargs='+',
required=True,
help='Input log glob(s). Example: ./*.log',
)
parser.add_argument(
'--output',
default='word-square/stages.sqlite3',
help='Output SQLite path. Default: word-square/stages.sqlite3',
)
parser.add_argument(
'--commit-every',
type=int,
default=1000,
help='Commit every N inserts. Default: 1000',
)
parser.add_argument(
'--max-boards',
type=int,
default=0,
help='Stop after inserting N boards (0 means no limit).',
)
parser.add_argument(
'--reset',
action='store_true',
help='Delete output DB if it exists before writing.',
)
return parser.parse_args()
def main() -> int:
args = parse_args()
out_path = os.path.expanduser(args.output)
if args.reset and os.path.exists(out_path):
os.remove(out_path)
os.makedirs(os.path.dirname(out_path), exist_ok=True)
conn = sqlite3.connect(out_path)
conn.execute('PRAGMA journal_mode=OFF')
conn.execute('PRAGMA synchronous=OFF')
conn.execute(
'CREATE TABLE IF NOT EXISTS stages ('
'id INTEGER PRIMARY KEY,'
'board TEXT NOT NULL UNIQUE,'
'rows TEXT NOT NULL,'
'cols TEXT NOT NULL,'
'unique_words INTEGER NOT NULL,'
'is_symmetric INTEGER NOT NULL'
')'
)
conn.execute('CREATE INDEX IF NOT EXISTS stages_board_idx ON stages(board)')
conn.execute('CREATE INDEX IF NOT EXISTS stages_is_symmetric_idx ON stages(is_symmetric)')
insert_sql = 'INSERT OR IGNORE INTO stages (board, rows, cols, unique_words, is_symmetric) VALUES (?, ?, ?, ?, ?)'
inserted = 0
dupes = 0
total_solutions = 0
commit_every = max(1, args.commit_every)
conn.execute('BEGIN')
try:
paths = list(iter_input_files(args.input))
if not paths:
print('No input files matched. Please pass --input with a valid glob.')
return 1
for path in paths:
collecting = False
rows: List[str] = []
with open(path, 'r', encoding='utf-8', errors='replace') as handle:
for raw_line in handle:
line = raw_line.strip()
if not line:
continue
if SOLUTION_RE.match(line):
collecting = True
rows = []
continue
if not collecting:
continue
if ROW_RE.match(line):
rows.append(line)
if len(rows) == 7:
board = ''.join(rows)
cols = compute_cols(rows)
unique_words = len(set(rows + cols))
is_symmetric = int(rows == cols)
before = conn.total_changes
conn.execute(insert_sql, (board, json.dumps(rows), json.dumps(cols), unique_words, is_symmetric))
total_solutions += 1
if conn.total_changes > before:
inserted += 1
else:
dupes += 1
if inserted % commit_every == 0:
conn.commit()
conn.execute('BEGIN')
if args.max_boards and inserted >= args.max_boards:
raise StopIteration
collecting = False
rows = []
continue
# Unexpected line inside a solution block. Reset.
collecting = False
rows = []
except StopIteration:
pass
finally:
conn.commit()
conn.close()
print(f'Parsed solutions: {total_solutions}')
print(f'Inserted boards: {inserted}')
print(f'Duplicates skipped: {dupes}')
return 0
if __name__ == '__main__':
sys.exit(main())