-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot.py
107 lines (81 loc) · 2.9 KB
/
chatbot.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
"""
This is the main script
"""
import datetime
import sqlite3
from sqlite3 import Error
import nltk.sentiment
from chatterbot import ChatBot
from pythonosc import udp_client
def _create_connection(db_file):
""" Create a database connection to the SQLite database """
try:
conn = sqlite3.connect(db_file)
cur = conn.cursor()
# Create a new SQLite table
cur.execute("CREATE TABLE {tn} ({r1}, {r2}, {time} {ft})"
.format(tn=TABLE_NAME, r1=INPUT_COLUMN, r2=OUTPUT_COLUMN,
time='time', ft='TEXT'))
except Error as err:
print(err)
finally:
conn.commit()
conn.close()
def _log_conversation(db_file, line):
""" Log conversation in SQLite database """
try:
conn = sqlite3.connect(db_file)
cur = conn.cursor()
cur.execute("""INSERT INTO {tn} ({c1}, {c2}, {time}) VALUES ("{v1}", "{v2}", "{now}")""".
format(tn=TABLE_NAME, c1=INPUT_COLUMN, c2=OUTPUT_COLUMN, time='time',
v1=' '.join(line.keys()), v2=' '.join(line.values()),
now=str(datetime.datetime.now())))
conn.commit()
except Error as err:
print(err)
finally:
conn.close()
def main(text):
"""This is the main function to run the CHATBOT, analyse
the responses with nltk and send OSC messages to Pure Data.
"""
# Get CHATBOT response from the user input.
bot_response = CHATBOT.get_response(text).text
print(bot_response)
# Get polarity score from CHATBOT response.
analysis = VADER_ANALYZER.polarity_scores(text)
# Change polarity score relatively to a audible frequency.
freq = (analysis['compound'] - -1) / (1 - -1) * (800 - 200) + 200
# Send OSC message, to be listened to by pd.
CLIENT.send_message("/filter", freq)
# Log conversation.
exchange = {text: bot_response}
_log_conversation("conversation.db", exchange)
if __name__ == '__main__':
# Set up database
TABLE_NAME = 'conversation_log'
INPUT_COLUMN = 'input_column'
OUTPUT_COLUMN = 'output_column'
CONVERSATION_DB = "conversation.db"
_create_connection(CONVERSATION_DB)
# Set up chatbot.
CHATBOT = ChatBot(
'Sentiment Music Bot',
trainer='chatterbot.trainers.ChatterBotCorpusTrainer')
# Train based on the english corpus.
CHATBOT.train("chatterbot.corpus.english")
# Download lexicon for nltk.
nltk.download('vader_lexicon')
# Set up sentiment analyzer.
VADER_ANALYZER = nltk.sentiment.vader.SentimentIntensityAnalyzer()
# Set up OSC client.
IP = 'localhost'
PORT = 9000
CLIENT = udp_client.SimpleUDPClient(IP, PORT)
# Run chatbot.
while True:
USER_RESPONSE = input("Talk ('exit' to exit): ")
if USER_RESPONSE == 'exit': # Exit on 'exit' string.
break
else:
main(USER_RESPONSE)