This repository has been archived by the owner on May 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_results.py
executable file
·191 lines (141 loc) · 4.95 KB
/
output_results.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python3
"""Transform the raw output from the models into something researchers can use."""
import argparse
import csv
import json
import logging
import string
import textwrap
from collections import defaultdict
from os import makedirs
from pathlib import Path
from typing import Generator
import enchant
from allometry.const import SPELL
from allometry.util import finished, started
Rows = list[list[str]]
INSIDE = 30
OUTSIDE = 42
HEIGHT = 48
LETTERS = set(list(string.ascii_uppercase))
NUMBERS = set(list(string.digits) + list('.-'))
DICT = enchant.DictWithPWL('en_US', str(SPELL))
def output(args: argparse.Namespace) -> None:
"""Output the sheets."""
make_dirs(args)
jsonl_paths = args.jsonl_dir.glob('*.jsonl')
for jsonl_path in sorted(jsonl_paths):
logging.info(f'{jsonl_path}')
with open(jsonl_path) as jsonl_file:
data = (json.loads(ln) for ln in jsonl_file.readlines())
chars = chars_by_line(data)
rows = lines_of_text(chars)
rows = post_process(rows)
if args.text_dir:
output_text(args.text_dir, jsonl_path, rows, 'txt')
if args.tsv_dir:
output_csv(args.tsv_dir, jsonl_path, rows, 'tsv')
def post_process(rows: Rows) -> Rows:
"""Try and fix spelling errors and problems with numbers."""
new_rows: Rows = []
for row in rows:
new_row = []
for cell in row:
words = []
for word in cell.split():
half = len(word) // 2
letters = sum(map(word.count, LETTERS)) > half
numbers = sum(map(word.count, NUMBERS)) > half
new_word = word
if len(word) <= 2:
pass
elif numbers:
new_word = word.replace('O', '0')
elif letters and not DICT.check(word):
suggest = DICT.suggest(word)
new_word = suggest[0] if suggest else word
words.append(new_word)
new_row.append(' '.join(words))
new_rows.append(new_row)
return new_rows
def chars_by_line(data: Generator) -> dict[int, list[dict]]:
"""Organize the data by lines of text."""
lines: dict[int, list[dict]] = defaultdict(list)
[lines[c['box']['top']].append(c) for c in data]
return lines
def lines_of_text(line_chars) -> Rows:
"""Convert the JSONL data into raw text."""
lines: list[str] = []
prev_ln = 0
for ln, chars in line_chars.items():
eol = round((ln - prev_ln) / HEIGHT) - 1
[lines.append('') for _ in range(eol)]
prev_ln = ln
text = []
prev_left = 0
for char in chars:
curr_left = char['box']['left']
spaces = round((curr_left - prev_left) / OUTSIDE) - 1
if spaces == 1:
text.append(' ')
elif spaces > 1:
text.append('\t')
text.append(char['chars'][0])
prev_left = curr_left
line = ''.join(text)
lines.append(line.strip())
rows = [ln.split('\t') for ln in lines]
return rows
def output_csv(
csv_dir: Path,
jsonl_path: Path,
rows: Rows,
ext: str,
delimiter: str = '\t',
) -> None:
"""Convert the JSONL data into a CSV file."""
output_path = csv_dir / (jsonl_path.stem + f'.{ext}')
with open(output_path, 'w') as output_file:
writer = csv.writer(output_file, delimiter=delimiter)
for row in rows:
writer.writerow(row)
def output_text(
text_dir: Path,
jsonl_path: Path,
rows: Rows,
ext: str,
) -> None:
"""Convert the JSONL data into raw text."""
output_path = text_dir / (jsonl_path.stem + f'.{ext}')
with open(output_path, 'w') as output_file:
for row in rows:
output_file.write('\t'.join(row))
output_file.write('\n')
def make_dirs(args):
"""Create output directories."""
if args.text_dir:
makedirs(args.text_dir, exist_ok=True)
if args.tsv_dir:
makedirs(args.tsv_dir, exist_ok=True)
def parse_args():
"""Process command-line arguments."""
description = """Output the raw model output into a form researchers can use."""
arg_parser = argparse.ArgumentParser(
description=textwrap.dedent(description),
fromfile_prefix_chars='@')
arg_parser.add_argument(
'--jsonl-dir', required=True, type=Path,
help="""The directory containing the raw data in JSONL format.""")
arg_parser.add_argument(
'--text-dir', type=Path, help="""Where to put the text output.""")
arg_parser.add_argument(
'--tsv-dir', type=Path, help="""Where to put the TSV output.""")
arg_parser.add_argument(
'--spell-check', action='store_true', help="""Try to correct spelling.""")
args = arg_parser.parse_args()
return args
if __name__ == '__main__':
started()
ARGS = parse_args()
output(ARGS)
finished()