-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_ngrams.py
executable file
·236 lines (223 loc) · 6.61 KB
/
gen_ngrams.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This is a generator of skippy n-grams from a string or a list of segments.
developed by Kow Kuroda ([email protected])
Creation: 2024/06/21
Modified:
2024/06/22: changed argument order to ensure compatibility with ngrams_skippy.py and ngrams.py
2024/06/28: renamed variable 'splitter' to 'sep' for compatibility; implemented max_distance
"""
## imports
import itertools
## functions
def segment (text: str, sep: str, check: bool):
"""
converts a string to a list of segmenets with the specified separator.
"""
try:
L = text.split(sep)
except ValueError:
L = list(text)
## remove null strings generated by inefficient splitter
L = [ x for x in L if len(x) > 0 ]
if check:
print(f"#L: {L}")
#
return L
##
def gen_ngrams (S: list, n: int, sep = " ", as_list = False, check = False):
"""
takes a list S of segments and returns a list R of n-grams out of them.
"""
if check:
print(f"#S: {S}")
#
assert n > 0
if len(S) <= n:
if as_list:
return S
else:
return [ sep.join(S) ]
#
R = [ ]
for i, x in enumerate(S):
try:
y = S[ i:i + n] # get an n-gram
if len(y) == n: # check its length
R.append(y)
except IndexError:
pass
##
if as_list:
return R
else:
return [ sep.join(x) for x in R ]
##
def gen_skippy_ngrams (S: list, n: int, max_distance = None, sep: str = " ", missing_mark: str = "…", as_list: bool = False, check: bool = False):
"""
takes a list of segments and returns a list of skippy n-grams out of them
"""
if check:
print(f"#S: {S}")
#
assert n > 0
if len(S) <= n:
if as_list:
return S
else:
return [ sep.join(S) ]
## generate target index list
S_len = len(S)
R = range(S_len)
##P = itertools.combinations(I, r = n) # turned out to be offensive
## [ x for x in ...] is necessary as in the following
## implementation of restriction by max gap distance
if max_distance is None: ## max_distance-free
P = [ x for x in itertools.combinations(R, r = n) if max(x) <= S_len ]
else: ## max_distance implementation
Rx = [[ x for x in itertools.combinations(range(i, i + max_distance), n) if max(x) < len(S) ] for i in R ]
## flatten U
P = [ ]
for rx in Rx:
P.extend(rx)
##
if check:
print(f"#P: {P}")
## generate substrings
Q = [ ]
for p in P:
q = [ ]
for j in range(len(p)):
i = p[j]
x = S[i]
if i == 0:
q.append(x)
last_i = 0
else:
if last_i + 1 == i:
q.append(x)
else:
q.append(missing_mark)
q.append(x)
last_i = i
#
Q.append(q)
## return result
if as_list: ## result is a list of unstrung lists
return Q
else: ## result is a list of strings
R = [ ]
for q in Q:
## remove the intial missing_mark wrongly generated
if q[0] == missing_mark:
R.append(q[1:])
else:
R.append(q)
#
return ([ sep.join(r) for r in R ])
## aliases
def gen_ngrams_from_str (text: str, n: int, sep = " ", as_list = False, check = False):
"""
takes a string and returns a list of n-grams out of segments generated using the separator
"""
S = segment(text, sep, check)
if check:
print(f"#S: {S}")
R = [ ]
for i, x in enumerate(S):
try:
y = S[ i:i + n] # get an n-gram
if len(y) == n: # check its length
R.append(y)
except IndexError:
pass
##
if as_list:
return R
else:
return [ sep.join(x) for x in R ]
##
def gen_skippy_ngrams_from_str (text: str, n: int, sep: str = " ", missing_mark: str = "…", max_distance = None, as_list: bool = False, check: bool = False):
"""
takes a string and returns a list of skippy n-grams out of segments generated the using separator
"""
## split into segments
S = segment(text, sep, check)
if check:
print(f"#S: {S}")
## generate target index list
I = range(len(S))
##
if max_distance is None:
##P = itertools.combinations(I, r = n) # turned out to be offensive
## [ x for x in ...] is necessary as in the following
#P = [ x for x in itertools.combinations(I, r = n) ]
P = list(itertools.combinations(I, r = n)) # suggested by Pylint
else:
Rx = [[ x for x in itertools.combinations(range(i, i + max_distance), n) if max(x) < len(S) ] for i in I ]
## flatten U
P = [ ]
for rx in Rx:
P.extend(rx)
if check:
print(f"#P: {P}")
## generate substrings
Q = [ ]
for p in P:
q = [ ]
for j in range(len(p)):
i = p[j]
x = S[i]
if i == 0:
q.append(x)
last_i = 0
else:
if last_i + 1 == i:
q.append(x)
else:
q.append(missing_mark)
q.append(x)
last_i = i
#
Q.append(q)
## return result
if as_list: ## result is a list of unstrung lists
return Q
else: ## result is a list of strings
R = [ ]
for q in Q:
## remove the intial missing_mark wrongly generated
if q[0] == missing_mark:
R.append(q[1:])
else:
R.append(q)
#
return ([ sep.join(r) for r in R ])
##
def main():
# test 1
text1 = "abcdefghij"
print(f"input: '{text1}'")
print(gen_skippy_ngrams_from_str (text1, 3, sep = " ", check = False))
# test 2
text2 = "abc def gh ijk lmn op"
print(f"input: '{text2}'")
print(gen_skippy_ngrams_from_str (text2, 3, sep = " ", as_list = False, check = False))
# test 3
text3 = "abde"
print(f"input: '{text3}'")
print(gen_skippy_ngrams_from_str (text3, 4, sep = " ", check = False))
# test 4
print(f"input: '{text3}'")
print(gen_skippy_ngrams_from_str (text3, 5, sep = " ", check = False))
## test 5
print(f"input: '{text1}'")
print(gen_ngrams (text1, 5, sep = "", check = False))
## test 6
print(f"input: '{text2}'")
print(gen_ngrams (text2, 3, sep ="", check = False))
## tests
if __name__ == "__main__":
main()
### end of script