Maiawen commited on
Commit
4a52993
1 Parent(s): a3a8104

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +41 -11
  2. requirements.txt +0 -0
app.py CHANGED
@@ -4,20 +4,50 @@ import pandas as pd
4
 
5
  # Function to query the dictionary
6
  def query_dictionary(word):
7
- url = f'https://api.dictionaryapi.dev/api/v2/entries/en/{word}'
8
- response = requests.get(url)
9
- if response.status_code == 200:
10
- return response.json()
 
 
 
11
  else:
12
- st.error(f"Query failed, status code: {response.status_code}") # Display error message if query fails
13
 
14
  st.title("Dictionary Query") # Title of the application
15
 
16
  word = st.text_input("Please enter the word to query") # Input field for the word
17
 
18
- result = query_dictionary(word)
19
- if result:
20
- print(result)
21
-
22
- data = pd.DataFrame(result)
23
- st.dataframe(data) # Display the table
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  # Function to query the dictionary
6
  def query_dictionary(word):
7
+ if word:
8
+ url = f'https://api.dictionaryapi.dev/api/v2/entries/en/{word}'
9
+ response = requests.get(url)
10
+ if response.status_code == 200:
11
+ return response.json()
12
+ else:
13
+ st.error(f"Query failed, status code: {response.status_code}") # Display error message if query fails
14
  else:
15
+ st.warning("Please enter a word.")
16
 
17
  st.title("Dictionary Query") # Title of the application
18
 
19
  word = st.text_input("Please enter the word to query") # Input field for the word
20
 
21
+ data = query_dictionary(word)
22
+
23
+ # Create an empty list to store the data
24
+ data_list = []
25
+ # Populate the list with data
26
+ data_list = []
27
+ for item in data:
28
+ word = item['word']
29
+ for phonetic in item['phonetics']:
30
+ audio = phonetic.get('audio', '')
31
+ text = phonetic.get('text', '')
32
+ for meaning in item['meanings']:
33
+ part_of_speech = meaning['partOfSpeech']
34
+ for definition in meaning['definitions']:
35
+ def_text = definition['definition']
36
+ example = definition.get('example', '')
37
+ # Append each item as a dictionary to the list
38
+ data_list.append({
39
+ 'Word': word,
40
+ 'Phonetic': text,
41
+ 'Audio': audio,
42
+ 'PartOfSpeech': part_of_speech,
43
+ 'Definition': def_text,
44
+ 'Example': example
45
+ })
46
+
47
+ # Convert the list of dictionaries to a DataFrame
48
+ df = pd.DataFrame(data_list)
49
+
50
+ data = pd.DataFrame(df)
51
+ if df is not None:
52
+ st.dataframe(df) # Display the table
53
+
requirements.txt ADDED
File without changes