-
Notifications
You must be signed in to change notification settings - Fork 0
/
magic.py
47 lines (34 loc) · 1.52 KB
/
magic.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
# matches questions from live chat with responses from lecturer/speaker
from fuzzywuzzy import fuzz
def is_responding_to_chat(transcript_text, i):
""" determines if speech from the transcript is in response to the chat"""
keywords_set = {"ask", "asking", "asks", "asked", "question", "questions",
"chat", "chats", "messages"}
transcript_line = transcript_text[i]
transcript_line_set = set(transcript_line.split(" "))
if not keywords_set.isdisjoint(transcript_line_set):
speech = transcript_text[i:i+3]
answer = transcript_text[i+3:i+20]
return (speech, answer)
return (None, None)
def magic(transcript, chat):
""" matches questions from live chats with answers from lecture video"""
qna = {}
for i in range(len(transcript) - 20):
speech, answer = is_responding_to_chat(transcript, i)
if speech == None:
continue
for comment in chat:
potential_question = comment['text']
# if the lecturer repeats or paraphrases the question
if (fuzz.ratio(speech, potential_question) > 48):
if potential_question not in qna:
qna[potential_question] = {
"question": potential_question,
"answer": " ".join(answer),
"time": comment['time'],
"moderator_response": False,
}
break
qna = list(qna.values())
return qna