-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter_substantial_urls.py
More file actions
163 lines (135 loc) · 5.47 KB
/
Copy pathfilter_substantial_urls.py
File metadata and controls
163 lines (135 loc) · 5.47 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
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
import csv
import os
from urllib.parse import urlparse, urlunparse
# Input and output files
INPUT_FILE = 'uncited_in_cited_conversations.csv'
OUTPUT_FILE = 'filtered_uncited_urls.csv'
# Exclusion criteria
EXCLUDED_EXTENSIONS = {
'.pdf', '.jpg', '.png', '.gif', '.jpeg',
'.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx',
'.zip', '.rar', '.mp3', '.mp4', '.avi'
}
# EXCLUDED_DOMAINS = {
# 'facebook.com', 'instagram.com', 'twitter.com', 'x.com',
# 'reddit.com', 'youtube.com', 'vimeo.com', 'tiktok.com',
# 'linkedin.com', 'amazon.com',
# 'docs.google.com', 'drive.google.com', 'sheets.google.com', 'slides.google.com',
# 'notion.so',
# 'wikipedia.org',
# 'scribd.com',
# 'pinterest.com'
# }
EXCLUDED_DOMAINS = {
'youtube.com', 'vimeo.com', 'tiktok.com',
'docs.google.com', 'drive.google.com', 'sheets.google.com', 'slides.google.com',
'notion.so',
'scribd.com',
'pinterest.com'
}
def normalize_url(url):
"""
Normalizes a URL by lowercasing scheme/netloc, removing trailing slash, and removing fragments.
Also handles URLs missing a scheme by assuming https://.
"""
try:
url = url.strip()
# If no scheme, assume https
if not url.startswith(('http://', 'https://')):
url = 'https://' + url
parsed = urlparse(url)
scheme = parsed.scheme.lower()
netloc = parsed.netloc.lower()
path = parsed.path.rstrip('/')
# Reconstruct WITHOUT fragment
return urlunparse((scheme, netloc, path, parsed.params, parsed.query, ''))
except Exception:
return url.strip().lower()
def is_substantial_content(url):
"""
Checks if a URL represents substantial web content based on exclusion rules.
"""
try:
parsed_url = urlparse(url)
path = parsed_url.path.lower()
netloc = parsed_url.netloc.lower()
# 1. Check File Extensions
for ext in EXCLUDED_EXTENSIONS:
if path.endswith(ext):
return False, f"Extension: {ext}"
# 2. Check Domains (checking if the netloc ends with the excluded domain to catch subdomains)
# Handle 'www.' prefix if present in netloc for cleaner matching, though endswith works well.
# We explicitly want to catch 'en.wikipedia.org' with 'wikipedia.org'
for domain in EXCLUDED_DOMAINS:
if netloc == domain or netloc.endswith('.' + domain):
return False, f"Domain: {domain}"
return True, "OK"
except Exception as e:
print(f"Error parsing URL {url}: {e}")
return False, "Error"
def main():
if not os.path.exists(INPUT_FILE):
print(f"Error: Input file '{INPUT_FILE}' not found.")
return
kept_urls = []
removed_count = 0
removal_reasons = {}
print(f"Reading from {INPUT_FILE}...")
seen_urls = set()
with open(INPUT_FILE, 'r', encoding='utf-8', newline='') as infile:
reader = csv.reader(infile)
try:
# Check if there's a header. The previous files had headers: conversation_id, title, url
# We'll read the first row and check.
first_row = next(reader)
# If the first row looks like a header, keep it.
if first_row and 'conversation_id' in first_row[0].lower():
header = first_row
else:
header = ['conversation_id', 'title', 'url'] # Default if missing
# Reset reader if it wasn't a header?
# Actually, based on previous context, the files created had headers.
# Let's assume headers exist.
pass
except StopIteration:
print("Input file is empty.")
return
# Process rows
for row in reader:
if not row: continue
# Assuming URL is the 3rd column (index 2) based on previous turn's grep commands
if len(row) >= 3:
url = row[2]
normalized = normalize_url(url)
if normalized in seen_urls:
continue
is_substantial, reason = is_substantial_content(url)
if is_substantial:
kept_urls.append(row)
seen_urls.add(normalized)
else:
removed_count += 1
# Track reasons for summary
base_reason = reason.split(':')[0] if ':' in reason else reason
# detailed reason
specific_reason = reason
removal_reasons[specific_reason] = removal_reasons.get(specific_reason, 0) + 1
# Write output
print(f"Writing {len(kept_urls)} URLs to {OUTPUT_FILE}...")
with open(OUTPUT_FILE, 'w', encoding='utf-8', newline='') as outfile:
writer = csv.writer(outfile)
writer.writerow(header)
writer.writerows(kept_urls)
print("-" * 30)
print("Filtering Complete.")
print(f"Total processed: {len(kept_urls) + removed_count}")
print(f"Removed: {removed_count}")
print(f"Remaining: {len(kept_urls)}")
print("-" * 30)
print("Top removal reasons:")
# Sort reasons by count
sorted_reasons = sorted(removal_reasons.items(), key=lambda x: x[1], reverse=True)
for reason, count in sorted_reasons[:20]:
print(f" {reason}: {count}")
if __name__ == "__main__":
main()