-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch_chat.py
142 lines (99 loc) · 4.27 KB
/
match_chat.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
# matches questions from live chat with responses from moderators
import get_chat
import re
from fuzzywuzzy import fuzz
def get_unique_users(chat):
""" get a dictionary of unique users from the chat """
unique_users = { }
for message in chat:
unique_users[message['user']] = message['privilege']
return unique_users
def is_question(message, unique_users, q):
""" returns the message if it is a question and None if it is not"""
line = get_tagged_user(message['text'], unique_users)[1]
if '?' in line:
return line
START_WORDS = ['can', 'do', 'will', 'how', 'when', 'what', 'where',
'why', 'is', 'does', "doesn't", 'if', 'for', 'did', 'is']
for word in START_WORDS:
if line.lower().startswith(word):
return line
if fuzz.ratio(line, q) > 20:
return line
return None
def find_question_expanded(message, unique_users, q):
""" finding potential questions with extra checks"""
user_tagged = get_tagged_user(message['text'], unique_users)[0]
# check if this message is an answer to another student's question
if user_tagged != None and unique_users[user_tagged] == 'member':
return None
# check if message is a question
question = is_question(message, unique_users, q)
return question
def get_tagged_user(line, unique_users):
""" given a chat message, return the user tagged"""
tagged_user = None
for user in unique_users:
tagged_user = re.search(f"@{user}\s*", line)
if tagged_user != None:
tagged_user = tagged_user.group(0).replace("@", "").strip()
line = line.replace(f"@{user} ", "")
break
return (tagged_user, line)
def find_corresponding_question(chat, tagged_user, prev_mod_index, index, q, unique_users):
""" given a moderator message, find the corresponding question """
# searches from the previous message sent by the moderator
for i in range(index, prev_mod_index[1], -1):
message = chat[i]
if message['user'] == tagged_user:
question = is_question(message, unique_users, q)
if question != None:
return question
# expanding the search space ft. some additional checks
for i in range(prev_mod_index[1], prev_mod_index[0], -1):
message = chat[i]
if message['user'] == tagged_user:
question = find_question_expanded(message, unique_users, q)
if question != None:
return question
return None
def match_chat(chat):
""" search through questions in the live chat and match them with
answers from moderators in the chat """
unique_users = get_unique_users(chat)
qna = {}
prev_mod_index = (0, 0) # index of the last message sent by a moderator
for index, message in enumerate(chat):
privilege, text = message['privilege'], message['text']
if privilege in ['moderator', 'owner']:
# match q&a if the moderator tags the person they're answering
(tagged_user, answer) = get_tagged_user(text, unique_users)
if tagged_user == None:
continue
# moderator probably asked a clarifying question. ignore these.
if '?' in answer:
continue
question = find_corresponding_question(chat, tagged_user, prev_mod_index, index, text, unique_users)
if question == None:
continue
if question in qna:
qna[question]['answer'] += f'\n{answer}'
else:
qna[question] = {
"question": question,
"answer": f'(MODERATOR) {answer}',
"time": message['time'],
"user": tagged_user,
"moderator": message['user'],
"moderator_response": True
}
# updates qna search space
prev_mod_index = (prev_mod_index[0], index)
qna = list(qna.values())
return qna
if __name__ == '__main__':
chat = get_chat.id_to_chat_split('PVUM2ezD9OE')
qna = match_chat(chat)
for question in qna:
print(f"QUESTION: {question}")
print(f"ANSWER: {qna[question]['answer']}\n")