-
Notifications
You must be signed in to change notification settings - Fork 18
/
preprocessings.py
417 lines (364 loc) · 15 KB
/
preprocessings.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!python
# -*- coding: utf-8 -*-
# @author: Kun
'''
Author: Kun
Date: 2021-09-16 11:15:47
LastEditTime: 2021-09-23 17:45:29
LastEditors: Kun
Description:
FilePath: /ThreatReportExtractor/preprocessings.py
'''
import re
import spacy
import nltk
import signal
import neuralcoref
from nltk import sent_tokenize
from textblob import TextBlob
from data_loader.pattern_loader import load_patterns, load_lists, all_lst
from data_loader.ioc_loader import iocs
from nlp_extractor.passive2active import PASS2ACT
from pattern.text.en import conjugate, PRESENT, SG
from project_config import RE_PATTERNS_FILE_PATH, SEC_PATTERNS_FILE_PATH
########################################################################
########################################################################
class PreProcessor(object):
def __init__(self, nlp) -> None:
super(PreProcessor, self).__init__()
self.nlp = nlp # nlp = spacy.load("en_core_web_lg")
self.Pass2Act = PASS2ACT(nlp)
def delete_brackets(self, stri):
stri = stri.replace("[", "")
stri = stri.replace("]", "")
return stri
########################################################################
def pass2acti(self, stri):
result = ' '
for i in sent_tokenize(stri):
pa2act = self.Pass2Act.pass2act(i)
result += pa2act
return result
def coref_(self, stri):
neuralcoref.add_to_pipe(self.nlp)
doc = self.nlp(stri)
corefed = doc._.coref_resolved.replace("\n", "")
if corefed:
return corefed
else:
return stri
########################################################################
def wild_card_extansions(self, stri):
pat = load_patterns(RE_PATTERNS_FILE_PATH)
files = re.findall(pat['*extension'], stri)
for i in files:
stri = stri.replace(i, " *" + i.strip())
return stri
def try_to(self, stri):
verb = ''
try_to_list = ['tries to', 'try to', 'attempts to', 'attempt to']
blob = TextBlob(stri)
sentences = sent_tokenize(stri)
for i in range(len(sentences)):
for element in try_to_list:
if element in sentences[i]:
# one or more space
match = re.search(element+'[ ]+(\S+)', stri)
if match:
verb = match.group(1) # +'s'
if verb:
for word, pos in blob.tags:
if word == verb and "V" in pos:
stri = stri.replace(match.group(), verb)
return stri
def is_capable_of(self, stri):
sentences = sent_tokenize(stri)
keywords = ['is capable of', 'are capable of']
outcome = ''
for sent in sentences:
for keyword in keywords:
if keyword in sent:
before_keyword, keyword, after_keyword = sent.partition(
keyword)
token = nltk.word_tokenize(sent)
for i in nltk.pos_tag(token):
if i[0] == after_keyword.split()[0] and i[1] == 'VBG':
outcome += " " + before_keyword + conjugate(verb=after_keyword.split(
)[0], tense=PRESENT, number=SG) + " " + ' '.join(after_keyword.split()[1:])
else:
if sent not in outcome:
outcome += " " + sent
return outcome
def ellipsis_subject(self, stri):
ellipsis_verbs = load_lists(SEC_PATTERNS_FILE_PATH)['verbs']
ellipsis_verbs = ellipsis_verbs.replace(
"'", "").strip('][').split(', ')
sent_text = nltk.sent_tokenize(stri)
result = ""
for sentence in sent_text:
token = nltk.word_tokenize(sentence)
doc = self.nlp(sentence)
if nltk.pos_tag(token)[0][1] == "VB" or nltk.pos_tag(token)[0][1] == "VBZ" or doc[0].pos_ == "VERB" or doc[0].text.lower() in ellipsis_verbs:
new_sentence = " It " + \
nltk.pos_tag(token)[0][0].lower() + " " + \
" ".join(sentence.split(" ")[1:])
result += " " + new_sentence
elif doc[0].dep_ == "ROOT":
if doc[0].text.lower in ellipsis_verbs:
new_sentence = " It " + \
doc[0].text.lower() + " " + \
" ".join(sentence.split(" ")[1:])
result += " " + new_sentence
elif doc[0].text.lower() in ellipsis_verbs and doc[0].dep_ != "ROOT":
result += " " + doc.text
else:
result += " " + sentence
return result
def detect_subj(self, sentence_list):
# buffer_nsubj = {}
subject = ''
for sentence in sentence_list:
doc = self.nlp(sentence)
for token in doc:
if token.dep_ == "nsubj":
subject = token.text
# buffer_nsubj[sentence] = token.text
# return buffer_nsubj
if subject:
return subject
def zero_word_verb(self, stri):
doc = self.nlp(stri.strip())
main_verbs = load_lists(SEC_PATTERNS_FILE_PATH)['verbs']
main_verbs = main_verbs.replace("'", "").strip('][').split(', ')
if not (doc[0].tag_ == "MD") and\
not (doc[0].tag_ == "VB" and
str(doc[0]).lower() in main_verbs) and\
not (doc[0].tag_ == "VB" and
str(doc[0]).lower() not in main_verbs) and\
not(str(doc[0]).lower() in main_verbs):
return False
else:
return True
def capitalize(self, line):
return ' '.join(s[:1].upper() + s[1:] for s in line.split(' '))
def replcae_surrounding_subject(self, stri):
sent_list = sent_tokenize(stri)
new_text = ''
for indx, sentence in enumerate(sent_list):
if self.zero_word_verb(sentence):
subject = self.detect_subj(sent_list[:indx])
new_text += " " + self.capitalize(subject) + \
" " + sentence[0].lower() + sentence[1:]
else:
new_text += " " + sentence
return new_text
def coref_the_following_colon(self, stri):
sentence2 = ' '
final_txt = ''
fl = len(final_txt)
# list1 = the_following_colon_lst()
list1 = load_lists(SEC_PATTERNS_FILE_PATH)['TFCL']
list1 = list1.replace("'", "").strip('][').split(', ')
sentences = sent_tokenize(stri)
l = len(sentences)
c = 0
for sentence in sentences:
c += 1
for value in list1:
if value in sentence:
sentence.strip() # to get ride of possible space at the end of sentence
if sentence[-1] == ".":
# removes the dot from the end
sentence = sentence[:-1]
if ":" in sentence:
one = sentence.split(value)[0]
two = sentence.split(value)[1]
# sentence2 = sentence.split(":")[0].replace(value[:-1],sentence.split(":",1)[1]) + ". " # replace the token with value
# sentence2 = sentence.replace(value, sentence.split(value)[1]) + ". " # replace the token with value
sentence2 = sentence.replace(value, " ") + ". "
final_txt += " " + sentence2
p = final_txt
fl += 1
break
if c > fl:
final_txt += " " + sentence
fl += 1
return final_txt
def coref_the_following_middle(self, stri):
final_txt = ''
list2 = load_lists(SEC_PATTERNS_FILE_PATH)['TFL']
list2 = list2.replace("'", "").strip('][').split(', ')
sentences = sent_tokenize(stri)
c = 0
fl = len(final_txt)
for sentence in sentences:
c += 1
for value in list2:
if value in sentence:
sentence.strip()
if sentence[-1] == "." and ":" in sentence:
sentence = sentence[:-1]
sentence2 = sentence.split(":")[0].replace(
value, sentence.split(":", 1)[1]) + ". "
final_txt += ' ' + sentence2
fl += 1
break
if c > fl:
final_txt += ' ' + sentence
fl += 1
return final_txt
def obscure_vocabs(self):
vb_send = ['send', 'exfiltrate', 'postsinformation',
'exfiled', 'exfil', 'beacon']
vb_sends = ['sends', 'exfiltrates', 'postsinformations',
'exfileds', 'exfils', 'beacons']
vb_write = ['write', 'entrench']
vb_writes = ['writes', 'entrenches']
return vb_send, vb_sends, vb_writes, vb_write
def translate_obscure_words(self, stri):
list1 = self.obscure_vocabs()
finalsent = ''
sentences = sent_tokenize(stri)
for index, sentence in enumerate(sentences):
for lis in list1:
big_regex = re.compile('|'.join(map(re.escape, lis)))
sent = big_regex.sub(lis[0], str(sentence))
sentence = sent
finalsent += ' ' + sent + ' '
return finalsent
def homogenization(self, stri):
# 均质化 ....
finalsent = ''
vars = all_lst()
# vars = load_lists(SEC_PATTERNS_FILE_PATH)['VAR']
# vars = vars.replace("'", "").strip('][').split(', ')
sentences = sent_tokenize(stri)
for index, sentence in enumerate(sentences):
for var in vars:
big_regex = re.compile('|'.join(map(re.escape, var)))
sent = big_regex.sub(var[0], str(sentence))
sentence = sent
finalsent += ' ' + sent + ' '
return finalsent
########################################################################
def communicate_to_sr(self, stri):
final_txt = ''
c = fl = 0
pattern = load_lists(SEC_PATTERNS_FILE_PATH)['COMU']
pattern = pattern.replace("'", "").strip('][').split(', ')
sentences = sent_tokenize(stri)
for sentence in sentences:
c += 1
for value in pattern:
if value in sentence:
sentence1 = sentence.split(
value)[0] + ' receives from' + sentence.split(value)[1]
sentence2 = sentence.split(
value)[0] + ' sends to' + sentence.split(value)[1]
final_txt += " " + sentence1 + " " + sentence2
fl += 2
c += 1
break
if c > fl:
final_txt += " " + sentence
fl += 1
return final_txt
def CـC(self, txt):
pattern = load_lists(SEC_PATTERNS_FILE_PATH)['C_C']
pattern = pattern.replace("'", "").strip('][').split(', ')
big_regex = re.compile(
'|'.join(map(re.escape, pattern)), re.IGNORECASE)
sentence = big_regex.sub('remote ip:*', str(txt))
return sentence
def following_subject(self, txt):
following_subject_list = load_lists(SEC_PATTERNS_FILE_PATH)['TFSL']
txt = txt.rstrip()
txt = txt.rstrip('.')
result = ""
for sent in sent_tokenize(txt):
for item in following_subject_list:
if item in sent and ":" in sent:
old_subj = item
new_sub = sent.split(":", 1)[1]
y = iocs.list_of_iocs(sent)
if y:
sentence_replicas = [new_sub[0]] * len(y)
for i in range(len(sentence_replicas)):
k = sent.split(":", 1)[0]
l = k.replace(old_subj, " ")
result += y[i] + l + " . "
break
return result
def verb_and_verb(self, txt):
verbs_list = load_lists(SEC_PATTERNS_FILE_PATH)['verbs']
doc = self.nlp(txt)
result = ""
for i in range(len(doc)+2):
if doc[i].pos_ == "VERB" and doc[i+1].pos_ == "CCONJ" and doc[i+2].pos_ == "VERB":
if doc[i].text in verbs_list and doc[i+2].text in verbs_list:
candidate = doc[i].text + " " + \
doc[i + 1].text + " " + doc[i + 2].text
result += txt.replace(candidate, doc[i].text) + " "
result += txt.replace(candidate, doc[i+2].text)
break
return result
# txt = preprocessing_input
# txt = delete_brackets(txt)
# txt = pass2acti(txt)
# txt = re.sub(' +', ' ', txt)
# print("*********8", txt)
# if main.args.crf == 'true':
# txt = coref_(txt)
# print("coref_", len(txt), txt)
# else:
# txt = wild_card_extansions(txt)
# txt = try_to(txt)
# print("try_to__", txt)
# txt = is_capable_of(txt)
########################################################################
# import main
# if main.args.elip == 'true':
# txt = replcae_surrounding_subject(txt)
# else:
# print("is capble of__", txt)
# txt = ellipsis_subject(txt)
# print("ellipsis_subject", len(txt), txt)
# print('------------ coref_the_following_colon ------------')
# out = coref_the_following_colon(txt)
# for i, val in enumerate(sent_tokenize(out)):
# print(i, val)
# print('------------ coref_the_following_middle ------------')
# midle = coref_the_following_middle(out)
# for i, val in enumerate(sent_tokenize(midle)):
# print(i, val)
# out_translate = translate_obscure_words(out)
# print("*****homogenization:", homogenization(out_translate))
# homo = homogenization(out_translate)
# comm = communicate_to_sr(homo)
# print(comm)
# cc = CـC(comm)
########################################################################
def modification_(self, cc):
final_txt = ''
c = fl = 0
pattern = load_lists(SEC_PATTERNS_FILE_PATH)['MDF']
print("pattern: ", pattern)
pattern = pattern.replace("'", "").strip('][').split(', ')
sentences = sent_tokenize(cc)
for sentence in sentences:
c += 1
for value in pattern:
if value in sentence:
sentence1 = sentence.split(
value)[0] + ' modifies ' + sentence.split(value)[1]
final_txt += " " + sentence1 + " "
fl += 2
break
if c > fl:
final_txt += " " + sentence
fl += 1
return final_txt.strip()
########################################################################
# print('----Preprocessed:----')
# for i, val in enumerate(sent_tokenize(modification_())):
# print(i, val)