-
Notifications
You must be signed in to change notification settings - Fork 44
/
strings.py
238 lines (183 loc) · 6.71 KB
/
strings.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import string
from pysparkling.sql.expressions.expressions import UnaryExpression, Expression
from pysparkling.sql.types import StringType
from pysparkling.utils import levenshtein_distance
class StringTrim(UnaryExpression):
def eval(self, row, schema):
return self.column.eval(row, schema).strip()
def __str__(self):
return "trim({0})".format(self.column)
class StringLTrim(UnaryExpression):
def eval(self, row, schema):
return self.column.eval(row, schema).lstrip()
def __str__(self):
return "ltrim({0})".format(self.column)
class StringRTrim(UnaryExpression):
def eval(self, row, schema):
return self.column.eval(row, schema).rstrip()
def __str__(self):
return "rtrim({0})".format(self.column)
class StringInStr(Expression):
def __init__(self, substr, column):
super(StringInStr, self).__init__(column)
self.substr = substr
self.column = column
def eval(self, row, schema):
value = self.column.cast(StringType()).eval(row, schema)
return int(self.substr in value)
def __str__(self):
return "instr({0}, {1})".format(
self.substr,
self.column
)
class StringLocate(Expression):
def __init__(self, substr, column, pos):
super(StringLocate, self).__init__(column)
self.substr = substr
self.column = column
self.start = pos - 1
def eval(self, row, schema):
value = self.column.cast(StringType()).eval(row, schema)
if self.substr not in value[self.start:]:
return 0
return value.index(self.substr, self.start) + 1
def __str__(self):
return "locate({0}, {1}{2})".format(
self.substr,
self.column,
", {0}".format(self.start) if self.start is not None else ""
)
class StringLPad(Expression):
def __init__(self, column, length, pad):
super(StringLPad, self).__init__(column)
self.column = column
self.length = length
self.pad = pad
def eval(self, row, schema):
value = self.column.cast(StringType()).eval(row, schema)
delta = self.length - len(value)
padding = (self.pad * delta)[:delta] # Handle pad with multiple characters
return "{0}{1}".format(padding, value)
def __str__(self):
return "lpad({0}, {1}, {2})".format(
self.column,
self.length,
self.pad
)
class StringRPad(Expression):
def __init__(self, column, length, pad):
super(StringRPad, self).__init__(column)
self.column = column
self.length = length
self.pad = pad
def eval(self, row, schema):
value = self.column.cast(StringType()).eval(row, schema)
delta = self.length - len(value)
padding = (self.pad * delta)[:delta] # Handle pad with multiple characters
return "{0}{1}".format(value, padding)
def __str__(self):
return "rpad({0}, {1}, {2})".format(
self.column,
self.length,
self.pad
)
class StringRepeat(Expression):
def __init__(self, column, n):
super(StringRepeat, self).__init__(column)
self.column = column
self.n = n
def eval(self, row, schema):
value = self.column.cast(StringType()).eval(row, schema)
return value * self.n
def __str__(self):
return "repeat({0}, {1})".format(
self.column,
self.n
)
class StringTranslate(Expression):
def __init__(self, column, matching_string, replace_string):
super(StringTranslate, self).__init__(column)
self.column = column
self.matching_string = matching_string
self.replace_string = replace_string
self.translation_table = str.maketrans(
# Python's translate use an opposite importance order as Spark
# when there are duplicates in matching_string mapped to different chars
matching_string[::-1],
replace_string[::-1]
)
def eval(self, row, schema):
return self.column.cast(StringType()).eval(row, schema).translate(self.translation_table)
def __str__(self):
return "translate({0}, {1}, {2})".format(
self.column,
self.matching_string,
self.replace_string
)
class InitCap(Expression):
def __init__(self, column):
super(InitCap, self).__init__(column)
self.column = column
def eval(self, row, schema):
value = self.column.cast(StringType()).eval(row, schema)
return " ".join(word.capitalize() for word in value.split())
def __str__(self):
return "initcap({0})".format(self.column)
class Levenshtein(Expression):
def __init__(self, column1, column2):
super(Levenshtein, self).__init__(column1, column2)
self.column1 = column1
self.column2 = column2
def eval(self, row, schema):
value_1 = self.column1.cast(StringType()).eval(row, schema)
value_2 = self.column2.cast(StringType()).eval(row, schema)
if value_1 is None or value_2 is None:
return None
return levenshtein_distance(value_1, value_2)
def __str__(self):
return "levenshtein({0}, {1})".format(self.column1, self.column2)
class SoundEx(UnaryExpression):
_soundex_mapping = {
letter: int(soundex_code)
for letter, soundex_code in zip(
string.ascii_uppercase,
"01230127022455012623017202"
)
}
def eval(self, row, schema):
raw_value = self.column.cast(StringType()).eval(row, schema)
if raw_value is None:
return None
if raw_value == "":
return ""
value = raw_value.upper()
initial = value[0]
last_code = self._encode(initial)
if last_code is None:
return raw_value
res = [initial]
for letter in value:
code = self._encode(letter)
if code is None:
continue
if code == 7:
continue
if code not in (0, last_code):
res.append(str(code))
if len(res) > 3:
break
last_code = code
return ("".join(res) + "000")[:4]
def _encode(self, letter):
"""
Encode a letter using the SoundEx mapping.
Returns None if the letter is not recognized
"""
return self._soundex_mapping.get(letter)
def __str__(self):
return "soundex({0})".format(self.column)
__all__ = [
"StringTrim", "StringTranslate", "StringRTrim", "StringRepeat", "StringRPad",
"StringLTrim", "StringLPad", "StringLocate", "Levenshtein", "StringInStr", "InitCap",
"SoundEx"
]