-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinterview.py
140 lines (111 loc) · 3.86 KB
/
interview.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
from email.mime.text import MIMEText
from termcolor import colored
import markovify
import datetime
import smtplib
import random
import time
import json
import sys
import os
# Get model for prompts
with open('./model.json') as data_file:
model = json.load(data_file)
# Get raw text as string.
with open("./words.txt") as f:
text = f.read()
index = 0
transcript = {}
interviewer = ''
email = ''
bot_email = os.environ.get('INTERVIEW_BOT_EMAIL')
bot_email_password = os.environ.get('INTERVIEW_BOT_EMAIL_PASSWORD')
interviewee = os.environ.get('INTERVIEWEE')
interviewee_email = os.environ.get('INTERVIEWEE_EMAIL')
def is_common_question(interview_question):
q = interview_question.lower()
answer = False
for j in model['common']:
hasQ = j in q
# question is within 10 characters of common question
closeEnough = len(q) <= (len(j) + 5) and len(q) >= (len(j) - 5)
if hasQ and closeEnough:
answer = str(model['common'][j])
return answer
def respond(interview_question):
global index
global transcript
answer = ''
# get question words
wordlist = interview_question.split()
# check if actually a question
if not wordlist[-1].endswith('?') and wordlist[-1] != '?':
return 'Please, let\'s stick to questions only.'
# check if already asked
if (len(transcript) >= 1):
for i in transcript:
if (transcript[i]['question'].lower() == interview_question.lower()):
return interviewer + ', please. You already asked me that.'
# check if common question
common = is_common_question(interview_question)
if (type(common) is str):
return common
# Build the model.
text_model = markovify.Text(text)
# generate response
for i in range(random.randint(1, 5)):
answer += text_model.make_sentence()
# save question + response
# transcript[str(index)]['answer'] = nswer
transcript.update({index: {
'question': interview_question,
'response': answer
}})
# increment index + return response
index += 1
return answer
def end_interview():
print colored('Ok, ', 'red') + colored(interviewer, 'magenta') + colored(', thats enough questions for today. Thank you.', 'red')
print colored('I\'ll send you an email of our conversation.', 'red')
print ''
message = ''
for q in transcript:
message += ('Question #' + str(q) + ': ')
message += '\n'
message += ('You asked: ' + transcript[q]['question'])
message += '\n'
message += ('I answered: ' + transcript[q]['response'])
message += '\n'
message += '\n'
content = MIMEText(str(message))
content['Subject'] = interviewer + ', here\'s the ' + interviewee + ' Interview from ' + str(datetime.date.today()) + ' | ' + time.strftime("%H:%M:%S")
s = smtplib.SMTP('smtp.gmail.com', 587)
s.ehlo()
s.starttls()
s.login(bot_email, bot_email_password)
s.sendmail(bot_email, email, content.as_string())
s.sendmail(interviewee_email, email, content.as_string())
s.close()
sys.exit()
def question_loop():
global index
if index > len(model['prompts']) - 1:
end_interview()
return
question_str = model['prompts'][index]
interview_question = raw_input(colored(question_str, 'yellow'))
response = respond(interview_question)
print colored('-----------------------------', 'yellow')
print (colored(response, 'white'))
print ''
time.sleep(1)
question_loop()
def start_interview():
global interviewer
global email
interviewer = raw_input(colored('Hey there! What\'s your name? => ', 'yellow'))
print ''
email = raw_input(colored(interviewer, 'magenta') + colored(', What\'s your email? I\'ll send a transcript of our interview => ', 'yellow'))
print ''
question_loop()
start_interview()