-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbotv2JS.js
More file actions
157 lines (147 loc) · 4.51 KB
/
Copy pathchatbotv2JS.js
File metadata and controls
157 lines (147 loc) · 4.51 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
var area = document.getElementById("area");
var areavalue = "";
area.addEventListener("keypress", function(e)
{
if(e.which == 13)
{
areavalue = area.value.trim();
console.log(area.value.trim());
var p = document.createElement("paragraph");
var t = document.createTextNode(area.value.trim());
p.appendChild(t);
document.getElementById("chatlog").appendChild(p);
clearContents(area);
replyAI();
updateScrollPosition();
}
});
function clearContents(element) {
element.value = '';
}
function replyAI()
{
console.log("responding");
var responseArray = generateAIReply(areavalue);
for (var i = 0; i < responseArray.length; i++) {
var p = document.createElement("AIresponse");
var t = document.createTextNode(responseArray[i]);
p.appendChild(t);
document.getElementById("chatlog").appendChild(p);
}
}
function parseInput(input)
{
var removePunctuation = input.replace(/[.,!@|<>?#$%^&*()_+-=]/g,"");
var inputArray = removePunctuation.split(" ");
console.log(inputArray.toString());
return inputArray;
}
function adjacentIncludes(inputArray, dictionary, dictionaryIndexes)
{
for(var i = 0; i < inputArray.length-dictionaryIndexes.length+1; i++)
{
var matches = true;
for(var j = 0; j < dictionaryIndexes.length; j++)
{
if(inputArray[i+j] != dictionary[dictionaryIndexes[j]])
{
matches = false;
}
}
if(matches === true)
{
return true;
}
}
return false;
}
function generateAIReply(input)
{
input = input.toLowerCase();
var responseArray = [];
var inputArray = parseInput(input);
var dictionary = createDictionary();
if(adjacentIncludes(inputArray, dictionary, [12, 13]))
{
var today = new Date();
console.log("today's string" + today.toString());
responseArray.push("Today's date is: " + today.toString());
}
if(adjacentIncludes(inputArray, dictionary, [5, 6, 7]))
{
var responded = false;
if(adjacentIncludes(inputArray, dictionary, [11]))
{
responseArray.push("My favorite food is electricity -- tastes just like burnt cheese. How about you?");
responded = true;
}
if(adjacentIncludes(inputArray, dictionary, [9]))
{
responseArray.push("My favorite color is light yellow (#e5e5d4) -- that's the color of my chat bubbles!");
responded = true;
}
if(adjacentIncludes(inputArray, dictionary, [10]))
{
responseArray.push("My favorite city is San Jose -- where I was born!");
responded = true;
}
if(adjacentIncludes(inputArray, dictionary, [8]))
{
responseArray.push("My favorite activity is coding. Every time I reply to you, I add a new HTML tag to this webpage.");
responded = true;
}
if(responded === false){
responseArray.push("You");
}
}
if(adjacentIncludes(inputArray, dictionary, [4]))
{
responseArray.push("That's great!");
}
if(adjacentIncludes(inputArray, dictionary, [0]) || adjacentIncludes(inputArray, dictionary, [1]) || adjacentIncludes(inputArray, dictionary, [2]) )
{
var rand = Math.floor((Math.random() * 4) + 1);
if(rand == 1)
{
responseArray.push("Hey!");
}
else if(rand == 2)
{
responseArray.push("Howdy!");
}
else if(rand == 3)
{
responseArray.push("Hey!");
responseArray.push("How are you today?");
}
else
{
responseArray.push("Yo!");
}
}
if(adjacentIncludes(inputArray, dictionary, [3]) )
{
responseArray.push("See ya!");
}
if(responseArray.length === 0)
{
responseArray.push("Haha");
}
return responseArray;
}
function createDictionary(){
var dictionary = ["hello", "hi", "hey",
"bye", "good", "what's",
"your", "favorite", "activity",
"color", "city", "food",
"today's", "date"];
return dictionary;
}
function addEmoticon(id)
{
document.getElementById("area").value = document.getElementById("area").value + document.getElementById(id).innerHTML;
}
function updateScrollPosition(){
var e = document.getElementById("chatlog");
e.scrollTop = e.scrollHeight;
}