From bbe37c89e3c790232a05b1106ce343273a2addb8 Mon Sep 17 00:00:00 2001
From: Nobelf <13517042@std.stei.itb.ac.id>
Date: Thu, 14 Apr 2022 21:09:42 +0700
Subject: [PATCH] add get_close_matches

---
 app.py     |  13 ++++++++++++-
 front.html |   4 ----
 qa.sqlite3 | Bin 16384 -> 16384 bytes
 3 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/app.py b/app.py
index 902016f..5ea2517 100644
--- a/app.py
+++ b/app.py
@@ -1,5 +1,7 @@
 from flask import Flask
 from flask_socketio import SocketIO, send
+from database import QAPair
+from difflib import get_close_matches
 import time
 import threading
 
@@ -9,10 +11,19 @@ socketio = SocketIO(app, cors_allowed_origins='*')
 
 waiting_time = 10
 last_msg = time.time() + 12
+question = QAPair.query.with_entities(QAPair.question).all()
+questions = [a.question for a in question]
 
 @socketio.on('message')
 def handleMessage(msg):
-    send("message received : " + msg, broadcast=False) #will be replaced with QA matching function
+    
+    q = get_close_matches(msg, questions)
+    # send(q, broadcast=False)
+    if q:
+        ans = QAPair.query.filter_by(question = q[0]).first().answer
+        send(ans, broadcast=False)
+    else:
+        send("sorry, I don't understand the question", broadcast = False)
     global last_msg
     last_msg = time.time()
 
diff --git a/front.html b/front.html
index 71ae430..8ef5556 100644
--- a/front.html
+++ b/front.html
@@ -9,10 +9,6 @@
         $(document).ready(function() {
             var socket = io.connect('http://127.0.0.1:5000');
 
-            socket.on('connect', function() {
-                socket.send('connected');
-            });
-
             socket.on('message', function(msg) {
                 $("#messages").append('<li>'+msg+'</li>')
             });
diff --git a/qa.sqlite3 b/qa.sqlite3
index 204e6af6d43c0ee809826f3dd19338a3caf3b550..3cd0e5c6fe5105c9d0d7d0c65082ff381a334774 100644
GIT binary patch
delta 208
zcmZo@U~Fh$oFL7}JW<A(m6<`${>H|X1^gUL{0A8LZ}K15EGV#r-$$H@g+W_1BfnfB
zKPN>Yu_#rcGQTu4SHV=3k%d8Dw>%@UM7<a!R-}-Zn44;!TM4EWGK&>*^OG|&xfmE2
znE0<V@L%VD4K(pIzo{^@HY1vOOd`zsoESzyv_59wf6V^|sP#I(EC;hFBgk$hVP;)U
GbejN>gFE~H

delta 64
wcmZo@U~Fh$oFL7}I8nx#m61WuL}6pf0)8e2fz5&jm-r`6&|u?Y00LZG0Q>9@m;e9(

-- 
GitLab