-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
153 lines (128 loc) · 4.59 KB
/
main.py
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
from flask import Flask
from flask import request
import pandas as pd
import re
from links import links
app = Flask(__name__)
df = pd.read_csv('lowercase-transcript.csv')
@app.route("/")
def index():
keywords = request.args.get("keywords", "")
if keywords:
res = filter_transcript(keywords)
else:
res = ""
return (
"""
<!DOCTYPE html>
<html>
<style>
body
{
margin:auto;
width:80%;
padding:10px;
background-color:#1a1a1a;
font-size:18px;
font-family:Helvetica;
color:#bbbbbb;
}
input:focus::placeholder
{
color: transparent;
}
input[type=text]
{
width: 100%;
height: 60px;
padding: 12px 20px;
margin: 8px 0;
box-sizing: border-box;
border: 2px solid red;
border-radius: 4px;
font-size: 20px;
}
input[type=submit]
{
background-color: white;
border: none;
color: black;
padding: 16px 32px;
text-decoration: none;
margin: 4px 2px;
cursor: pointer;
}
a:link
{
color:#0096FF;
}
a:visited {
color:#0096FF;
}
a:hover {
color:#89CFF0;
}
</style>
<h1> Helper 138 </h1>
<h3> <a href="https://cdn.discordapp.com/attachments/1013687788807405579/1049190892583526443/all138slidesAnnotated.pdf">Link to PDF of all slides</a>
(Includes table of contents for each chapter, section, and subsection!) </h3>
<h3> <a href="https://youtube.com/playlist?list=PLis31mB9Uihr9Z_wFL0rMAP__CbuFYZVx" target="_blank" rel="noopener noreferrer">Link to all lectures Youtube Playlist</a> </h3>
<h2> Instructions </h2>
This is a study tool for CSC138.
In the box below, you can search keywords or phrases from all lectures.
To search multiple keywords, separate them by a comma as shown in the example below.
Only exact matches will be found.
<br><br>
For example, you can type something like <code style="background-color:#301934"> test,exam,DNS,dns</code> into the search box
and all occurences of the professor saying any of those keywords will be displayed.
<br>
<br>
Click the timestamp link to go to that part of the lecture. Goodluck on the final!
<br><br>
<form action="" method="get">
<input type="text" placeholder="type something here" name="keywords">
<input type="submit" value="Search">
</form>"""
+ res +
"""
</body>
</html>"""
)
def filter_transcript(keywords: str):
_keywords = keywords.lower().replace(',', '|')
rowsExpanded = []
res = ''
# Regex pattern for exact matching words e.g. '\b(final|exam|test)\b'
# not that \b needs to be escaped
pattern = rf'\b({_keywords})\b'
rowMatches = df.index[df['text'].str.contains(pattern) == True].tolist()
# adding surrounding rows for more context
for i in rowMatches:
rowsExpanded.append([i - 2, i - 1, i, i + 1, i + 2])
for i in rowsExpanded:
try:
timestamp = df.iloc[i[0]]['timestamp']
video = df.iloc[i[0]]['video']
text = ''
# Calculate time in seconds from timestamp
time = timestamp.split(':', 1)
seconds = (int(time[0]) * 60) + int(time[1])
# Accumulate all text from surrounding rows
for rownum in i:
text += df.iloc[rownum]['text'] + '\n'
# Find and replace keywords with HTML color tags using regex
# FIXME: paragraphs with more than one match get double colored in html
for m in re.finditer(pattern, text):
text = re.sub(
rf'\b{m.group(0)}\b', f'<b><span style="color:#FF3131">{m.group(0)}</span></b>', text)
# Accumulate
res += f'<p><a href="{links[video]}?t={seconds}" target="_blank" rel="noopener noreferrer">{video} Lecture - {timestamp}</a>' \
+ '<br>' + text + '</p>'
except Exception as e:
print(e) # Most likely due to index OOB
continue
return str(res)
def replColor(m):
return f'<b><span style="color:# FF3131">{m.group(0)}</span></b>'
if __name__ == "__main__":
app.run(host="127.0.0.1", port=8080, debug=True)