-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
209 lines (181 loc) · 5.69 KB
/
model.py
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"""
VIN team's project.
Main module for preparing data
"""
from typing import Callable, List
import numpy as np
import pandas as pd
class TextTransform:
"""
Text transformation functions.
Text transformation module with basic functions which
used for building pipeline (class Pipe from this module).
"""
def for_pipeline(func):
def wrapper(*args, **kwargs):
return func, args, kwargs
return wrapper
@for_pipeline
def to_lower_case(s: str) -> str:
return s.lower()
@for_pipeline
def drop_char(s: str, chars: str = r".,()0123456789«»", replace: str = "") -> str:
for c in chars:
s = s.replace(c, replace)
return s
@for_pipeline
def drop_words(s: str, words: list[str]) -> str:
s = " " + s + " "
for w in words:
s = s.replace(" " + w + " ", " ")
return s.strip()
@for_pipeline
def drop_whitespaces(s: str) -> str:
return " ".join(s.strip().split())
@for_pipeline
def transliterate(string: str) -> str:
capital_letters = {
"А": "A",
"Б": "B",
"В": "V",
"Г": "G",
"Д": "D",
"Е": "E",
"Ё": "E",
"З": "Z",
"И": "I",
"Й": "Y",
"К": "K",
"Л": "L",
"М": "M",
"Н": "N",
"О": "O",
"П": "P",
"Р": "R",
"С": "S",
"Т": "T",
"У": "U",
"Ф": "F",
"Х": "H",
"Ъ": "",
"Ы": "Y",
"Ь": "",
"Э": "E",
"Ä": "A",
"Ö": "O",
"Ü": "U",
}
capital_letters_transliterated_to_multiple_letters = {
"Ж": "Zh",
"Ц": "Ts",
"Ч": "Ch",
"Ш": "Sh",
"Щ": "Sch",
"Ю": "Yu",
"Я": "Ya",
}
lower_case_letters = {
"а": "a",
"б": "b",
"в": "v",
"г": "g",
"д": "d",
"е": "e",
"ё": "e",
"ж": "zh",
"з": "z",
"и": "i",
"й": "y",
"к": "k",
"л": "l",
"м": "m",
"н": "n",
"о": "o",
"п": "p",
"р": "r",
"с": "s",
"т": "t",
"у": "u",
"ф": "f",
"х": "h",
"ц": "ts",
"ч": "ch",
"ш": "sh",
"щ": "sch",
"ъ": "",
"ы": "y",
"ь": "",
"э": "e",
"ю": "yu",
"я": "ya",
"ä": "a",
"ö": "o",
"ü": "u",
"ß": "ss",
"ç": "c",
"ş": "s",
}
capital_and_lower_case_letter_pairs = {}
for (
capital_letter,
capital_letter_translit,
) in capital_letters_transliterated_to_multiple_letters.items():
for (
lowercase_letter,
lowercase_letter_translit,
) in lower_case_letters.items():
capital_and_lower_case_letter_pairs[
"%s%s" % (capital_letter, lowercase_letter)
] = "%s%s" % (capital_letter_translit, lowercase_letter_translit)
for dictionary in (
capital_and_lower_case_letter_pairs,
capital_letters,
lower_case_letters,
):
for cyrillic_string, latin_string in dictionary.items():
string = string.replace(cyrillic_string, latin_string)
for (
cyrillic_string,
latin_string,
) in capital_letters_transliterated_to_multiple_letters.items():
string = string.replace(cyrillic_string, latin_string.upper())
return string
class Pipe:
"""
Data pipeline class.
Create pipelines for data preparing.
"""
sequence: List[TextTransform]
def ndarray_map(data: np.ndarray, func: Callable, args, kwargs):
return np.vectorize(func)(data, *args, **kwargs)
def series_map(data: pd.Series, func: Callable, args, kwargs):
return data.apply(func, args=args, **kwargs)
def dataframe_map(data: pd.DataFrame, func: Callable, args, kwargs):
for c in data.columns:
data[c] = data[c].apply(func, args=args, **kwargs)
return data
def __init__(self, *sequence):
self.sequence = sequence
def __call__(self, data: pd.Series, inline: bool = False) -> pd.Series:
if type(data) != str:
d = data if inline else data.copy()
else:
d = data
if type(d) == np.ndarray:
for s in self.sequence:
d = Pipe.ndarray_map(d, s[0], s[1], s[2])
elif type(d) == pd.Series:
for s in self.sequence:
d = Pipe.series_map(d, s[0], s[1], s[2])
elif type(d) == str:
for s in self.sequence:
d = s[0](d, *s[1], **s[2])
elif type(d) == pd.DataFrame:
for s in self.sequence:
d = Pipe.dataframe_map(d, s[0], s[1], s[2])
return d
def __repr__(self) -> str:
r = [s[0].__name__ for s in self.sequence]
return " -> ".join(r)
def add2pipe(self, func: Callable, *args, **kwargs):
return self.sequence.append(func, args, kwargs)