|
@@ -0,0 +1,103 @@
|
|
|
+import pandas as pd
|
|
|
+from flask_cors import CORS
|
|
|
+from flask import (
|
|
|
+ Blueprint, request, jsonify
|
|
|
+)
|
|
|
+
|
|
|
+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
|
|
|
+ #}
|
|
|
+
|
|
|
+ #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'])
|
|
|
+
|
|
|
+ recommendations = []
|
|
|
+ for similarity, name, ruleId in top_matches:
|
|
|
+ finalValue = f"https://communityrule.info/create/?r={ruleId}"
|
|
|
+ recommendations.append({
|
|
|
+ 'community': name,
|
|
|
+ 'link': finalValue,
|
|
|
+ })
|
|
|
+
|
|
|
+ print(jsonify(recommendations))
|
|
|
+ return jsonify(recommendations)
|
|
|
+
|
|
|
+
|
|
|
+ #finalValue = "https://communityrule.info/create/?r=" + str(ruleId)
|
|
|
+ #response_message = f"{closest_value}"
|
|
|
+
|
|
|
+ #response_data = {
|
|
|
+ # 'community': response_message,
|
|
|
+ # 'link': finalValue
|
|
|
+ #}
|
|
|
+
|
|
|
+ #return jsonify(response_data)
|
|
|
+
|
|
|
+def find_closest_match(user_input, compare_column1_values, compare_column2_values, return_column_values, rule_id_values):
|
|
|
+ print(user_input)
|
|
|
+ max_similarity_curr = -1
|
|
|
+ closest_value = None
|
|
|
+ rule_id = None
|
|
|
+ user_tokens = user_input.split()
|
|
|
+ results = []
|
|
|
+ # 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):
|
|
|
+ # Convert compare_value2 to string if it's a float
|
|
|
+ if isinstance(compare_value1, float):
|
|
|
+ compare_value1 = str(compare_value1)
|
|
|
+ if isinstance(compare_value2, float):
|
|
|
+ compare_value2 = str(compare_value2)
|
|
|
+
|
|
|
+ # Split compare_value2 into tokens
|
|
|
+ compare_tokens1 = compare_value1.split()
|
|
|
+ compare_tokens2 = compare_value2.split()
|
|
|
+
|
|
|
+ # Calculate similarity between user input and compare_value1 and compare_value2
|
|
|
+ similarity1 = similarity_score(user_tokens, compare_tokens1)
|
|
|
+ similarity2 = similarity_score(user_tokens, compare_tokens2)
|
|
|
+
|
|
|
+ # 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
|
|
|
+ #closest_value = return_value
|
|
|
+ #rule_id = ruleId
|
|
|
+ results.append((max_similarity_curr, return_value, ruleId))
|
|
|
+ # print(results)
|
|
|
+ results.sort(reverse=True, key=lambda x: x[0])
|
|
|
+ return results[:3]
|
|
|
+
|
|
|
+def similarity_score(set1, set2):
|
|
|
+ # Calculate the intersection of the two sets
|
|
|
+ intersection = len(set(set1).intersection(set(set2)))
|
|
|
+
|
|
|
+ # Calculate the union of the two sets
|
|
|
+ union = len(set(set1).union(set(set2)))
|
|
|
+
|
|
|
+ # Calculate the Jaccard similarity coefficient
|
|
|
+ if union == 0:
|
|
|
+ return 0
|
|
|
+ else:
|
|
|
+ return intersection / union
|
|
|
+
|