-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcleaner.py
36 lines (25 loc) · 1.07 KB
/
cleaner.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
import fire
from jsonlines import jsonlines
from rich.progress import track
class DataCleaner:
def __init__(self, input_path: str, output_path: str = None):
self.input_path = input_path
if not output_path:
self.output_path = input_path.replace('.jsonl', '_cleaned.jsonl')
else:
self.output_path = output_path
def clean(self):
total = 0
valid = 0
with (jsonlines.open(self.input_path, 'r') as reader):
with jsonlines.open(self.output_path, 'w') as writer:
for datapoint in track(reader, description='Cleaning datapoints: '):
total += 1
if datapoint['zh']['question'].startswith('<error>') or \
datapoint['zh']['response'].startswith('<error>'):
continue
valid += 1
writer.write(datapoint)
print(f'Cleaning completed. {valid} / {total} = {valid / total:.2%} datapoints are valid.')
if __name__ == '__main__':
fire.Fire(DataCleaner)