-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent_extractor.py
More file actions
267 lines (217 loc) · 10.8 KB
/
content_extractor.py
File metadata and controls
267 lines (217 loc) · 10.8 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
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
import os
import re
import json
import argparse
from collections import defaultdict
class ContentExtractor:
def __init__(self, input_file, output_dir="extracted_content"):
"""
Initialize the content extractor with input and output locations.
Args:
input_file (str): Path to the markdown file with crawled content
output_dir (str): Directory to save organized content files
"""
self.input_file = input_file
self.output_dir = output_dir
self.content_sections = []
# Create output directory if it doesn't exist
os.makedirs(output_dir, exist_ok=True)
# Categorized content storage
self.categories = {
"achievements": [],
"courses": [],
"concentrations": [],
"student_organizations": [],
"faculty": [],
"research": [],
"events": [],
"facilities": [],
"general_info": []
}
# Keywords for categorization
self.category_keywords = {
"achievements": ["achievement", "award", "recognition", "honor", "success", "won", "ranked", "accomplishment"],
"courses": ["course", "class", "curriculum", "syllabus", "credit hour", "prerequisite", "corequisite", "CSCI", "ITIS"],
"concentrations": ["concentration", "major", "minor", "specialization", "track", "degree", "program", "BS in", "MS in"],
"student_organizations": ["club", "organization", "society", "association", "student group", "ACM", "student chapter"],
"faculty": ["faculty", "professor", "instructor", "dean", "chair", "director", "Dr.", "Ph.D"],
"research": ["research", "thesis", "dissertation", "publication", "journal", "conference", "study", "investigation", "project", "honors college", "graduate studies", "independent studies", "library resources", "thesis preparation", "research honors"],
"events": ["event", "seminar", "workshop", "conference", "hackathon", "competition", "meeting", "ceremony"],
"facilities": ["lab", "laboratory", "classroom", "center", "building", "brinkley", "facility", "equipment"],
"general_info": ["about", "mission", "vision", "contact", "location", "schedule", "deadline", "application"]
}
def _parse_markdown(self):
"""Parse the markdown file into sections"""
with open(self.input_file, 'r', encoding='utf-8') as f:
content = f.read()
# Split the content by the separator
sections = content.split("---")
# Process each section
for section in sections:
if not section.strip():
continue
# Extract title and URL if available
title_match = re.search(r'##\s+(.*?)\n', section)
url_match = re.search(r'\*\*URL:\*\*\s+(.*?)\n', section)
if title_match:
title = title_match.group(1).strip()
url = url_match.group(1).strip() if url_match else "No URL"
# Remove the title and URL lines to get just the content
content_text = section
content_text = re.sub(r'##\s+.*?\n', '', content_text)
content_text = re.sub(r'\*\*URL:\*\*\s+.*?\n', '', content_text)
self.content_sections.append({
"title": title,
"url": url,
"content": content_text.strip()
})
def _categorize_content(self):
"""Categorize content based on keywords and URL patterns"""
for section in self.content_sections:
categorized = False
combined_text = f"{section['title']} {section['content']}".lower()
url = section['url'].lower()
# First check URL patterns which are more reliable indicators
url_patterns = {
"research": ["/research/", "honors-in-discipline", "gradschool/etd", "thesis"],
"courses": ["/courses/", "/curriculum/", "/syllabus/"],
"faculty": ["/faculty/", "/staff/", "/people/", "/directory/"],
"student_organizations": ["/student-organizations/", "/clubs/", "/societies/"],
"achievements": ["/news/", "/achievements/", "/awards/"],
"concentrations": ["/programs/", "/degrees/", "/majors/", "/concentrations/"],
"events": ["/events/", "/calendar/", "/schedule/"],
"facilities": ["/facilities/", "/labs/", "/resources/"]
}
# Check URL patterns first
for category, patterns in url_patterns.items():
for pattern in patterns:
if pattern in url:
self.categories[category].append(section)
categorized = True
break
if categorized:
break
# If not categorized by URL, check keywords
if not categorized:
for category, keywords in self.category_keywords.items():
for keyword in keywords:
if keyword.lower() in combined_text:
self.categories[category].append(section)
categorized = True
break
if categorized:
break
# If still not categorized, add to general info
if not categorized:
self.categories["general_info"].append(section)
def _extract_key_facts(self, section):
"""Extract potential key facts based on patterns"""
content = section["content"]
facts = []
# Patterns for potential facts
patterns = [
# Lists
r'• (.*?)(?=\n• |\n\n|$)', # Bullet points
r'\d+\.\s+(.*?)(?=\n\d+\. |\n\n|$)', # Numbered lists
# Sentences with significant markers
r'(.*?students.*?\.)',
r'(.*?faculty.*?\.)',
r'(.*?program.*?\.)',
r'(.*?course.*?\.)',
r'(.*?award.*?\.)',
r'(.*?research.*?\.)',
r'(.*?new.*?\.)',
r'(.*?first.*?\.)',
r'(.*?best.*?\.)',
r'(.*?only.*?\.)',
r'(.*?largest.*?\.)',
r'(.*?highest.*?\.)',
]
for pattern in patterns:
matches = re.findall(pattern, content, re.IGNORECASE)
for match in matches:
# Clean up the fact and add if it's not too short
fact = match.strip()
if len(fact) > 15 and fact not in facts:
facts.append(fact)
return facts
def process(self):
"""Process the content file and generate organized output"""
print(f"Processing content from {self.input_file}")
# Parse the markdown file
self._parse_markdown()
print(f"Found {len(self.content_sections)} content sections")
# Categorize the content
self._categorize_content()
# Generate category files
for category, sections in self.categories.items():
if sections:
self._generate_category_file(category, sections)
# Generate a key facts file
self._generate_key_facts_file()
# Generate a combined markdown file
self._generate_combined_file()
print(f"Processing complete. Results saved to {self.output_dir}/")
def _generate_category_file(self, category, sections):
"""Generate a markdown file for a specific category"""
output_file = os.path.join(self.output_dir, f"{category}.md")
with open(output_file, 'w', encoding='utf-8') as f:
f.write(f"# ETSU Computing: {category.replace('_', ' ').title()}\n\n")
for section in sections:
f.write(f"## {section['title']}\n\n")
f.write(f"**Source:** {section['url']}\n\n")
f.write(f"{section['content']}\n\n")
f.write("---\n\n")
def _generate_key_facts_file(self):
"""Generate a file with potential key facts"""
output_file = os.path.join(self.output_dir, "key_facts.md")
with open(output_file, 'w', encoding='utf-8') as f:
f.write("# ETSU Computing: Key Facts\n\n")
f.write("*These are potential talking points extracted from the website content*\n\n")
for category, sections in self.categories.items():
if sections:
f.write(f"## {category.replace('_', ' ').title()}\n\n")
for section in sections:
facts = self._extract_key_facts(section)
if facts:
f.write(f"### From: {section['title']}\n\n")
for fact in facts:
f.write(f"- {fact}\n")
f.write("\n")
f.write("\n*Note: These facts were automatically extracted and may need review.*\n")
def _generate_combined_file(self):
"""Generate a single combined file with all content organized by category"""
output_file = os.path.join(self.output_dir, "all_content.md")
with open(output_file, 'w', encoding='utf-8') as f:
f.write("# ETSU Computing Department: Complete Extracted Content\n\n")
f.write("*This file contains all extracted content organized by category*\n\n")
# Table of contents
f.write("## Table of Contents\n\n")
for category in self.categories.keys():
if self.categories[category]:
f.write(f"- [{category.replace('_', ' ').title()}](#{category})\n")
f.write("\n")
# Category content
for category, sections in self.categories.items():
if sections:
f.write(f"<a id='{category}'></a>\n")
f.write(f"## {category.replace('_', ' ').title()}\n\n")
for section in sections:
f.write(f"### {section['title']}\n\n")
f.write(f"**Source:** {section['url']}\n\n")
f.write(f"{section['content']}\n\n")
f.write("---\n\n")
def main():
parser = argparse.ArgumentParser(description='Extract and organize content from crawled website data')
parser.add_argument('--input', type=str, default='etsu_computing_content.md',
help='Input markdown file with crawled content')
parser.add_argument('--output-dir', type=str, default='extracted_content',
help='Directory to save organized content files')
args = parser.parse_args()
extractor = ContentExtractor(
input_file=args.input,
output_dir=args.output_dir
)
extractor.process()
if __name__ == "__main__":
main()