Final Chatbot code
This commit is contained in:
		
							
								
								
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | |||||||
|  | myenv/ | ||||||
|  | __pycache__/ | ||||||
|  |  | ||||||
							
								
								
									
										12
									
								
								chatbot/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								chatbot/__init__.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | |||||||
|  | from flask import Flask | ||||||
|  | from flask_cors import CORS | ||||||
|  |  | ||||||
|  | def create_app(test_config=None): | ||||||
|  |     # create and configure the app | ||||||
|  |     app = Flask(__name__, instance_relative_config=True) | ||||||
|  |     CORS(app) | ||||||
|  |  | ||||||
|  |     from . import routes | ||||||
|  |     app.register_blueprint(routes.bp) | ||||||
|  |  | ||||||
|  |     return app | ||||||
							
								
								
									
										103
									
								
								chatbot/routes.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										103
									
								
								chatbot/routes.py
									
									
									
									
									
										Normal file
									
								
							| @@ -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 | ||||||
|  |      | ||||||
							
								
								
									
										14
									
								
								requirements.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								requirements.txt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,14 @@ | |||||||
|  | blinker==1.7.0 | ||||||
|  | click==8.1.7 | ||||||
|  | Flask==3.0.3 | ||||||
|  | Flask-Cors==4.0.0 | ||||||
|  | itsdangerous==2.1.2 | ||||||
|  | Jinja2==3.1.3 | ||||||
|  | MarkupSafe==2.1.5 | ||||||
|  | numpy==1.26.4 | ||||||
|  | pandas==2.2.2 | ||||||
|  | python-dateutil==2.9.0.post0 | ||||||
|  | pytz==2024.1 | ||||||
|  | six==1.16.0 | ||||||
|  | tzdata==2024.1 | ||||||
|  | Werkzeug==3.0.2 | ||||||
		Reference in New Issue
	
	Block a user