Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import pandas as pd
|
4 |
+
|
5 |
+
# Function to query the dictionary
|
6 |
+
def query_dictionary(word):
|
7 |
+
if not word:
|
8 |
+
return [] # Return empty if no word is entered to avoid unnecessary API calls
|
9 |
+
url = f'https://api.dictionaryapi.dev/api/v2/entries/en/{word}'
|
10 |
+
response = requests.get(url)
|
11 |
+
if response.status_code == 200:
|
12 |
+
return response.json()
|
13 |
+
else:
|
14 |
+
st.error(f"Query failed, status code: {response.status_code}")
|
15 |
+
return [] # Return empty list on failure
|
16 |
+
|
17 |
+
st.title("Dictionary Query")
|
18 |
+
|
19 |
+
word = st.text_input("Please enter the word to query")
|
20 |
+
|
21 |
+
if word: # Only query the dictionary if a word has been entered
|
22 |
+
data = query_dictionary(word)
|
23 |
+
|
24 |
+
data_list = []
|
25 |
+
for item in data:
|
26 |
+
word_item = item['word']
|
27 |
+
for phonetic in item['phonetics']:
|
28 |
+
text = phonetic.get('text', '')
|
29 |
+
for meaning in item['meanings']:
|
30 |
+
part_of_speech = meaning['partOfSpeech']
|
31 |
+
for definition in meaning['definitions']:
|
32 |
+
def_text = definition['definition']
|
33 |
+
example = definition.get('example', '')
|
34 |
+
data_list.append({
|
35 |
+
'Word': word_item,
|
36 |
+
'Phonetic': text,
|
37 |
+
'PartOfSpeech': part_of_speech,
|
38 |
+
'Definition': def_text,
|
39 |
+
'Example': example
|
40 |
+
})
|
41 |
+
|
42 |
+
if data_list:
|
43 |
+
df = pd.DataFrame(data_list)
|
44 |
+
df = df.drop_duplicates()
|
45 |
+
st.dataframe(df) # Display the table
|
46 |
+
else:
|
47 |
+
st.write("No results found for the entered word.")
|