routes.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import pandas as pd
  2. from flask_cors import CORS
  3. from flask import (
  4. Blueprint, request, jsonify
  5. )
  6. bp = Blueprint('routes', __name__,)
  7. df = pd.read_csv('rule.csv')
  8. user_input = "Improve the living and working conditions of everyone everywhere Fair and humane laws and practices"
  9. compare_column1 = df['summary']
  10. compare_column2 = df['modules']
  11. return_column = df['name']
  12. @bp.route('/request', methods=('GET', 'POST'))
  13. def handle_request():
  14. #user_input = request.args.get('question')
  15. #closest_value, ruleId = find_closest_match(user_input, df['summary'], df['modules'], df['name'], df['ruleID'])
  16. #finalValue = "https://communityrule.info/create/?r=" + str(ruleId)
  17. #response_message = f"{closest_value}"
  18. #response_data = {
  19. # 'community': response_message,
  20. # 'link': finalValue
  21. #}
  22. #return jsonify(response_data)
  23. user_input = request.args.get('question')
  24. top_matches = find_closest_match(user_input, df['summary'], df['modules'], df['name'], df['ruleID'])
  25. recommendations = []
  26. for similarity, name, ruleId in top_matches:
  27. finalValue = f"https://communityrule.info/create/?r={ruleId}"
  28. recommendations.append({
  29. 'community': name,
  30. 'link': finalValue,
  31. })
  32. print(jsonify(recommendations))
  33. return jsonify(recommendations)
  34. #finalValue = "https://communityrule.info/create/?r=" + str(ruleId)
  35. #response_message = f"{closest_value}"
  36. #response_data = {
  37. # 'community': response_message,
  38. # 'link': finalValue
  39. #}
  40. #return jsonify(response_data)
  41. def find_closest_match(user_input, compare_column1_values, compare_column2_values, return_column_values, rule_id_values):
  42. print(user_input)
  43. max_similarity_curr = -1
  44. closest_value = None
  45. rule_id = None
  46. user_tokens = user_input.split()
  47. results = []
  48. # Compare user input with each value in the compare columns
  49. for compare_value1, compare_value2, return_value, ruleId in zip(compare_column1_values, compare_column2_values, return_column_values, rule_id_values):
  50. # Convert compare_value2 to string if it's a float
  51. if isinstance(compare_value1, float):
  52. compare_value1 = str(compare_value1)
  53. if isinstance(compare_value2, float):
  54. compare_value2 = str(compare_value2)
  55. # Split compare_value2 into tokens
  56. compare_tokens1 = compare_value1.split()
  57. compare_tokens2 = compare_value2.split()
  58. # Calculate similarity between user input and compare_value1 and compare_value2
  59. similarity1 = similarity_score(user_tokens, compare_tokens1)
  60. similarity2 = similarity_score(user_tokens, compare_tokens2)
  61. # Take the maximum similarity between similarity1 and similarity2
  62. max_similarity_curr = max(similarity1, similarity2)
  63. print(similarity1, similarity2)
  64. # Update closest value if current similarity is greater
  65. #if max_similarity_curr > max_similarity:
  66. #max_similarity = max_similarity_curr
  67. #closest_value = return_value
  68. #rule_id = ruleId
  69. results.append((max_similarity_curr, return_value, ruleId))
  70. # print(results)
  71. results.sort(reverse=True, key=lambda x: x[0])
  72. return results[:3]
  73. def similarity_score(set1, set2):
  74. # Calculate the intersection of the two sets
  75. intersection = len(set(set1).intersection(set(set2)))
  76. # Calculate the union of the two sets
  77. union = len(set(set1).union(set(set2)))
  78. # Calculate the Jaccard similarity coefficient
  79. if union == 0:
  80. return 0
  81. else:
  82. return intersection / union