-
Notifications
You must be signed in to change notification settings - Fork 20
/
character.py
209 lines (174 loc) · 6.84 KB
/
character.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
"""
SpaCy augmenters for character level augmentation.
"""
from dacy.augmenters.keyboard import Keyboard
import random
from functools import partial
from typing import Dict, Iterable, Iterator, Callable, Union
import spacy
from spacy.language import Language
from spacy.training import Example
from .utils import make_text_from_orth
from .keyboard import KEYBOARDS, Keyboard
@spacy.registry.augmenters("char_swap_augmenter.v1")
def create_char_swap_augmenter(
doc_level: float, char_level: float
) -> Callable[[Language, Example], Iterator[Example]]:
"""Creates an augmenter that swaps two characters in a token.
Args:
doc_level (float): probability to augment document.
char_level (float): probability to augment character, if document is augmented.
Returns:
Callable[[Language, Example], Iterator[Example]]: The augmenter function.
"""
return partial(char_swap_augmenter, doc_level=doc_level, char_level=char_level)
@spacy.registry.augmenters("spacing_augmenter.v1")
def create_spacing_augmenter(
doc_level: float, spacing_level: float
) -> Callable[[Language, Example], Iterator[Example]]:
"""Creates an augmenter that removes spacing.
Args:
doc_level (float): probability to augment document.
spacing_level (float): probability to remove spacing, if document is augmented.
Returns:
Callable[[Language, Example], Iterator[Example]]: The augmenter function.
"""
return partial(spacing_augmenter, doc_level=doc_level, spacing_level=spacing_level)
@spacy.registry.augmenters("char_random_augmenter.v1")
def create_char_random_augmenter(
doc_level: float, char_level: float, keyboard: Union[str, Keyboard] = "QWERTY_EN"
) -> Callable[[Language, Example], Iterator[Example]]:
"""Creates an augmenter that replacies a character with a random character from the
keyboard.
Args:
doc_level (float): probability to augment document.
char_level (float): probability to augment character, if document is augmented.
keyboard (str, Keyboard, optional): A Keyboard class or a string denoting a default keyboard from
which replace characters are sampled from. Possible options for string include:
"QWERTY_EN": English QWERTY keyboard
"QWERTY_DA": Danish QWERTY keyboard
Defaults to "QWERTY_EN".
Returns:
Callable[[Language, Example], Iterator[Example]]: The augmenter function.
"""
kb = Keyboard(keyboard_array=KEYBOARDS[keyboard])
replace_dict = {k: list(kb.all_keys()) for k in kb.all_keys()}
return partial(
char_replace_augmenter,
replacement=replace_dict,
doc_level=doc_level,
char_level=char_level,
)
@spacy.registry.augmenters("keyboard_augmenter.v1")
def create_keyboard_augmenter(
doc_level: float,
char_level: float,
distance=1,
keyboard: Union[str, Keyboard] = "QWERTY_EN",
) -> Callable[[Language, Example], Iterator[Example]]:
"""Creates a document level augmenter using plausible typos based on keyboard distance.
Args:
doc_level (float): probability to augment document.
char_level (float): probability to augment character, if document is augmented.
distance (int, optional): keyboard distance. Defaults to 1.
keyboard (str, Keyboard, optional): A Keyboard class or a string denoting a default keyboard.
Possible options for string include:
"QWERTY_EN": English QWERTY keyboard
"QWERTY_DA": Danish QWERTY keyboard
Defaults to "QWERTY_EN".
Returns:
Callable[[Language, Example], Iterator[Example]]: The augmentation function
"""
kb = Keyboard(keyboard_array=KEYBOARDS[keyboard])
replace_dict = kb.create_distance_dict(distance=distance)
return partial(
char_replace_augmenter,
replacement=replace_dict,
doc_level=doc_level,
char_level=char_level,
)
@spacy.registry.augmenters("char_replace_augmenter.v1")
def create_char_replace_augmenter(
doc_level: float, char_level: float, replacement: dict
) -> Callable[[Language, Example], Iterator[Example]]:
"""Creates an augmenter that replaces a character with a random character from the
keyboard.
Args:
doc_level (float): probability to augment document.
char_level (float): probability to augment character, if document is augmented.
replace (dict): A dictionary denoting which characters denote potentials replacement for each character.
E.g. {"æ": "ae"}
Returns:
Callable[[Language, Example], Iterator[Example]]: The augmenter function.
"""
return partial(
char_replace_augmenter,
replacement=replacement,
doc_level=doc_level,
char_level=char_level,
)
def char_replace_augmenter(
nlp: Language,
example: Example,
replacement: dict,
doc_level: float = 0.5,
char_level: float = 0.1,
) -> Iterator[Example]:
def __replace(t):
t_ = []
for i, c in enumerate(t.text):
if random.random() < char_level and c in replacement:
c = random.sample(replacement[c], k=1)[0]
t_.append(c)
return "".join(t_)
if random.random() >= doc_level:
yield example
else:
example_dict = example.to_dict()
example_dict["token_annotation"]["ORTH"] = [
__replace(t) for t in example.reference
]
text = make_text_from_orth(example_dict)
doc = nlp.make_doc(text)
yield example.from_dict(doc, example_dict)
def char_swap_augmenter(
nlp: Language,
example: Example,
doc_level: float = 0.5,
char_level: float = 0.1,
) -> Iterator[Example]:
def __replace(t):
for i, c in enumerate(t.text[:-1]):
if random.random() < char_level:
return t.text[:i] + t.text[i + 1] + c + t.text[i + 2 :]
return t
if random.random() >= doc_level:
yield example
else:
example_dict = example.to_dict()
example_dict["token_annotation"]["ORTH"] = [
__replace(t) for t in example.reference
]
text = make_text_from_orth(example_dict)
doc = nlp.make_doc(text)
yield example.from_dict(doc, example_dict)
def spacing_augmenter(
nlp: Language,
example: Example,
doc_level: float = 0.5,
spacing_level: float = 0.1,
) -> Iterator[Example]:
def __replace(s):
if random.random() < spacing_level and (s is True):
return False
return s
if random.random() >= doc_level:
yield example
else:
example_dict = example.to_dict()
example_dict["token_annotation"]["SPACY"] = [
__replace(s) for s in example_dict["token_annotation"]["SPACY"]
]
text = make_text_from_orth(example_dict)
doc = nlp.make_doc(text)
yield example.from_dict(doc, example_dict)