|
import json |
|
import os |
|
import re |
|
|
|
dir_in = "raw_jsonl" |
|
dir_out = "jsonl" |
|
|
|
def remove_single_newlines_with_space(text): |
|
new_text = [] |
|
i = 0 |
|
|
|
def is_valid_char(c): |
|
|
|
return c.isalpha() or c in {' ', '"', "'", ')', ']', '(' , '[', '-', ',', '.', '!', '?', ';', ':'} or c.isdigit() |
|
|
|
while i < len(text): |
|
if i > 0 and i < len(text) - 1 and text[i] == '\n': |
|
|
|
if is_valid_char(text[i-1]) and is_valid_char(text[i+1]): |
|
new_text.append(' ') |
|
i += 1 |
|
continue |
|
new_text.append(text[i]) |
|
i += 1 |
|
|
|
return ''.join(new_text) |
|
|
|
char_count_per_chunk = 10737418240 |
|
cur_dir = os.path.dirname(os.path.realpath(__file__)) |
|
|
|
allowed_licenses = [ |
|
"https://creativecommons.org/share-your-work/public-domain/cc0/", |
|
"http://creativecommons.org/share-your-work/public-domain/cc0/", |
|
"http://creativecommons.org/publicdomain/zero/1.0/", |
|
"http://creativecommons.org/licenses/publicdomain/", |
|
] |
|
|
|
disallowed_licenses = [ |
|
"https://creativecommons.org/licenses/by/4.0/", |
|
"http://creativecommons.org/licenses/by/4.0/", |
|
"http://creativecommons.org/licenses/by/3.0/", |
|
|
|
|
|
"https://creativecommons.org/licenses/by-sa/4.0/", |
|
"http://creativecommons.org/licenses/by-sa/4.0/", |
|
|
|
"https://creativecommons.org/licenses/by-nc-sa/4.0/", |
|
"http://creativecommons.org/licenses/by-nc-sa/4.0/", |
|
"http://creativecommons.org/licenses/by-nc-sa/3.0/", |
|
|
|
"https://creativecommons.org/licenses/by-nc/4.0/", |
|
"http://creativecommons.org/licenses/by-nc/4.0/", |
|
|
|
"https://creativecommons.org/licenses/by-nc-nd/4.0/", |
|
"http://creativecommons.org/licenses/by-nc-nd/4.0/", |
|
|
|
"https://arxiv.org/licenses/nonexclusive-distrib/1.0/", |
|
"http://arxiv.org/licenses/nonexclusive-distrib/1.0/", |
|
] |
|
|
|
|
|
file_paths = os.listdir(cur_dir + '/' + dir_in) |
|
|
|
|
|
def alphanumeric_sort(file_paths): |
|
|
|
convert = lambda text: int(text) if text.isdigit() else text.lower() |
|
|
|
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)] |
|
|
|
return sorted(file_paths, key=alphanum_key) |
|
|
|
file_paths = alphanumeric_sort(file_paths) |
|
|
|
file_paths = [cur_dir + f'/{dir_in}/' + file for file in file_paths] |
|
|
|
|
|
buffer = [] |
|
current_char_count = 0 |
|
chunk_id = 0 |
|
|
|
print("Processing files...") |
|
line_count = 0 |
|
for file in file_paths: |
|
print("Processing file " + file + "...") |
|
with open(file) as f: |
|
for line in f.readlines(): |
|
line_json = json.loads(line) |
|
if line_json['license'] == None: |
|
continue |
|
elif line_json['license'] not in allowed_licenses: |
|
if line_json['license'] not in disallowed_licenses: |
|
|
|
print("License not in allowed licenses or disallowed licenses: " + line_json['license']) |
|
continue |
|
|
|
|
|
current_char_count += len(line) |
|
|
|
|
|
|
|
line_json['text'] = line_json['text'].replace('\\"', '') |
|
line_json['text'] = line_json['text'].replace('\"', '') |
|
line_json['text'] = line_json['text'].replace('\\', '') |
|
line_json['text'] = line_json['text'].replace('\f', '') |
|
line_json['text'] = remove_single_newlines_with_space(line_json['text']) |
|
|
|
buffer.append(line_json) |
|
line_count += 1 |
|
|
|
if current_char_count > char_count_per_chunk: |
|
|
|
with open(cur_dir + f'/{dir_out}/' + "arxiv_" + str(chunk_id) + ".jsonl", 'w') as f: |
|
for line in buffer: |
|
f.write(json.dumps(line) + '\n') |
|
|
|
|
|
buffer = [] |
|
current_char_count = 0 |
|
chunk_id += 1 |
|
|
|
if line_count % 1000 == 0: |
|
print("Found " + str(line_count) + " copyright-free lines...") |
|
|
|
|
|
|
|
with open(cur_dir + f'/{dir_out}/' + "arxiv_" + str(chunk_id) + ".jsonl", 'w') as f: |
|
for line in buffer: |
|
f.write(json.dumps(line) + '\n') |