File size: 4,737 Bytes
313a961
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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')