Commit 8d2c463f authored by KMaleesha's avatar KMaleesha

update python backend.

parents
import pandas as pd
import difflib
# Read data
df = pd.read_excel('DataSet.xlsx')
def find_most_similar_word(input_word, word_list):
similarities = difflib.get_close_matches(input_word, word_list)
return similarities[0] if similarities else None
def find_different_letters(input_word, similar_word):
differences = []
for char1, char2 in zip(input_word, similar_word):
if char1 != char2:
differences.append(char1)
return ''.join(differences)
# Input word
input_word = input()
most_similar_word = find_most_similar_word(input_word, df['words'])
if most_similar_word:
# Find different letters
different_letters = find_different_letters(input_word, most_similar_word)
print("User Input:", input_word)
print("Most Match:", most_similar_word)
else:
print("No similar word found in the dataset.")
user_input = input_word
candidate_words = [most_similar_word]
# Function to find differing letters
def find_differing_letters(input_word, candidate_word):
differing_letters = list(difflib.ndiff(input_word, candidate_word))
differing_letters = [char[2] for char in differing_letters if char[0] != ' ']
return ''.join(differing_letters)
# Initialize variables to track the most matching word and its differing letters
most_matching_word = None
differing_letters = None
for candidate_word in candidate_words:
candidate_differing_letters = find_differing_letters(user_input, candidate_word)
if differing_letters is None or len(candidate_differing_letters) < len(differing_letters):
differing_letters = candidate_differing_letters
most_matching_word = candidate_word
if differing_letters:
print(f"Differing Letters: {differing_letters}")
# Differentiating stage here
else:
print("No differing letters found.")
#--------------xxxxx
# Input word and letter
input_word = most_similar_word
input_letter = differing_letters
# Find the first row
row = df[df['words'] == input_word].iloc[0]
initial_position = None
middle_position = None
end_position = None
if input_letter in row['initial']:
initial_position = row['initial'].index(input_letter)
if input_letter in row['middle']:
middle_position = row['middle'].index(input_letter)
if input_letter in row['end']:
end_position = row['end'].index(input_letter)
# Print
if initial_position is not None:
print("Position: initial")
if middle_position is not None:
print("Position: middle")
if end_position is not None:
print("Position: end")
# Check if differing_letter is not None before getting its length
if differing_letters is not None:
differing_letter_count = len(differing_letters)
print(f"Number of Different Letters: {differing_letter_count}")
else:
print("No differing letter found.")
File added
from flask import Flask, request, jsonify
from flask_cors import CORS
import pandas as pd
import difflib
# Read data
df = pd.read_excel('DataSet.xlsx')
app = Flask(__name__)
CORS(app)
def find_most_similar_word(input_word, word_list):
similarities = difflib.get_close_matches(input_word, word_list)
return similarities[0] if similarities else None
def find_different_letters(input_word, similar_word):
differences = []
for char1, char2 in zip(input_word, similar_word):
if char1 != char2:
differences.append(char1)
return ''.join(differences)
@app.route('/API_Word', methods=['POST'])
def compare_words():
try:
data = request.get_json()
input_word = data['input_word']
user_input = input_word
most_similar_word = find_most_similar_word(input_word, df['words'])
if most_similar_word:
different_letters = find_different_letters(input_word, most_similar_word)
user_input = input_word
candidate_words = [most_similar_word]
def find_differing_letters(input_word, candidate_word):
differing_letters = list(difflib.ndiff(input_word, candidate_word))
differing_letters = [char[2] for char in differing_letters if char[0] != ' ']
return ''.join(differing_letters)
most_matching_word = None
differing_letters = None
for candidate_word in candidate_words:
candidate_differing_letters = find_differing_letters(user_input, candidate_word)
if differing_letters is None or len(candidate_differing_letters) < len(differing_letters):
differing_letters = candidate_differing_letters
most_matching_word = candidate_word
if differing_letters:
position_info = []
input_word = most_matching_word
input_letter = differing_letters
row = df[df['words'] == input_word].iloc[0]
if input_letter in row['initial']:
position_info.append("Position: initial")
if input_letter in row['middle']:
position_info.append("Position: middle")
if input_letter in row['end']:
position_info.append("Position: end")
differing_letter_count = len(differing_letters)
position_info.append(f"Number of Different Letters: {differing_letter_count}")
return jsonify({
"User Input": user_input,
"Most Match": most_matching_word,
"Differing Letters": differing_letters,
"Position Info": position_info
})
return jsonify({"message": "No similar word found in the dataset."})
except Exception as e:
return jsonify({"error": str(e)}), 400
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment