-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_noise.py
143 lines (118 loc) · 4 KB
/
add_noise.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
import random
random.seed(42)
error_rate = 0.1
path = "./data/conllpp_train.txt"
out_path = "./data/conllpp_train_wrong_2.txt"
with open(path, "r", encoding="utf-8") as f:
row_data = f.readlines()
ner_dict = {
"O": 0,
"B-PER": 1,
"I-PER": 2,
"B-ORG": 3,
"I-ORG": 4,
"B-LOC": 5,
"I-LOC": 6,
"B-MISC": 7,
"I-MISC": 8,
"PAD": 9,
}
ner_label = ["O", "B-PER", "B-ORG", "B-LOC", "B-MISC"]
def key_to_val(key, dic):
return dic[key] if key in dic else dic["UNK"]
def get_wrong_label(correct_label):
if correct_label[0] == "B" or correct_label == "O":
candidates = [label for label in ner_label if label != correct_label]
else:
candidates = ner_label
return random.choice(candidates)
tokens = []
labels = []
sentences = {}
data_num = 0
label_id_to_sentence = []
for line in row_data:
if "-DOCSTART-" in line:
continue
elif len(line) <= 5:
sentence = " ".join(tokens)
if sentence not in sentences.keys():
sentences[sentence] = {
"labels": labels,
"count": 1,
"label_ids": range(len(label_id_to_sentence), len(label_id_to_sentence) + len(labels)),
}
label_id_to_sentence += [(sentence, i) for i in range(len(labels))]
else:
sentences[sentence]["count"] += 1
tokens = []
labels = []
continue
else:
line = line.strip().split()
tokens.append(line[0])
labels.append(line[-1])
data_num += 1
if len(tokens) != 0:
sentence = " ".join(tokens)
if sentence not in sentences.keys():
sentences[sentence] = {
"labels": labels,
"count": 1,
"label_ids": range(len(label_id_to_sentence), len(label_id_to_sentence) + len(labels)),
}
label_id_to_sentence += labels
else:
label_id_to_sentence += [(sentence, i) for i in range(len(labels))]
error_num = int(data_num * error_rate)
errors = random.sample(range(len(label_id_to_sentence)), k=error_num)
changed = 0
for i in errors:
change_labels = sentences[label_id_to_sentence[i][0]]["labels"]
label_idx = label_id_to_sentence[i][1]
change_label = change_labels[label_idx]
count = sentences[label_id_to_sentence[i][0]]["count"]
wrong_label = get_wrong_label(change_label)
if change_label == "O":
if len(change_labels) > label_idx + 1 and change_labels[label_idx + 1] == wrong_label:
change_labels[label_idx + 1] = "I" + wrong_label[1:]
changed += count
else:
if wrong_label == "O":
if len(change_labels) > label_idx + 1 and change_labels[label_idx + 1][0] == "I":
change_labels[label_idx + 1] = "B" + change_labels[label_idx + 1][1:]
changed += count
else:
j = 1
while len(change_labels) > label_idx + j and change_labels[label_idx + j][0] == "I":
change_labels[label_idx + j] = "I" + wrong_label[1:]
changed += count
j += 1
change_labels[label_idx] = wrong_label
changed += count
sentences[label_id_to_sentence[i][0]]["labels"] = change_labels
if changed >= error_num:
break
out = []
data1 = []
data2 = []
tokens = []
labels = []
for line in row_data:
if "-DOCSTART-" in line:
out.append(line.strip())
elif len(line) <= 5:
sentence = " ".join(tokens)
labels = sentences[sentence]["labels"]
for token, d1, d2, label in zip(tokens, data1, data2, labels):
out.append(f"{token} {d1} {d2} {label}")
tokens, data1, data2 = [], [], []
out.append(line.strip())
else:
line = line.strip().split()
tokens.append(line[0])
data1.append(line[1])
data2.append(line[2])
out = "\n".join(out)
with open(out_path, "w", encoding="utf-8") as f:
f.write(out)