|
@@ -1,34 +1,19 @@
|
|
|
import pandas as pd
|
|
|
+import mysql.connector
|
|
|
+import os
|
|
|
from flask_cors import CORS
|
|
|
from flask import (
|
|
|
Blueprint, request, jsonify
|
|
|
)
|
|
|
+conn = None
|
|
|
|
|
|
bp = Blueprint('routes', __name__,)
|
|
|
-df = pd.read_csv('rule.csv')
|
|
|
-user_input = "Improve the living and working conditions of everyone everywhere Fair and humane laws and practices"
|
|
|
-compare_column1 = df['summary']
|
|
|
-compare_column2 = df['modules']
|
|
|
-return_column = df['name']
|
|
|
-@bp.route('/request', methods=('GET', 'POST'))
|
|
|
-def handle_request():
|
|
|
-
|
|
|
- #user_input = request.args.get('question')
|
|
|
- #closest_value, ruleId = find_closest_match(user_input, df['summary'], df['modules'], df['name'], df['ruleID'])
|
|
|
-
|
|
|
|
|
|
- #finalValue = "https://communityrule.info/create/?r=" + str(ruleId)
|
|
|
- #response_message = f"{closest_value}"
|
|
|
-
|
|
|
- #response_data = {
|
|
|
- # 'community': response_message,
|
|
|
- # 'link': finalValue
|
|
|
- #}
|
|
|
+@bp.route('/chat/request', methods=('GET', 'POST'))
|
|
|
+def handle_request():
|
|
|
|
|
|
- #return jsonify(response_data)
|
|
|
-
|
|
|
user_input = request.args.get('question')
|
|
|
- top_matches = find_closest_match(user_input, df['summary'], df['modules'], df['name'], df['ruleID'])
|
|
|
+ top_matches = find_closest_match(user_input)
|
|
|
|
|
|
recommendations = []
|
|
|
for similarity, name, ruleId in top_matches:
|
|
@@ -38,29 +23,38 @@ def handle_request():
|
|
|
'link': finalValue,
|
|
|
})
|
|
|
|
|
|
- print(jsonify(recommendations))
|
|
|
return jsonify(recommendations)
|
|
|
|
|
|
+def get_db_connection():
|
|
|
+ global conn
|
|
|
+ if conn is None or not conn.is_connected():
|
|
|
+ conn = mysql.connector.connect(
|
|
|
+ host=os.getenv('CLOUDRON_MYSQL_HOST'),
|
|
|
+ user=os.getenv('CLOUDRON_MYSQL_USERNAME'),
|
|
|
+ port=os.getenv('CLOUDRON_MYSQL_PORT'),
|
|
|
+ password=os.getenv('CLOUDRON_MYSQL_PASSWORD'),
|
|
|
+ database=os.getenv('CLOUDRON_MYSQL_DATABASE')
|
|
|
+ )
|
|
|
+ print("Database connection was successful")
|
|
|
|
|
|
- #finalValue = "https://communityrule.info/create/?r=" + str(ruleId)
|
|
|
- #response_message = f"{closest_value}"
|
|
|
+ return conn
|
|
|
|
|
|
- #response_data = {
|
|
|
- # 'community': response_message,
|
|
|
- # 'link': finalValue
|
|
|
- #}
|
|
|
|
|
|
- #return jsonify(response_data)
|
|
|
+def find_closest_match(user_input):
|
|
|
|
|
|
-def find_closest_match(user_input, compare_column1_values, compare_column2_values, return_column_values, rule_id_values):
|
|
|
- print(user_input)
|
|
|
+ conn = get_db_connection()
|
|
|
max_similarity_curr = -1
|
|
|
closest_value = None
|
|
|
rule_id = None
|
|
|
user_tokens = user_input.split()
|
|
|
results = []
|
|
|
+ cursor = conn.cursor()
|
|
|
+ cursor.execute("SELECT summary, modules, name, rule_id FROM rules")
|
|
|
+ rows = cursor.fetchall()
|
|
|
+ cursor.close()
|
|
|
+
|
|
|
# Compare user input with each value in the compare columns
|
|
|
- for compare_value1, compare_value2, return_value, ruleId in zip(compare_column1_values, compare_column2_values, return_column_values, rule_id_values):
|
|
|
+ for compare_value1, compare_value2, return_value, ruleId in rows:
|
|
|
# Convert compare_value2 to string if it's a float
|
|
|
if isinstance(compare_value1, float):
|
|
|
compare_value1 = str(compare_value1)
|
|
@@ -77,7 +71,7 @@ def find_closest_match(user_input, compare_column1_values, compare_column2_value
|
|
|
|
|
|
# Take the maximum similarity between similarity1 and similarity2
|
|
|
max_similarity_curr = max(similarity1, similarity2)
|
|
|
- print(similarity1, similarity2)
|
|
|
+
|
|
|
# Update closest value if current similarity is greater
|
|
|
#if max_similarity_curr > max_similarity:
|
|
|
#max_similarity = max_similarity_curr
|
|
@@ -100,4 +94,5 @@ def similarity_score(set1, set2):
|
|
|
return 0
|
|
|
else:
|
|
|
return intersection / union
|
|
|
-
|
|
|
+
|
|
|
+
|