UBS-Server / routes /clumsy.py
m-abdur2024's picture
Upload 40 files
0d3476b verified
raw
history blame
No virus
1.78 kB
import logging
from flask import request, jsonify
from routes import app
logger = logging.getLogger(__name__)
@app.route('/the-clumsy-programmer', methods=['POST'])
def the_clumsy_programmer():
data = request.get_json()
logger.info(f"Data received for processing: {data}")
if not data or not isinstance(data, list) or len(data) == 0:
return jsonify({"error": "Invalid input format"}), 400
results = []
for input_entry in data:
dictionary = input_entry.get("dictionary", [])
mistypes = input_entry.get("mistypes", [])
# Build the patterns dictionary
patterns_dict = {}
L = len(dictionary[0]) if dictionary else 0
for word in dictionary:
for i in range(L):
pattern = word[:i] + '*' + word[i+1:]
patterns_dict.setdefault(pattern, []).append(word)
corrections = []
for mistyped_word in mistypes:
found = False
for i in range(L):
pattern = mistyped_word[:i] + '*' + mistyped_word[i+1:]
candidate_words = patterns_dict.get(pattern, [])
for candidate_word in candidate_words:
if candidate_word != mistyped_word:
corrections.append(candidate_word)
found = True
break
if found:
break
if not found:
# This case should not happen as per problem constraints
corrections.append(mistyped_word)
results.append({"corrections": corrections})
logger.info(f"Corrections made: {corrections}")
return jsonify(results)