-
Notifications
You must be signed in to change notification settings - Fork 6
/
util.py
245 lines (190 loc) · 5.97 KB
/
util.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
239
240
241
242
243
244
245
import nltk
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from nltk.tokenize import word_tokenize
from nltk.corpus import wordnet
import re
import pickle as pk
import csv
# number, adjective, noun, adverb, verb,
type = ['CD',
'JJ', 'JJR', 'JJS',
'NN', 'NNS', 'NNP', 'NNPS',
'RB', 'RBR', 'RBS',
'VB', 'VBD', 'VBG', 'VBN', 'VNP', 'VBZ']
def curl_cmd(url, path):
return 'curl ' + url + ' -o ' + path
def load_txt(path):
with open(path, 'r', encoding='utf-8') as infile:
return infile.readlines()
def save_txt(path, lines):
with open(path, 'w', encoding='utf-8') as infile:
infile.writelines(lines)
def load_csv(path):
with open(path, 'r', encoding='utf-8') as file_reader:
csv_reader = csv.reader(file_reader, delimiter=',')
lines = []
for line in csv_reader:
lines.append(line)
return lines
def save_csv(path, lines, de):
with open(path, 'w', encoding='utf-8', newline='') as f:
writer = csv.writer(f, delimiter=de)
writer.writerows(lines)
def save_pkl(path, data):
pk.dump(data, open(path, 'wb'))
def load_pkl(path):
return pk.load(open(path, 'rb'))
def camel_split(name):
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower().replace('_', ' ')
def camel_split_for_tokens(name):
name = re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower().replace('_', ' ')
name = re.findall(r'[0-9]+|[a-z]+', name)
return name
def get_camels(lines):
data = list()
for line in lines:
if len(line) == 0:
data.append([])
continue
data.append(camel_split(line))
return data
def filter_digit_english(line):
return re.sub(r'[^\x30-\x39, ^\x41-\x5A,^\x61-\x7A]+', ' ', line)
def get_filters(lines):
data = list()
for line in lines:
if len(line) == 0:
data.append([])
continue
data.append(filter_digit_english(line))
return data
def remove_stopwords(tokens):
clean_tokens = list()
sr = stopwords.words('english')
for token in tokens:
if token not in sr:
clean_tokens.append(token)
return clean_tokens
def remove_keywords(tokens):
clean_tokens = list()
sr = dict(load_pkl('data/keywords_vocab.pkl')).keys()
for token in tokens:
if token not in sr:
clean_tokens.append(token)
return clean_tokens
def get_synonyms(token):
synonyms = []
for syn in wordnet.synsets(token):
for lemma in syn.lemmas():
lem = lemma.name()
if lem not in synonyms and lem != token:
synonyms.append(lem)
return synonyms
def get_stemmed_words(tokens):
stemmer = PorterStemmer()
stemmed_tokens = list()
for token in tokens:
stemmed_tokens.append(stemmer.stem(token))
return stemmed_tokens
def get_stemmed(token):
stemmer = PorterStemmer()
return stemmer.stem(token)
def get_stemmed_lines(lines):
data = list()
for line in lines:
if len(line) == 0:
data.append([])
continue
data.append(get_stemmed_words(line))
return data
def get_tokens(line):
tokens = list()
for token in word_tokenize(line, 'english'):
if len(token) > 1:
if '-' in token:
ts = token.split('-')
for t in ts:
tokens.append(t)
else:
tokens.append(token)
return tokens
def get_tokens_lines(lines):
data = list()
for line in lines:
if len(line) == 0:
data.append([])
continue
data.append(get_tokens(line))
return data
def get_jdk_objects(tokens):
jdk = dict(load_pkl('data/jdk_vocab.pkl'))
objects = list()
for token in tokens:
if token in jdk.values():
key = list(jdk.keys())[list(jdk.values()).index(token)]
objects.append(key)
return objects
def get_token_class(tokens):
used_tokens = list()
tokens = nltk.pos_tag(tokens)
for token in tokens:
if token[1] in type:
used_tokens.append(token[0])
return used_tokens
def filter_tokens(tokens):
filterred_tokens = list()
for token in tokens:
token = re.sub('[^a-zA-Z0-9 ]', ' ', token)
token = re.sub(' +', ' ', token).strip()
if ' ' in token:
filterred_tokens.extend(token.split(' '))
else:
filterred_tokens.append(token)
return filterred_tokens
def match(key, line):
return [i.start() for i in re.compile(key).finditer(line)]
def data2txt(path_from, path_to):
lines = load_pkl(path_from)
with open(path_to, 'w', encoding='utf-8') as f:
for i in range(len(lines)):
for j in range(len(lines[i])):
ls = lines[i][j]
for k in range(len(ls)):
f.write(str(ls[k]))
if k < len(ls) - 1:
f.write(',')
if j < len(lines[i]) - 1:
f.write(';')
if i < len(lines) - 1:
f.write('\n')
def txt2data(path_from):
lines = load_txt(path_from)
data = []
for line in lines:
ls = line.split(';')
data_row = []
for i in range(len(ls)):
l = ls[i].split(',')
for j in range(2, len(l)):
l[j] = int(l[j])
data_row.append(l)
data.append(data_row)
return data
if __name__ == '__main__':
# data2txt('data/queries_parse.pkl', 'data/queries_parse.txt')
data = txt2data('data/queries_parse.txt')
print()
# a = get_synonyms('concatenation')
# a = camel_split_for_tokens('convertA22B')
# print(a)
# b = nltk.pos_tag(a)
# print(b)
# nltk.download()
# print(get_synonyms('square'))
# print(camel_split('CasdfSaaDkkd'))
# jacoma_query()
# jacoma_code()
# lem = WordNetLemmatizer()
# print(lem.lemmatize('ini', 'v'))
# save_csv('f://test.txt', [[1, 2, 3], [4, 5, 6]])