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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import time
|
| 3 |
+
import random
|
| 4 |
+
import re
|
| 5 |
+
import sqlite3
|
| 6 |
+
from string import punctuation
|
| 7 |
+
from collections import Counter
|
| 8 |
+
from math import sqrt
|
| 9 |
+
|
| 10 |
+
def next():
|
| 11 |
+
print("\n" * 18)
|
| 12 |
+
|
| 13 |
+
def slow_type(x):
|
| 14 |
+
typing_speed = 50
|
| 15 |
+
for l in x:
|
| 16 |
+
sys.stdout.write(l)
|
| 17 |
+
sys.stdout.flush()
|
| 18 |
+
time.sleep(random.random() * 10.0/typing_speed)
|
| 19 |
+
L = "Person"
|
| 20 |
+
next()
|
| 21 |
+
print("\n\n🐶 Hi " + L, end = "")
|
| 22 |
+
|
| 23 |
+
A = ', how are you '
|
| 24 |
+
need_be = 5
|
| 25 |
+
count = 1
|
| 26 |
+
ae = 0
|
| 27 |
+
users_time = 0
|
| 28 |
+
emotion_lvl = 0
|
| 29 |
+
check = 1
|
| 30 |
+
total_time = 0
|
| 31 |
+
response_length = 0
|
| 32 |
+
analysis = "off"
|
| 33 |
+
total_rlen = 0
|
| 34 |
+
warning_rlen = 0
|
| 35 |
+
warning_low = .2
|
| 36 |
+
warning_hi = 1.275
|
| 37 |
+
l_wl = .2
|
| 38 |
+
Q = ["Why", "What", "How", "When", "?"]
|
| 39 |
+
while True:
|
| 40 |
+
if emotion_lvl != check:
|
| 41 |
+
check = emotion_lvl
|
| 42 |
+
dbase = str(emotion_lvl) + ".db"
|
| 43 |
+
connection = sqlite3.connect(dbase)
|
| 44 |
+
cursor = connection.cursor()
|
| 45 |
+
|
| 46 |
+
try:
|
| 47 |
+
|
| 48 |
+
cursor.execute('''
|
| 49 |
+
CREATE TABLE words (word TEXT UNIQUE)''')
|
| 50 |
+
|
| 51 |
+
cursor.execute('''
|
| 52 |
+
CREATE TABLE sentences (sentence TEXT UNIQUE, used INT NOT NULL DEFAULT 0)''')
|
| 53 |
+
|
| 54 |
+
cursor.execute('''
|
| 55 |
+
CREATE TABLE associations (word_id INT NOT NULL, sentence_id INT NOT NULL, weight REAL NOT NULL)''')
|
| 56 |
+
|
| 57 |
+
except:
|
| 58 |
+
pass
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def get_id(entityName, text):
|
| 62 |
+
tableName = entityName + 's'
|
| 63 |
+
columnName = entityName
|
| 64 |
+
cursor.execute('SELECT rowid FROM ' + tableName + ' WHERE ' + columnName + ' = ?', (text,))
|
| 65 |
+
row = cursor.fetchone()
|
| 66 |
+
if row:
|
| 67 |
+
return row[0]
|
| 68 |
+
else:
|
| 69 |
+
cursor.execute('INSERT INTO ' + tableName + ' (' + columnName + ') VALUES (?)', (text,))
|
| 70 |
+
return cursor.lastrowid
|
| 71 |
+
|
| 72 |
+
def get_words(text):
|
| 73 |
+
wordsRegexpString = '(?:\w+|[' + re.escape(punctuation) + ']+)'
|
| 74 |
+
wordsRegexp = re.compile(wordsRegexpString)
|
| 75 |
+
wordsList = wordsRegexp.findall(text.lower())
|
| 76 |
+
return Counter(wordsList).items()
|
| 77 |
+
|
| 78 |
+
if ae > 0:
|
| 79 |
+
print("\n" * 15)
|
| 80 |
+
name_random = random.randint(1, 5)
|
| 81 |
+
response_wait = random.randint(0, 2)
|
| 82 |
+
|
| 83 |
+
total_time += users_time
|
| 84 |
+
avg_time = total_time/count
|
| 85 |
+
|
| 86 |
+
total_rlen += response_length
|
| 87 |
+
avg_rlen = int(total_rlen/count)
|
| 88 |
+
|
| 89 |
+
if ae <= 1:
|
| 90 |
+
Z = ("")
|
| 91 |
+
id_number = 2
|
| 92 |
+
ae += 1
|
| 93 |
+
|
| 94 |
+
if ae >= 2:
|
| 95 |
+
ae += 1
|
| 96 |
+
count += 1
|
| 97 |
+
if avg_time * warning_low >= users_time >= avg_time * warning_hi:
|
| 98 |
+
if count - 1 > need_be:
|
| 99 |
+
emotion_lvl = "Annoyed"
|
| 100 |
+
# 4s
|
| 101 |
+
# 1.35 = 5s
|
| 102 |
+
# * .075 = 1s
|
| 103 |
+
if response_length < avg_rlen * l_wl:
|
| 104 |
+
if count - 1 > need_be:
|
| 105 |
+
emotion_lvl = "Annoyed"
|
| 106 |
+
|
| 107 |
+
inB = B.title()
|
| 108 |
+
if "?" in inB:
|
| 109 |
+
emotion_lvl = "Normal"
|
| 110 |
+
id_number = 1
|
| 111 |
+
if "!" in inB:
|
| 112 |
+
emotion_lvl = "Confused"
|
| 113 |
+
id_number = 1
|
| 114 |
+
if inB == Q:
|
| 115 |
+
emotion_lvl = "Normal"
|
| 116 |
+
id_number = 1
|
| 117 |
+
if "Happy" in inB:
|
| 118 |
+
emotion_lvl = "Happy"
|
| 119 |
+
id_number = 1
|
| 120 |
+
if "Sad" in inB:
|
| 121 |
+
emotion_lvl = "Sad"
|
| 122 |
+
id_number = 1
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
inA = A.title()
|
| 126 |
+
if "Happy" in inA:
|
| 127 |
+
emotion_lvl = 3
|
| 128 |
+
id_number = 2
|
| 129 |
+
if "Sad" in inA:
|
| 130 |
+
emotion_lvl = 4
|
| 131 |
+
id_number = 2
|
| 132 |
+
if "Idk" in inA:
|
| 133 |
+
emotion_lvl = 5
|
| 134 |
+
id_number = 2
|
| 135 |
+
|
| 136 |
+
if emotion_lvl == "Normal":
|
| 137 |
+
Z = ("🐶<Normal> ")
|
| 138 |
+
if emotion_lvl == "Confused":
|
| 139 |
+
Z = ("🐶<What The Fuck?> ")
|
| 140 |
+
response_wait += 1
|
| 141 |
+
if emotion_lvl == "Happy":
|
| 142 |
+
Z = ("🐶<Wants To Hurt Ypu> ")
|
| 143 |
+
response_wait = 0
|
| 144 |
+
if emotion_lvl == "Very Happy":
|
| 145 |
+
Z = ("🐶<Wants To Hurt You A Lot> ")
|
| 146 |
+
if emotion_lvl == "Annoyed":
|
| 147 |
+
Z = ("🐶<I Hate You> ")
|
| 148 |
+
name_random = random.randint(1,3)
|
| 149 |
+
response_wait += 3
|
| 150 |
+
if emotion_lvl == "Sad":
|
| 151 |
+
Z = ("🐶<I'm Sad> ")
|
| 152 |
+
|
| 153 |
+
reading_time = response_length * .3
|
| 154 |
+
response_final = response_wait + reading_time
|
| 155 |
+
|
| 156 |
+
if ae > 1:
|
| 157 |
+
if "on" in analysis:
|
| 158 |
+
l = L.title()
|
| 159 |
+
if id_number == 1:
|
| 160 |
+
user = L
|
| 161 |
+
if id_number == 2:
|
| 162 |
+
user = "Computer"
|
| 163 |
+
if name_random == 1:
|
| 164 |
+
yes_no = "yes"
|
| 165 |
+
if name_random > 1:
|
| 166 |
+
yes_no = "no"
|
| 167 |
+
prop_check = users_time / avg_time
|
| 168 |
+
lw_wl = int(avg_rlen * l_wl)
|
| 169 |
+
print("(Anaylsis:\n")
|
| 170 |
+
print("Count: ", count - 1, "\n")
|
| 171 |
+
|
| 172 |
+
print("Subject: ", l)
|
| 173 |
+
print("Avg response time: ", avg_time)
|
| 174 |
+
print("Last response time: ", users_time)
|
| 175 |
+
print("Avg word length: ", avg_rlen)
|
| 176 |
+
print("Last word length: ", response_length, "\n")
|
| 177 |
+
|
| 178 |
+
print("Subject: Computer")
|
| 179 |
+
print("Reading time:", reading_time)
|
| 180 |
+
print("Response time selected:", response_wait)
|
| 181 |
+
print("Final response time: ", response_final)
|
| 182 |
+
print("Used Subjects Name?: ", yes_no, "(", name_random, ")", "\n")
|
| 183 |
+
if count - 1 >= need_be:
|
| 184 |
+
print("(AI)")
|
| 185 |
+
print("Time proportion check: ", prop_check)
|
| 186 |
+
print("Count ", count - 1, " warning response word length(L): ", lw_wl, "\n")
|
| 187 |
+
print("Emotion lvl: ", emotion_lvl, "(", user, ")", ")\n")
|
| 188 |
+
# Anaylsis ends
|
| 189 |
+
|
| 190 |
+
print(Z, end = "")
|
| 191 |
+
# time.sleep(response_final)
|
| 192 |
+
if name_random == 1:
|
| 193 |
+
slow_type(A + " " + L + "\n")
|
| 194 |
+
else:
|
| 195 |
+
slow_type(A + "\n")
|
| 196 |
+
start = time.time()
|
| 197 |
+
print("")
|
| 198 |
+
|
| 199 |
+
B = input(L + ": ").strip()
|
| 200 |
+
next()
|
| 201 |
+
stop = time.time()
|
| 202 |
+
users_time = stop - start
|
| 203 |
+
response_length = len(B.split())
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
words = get_words(A)
|
| 207 |
+
words_length = sum([n * len(word) for word, n in words])
|
| 208 |
+
sentence_id = get_id('sentence', B)
|
| 209 |
+
for word, n in words:
|
| 210 |
+
word_id = get_id('word', word)
|
| 211 |
+
weight = sqrt(n / float(words_length))
|
| 212 |
+
cursor.execute('INSERT INTO associations VALUES (?, ?, ?)', (word_id, sentence_id, weight,))
|
| 213 |
+
connection.commit()
|
| 214 |
+
|
| 215 |
+
cursor.execute('CREATE TEMPORARY TABLE results(sentence_id INT, sentence TEXT, weight REAL)')
|
| 216 |
+
|
| 217 |
+
words = get_words(B)
|
| 218 |
+
words_length = sum([n * len(word) for word, n in words])
|
| 219 |
+
for word, n in words:
|
| 220 |
+
weight = sqrt(n / float(words_length))
|
| 221 |
+
cursor.execute(
|
| 222 |
+
'INSERT INTO results SELECT associations.sentence_id, sentences.sentence, ?*associations.weight/(4+sentences.used) FROM words INNER JOIN associations ON associations.word_id=words.rowid INNER JOIN sentences ON sentences.rowid=associations.sentence_id WHERE words.word=?',
|
| 223 |
+
(weight, word,))
|
| 224 |
+
|
| 225 |
+
cursor.execute(
|
| 226 |
+
'SELECT sentence_id, sentence, SUM(weight) AS sum_weight FROM results GROUP BY sentence_id ORDER BY sum_weight DESC LIMIT 1')
|
| 227 |
+
row = cursor.fetchone()
|
| 228 |
+
cursor.execute('DROP TABLE results')
|
| 229 |
+
|
| 230 |
+
if row is None:
|
| 231 |
+
cursor.execute(
|
| 232 |
+
'SELECT rowid, sentence FROM sentences WHERE used = (SELECT MIN(used) FROM sentences) ORDER BY RANDOM() LIMIT 1')
|
| 233 |
+
row = cursor.fetchone()
|
| 234 |
+
|
| 235 |
+
A = row[1]
|
| 236 |
+
cursor.execute('UPDATE sentences SET used = used + 1 WHERE rowid = ?', (row[0],))
|