-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggregate.py
More file actions
59 lines (45 loc) · 2.04 KB
/
aggregate.py
File metadata and controls
59 lines (45 loc) · 2.04 KB
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
import csv
from pathlib import Path
def main() -> None:
# This script aggregates profanity lists from various CSV and TXT files,
# removes duplicates, and writes the unique entries to a single CSV file.
base_dir = Path(__file__).resolve().parent
target_csv = base_dir / "profanity_aggregate.csv"
# Gather all source files from specified directories: CSV files from 'profanity_csv'
# and TXT files from 'profanity-list-main/list'.
source_files = sorted(
path
for path in base_dir.glob("profanity_csv/*.csv")
)
source_files.extend(
sorted(
path
for path in base_dir.glob("profanity-list-main/list/*.txt")
)
)
if not source_files:
raise SystemExit("No source CSV or TXT files found to aggregate.")
# Process each source file, reading its content and adding unique rows to a set.
# CSV files are read using csv.reader, and TXT files are read line by line.
unique_rows = set()
for source_file in source_files:
print(f"Processing {source_file} ...")
if source_file.suffix == ".csv":
with source_file.open("r", encoding="utf-8", newline="") as infile:
reader = csv.reader(infile)
for row in reader:
if row:
unique_rows.add(tuple(row))
elif source_file.suffix == ".txt":
with source_file.open("r", encoding="utf-8") as infile:
for line in infile:
word = line.strip()
if word:
unique_rows.add((word,))
# Write all collected unique rows to the target CSV file, sorted alphabetically.
with target_csv.open("w", encoding="utf-8", newline="") as outfile:
writer = csv.writer(outfile)
writer.writerows(sorted(list(unique_rows)))
print(f"Wrote {len(unique_rows)} unique rows from {len(source_files)} CSV and TXT files to {target_csv.name}")
if __name__ == "__main__":
main()