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): # Check if the character is a letter, space, quote or closing parenthesis 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': # Check if the characters before and after the newline are valid if is_valid_char(text[i-1]) and is_valid_char(text[i+1]): new_text.append(' ') # Add a space instead of the newline 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/", ] # generate a list of file paths from raw_jsonl file_paths = os.listdir(cur_dir + '/' + dir_in) # sort file paths in alphanumeric order def alphanumeric_sort(file_paths): # Function to convert text to integer if possible convert = lambda text: int(text) if text.isdigit() else text.lower() # Function to split the file path into parts and convert numbers alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)] # Sort the list with the custom 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 # loop through each file path building up the buffer till its over the char count 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: # log the license print("License not in allowed licenses or disallowed licenses: " + line_json['license']) continue current_char_count += len(line) # filter line text here 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: # write buffer to file 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') # reset buffer and char count 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')