Update openthreatner.py
Browse files- openthreatner.py +164 -145
openthreatner.py
CHANGED
@@ -1,146 +1,165 @@
|
|
1 |
-
# coding=utf-8
|
2 |
-
# Copyright 2020 HuggingFace Datasets Authors.
|
3 |
-
#
|
4 |
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
-
# you may not use this file except in compliance with the License.
|
6 |
-
# You may obtain a copy of the License at
|
7 |
-
#
|
8 |
-
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
-
#
|
10 |
-
# Unless required by applicable law or agreed to in writing, software
|
11 |
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
-
# See the License for the specific language governing permissions and
|
14 |
-
# limitations under the License.
|
15 |
-
|
16 |
-
# Lint as: python3
|
17 |
-
"""The Open Threat dataset"""
|
18 |
-
|
19 |
-
|
20 |
-
import datasets
|
21 |
-
|
22 |
-
|
23 |
-
logger = datasets.logging.get_logger(__name__)
|
24 |
-
|
25 |
-
_DESCRIPTION = """\
|
26 |
-
TBD
|
27 |
-
"""
|
28 |
-
|
29 |
-
_CITATION = """\
|
30 |
-
TBD
|
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 |
}
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2020 HuggingFace Datasets Authors.
|
3 |
+
#
|
4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
+
# you may not use this file except in compliance with the License.
|
6 |
+
# You may obtain a copy of the License at
|
7 |
+
#
|
8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
+
#
|
10 |
+
# Unless required by applicable law or agreed to in writing, software
|
11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
+
# See the License for the specific language governing permissions and
|
14 |
+
# limitations under the License.
|
15 |
+
|
16 |
+
# Lint as: python3
|
17 |
+
"""The Open Threat dataset"""
|
18 |
+
|
19 |
+
|
20 |
+
import datasets
|
21 |
+
|
22 |
+
|
23 |
+
logger = datasets.logging.get_logger(__name__)
|
24 |
+
|
25 |
+
_DESCRIPTION = """\
|
26 |
+
TBD
|
27 |
+
"""
|
28 |
+
|
29 |
+
_CITATION = """\
|
30 |
+
TBD
|
31 |
+
"""
|
32 |
+
|
33 |
+
|
34 |
+
_URL = "https://huggingface.co/datasets/priamai/openthreatner/raw/main/conll/"
|
35 |
+
_TRAINING_FILE = "text_32.conll"
|
36 |
+
_DEV_FILE = "text_23.conll"
|
37 |
+
_TEST_FILE = "text_1.conll"
|
38 |
+
|
39 |
+
class OurDatasetConfig(datasets.BuilderConfig):
|
40 |
+
"""The Open NER dataset."""
|
41 |
+
|
42 |
+
def __init__(self, **kwargs):
|
43 |
+
"""BuilderConfig for Open Threat dataset.
|
44 |
+
Args:
|
45 |
+
**kwargs: keyword arguments forwarded to super.
|
46 |
+
"""
|
47 |
+
super(OurDatasetConfig, self).__init__(**kwargs)
|
48 |
+
|
49 |
+
|
50 |
+
class OurDataset(datasets.GeneratorBasedBuilder):
|
51 |
+
"""The Open NER dataset Entities Dataset."""
|
52 |
+
|
53 |
+
BUILDER_CONFIGS = [
|
54 |
+
OurDatasetConfig(
|
55 |
+
name="Open Threat", version=datasets.Version("1.0.0"), description="The Open Cyber Threat Entities Dataset"
|
56 |
+
),
|
57 |
+
]
|
58 |
+
|
59 |
+
def _info(self):
|
60 |
+
|
61 |
+
names = names = [
|
62 |
+
"O",
|
63 |
+
"B-date",
|
64 |
+
"I-date",
|
65 |
+
"B-time",
|
66 |
+
"I-time",
|
67 |
+
"B-geo_location",
|
68 |
+
"I-geo_location",
|
69 |
+
"B-organization",
|
70 |
+
"I-organization",
|
71 |
+
"B-sector",
|
72 |
+
"I-sector",
|
73 |
+
"B-threat_actor",
|
74 |
+
"I-threat_actor",
|
75 |
+
"B-exploit_name",
|
76 |
+
"I-exploit_name",
|
77 |
+
"B-malware",
|
78 |
+
"I-malware",
|
79 |
+
"B-os",
|
80 |
+
"I-os",
|
81 |
+
"B-software",
|
82 |
+
"I-software",
|
83 |
+
"B-hardware",
|
84 |
+
"I-hardware",
|
85 |
+
"B-username",
|
86 |
+
"I-username",
|
87 |
+
"B-ttp",
|
88 |
+
"I-ttp",
|
89 |
+
"B-code_cmd",
|
90 |
+
"I-code_cmd",
|
91 |
+
"B-classification",
|
92 |
+
"I-classification",
|
93 |
+
]
|
94 |
+
|
95 |
+
return datasets.DatasetInfo(
|
96 |
+
description=_DESCRIPTION,
|
97 |
+
features=datasets.Features(
|
98 |
+
{
|
99 |
+
"id": datasets.Value("string"),
|
100 |
+
"tokens": datasets.Sequence(datasets.Value("string")),
|
101 |
+
"ner_tags": datasets.Sequence(
|
102 |
+
#TODO here: replace with the apprioriate list
|
103 |
+
datasets.features.ClassLabel(
|
104 |
+
names= list(map(str.lower,names))
|
105 |
+
)
|
106 |
+
),
|
107 |
+
}
|
108 |
+
),
|
109 |
+
supervised_keys=None,
|
110 |
+
homepage="https://test.cti.tools/",
|
111 |
+
citation=_CITATION,
|
112 |
+
)
|
113 |
+
|
114 |
+
def _split_generators(self, dl_manager):
|
115 |
+
"""Returns SplitGenerators."""
|
116 |
+
urls_to_download = {
|
117 |
+
"train": f"{_URL}{_TRAINING_FILE}",
|
118 |
+
"dev": f"{_URL}{_DEV_FILE}",
|
119 |
+
"test": f"{_URL}{_TEST_FILE}",
|
120 |
+
}
|
121 |
+
downloaded_files = dl_manager.download_and_extract(urls_to_download)
|
122 |
+
|
123 |
+
return [
|
124 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}),
|
125 |
+
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["dev"]}),
|
126 |
+
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": downloaded_files["test"]}),
|
127 |
+
]
|
128 |
+
|
129 |
+
def _generate_examples(self, filepath):
|
130 |
+
logger.info("⏳ Generating examples from = %s", filepath)
|
131 |
+
with open(filepath, encoding="utf-8") as f:
|
132 |
+
current_tokens = []
|
133 |
+
current_labels = []
|
134 |
+
sentence_counter = 0
|
135 |
+
for row in f:
|
136 |
+
row = row.rstrip()
|
137 |
+
if row:
|
138 |
+
token, label = row.split("\t")
|
139 |
+
current_tokens.append(token)
|
140 |
+
current_labels.append(label)
|
141 |
+
else:
|
142 |
+
# New sentence
|
143 |
+
if not current_tokens:
|
144 |
+
# Consecutive empty lines will cause empty sentences
|
145 |
+
continue
|
146 |
+
assert len(current_tokens) == len(current_labels), "💔 between len of tokens & labels"
|
147 |
+
sentence = (
|
148 |
+
sentence_counter,
|
149 |
+
{
|
150 |
+
"id": str(sentence_counter),
|
151 |
+
"tokens": current_tokens,
|
152 |
+
"ner_tags": current_labels,
|
153 |
+
},
|
154 |
+
)
|
155 |
+
sentence_counter += 1
|
156 |
+
current_tokens = []
|
157 |
+
current_labels = []
|
158 |
+
yield sentence
|
159 |
+
# Don't forget last sentence in dataset 🧐
|
160 |
+
if current_tokens:
|
161 |
+
yield sentence_counter, {
|
162 |
+
"id": str(sentence_counter),
|
163 |
+
"tokens": current_tokens,
|
164 |
+
"ner_tags": current_labels,
|
165 |
}
|