Datasets:

Modalities:
Text
Formats:
text
Libraries:
Datasets
License:
File size: 11,261 Bytes
d9aeb25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import os
import re
import subprocess
from typing import Set, Tuple

# Third-party imports
import spacy
import jieba

import gzip
import json
from pathlib import Path
import logging
from datatrove.executor.local import LocalPipelineExecutor
from datatrove.pipeline.filters import (
    RegexFilter,
    LanguageFilter,
    GopherQualityFilter,
    C4QualityFilter,
)
from datatrove.pipeline.dedup import (
    MinhashDedupCluster,
    MinhashDedupFilter,
    MinhashDedupSignature,
    MinhashConfig,
    MinhashDedupBuckets,
)
from datatrove.pipeline.writers.jsonl import JsonlWriter
from datatrove.pipeline.writers.parquet import ParquetWriter
from datatrove.pipeline.readers import ParquetReader
from datatrove.pipeline.tokens import TokensCounter
from datatrove.pipeline.formatters import PIIFormatter

# Constants
MAIN_OUTPUT_PATH = "./output"
FILTERING_OUTPUT_PATH = f"{MAIN_OUTPUT_PATH}/base_processing"
DUMP = "CC-MAIN-2024-26"
S3_MINHASH_BASE_PATH = f"{MAIN_OUTPUT_PATH}/minhash"
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# Configuration
minhash_config = MinhashConfig(
    num_buckets=14,
    hashes_per_bucket=8,
    n_grams=2,
)

# Chinese stop words
chinese_stop_words = [
    "的", "了", "和", "是", "就", "都", "而", "及", "與", "這", "其", "但", "並", "個", "我",
    "你", "他", "她", "它", "們", "我們", "你們", "他們", "她們", "它們", "在", "有", "人",
    "這個", "那個", "如果", "因為", "所以", "可以", "沒有", "很", "非常", "得", "著", "過", "為", "再",
    "吧", "呢", "啊", "哪", "那", "麼", "什麼", "誰", "哪裡", "哪裡", "怎麼", "怎麼樣", "為什麼", "將"
]
import shutil

def clear_previous_outputs():
    directories_to_clear = [
        f"{MAIN_OUTPUT_PATH}/base_processing",
        f"{MAIN_OUTPUT_PATH}/filtered_output",
        f"{S3_MINHASH_BASE_PATH}/{DUMP}/signatures",
        f"{S3_MINHASH_BASE_PATH}/{DUMP}/buckets",
        f"{S3_MINHASH_BASE_PATH}/{DUMP}/remove_ids",
        f"{S3_MINHASH_BASE_PATH}/{DUMP}/deduped_output",
        f"{MAIN_OUTPUT_PATH}/logs",
    ]
    
    for directory in directories_to_clear:
        if os.path.exists(directory):
            print(f"Removing: {directory}")
            shutil.rmtree(directory)
        os.makedirs(directory, exist_ok=True)

def read_character_mappings(file_path: str) -> Tuple[Set[str], Set[str]]:
    traditional = set()
    simplified = set()
    with open(file_path, 'r', encoding='utf-8') as f:
        for line in f:
            chars = line.strip().split('\t')
            if len(chars) >= 2:
                trad, simps = chars[0], chars[1].split()
                traditional.add(trad)
                simplified.update(simps)
    return traditional, simplified

def read_traditional_characters(file_path: str) -> Set[str]:
    with open(file_path, 'r', encoding='utf-8') as f:
        return set(f.read().strip())

def create_simplified_filter(simplified_only: Set[str]) -> RegexFilter:
    simplified_pattern = '|'.join(re.escape(char) for char in simplified_only)
    return RegexFilter(
        regex_exp=simplified_pattern,
        exclusion_writer=JsonlWriter(
            f"{FILTERING_OUTPUT_PATH}/removed/simplified/{DUMP}",
            output_filename="${rank}.jsonl.gz"
        )
    )

def initial_filtering_pipeline(simplified_filter: RegexFilter) -> Tuple[LocalPipelineExecutor, LocalPipelineExecutor, LocalPipelineExecutor]:
    # Initial filtering pipeline - Part 1
    initial_executor_part1 = LocalPipelineExecutor(
        pipeline=[
            ParquetReader("./input/hugg-init/out-1720278098.parquet"),
            LanguageFilter(languages=["zh"], language_threshold=0.65),
            simplified_filter,
            ParquetWriter(f"{MAIN_OUTPUT_PATH}/temp_output_part1")
        ],
        tasks=8,
        workers=8,
        logging_dir=f"{MAIN_OUTPUT_PATH}/logs/base_processing/{DUMP}/part1",
    )

    # Initial filtering pipeline - Part 2
    initial_executor_part2 = LocalPipelineExecutor(
        pipeline=[
            ParquetReader(f"{MAIN_OUTPUT_PATH}/temp_output_part1"),
            GopherQualityFilter(
                min_doc_words=15,
                max_doc_words=10000,
                min_avg_word_length=1,
                max_avg_word_length=4,
                max_symbol_word_ratio=0.1,
                max_bullet_lines_ratio=0.9,
                max_ellipsis_lines_ratio=0.3,
                min_stop_words=1,
                stop_words=chinese_stop_words,
                exclusion_writer=JsonlWriter(f"{FILTERING_OUTPUT_PATH}/removed/4_gopher_qual/{DUMP}"),
                language="zh",
            ),
            ParquetWriter(f"{MAIN_OUTPUT_PATH}/temp_output_part2")
        ],
        tasks=8,
        workers=8,
        logging_dir=f"{MAIN_OUTPUT_PATH}/logs/base_processing/{DUMP}/part2",
    )

    # Initial filtering pipeline - Part 3
    initial_executor_part3 = LocalPipelineExecutor(
        pipeline=[
            ParquetReader(f"{MAIN_OUTPUT_PATH}/temp_output_part2"),
            C4QualityFilter(
                split_paragraph=False,
                remove_citations=False,
                filter_no_terminal_punct=False,
                min_num_sentences=-1,
                min_words_per_line=-1,
                max_word_length=-1,
                filter_lorem_ipsum=False,
                filter_javascript=True,
                filter_curly_bracket=True,
                filter_policy=True,
                language="zh",
                exclusion_writer=JsonlWriter(f"{FILTERING_OUTPUT_PATH}/removed/5_c4/{DUMP}"),
            ),
            ParquetWriter(f"{MAIN_OUTPUT_PATH}/filtered_output")
        ],
        tasks=8,
        workers=8,
        logging_dir=f"{MAIN_OUTPUT_PATH}/logs/base_processing/{DUMP}/part3",
    )
    return initial_executor_part1, initial_executor_part2, initial_executor_part3 

def deduplication_pipeline() -> Tuple[LocalPipelineExecutor, LocalPipelineExecutor, LocalPipelineExecutor, LocalPipelineExecutor]:
    stage1 = LocalPipelineExecutor(
        pipeline=[
            ParquetReader(f"{MAIN_OUTPUT_PATH}/filtered_output"),
            MinhashDedupSignature(
                output_folder=f"{S3_MINHASH_BASE_PATH}/{DUMP}/signatures",
                config=minhash_config,
                language="zh",
            ),
        ],
        tasks=8,
        workers=8,
        logging_dir=f"{MAIN_OUTPUT_PATH}/logs/minhash/signatures",
    )

    stage2 = LocalPipelineExecutor(
        pipeline=[
            MinhashDedupBuckets(
                input_folder=f"{S3_MINHASH_BASE_PATH}/{DUMP}/signatures",
                output_folder=f"{S3_MINHASH_BASE_PATH}/{DUMP}/buckets",
                config=minhash_config,
            ),
        ],
        tasks=minhash_config.num_buckets,
        workers=8,
        logging_dir=f"{MAIN_OUTPUT_PATH}/logs/minhash/buckets",
    )

    stage3 = LocalPipelineExecutor(
        pipeline=[
            MinhashDedupCluster(
                input_folder=f"{S3_MINHASH_BASE_PATH}/{DUMP}/buckets",
                output_folder=f"{S3_MINHASH_BASE_PATH}/{DUMP}/remove_ids",
                config=minhash_config,
            ),
        ],
        tasks=1,
        workers=1,
        logging_dir=f"{MAIN_OUTPUT_PATH}/logs/minhash/clustering",
    )

    stage4 = LocalPipelineExecutor(
        pipeline=[
            ParquetReader(f"{MAIN_OUTPUT_PATH}/filtered_output"),
            TokensCounter(),
            MinhashDedupFilter(input_folder=f"{S3_MINHASH_BASE_PATH}/{DUMP}/remove_ids"),
            PIIFormatter(),
            ParquetWriter(f"{S3_MINHASH_BASE_PATH}/{DUMP}/deduped_output"),
        ],
        tasks=8,
        workers=8,
        logging_dir=f"{MAIN_OUTPUT_PATH}/logs/minhash/filtering",
    )

    return stage1, stage2, stage3, stage4

def analyze_removed_documents(removed_dir: str, simplified_pattern: str, output_file: str):
    removed_files = Path(removed_dir).glob('*.jsonl.gz')
    simplified_regex = re.compile(f'[{simplified_pattern}]')
    
    total_documents = 0
    documents_with_simplified = 0
    
    with gzip.open(output_file, 'wt', encoding='utf-8') as out_f:
        for file in removed_files:
            logger.info(f"Analyzing file: {file.name}")
            with gzip.open(file, 'rt', encoding='utf-8') as in_f:
                for line in in_f:
                    total_documents += 1
                    document = json.loads(line)
                    text = document.get('text', '')
                    
                    matches = list(set(simplified_regex.findall(text)))
                    if matches:
                        documents_with_simplified += 1
                        result = {
                            "document_number": total_documents,
                            "simplified_characters": matches,
                            "text_snippet": text  
                        }
                        out_f.write(json.dumps(result, ensure_ascii=False) + '\n')
    
    logger.info(f"Analysis complete. Results written to {output_file}")
    logger.info(f"Total documents analyzed: {total_documents}")
    logger.info(f"Documents with simplified characters: {documents_with_simplified}")

def main(restart=False):
    if restart:
        clear_previous_outputs()

    # Read character mappings
    ts_traditional, ts_simplified = read_character_mappings("./input/ts-map/TSCharacters.txt")
    st_traditional, st_simplified = read_character_mappings("./input/ts-map/STCharacters.txt")

    # Combine sets
    traditional = ts_traditional.union(st_traditional)
    simplified = ts_simplified.union(st_simplified)

    # Additional processing
    white_list = set("床峰群秘霉庄痴雇简体踪")
    additional_set = read_traditional_characters("./input/dict-tradi2/trad.txt")
    print(f"Number of additional traditional characters: {len(additional_set)}")

    simplified_only = simplified - traditional - white_list

    # Create simplified filter
    simplified_filter = create_simplified_filter(simplified_only)

    # Run initial filtering pipeline

    initial_executor_part1, initial_executor_part2, initial_executor_part3 = initial_filtering_pipeline(simplified_filter)
    initial_executor_part1.run()
    initial_executor_part2.run()
    initial_executor_part3.run()

    logger.info("Initial filtering complete. Starting analysis of removed documents.")
    removed_dir = f"{FILTERING_OUTPUT_PATH}/removed/simplified/{DUMP}"
    output_file = f"{MAIN_OUTPUT_PATH}/simplified_analysis_results.jsonl.gz"
    
    # Create the simplified pattern
    simplified_pattern = ''.join(simplified_only)
    
    analyze_removed_documents(removed_dir, simplified_pattern, output_file)

    # Run deduplication pipeline
    # stage1, stage2, stage3, stage4 = deduplication_pipeline()
    # stage1.run()
    # stage2.run()
    # stage3.run()
    # stage4.run()
    

if __name__ == "__main__":
    import argparse
    
    parser = argparse.ArgumentParser(description="Run Chinese text processing pipeline")
    parser.add_argument("--restart", action="store_true", help="Clear previous outputs and restart processing")
    args = parser.parse_args()
    
    main(restart=args.restart)