chmawia commited on
Commit
ed17812
·
verified ·
1 Parent(s): aec075c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -43
app.py CHANGED
@@ -1,44 +1,73 @@
1
  import streamlit as st
2
- from PyDictionary import PyDictionary
3
- import requests
4
-
5
- # Function to get Urdu meaning using an API
6
- def get_urdu_meaning(word):
7
- url = f"https://api.dictionaryapi.dev/api/v2/entries/en/{word}"
8
- try:
9
- response = requests.get(url)
10
- data = response.json()
11
- meanings = data[0]['meanings'][0]['definitions'][0]['definition']
12
- return meanings
13
- except:
14
- return "Meaning not found in Urdu."
15
-
16
- # Create Streamlit UI
17
- st.title("Dictionary App")
18
- st.write("This app provides synonyms, antonyms, and Urdu meanings for words.")
19
-
20
- # Input field for the word
21
- word = st.text_input("Enter a word:")
22
-
23
- if word:
24
- dictionary = PyDictionary()
25
-
26
- # Synonyms and Antonyms
27
- synonyms = dictionary.synonym(word)
28
- antonyms = dictionary.antonym(word)
29
-
30
- # Urdu Meaning
31
- urdu_meaning = get_urdu_meaning(word)
32
-
33
- # Display results
34
- if synonyms:
35
- st.write(f"**Synonyms:** {', '.join(synonyms)}")
36
- else:
37
- st.write("No synonyms found.")
38
-
39
- if antonyms:
40
- st.write(f"**Antonyms:** {', '.join(antonyms)}")
41
- else:
42
- st.write("No antonyms found.")
43
-
44
- st.write(f"**Urdu Meaning:** {urdu_meaning}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+
3
+ # Dictionary dataset (for demo purposes, you can replace this with an API or a larger dataset)
4
+ urdu_dictionary = {
5
+ "happy": {
6
+ "urdu_meaning": "خوش",
7
+ "synonyms": ["joyful", "cheerful", "content"]
8
+ },
9
+ "sad": {
10
+ "urdu_meaning": "اداس",
11
+ "synonyms": ["unhappy", "sorrowful", "melancholy"]
12
+ },
13
+ "beautiful": {
14
+ "urdu_meaning": "خوبصورت",
15
+ "synonyms": ["attractive", "lovely", "gorgeous"]
16
+ },
17
+ "strong": {
18
+ "urdu_meaning": "مضبوط",
19
+ "synonyms": ["powerful", "sturdy", "resilient"]
20
+ }
21
+ }
22
+
23
+ # Function to fetch word details
24
+ def get_word_info(word):
25
+ word = word.lower()
26
+ return urdu_dictionary.get(word, None)
27
+
28
+ # Streamlit UI
29
+ def main():
30
+ st.title("📖 English to Urdu Dictionary")
31
+ st.write("Enter an English word to get its Urdu meaning and synonyms.")
32
+
33
+ # Sidebar for additional options
34
+ with st.sidebar:
35
+ st.header("Options")
36
+ show_history = st.checkbox("Show search history", value=True)
37
+
38
+ # User input: Dropdown or text input
39
+ word_list = list(urdu_dictionary.keys())
40
+ selected_word = st.selectbox("Or select a word from the list:", word_list)
41
+ word = st.text_input("Enter an English word:", "")
42
+
43
+ # Use the selected word if no text input is provided
44
+ if not word:
45
+ word = selected_word
46
+
47
+ # Initialize session state for search history
48
+ if "search_history" not in st.session_state:
49
+ st.session_state.search_history = []
50
+
51
+ if word:
52
+ word_info = get_word_info(word)
53
+
54
+ if word_info:
55
+ st.success(f"**Urdu Meaning:** {word_info['urdu_meaning']}")
56
+ st.write(f"**Synonyms:** {', '.join(word_info['synonyms']}")
57
+
58
+ # Update search history
59
+ if word not in st.session_state.search_history:
60
+ st.session_state.search_history.append(word)
61
+ if len(st.session_state.search_history) > 5:
62
+ st.session_state.search_history.pop(0)
63
+ else:
64
+ st.error("Word not found in the dictionary. Try another word!")
65
+
66
+ # Display search history
67
+ if show_history and st.session_state.search_history:
68
+ st.subheader("Search History")
69
+ for i, searched_word in enumerate(st.session_state.search_history[::-1], 1):
70
+ st.write(f"{i}. {searched_word}")
71
+
72
+ if __name__ == "__main__":
73
+ main()