-
Notifications
You must be signed in to change notification settings - Fork 5
/
annotator.py
97 lines (69 loc) · 2.61 KB
/
annotator.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
import csv
import json
import sys
import re
import en_core_web_lg
def usage():
print 'usage: python annotator.py <path_to_dataset_in_csv> <output_file>'
def _get_annotations(desc, pattern, label):
regex = re.compile(pattern)
return [(match.start(), match.start() + len(match.group()), label) for match in regex.finditer(desc)]
# ------------------- ANNOTATION RULES -------------------
def find_functions(desc):
pattern = r'[a-zA-Z0-9]+(_[a-zA-Z0-9]+)+'
return _get_annotations(desc, pattern, "FUNCTION")
def find_kernel_version(desc):
pattern = r'\d+(\.\d+)+(-?)(\w+)?'
return _get_annotations(desc, pattern, "VERSION")
def find_file_path(desc):
pattern = r'(/)?\w+(/\w+)+\.c'
return _get_annotations(desc, pattern, "SOURCECODE")
def find_fd_driver(desc):
pattern = r'(/)?\w+(/\w+(?!\.c))+'
return _get_annotations(desc, pattern, "DRIVER")
def find_driver(desc):
pattern = r'[a-zA-Z]+\s[a-zA-Z\-]+\s(?=driver)'
return _get_annotations(desc, pattern, "DRIVER")
def find_structs(desc):
pattern = r'(?<=struct\s)\w+(\s\w+)'
return _get_annotations(desc, pattern, "STRUCT")
# ------------------- END ANNOTATION RULES -------------------
TAGGERS = [
find_functions,
find_kernel_version,
find_file_path,
find_driver,
find_structs,
find_fd_driver
]
def annotate_NER(dataset_file):
TRAIN_DATA = []
with open(dataset_file, 'r') as cve_dataset_f:
# cve_reader = csv.DictReader(cve_dataset_f, delimiter=';')
for cve in cve_dataset_f:
entities = []
for tagger in TAGGERS:
entities += tagger(cve)
TRAIN_DATA += [[cve, {'entities': entities}]]
with open('annotated_{}_NER_train.json'.format(dataset_file.replace('.csv', '')), 'w') as annotated_f:
json.dump(TRAIN_DATA, annotated_f)
def annotate_DEP(dataset_file):
TRAIN_DATA = []
model = en_core_web_lg.load()
with open(dataset_file, 'r') as cve_dataset_f:
cve_reader = csv.DictReader(cve_dataset_f, delimiter=';')
for cve in cve_reader:
tagged_desc = model(unicode(cve['Avail.']))
heads = [tok.head.i for tok in tagged_desc]
deps = ['-']*len(heads)
TRAIN_DATA += [[cve['Avail.'], {'heads': heads, 'deps': deps}]]
with open('annotated_{}_DEP_train.json'.format(dataset_file.replace('.csv', '')), 'w') as annotated_f:
json.dump(TRAIN_DATA, annotated_f)
def main(dataset_file):
annotate_NER(dataset_file)
# annotate_DEP(dataset_file)
if __name__ == '__main__':
if len(sys.argv) <= 1:
usage()
sys.exit(0)
main(sys.argv[1])