christopher commited on
Commit
caa7add
1 Parent(s): 5405e46

Update write_to_filtered_file.py

Browse files
Files changed (1) hide show
  1. write_to_filtered_file.py +24 -15
write_to_filtered_file.py CHANGED
@@ -1,22 +1,31 @@
1
  #!/usr/bin/env python3
2
- import gzip
3
  from ast import literal_eval
4
  from tqdm import tqdm
5
  import sys
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  if __name__ == '__main__':
7
  file_name = sys.argv[1]
8
  new_file_name = ".".join(file_name.split(".")[:1] + ["jsonl"])
9
- with gzip.open(file_name, 'rt') as f: #TODO test this with de_head_0000_2019-30.tar.gz
10
- a = f.readline()
11
- a = a.split("{'url'")
12
- a = [("{'url'" + item) for item in a]
13
- b = []
14
- for item in tqdm(a):
15
- try:
16
- if literal_eval(item)['language_score'] > 0.98:
17
- b.append(item)
18
- except:
19
- None
20
- with open(new_file_name, 'wt') as file_new:
21
- for part in b:
22
- file_new.write(part + '\n')
 
1
  #!/usr/bin/env python3
2
+ import tarfile
3
  from ast import literal_eval
4
  from tqdm import tqdm
5
  import sys
6
+
7
+ def tar_file_to_string(file_name):
8
+ with tarfile.open(file_name, "r:gz") as tar:
9
+ for member in tar.getmembers():
10
+ f = tar.extractfile(member)
11
+ data = f.readline()
12
+ data = data.decode("utf-8")
13
+ data = data.split("{'url'")
14
+ data = [("{'url'" + item) for item in data]
15
+ data = data[1:]
16
+ gc.collect()
17
+ return data
18
+
19
  if __name__ == '__main__':
20
  file_name = sys.argv[1]
21
  new_file_name = ".".join(file_name.split(".")[:1] + ["jsonl"])
22
+ a = tar_file_to_string(file_name)
23
+ for item in tqdm(a):
24
+ try:
25
+ if literal_eval(item)['language_score'] > 0.98:
26
+ b.append(item)
27
+ except:
28
+ None
29
+ with open(new_file_name, 'wt') as file_new:
30
+ for part in b:
31
+ file_new.write(part + '\n')