-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_transcripts.py
More file actions
43 lines (37 loc) · 1.46 KB
/
Copy pathsearch_transcripts.py
File metadata and controls
43 lines (37 loc) · 1.46 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
import json
import re
TRANSCRIPT_FILE = "all_transcripts.json"
def load_transcripts(file_path):
try:
with open(file_path, "r", encoding="utf-8") as f:
return json.load(f)
except FileNotFoundError:
print(f"⚠️ File not found: {file_path}")
return []
except json.JSONDecodeError:
print(f"⚠️ Failed to parse JSON in {file_path}")
return []
def count_occurrences(text, search_term):
pattern = re.compile(re.escape(search_term), re.IGNORECASE)
return len(pattern.findall(text))
def search_transcripts(transcripts, search_term):
results = []
for item in transcripts:
title = item.get("title", "Untitled")
transcript = item.get("transcript", "")
count = count_occurrences(transcript, search_term)
if count > 0:
results.append((title, count))
return sorted(results, key=lambda x: x[1], reverse=True)
def display_results(results, search_term):
if not results:
print(f"🔍 No results found for '{search_term}'.")
return
print(f"\n📊 Search results for: '{search_term}'")
for rank, (title, count) in enumerate(results, 1):
print(f"{rank:>2}. {title} — {count} mention(s)")
if __name__ == "__main__":
search_term = input("Enter the word or phrase to search for: ").strip()
transcripts = load_transcripts(TRANSCRIPT_FILE)
matches = search_transcripts(transcripts, search_term)
display_results(matches, search_term)