amve commited on
Commit
ac9eb14
·
verified ·
1 Parent(s): 392c3fa

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+ from rapidfuzz import process, fuzz
4
+ import phonetics
5
+ from indic_transliteration import sanscript
6
+ from indic_transliteration.sanscript import transliterate
7
+ from collections import defaultdict
8
+
9
+ data = []
10
+ # Load and parse the JSON data efficiently
11
+ with open("hin_test.json", "r", encoding="utf-8") as file:
12
+ for line in file:
13
+ try:
14
+ # Parse each line as a JSON object
15
+ data.append(json.loads(line))
16
+ except json.JSONDecodeError as e:
17
+ print(f"Error parsing line: {line}. Error: {e}")
18
+
19
+ # Extract names from the dataset
20
+ names_database = [entry['english word'] for entry in data if 'english word' in entry]
21
+
22
+ # Precompute metaphone codes for the database names
23
+ phonetic_dict = defaultdict(list)
24
+ for name in names_database:
25
+ code = phonetics.metaphone(name)
26
+ phonetic_dict[code].append(name)
27
+
28
+ # Function to perform fuzzy matching
29
+ def fuzzy_match(name, threshold=70):
30
+ matches = process.extract(name, names_database, scorer=fuzz.ratio, limit=5)
31
+ matched_names = [
32
+ f"{match_name} - {score:.2f}" for match_name, score, _ in matches if score >= threshold
33
+ ]
34
+ return ", ".join(matched_names) if matched_names else "No matches found."
35
+
36
+ # Function to perform phonetic matching
37
+ def phonetic_match(name):
38
+ input_phonetic = phonetics.metaphone(name)
39
+ matches = phonetic_dict.get(input_phonetic, [])
40
+ return ", ".join(matches) if matches else "No phonetic matches found."
41
+
42
+ # Function to transliterate Devanagari to Roman script
43
+ def devanagari_to_roman(hindi_name):
44
+ roman_name_itrans = transliterate(hindi_name, sanscript.DEVANAGARI, sanscript.ITRANS)
45
+ return roman_name_itrans
46
+
47
+ # Function to handle different types of matching/transliteration
48
+ def match_name(name, method):
49
+ if method == "Fuzzy Match":
50
+ return fuzzy_match(name)
51
+ elif method == "Phonetic Match":
52
+ return phonetic_match(name)
53
+ elif method == "Devanagari to Roman Transliteration":
54
+ return devanagari_to_roman(name)
55
+ else:
56
+ return "Select a valid method."
57
+
58
+ # Gradio Interface with updated components and parameters
59
+ interface = gr.Interface(
60
+ fn=match_name,
61
+ inputs=[
62
+ gr.Textbox(label="Enter Name"),
63
+ gr.Radio(
64
+ ["Fuzzy Match", "Phonetic Match", "Devanagari to Roman Transliteration"],
65
+ label="Matching Method"
66
+ )
67
+ ],
68
+ outputs=gr.Textbox(label="Result"),
69
+ title="नाम-सूचक (Naam-Suchak)",
70
+ allow_flagging='never',
71
+ description=(
72
+ "Welcome to **नाम-सूचक**, an innovative solution developed for the SIH1787 problem statement. "
73
+ "This tool offers a robust and intelligent approach for **fuzzy matching**, **phonetic matching**, "
74
+ "and **transliteration** of Hindi names, ensuring seamless handling of variations and phonetic similarities. "
75
+ "Effortlessly search, match, and transliterate names with high accuracy and efficiency."
76
+ ),
77
+ examples=[
78
+ ["Suresh", "Fuzzy Match"],
79
+ ["Shiv", "Phonetic Match"],
80
+ ["राहुल", "Devanagari to Roman Transliteration"]
81
+ ],
82
+ cache_examples=True
83
+ )
84
+
85
+ # Launch the Gradio app
86
+ if __name__ == "__main__":
87
+ interface.launch()