Skip to content

Commit

Permalink
Implemented Jaro-Winkler algorithm to improve question search
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-i committed Apr 18, 2023
1 parent b97f1dd commit 1a0a7aa
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/assets/talk.glowbom
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"title":"Glowby","main_color":"Black","conclusion":"Please send us your information and we'll come back to you shortly!","start_over":"Glowby","show_number_result":true,"show_percentage_result":true,"voice":true,"questions":[{"title":"","description":"Hello","buttonsTexts":["Hello!","Hi!","Hi there!","Hey, What's up?"],"buttonAnswers":[0,0,0,0],"answersCount":0,"goIndexes":[2,2,2,2],"answerPicture":"","answerPictureDelay":0,"goConditions":[],"heroValues":[],"picturesSpriteNames":["","","","","",""]},{"title":"","description":"How's it going?","buttonsTexts":["I'm ok.","I'm doing quite well, thank you.","Pretty good. How goes it for you?","I'm good, thanks for asking!"],"buttonAnswers":[0,0,0,0],"answersCount":0,"goIndexes":[2,2,2,2],"answerPicture":"","answerPictureDelay":0,"goConditions":[],"heroValues":[],"picturesSpriteNames":["","","","","",""]},{"title":"","description":"What's up?","buttonsTexts":["Not much. Just enjoying my time on this sim.","You just summed up my worldview.","Nothing really, just keeping busy.","I can't tell you!"],"buttonAnswers":[0,0,0,0],"answersCount":0,"goIndexes":[2,2,2,2],"answerPicture":"","answerPictureDelay":0,"goConditions":[],"heroValues":[],"picturesSpriteNames":["","","","","",""]}],"dnsgs":false}
{"title":"Glowby","main_color":"Black","conclusion":"Please send us your information and we'll come back to you shortly!","start_over":"Glowby","show_number_result":true,"show_percentage_result":true,"voice":true,"questions":[{"title":"","description":"Hello","buttonsTexts":["Hello!","Hi!","Hi there!","Hey, What's up?"],"buttonAnswers":[0,0,0,0],"answersCount":0,"goIndexes":[2,2,2,2],"answerPicture":"","answerPictureDelay":0,"goConditions":[],"heroValues":[],"picturesSpriteNames":["","","","","",""]},{"title":"","description":"How's it going?","buttonsTexts":["I'm ok.","I'm doing quite well, thank you.","Pretty good. How goes it for you?","I'm good, thanks for asking!"],"buttonAnswers":[0,0,0,0],"answersCount":0,"goIndexes":[2,2,2,2],"answerPicture":"","answerPictureDelay":0,"goConditions":[],"heroValues":[],"picturesSpriteNames":["","","","","",""]},{"title":"","description":"What's up?","buttonsTexts":["Not much. Just enjoying my time on this sim.","You just summed up my worldview.","Nothing really, just keeping busy.","I can't tell you!"],"buttonAnswers":[0,0,0,0],"answersCount":0,"goIndexes":[2,2,2,2],"answerPicture":"","answerPictureDelay":0,"goConditions":[],"heroValues":[],"picturesSpriteNames":["","","","","",""]}],"dnsgs":true}
63 changes: 62 additions & 1 deletion app/lib/ai.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,56 @@ class Ai {
return [];
}

double jaroSimilarity(String s1, String s2) {
int maxDistance = (s1.length / 2).floor() - 1;

List<bool> matches1 = List.filled(s1.length, false);
List<bool> matches2 = List.filled(s2.length, false);

int matches = 0;
int transpositions = 0;

for (int i = 0; i < s1.length; i++) {
int start = max(0, i - maxDistance);
int end = min(i + maxDistance + 1, s2.length);

for (int j = start; j < end; j++) {
if (matches2[j]) continue;
if (s1[i] != s2[j]) continue;
matches1[i] = true;
matches2[j] = true;
matches++;
break;
}
}

if (matches == 0) return 0.0;

int k = 0;
for (int i = 0; i < s1.length; i++) {
if (!matches1[i]) continue;
while (!matches2[k]) k++;
if (s1[i] != s2[k]) transpositions++;
k++;
}

double m = matches.toDouble();
return (m / s1.length + m / s2.length + (m - transpositions / 2) / m) / 3;
}

double jaroWinkler(String s1, String s2, {double p = 0.1}) {
double jaro = jaroSimilarity(s1, s2);
int prefix = 0;
for (int i = 0; i < min(s1.length, s2.length); i++) {
if (s1[i] == s2[i]) {
prefix++;
} else {
break;
}
}
return jaro + prefix * p * (1 - jaro);
}

/// Searches the AI's question database for matching questions based on the user's input.
///
/// [message] is the sanitized input message from the user.
Expand Down Expand Up @@ -76,15 +126,26 @@ class Ai {
/// [userMessage] is the sanitized input message from the user.
List<Map<String, Object>> _searchForQuestions(String userMessage) {
List<Map<String, Object>> foundQuestions = [];
double similarityThreshold =
0.98; // You can adjust this value to fine-tune the matching algorithm

for (var questionMap in _questions!) {
var question = _sanitizeMessage(questionMap['description'].toString());
double similarity = jaroWinkler(userMessage, question);

if (userMessage.contains(question)) {
if (similarity >= similarityThreshold) {
foundQuestions.add(questionMap);
}
}

// Sort the found questions by their similarity in descending order
foundQuestions.sort((a, b) {
var aQuestion = _sanitizeMessage(a['description'].toString());
var bQuestion = _sanitizeMessage(b['description'].toString());
return jaroWinkler(userMessage, bQuestion)
.compareTo(jaroWinkler(userMessage, aQuestion));
});

return foundQuestions;
}

Expand Down

0 comments on commit 1a0a7aa

Please sign in to comment.