-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvocabulary_process.py
More file actions
94 lines (79 loc) · 2.57 KB
/
Copy pathvocabulary_process.py
File metadata and controls
94 lines (79 loc) · 2.57 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# MODULE: vocabulary_process.py
# VERSION: 0.4
# DIRECTORY: <masked>
# DATE: 2023-06-04
# AUTHOR: RandomCollection
# DESCRIPTION: See https://github.com/RandomCollection/Vocabulary-Trainer.
# LIBRARIES ############################################################################################################
import os
import pandas as pd
from projects.vocabulary_trainer import cfg
# FUNCTIONS ############################################################################################################
def unstack_vocabulary_languages(df: pd.DataFrame) -> pd.DataFrame:
_df = (
pd.concat(
[
(
df
.filter(items=["ID_ES", "WORD_DE", "CATEGORY"])
.rename(columns={"ID_ES": "WORD", "WORD_DE": "TRANSLATION"})
.assign(
LANGUAGE="ES",
LEVEL=0,
)
),
(
df
.filter(items=["ID_DE", "WORD_ES", "CATEGORY"])
.rename(columns={"ID_DE": "WORD", "WORD_ES": "TRANSLATION"})
.assign(
LANGUAGE="DE",
LEVEL=0
)
)
]
)
.reset_index(drop=True)
)
return _df
def unstack_vocabulary_singular_plural(df: pd.DataFrame) -> pd.DataFrame:
cols_sin = [col for col in df.columns if "SINGULAR" in col]
cols_plu = [col for col in df.columns if "PLURAL" in col]
cols = [col.split("_", 1)[-1] for col in cols_sin]
_df = (
pd.concat(
[
(
df
.filter(items=cols_sin + ["CATEGORY"])
.rename(columns=dict(zip(cols_sin, cols)))
),
(
df
.filter(items=cols_plu + ["CATEGORY"])
.rename(columns=dict(zip(cols_plu, cols)))
)
]
)
.sort_values(by=["CATEGORY", "ID_ES"])
.filter(items=["ID_ES", "ID_DE", "WORD_ES", "WORD_DE", "CATEGORY"])
.loc[lambda _df: _df["ID_ES"] != ""]
.reset_index(drop=True)
)
return _df
# MAIN FUNCTION ########################################################################################################
def vocabulary_process():
(
pd.read_excel(
io=os.path.join(cfg.PATH_ROOT, cfg.NAME_VOCABULARY_DIRTY),
engine="openpyxl",
keep_default_na=False
)
.pipe(unstack_vocabulary_singular_plural)
.pipe(unstack_vocabulary_languages)
.to_excel(excel_writer=os.path.join(cfg.PATH_ROOT, cfg.NAME_VOCABULARY_CLEAN), index=False)
)
# MAIN #################################################################################################################
if __name__ == "__main__":
vocabulary_process()
# END ##################################################################################################################