File size: 1,696 Bytes
c5d9f65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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.")