u84u commited on
Commit
dcb05ab
·
verified ·
1 Parent(s): 30252fb

Upload main.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. main.py +97 -0
main.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import re
3
+ import html
4
+ import pyarrow as pa
5
+ import pyarrow.parquet as pq
6
+ import logging
7
+ import time
8
+ import sys
9
+
10
+ OUTPUT = "./4chan.parquet"
11
+
12
+ logging.basicConfig(level=logging.INFO, format="%(asctime)s | %(levelname)s | %(message)s")
13
+ logger = logging.getLogger("4chan")
14
+
15
+ def clean(text):
16
+ text = html.unescape(text)
17
+ text = re.sub(r'<a[^>]+class="quotelink"[^>]*>>>?\d+</a>', '', text)
18
+ text = re.sub(r'<span class="quote">&gt;(.*?)</span>', r'>\1', text)
19
+ text = re.sub(r'<wbr>', '', text)
20
+ text = re.sub(r'<br\s*/?>', '\n', text)
21
+ text = re.sub(r'<[^>]+>', '', text)
22
+ text = re.sub(r'\n{3,}', '\n\n', text)
23
+ return text.strip()
24
+
25
+ schema = pa.schema([('system', pa.string()), ('user', pa.string())])
26
+ writer = None
27
+ systems = []
28
+ users = []
29
+ total_pairs = 0
30
+ total_threads = 0
31
+ start = time.time()
32
+
33
+ for line in sys.stdin:
34
+ line = line.strip()
35
+ if not line:
36
+ continue
37
+
38
+ try:
39
+ thread = json.loads(line)
40
+ except json.JSONDecodeError:
41
+ continue
42
+
43
+ posts = thread.get('posts', [])
44
+ if len(posts) < 2:
45
+ continue
46
+
47
+ total_threads += 1
48
+ by_no = {p['no']: clean(p.get('com', '')) for p in posts}
49
+ op = posts[0]
50
+ op_text = clean(op.get('com', ''))
51
+
52
+ for post in posts[1:]:
53
+ com = post.get('com', '')
54
+ refs = re.findall(r'>>?(\d+)', html.unescape(com))
55
+ refs = [int(r) for r in refs if int(r) in by_no]
56
+
57
+ if refs:
58
+ for ref in refs:
59
+ systems.append(by_no[ref])
60
+ users.append(clean(com))
61
+ else:
62
+ systems.append(op_text)
63
+ users.append(clean(com))
64
+
65
+ if len(systems) >= 10_000:
66
+ batch = pa.record_batch([
67
+ pa.array(systems, type=pa.string()),
68
+ pa.array(users, type=pa.string()),
69
+ ], schema=schema)
70
+
71
+ if writer is None:
72
+ writer = pq.ParquetWriter(OUTPUT, schema, compression='zstd', compression_level=3)
73
+ logger.info("Writer initialized")
74
+
75
+ writer.write_batch(batch)
76
+ total_pairs += len(systems)
77
+ systems.clear()
78
+ users.clear()
79
+
80
+ elapsed = time.time() - start
81
+ logger.info(f"Threads: {total_threads} | Pairs: {total_pairs} | Elapsed: {elapsed:.1f}s")
82
+
83
+ if systems:
84
+ batch = pa.record_batch([
85
+ pa.array(systems, type=pa.string()),
86
+ pa.array(users, type=pa.string()),
87
+ ], schema=schema)
88
+ if writer is None:
89
+ writer = pq.ParquetWriter(OUTPUT, schema, compression='zstd', compression_level=3)
90
+ writer.write_batch(batch)
91
+ total_pairs += len(systems)
92
+
93
+ if writer:
94
+ writer.close()
95
+
96
+ elapsed = time.time() - start
97
+ logger.info(f"Done. Threads: {total_threads} | Total pairs: {total_pairs} | Time: {elapsed:.1f}s")