-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionary.py
More file actions
59 lines (48 loc) · 2.24 KB
/
dictionary.py
File metadata and controls
59 lines (48 loc) · 2.24 KB
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
49
50
51
52
53
54
55
56
57
58
59
import ast
from typing import Optional
import pandas as pd
# ===================================================================
# DATAFRAME CREATION
# ===================================================================
print("Main dataframe creation...")
df = pd.read_csv("files/dico.csv") # Dataframe creation
df = df.sort_values('Mot') # Alphabetic order
df = df.dropna() # Remove missing data
df = df.reset_index()
# ===================================================================
# FUNCTION
# ===================================================================
def define(dataframe: pd.DataFrame, word_column_name: str, definition_column_name: str, word: str) -> Optional[list]:
"""
Displays the definition of a word in the word dictionary.
Args:
dataframe (pandas.DataFrame): Pandas dataframe with a word column and a definition column.
word_column_name (str): Name of the column containing the words.
definition_column_name (str): Name of column containing definitions.
word (str): Word to search.
Returns:
(list): List containing the definition(s) of the word searched for.
(None): If the word is not in the dictionary.
"""
print("Searching...")
word = word.capitalize()
definition = dataframe.loc[df[word_column_name] == word][definition_column_name]
# WORD NOT FOUND
if len(definition) == 0:
print(f"'{word}' not in dictionary")
return None
# WORD FOUND
# Definitions in the dataframe are stored as strings representing lists. To convert these strings into usable
# `list` objects, we use `ast.literal_eval`, a more secure method than `eval` as it only evaluates Python literals
# (lists, dictionaries, etc.), without executing arbitrary code.
else:
definition = ast.literal_eval(definition.values[0])
return definition
# ===================================================================
# MAIN
# ===================================================================
if __name__ == "__main__":
word_to_define = "hallali"
word_definition = define(df, "Mot", "Définitions", word_to_define)
print(f"{word_to_define}:")
print(word_definition)