/
LucyResponder.cs
203 lines (160 loc) · 5.97 KB
/
LucyResponder.cs
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Linq;
using System.Collections.Generic;
using Lucy.Knowledge;
using Lucy.Communication;
using Lucy.DatabaseHandling;
namespace Lucy
{
class LucyResponder
{
const int KEYWORD_NOT_FOUND_IN_INPUT = -1;
string userInput;
Database database;
KnowledgeSearch knowledgeSearch;
string response;
string lastResponse;
List<string> availableResponses;
string lastContext;
string currentContext;
public string Response { get { return response; } }
public LucyResponder(Database database)
{
this.lastContext = string.Empty;
this.currentContext = string.Empty;
this.response = string.Empty;
this.lastResponse = string.Empty;
this.availableResponses = new List<string>();
this.database = database;
this.knowledgeSearch = new KnowledgeSearch(database);
}
public void GetResponse(string userInput)
{
this.userInput = userInput;
GetAvailableResponses();
if (UnderstandsUserInput())
{
SelectResponse();
PostProcessTransposition();
lastResponse = response;
}
else
response = "SORRY, I DIDN'T UNDERSTAND WHAT YOU JUST SAID.";
}
private void GetAvailableResponses()
{
availableResponses.Clear();
foreach (Expression expr in database.Expressions.Expressions)
if (expr.Type == ExpressionType.Communication)
GetCommunicationResponses(expr);
else
GetKnowledgeResponse();
}
private void GetCommunicationResponses(Expression expr)
{
foreach (string keyword in expr.Keywords)
if (EligibleAsResponse(expr, keyword))
AddExpressionResponsesToResponseList(expr);
}
private bool EligibleAsResponse(Expression expr, string keyword)
{
string ui = StringHelper.InsertSpacesBAF(userInput);
int keywordPosition = ui.IndexOf(StringHelper.InsertSpacesBAF(keyword));
if (keywordPosition != KEYWORD_NOT_FOUND_IN_INPUT)
{
if (WrongLocation(keyword, expr.Location, keywordPosition))
return false;
if (WrongContext(expr.Contexts))
return false;
return true;
}
return false;
}
private void AddExpressionResponsesToResponseList(Expression expr)
{
foreach (string response in expr.Responses)
if (!availableResponses.Contains(response))
availableResponses.Add(response);
}
private bool WrongLocation(string keyword, ExpressionLocation exprLocation,
int pos)
{
if (exprLocation == ExpressionLocation.IsIrrelevant) return false;
bool isNotAlone = exprLocation == ExpressionLocation.Alone && userInput != keyword;
bool isNotMiddle = exprLocation == ExpressionLocation.MiddleOrEnd && pos != userInput.Length;
bool isNotBegin = exprLocation == ExpressionLocation.BeginOrMiddle && pos == userInput.Length;
return isNotAlone || isNotMiddle || isNotBegin;
}
private bool WrongContext(string[] contexts)
{
if (contexts.Length == 0) return false;
string lastResponseContext = StringHelper.CleanString(lastResponse);
foreach (string context in contexts)
if (context == lastResponseContext)
{
lastContext = currentContext;
currentContext = context;
return false;
}
return true;
}
private void GetKnowledgeResponse()
{
string knowledge = knowledgeSearch.GetKnowledgeResponse(userInput);
availableResponses.Add(knowledge);
availableResponses.Remove(string.Empty);
}
public bool UnderstandsUserInput()
{
return availableResponses.Count > 0;
}
private void SelectResponse()
{
availableResponses.Remove(lastResponse);
string previousResponse = FindBestResponse();
response = previousResponse;
}
private string FindBestResponse()
{
string previousResponse = string.Empty;
foreach (string response in availableResponses)
if (response.Length > previousResponse.Length)
previousResponse = response;
return previousResponse;
}
private void PostProcessTransposition()
{
string subject = string.Empty;
if (response.Contains('*'))
{
subject = Transpose(userInput);
subject = ReplaceSymbol(response, '*', subject);
}
}
private string Transpose(string subject)
{
string transposed = subject;
foreach (Transposition transpo in database.Transpositions)
transposed = DoTransposition(transpo, transposed);
return transposed;
}
private string DoTransposition(Transposition transpo, string transposed)
{
string first = transpo.First;
string second = transpo.Second;
InsertSpace(first);
InsertSpace(second);
return transposed.Replace(first, second);
}
private string ReplaceSymbol(string toChange, char symbol, string change)
{
int pos = toChange.IndexOf(symbol);
return toChange.Remove(pos) + change;
}
private void InsertSpace(string str)
{
str = str.Insert(0, " ");
str = str.Insert(str.Length - 1, " ");
}
}
}