-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.py
88 lines (66 loc) · 2.7 KB
/
ui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from fastapi import FastAPI, Form
from fastapi.responses import JSONResponse, HTMLResponse
from fuzzywuzzy import fuzz
from typing import List, Union
app = FastAPI()
def convert_string_to_list(input_str: str) -> List[str]:
# Remove leading and trailing whitespaces, and split by ','
return [element.strip() for element in input_str.strip('[]').split(',')]
def compare_lists_with_fuzzy(l1, l2, threshold=50):
matching_elements_l1 = []
matching_elements_l2 = []
non_matching_elements_l1 = []
non_matching_elements_l2 = []
for element_l1 in l1:
max_similarity = 0
matching_element_l2 = ''
for element_l2 in l2:
similarity = fuzz.ratio(
str(element_l1).lower(), str(element_l2).lower()
) # Convert to lowercase for case-insensitive comparison
if similarity > max_similarity and similarity >= threshold:
max_similarity = similarity
matching_element_l2 = element_l2
if matching_element_l2:
matching_elements_l1.append(element_l1.strip("'"))
matching_elements_l2.append(matching_element_l2.strip("'"))
else:
non_matching_elements_l1.append(element_l1.strip("'"))
non_matching_elements_l2 = [
element_l2.strip("'")
for element_l2 in l2
if element_l2.strip("'") not in matching_elements_l2
]
# print("Matching Elements in l1:", matching_elements_l1)
# print("Matching Elements in l2:", matching_elements_l2)
# print("Non-Matching Elements in l1:", non_matching_elements_l1)
# print("Non-Matching Elements in l2:", non_matching_elements_l2)
similar_elements = []
for element_l1, element_l2 in zip(matching_elements_l1, matching_elements_l2):
similar_elements.append({"element_name_l1": element_l1, "element_name_l2": element_l2})
result = {"similar_elements": similar_elements}
return result
@app.post("/compare", response_model=dict)
def compare_lists(
l1: Union[str, List[str]] = Form(...),
l2: Union[str, List[str]] = Form(...),
threshold: int = Form(70, gt=0, le=100),
):
if isinstance(l1, str):
l1_list = convert_string_to_list(l1)
else:
l1_list = l1
if isinstance(l2, str):
l2_list = convert_string_to_list(l2)
else:
l2_list = l2
result = compare_lists_with_fuzzy(l1_list, l2_list, threshold)
return JSONResponse(content=result)
# Route to serve the index.html file
@app.get("/", response_class=HTMLResponse)
async def serve_index():
with open("sp.html", "r") as file:
return file.read()
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="127.0.0.1", port=8000)