Dictionary / app.py
Maiawen's picture
Upload app.py
c5d9f65 verified
raw
history blame contribute delete
No virus
1.7 kB
import streamlit as st
import requests
import pandas as pd
# Function to query the dictionary
def query_dictionary(word):
if not word:
return [] # Return empty if no word is entered to avoid unnecessary API calls
url = f'https://api.dictionaryapi.dev/api/v2/entries/en/{word}'
response = requests.get(url)
if response.status_code == 200:
return response.json()
else:
st.error(f"Query failed, status code: {response.status_code}")
return [] # Return empty list on failure
st.title("Dictionary Query")
word = st.text_input("Please enter the word to query")
if word: # Only query the dictionary if a word has been entered
data = query_dictionary(word)
data_list = []
for item in data:
word_item = item['word']
for phonetic in item['phonetics']:
text = phonetic.get('text', '')
for meaning in item['meanings']:
part_of_speech = meaning['partOfSpeech']
for definition in meaning['definitions']:
def_text = definition['definition']
example = definition.get('example', '')
data_list.append({
'Word': word_item,
'Phonetic': text,
'PartOfSpeech': part_of_speech,
'Definition': def_text,
'Example': example
})
if data_list:
df = pd.DataFrame(data_list)
df = df.drop_duplicates()
st.dataframe(df) # Display the table
else:
st.write("No results found for the entered word.")