glitchlings 0.2.0__cp312-cp312-manylinux_2_17_x86_64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- glitchlings/__init__.py +42 -0
- glitchlings/__main__.py +9 -0
- glitchlings/_typogre_rust.cpython-312-x86_64-linux-gnu.so +0 -0
- glitchlings/_zoo_rust.cpython-312-x86_64-linux-gnu.so +0 -0
- glitchlings/dlc/__init__.py +5 -0
- glitchlings/dlc/huggingface.py +96 -0
- glitchlings/dlc/prime.py +252 -0
- glitchlings/main.py +240 -0
- glitchlings/util/__init__.py +151 -0
- glitchlings/zoo/__init__.py +57 -0
- glitchlings/zoo/core.py +282 -0
- glitchlings/zoo/jargoyle.py +225 -0
- glitchlings/zoo/mim1c.py +79 -0
- glitchlings/zoo/redactyl.py +128 -0
- glitchlings/zoo/reduple.py +100 -0
- glitchlings/zoo/rushmore.py +104 -0
- glitchlings/zoo/scannequin.py +166 -0
- glitchlings/zoo/typogre.py +184 -0
- glitchlings-0.2.0.dist-info/METADATA +493 -0
- glitchlings-0.2.0.dist-info/RECORD +24 -0
- glitchlings-0.2.0.dist-info/WHEEL +5 -0
- glitchlings-0.2.0.dist-info/entry_points.txt +2 -0
- glitchlings-0.2.0.dist-info/licenses/LICENSE +201 -0
- glitchlings-0.2.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,225 @@
|
|
1
|
+
import random
|
2
|
+
import re
|
3
|
+
from collections.abc import Iterable
|
4
|
+
from dataclasses import dataclass
|
5
|
+
from typing import Any, Literal, cast
|
6
|
+
|
7
|
+
import nltk
|
8
|
+
from nltk.corpus import wordnet as wn
|
9
|
+
from .core import Glitchling, AttackWave
|
10
|
+
|
11
|
+
_wordnet_ready = False
|
12
|
+
|
13
|
+
|
14
|
+
def _ensure_wordnet() -> None:
|
15
|
+
"""Ensure the WordNet corpus is available before use."""
|
16
|
+
|
17
|
+
global _wordnet_ready
|
18
|
+
if _wordnet_ready:
|
19
|
+
return
|
20
|
+
|
21
|
+
try:
|
22
|
+
wn.ensure_loaded()
|
23
|
+
except LookupError:
|
24
|
+
nltk.download("wordnet", quiet=True)
|
25
|
+
try:
|
26
|
+
wn.ensure_loaded()
|
27
|
+
except LookupError as exc: # pragma: no cover - only triggered when download fails
|
28
|
+
raise RuntimeError(
|
29
|
+
"Unable to load NLTK WordNet corpus for the jargoyle glitchling."
|
30
|
+
) from exc
|
31
|
+
|
32
|
+
_wordnet_ready = True
|
33
|
+
|
34
|
+
|
35
|
+
PartOfSpeech = Literal["n", "v", "a", "r"]
|
36
|
+
PartOfSpeechInput = PartOfSpeech | Iterable[PartOfSpeech] | Literal["any"]
|
37
|
+
NormalizedPartsOfSpeech = tuple[PartOfSpeech, ...]
|
38
|
+
|
39
|
+
_VALID_POS: tuple[PartOfSpeech, ...] = ("n", "v", "a", "r")
|
40
|
+
|
41
|
+
|
42
|
+
def _split_token(token: str) -> tuple[str, str, str]:
|
43
|
+
"""Split a token into leading punctuation, core word, and trailing punctuation."""
|
44
|
+
|
45
|
+
match = re.match(r"^(\W*)(.*?)(\W*)$", token)
|
46
|
+
if not match:
|
47
|
+
return "", token, ""
|
48
|
+
prefix, core, suffix = match.groups()
|
49
|
+
return prefix, core, suffix
|
50
|
+
|
51
|
+
|
52
|
+
def _normalize_parts_of_speech(part_of_speech: PartOfSpeechInput) -> NormalizedPartsOfSpeech:
|
53
|
+
"""Coerce user input into a tuple of valid WordNet POS tags."""
|
54
|
+
|
55
|
+
if isinstance(part_of_speech, str):
|
56
|
+
lowered = part_of_speech.lower()
|
57
|
+
if lowered == "any":
|
58
|
+
return _VALID_POS
|
59
|
+
if lowered not in _VALID_POS:
|
60
|
+
raise ValueError(
|
61
|
+
"part_of_speech must be one of 'n', 'v', 'a', 'r', or 'any'"
|
62
|
+
)
|
63
|
+
return (cast(PartOfSpeech, lowered),)
|
64
|
+
|
65
|
+
normalized: list[PartOfSpeech] = []
|
66
|
+
for pos in part_of_speech:
|
67
|
+
if pos not in _VALID_POS:
|
68
|
+
raise ValueError(
|
69
|
+
"part_of_speech entries must be one of 'n', 'v', 'a', or 'r'"
|
70
|
+
)
|
71
|
+
if pos not in normalized:
|
72
|
+
normalized.append(pos)
|
73
|
+
if not normalized:
|
74
|
+
raise ValueError("part_of_speech iterable may not be empty")
|
75
|
+
return tuple(normalized)
|
76
|
+
|
77
|
+
|
78
|
+
@dataclass(frozen=True)
|
79
|
+
class CandidateInfo:
|
80
|
+
"""Metadata for a candidate token that may be replaced."""
|
81
|
+
|
82
|
+
prefix: str
|
83
|
+
core_word: str
|
84
|
+
suffix: str
|
85
|
+
parts_of_speech: NormalizedPartsOfSpeech
|
86
|
+
|
87
|
+
|
88
|
+
def _collect_synonyms(
|
89
|
+
word: str, parts_of_speech: NormalizedPartsOfSpeech
|
90
|
+
) -> list[str]:
|
91
|
+
"""Gather deterministic synonym candidates for the supplied word."""
|
92
|
+
|
93
|
+
normalized_word = word.lower()
|
94
|
+
synonyms: set[str] = set()
|
95
|
+
for pos_tag in parts_of_speech:
|
96
|
+
synsets = wn.synsets(word, pos=pos_tag)
|
97
|
+
if not synsets:
|
98
|
+
continue
|
99
|
+
|
100
|
+
for synset in synsets:
|
101
|
+
lemmas_list = [lemma.name() for lemma in cast(Any, synset).lemmas()]
|
102
|
+
if not lemmas_list:
|
103
|
+
continue
|
104
|
+
|
105
|
+
filtered = []
|
106
|
+
for lemma_str in lemmas_list:
|
107
|
+
cleaned = lemma_str.replace("_", " ")
|
108
|
+
if cleaned.lower() != normalized_word:
|
109
|
+
filtered.append(cleaned)
|
110
|
+
|
111
|
+
if filtered:
|
112
|
+
synonyms.update(filtered)
|
113
|
+
break
|
114
|
+
|
115
|
+
if synonyms:
|
116
|
+
break
|
117
|
+
|
118
|
+
return sorted(synonyms)
|
119
|
+
|
120
|
+
|
121
|
+
def substitute_random_synonyms(
|
122
|
+
text: str,
|
123
|
+
replacement_rate: float = 0.1,
|
124
|
+
part_of_speech: PartOfSpeechInput = "n",
|
125
|
+
seed: int | None = None,
|
126
|
+
rng: random.Random | None = None,
|
127
|
+
) -> str:
|
128
|
+
"""Replace words with random WordNet synonyms.
|
129
|
+
|
130
|
+
Parameters
|
131
|
+
- text: Input text.
|
132
|
+
- replacement_rate: Max proportion of candidate words to replace (default 0.1).
|
133
|
+
- part_of_speech: WordNet POS tag(s) to target. Accepts "n", "v", "a", "r",
|
134
|
+
any iterable of those tags, or "any" to include all four.
|
135
|
+
- rng: Optional RNG instance used for deterministic sampling.
|
136
|
+
- seed: Optional seed if `rng` not provided.
|
137
|
+
|
138
|
+
Determinism
|
139
|
+
- Candidates collected in left-to-right order; no set() reordering.
|
140
|
+
- Replacement positions chosen via rng.sample.
|
141
|
+
- Synonyms sorted before rng.choice to fix ordering.
|
142
|
+
- For each POS, the first synset containing alternate lemmas is used for stability.
|
143
|
+
"""
|
144
|
+
_ensure_wordnet()
|
145
|
+
|
146
|
+
active_rng: random.Random
|
147
|
+
if rng is not None:
|
148
|
+
active_rng = rng
|
149
|
+
else:
|
150
|
+
active_rng = random.Random(seed)
|
151
|
+
|
152
|
+
target_pos = _normalize_parts_of_speech(part_of_speech)
|
153
|
+
|
154
|
+
# Split but keep whitespace separators so we can rebuild easily
|
155
|
+
tokens = re.split(r"(\s+)", text)
|
156
|
+
|
157
|
+
# Collect indices of candidate tokens (even positions 0,2,.. are words given our split design)
|
158
|
+
candidate_indices: list[int] = []
|
159
|
+
candidate_metadata: dict[int, CandidateInfo] = {}
|
160
|
+
for idx, tok in enumerate(tokens):
|
161
|
+
if idx % 2 == 0 and tok and not tok.isspace():
|
162
|
+
prefix, core_word, suffix = _split_token(tok)
|
163
|
+
if not core_word:
|
164
|
+
continue
|
165
|
+
|
166
|
+
available_pos: NormalizedPartsOfSpeech = tuple(
|
167
|
+
pos for pos in target_pos if wn.synsets(core_word, pos=pos)
|
168
|
+
)
|
169
|
+
if available_pos:
|
170
|
+
candidate_indices.append(idx)
|
171
|
+
candidate_metadata[idx] = CandidateInfo(
|
172
|
+
prefix=prefix,
|
173
|
+
core_word=core_word,
|
174
|
+
suffix=suffix,
|
175
|
+
parts_of_speech=available_pos,
|
176
|
+
)
|
177
|
+
|
178
|
+
if not candidate_indices:
|
179
|
+
return text
|
180
|
+
|
181
|
+
max_replacements = int(len(candidate_indices) * replacement_rate)
|
182
|
+
if max_replacements <= 0:
|
183
|
+
return text
|
184
|
+
|
185
|
+
# Choose which positions to replace deterministically via rng.sample
|
186
|
+
replace_positions = active_rng.sample(candidate_indices, k=max_replacements)
|
187
|
+
# Process in ascending order to avoid affecting later indices
|
188
|
+
replace_positions.sort()
|
189
|
+
|
190
|
+
for pos in replace_positions:
|
191
|
+
metadata = candidate_metadata[pos]
|
192
|
+
synonyms = _collect_synonyms(metadata.core_word, metadata.parts_of_speech)
|
193
|
+
if not synonyms:
|
194
|
+
continue
|
195
|
+
|
196
|
+
replacement = active_rng.choice(synonyms)
|
197
|
+
tokens[pos] = f"{metadata.prefix}{replacement}{metadata.suffix}"
|
198
|
+
|
199
|
+
return "".join(tokens)
|
200
|
+
|
201
|
+
|
202
|
+
class Jargoyle(Glitchling):
|
203
|
+
"""Glitchling that swaps words with random WordNet synonyms."""
|
204
|
+
|
205
|
+
def __init__(
|
206
|
+
self,
|
207
|
+
*,
|
208
|
+
replacement_rate: float = 0.1,
|
209
|
+
part_of_speech: PartOfSpeechInput = "n",
|
210
|
+
seed: int | None = None,
|
211
|
+
) -> None:
|
212
|
+
super().__init__(
|
213
|
+
name="Jargoyle",
|
214
|
+
corruption_function=substitute_random_synonyms,
|
215
|
+
scope=AttackWave.WORD,
|
216
|
+
seed=seed,
|
217
|
+
replacement_rate=replacement_rate,
|
218
|
+
part_of_speech=part_of_speech,
|
219
|
+
)
|
220
|
+
|
221
|
+
|
222
|
+
jargoyle = Jargoyle()
|
223
|
+
|
224
|
+
|
225
|
+
__all__ = ["Jargoyle", "jargoyle"]
|
glitchlings/zoo/mim1c.py
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
from typing import Literal
|
2
|
+
from .core import Glitchling, AttackWave, AttackOrder
|
3
|
+
import random
|
4
|
+
from confusable_homoglyphs import confusables
|
5
|
+
|
6
|
+
|
7
|
+
def swap_homoglyphs(
|
8
|
+
text: str,
|
9
|
+
replacement_rate: float = 0.02,
|
10
|
+
classes: list[str] | Literal["all"] | None = None,
|
11
|
+
seed: int | None = None,
|
12
|
+
rng: random.Random | None = None,
|
13
|
+
) -> str:
|
14
|
+
"""Replace characters with visually confusable homoglyphs.
|
15
|
+
|
16
|
+
Parameters
|
17
|
+
- text: Input text.
|
18
|
+
- replacement_rate: Max proportion of eligible characters to replace (default 0.02).
|
19
|
+
- classes: Restrict replacements to these Unicode script classes (default ["LATIN","GREEK","CYRILLIC"]). Use "all" to allow any.
|
20
|
+
- seed: Optional seed if `rng` not provided.
|
21
|
+
- rng: Optional RNG; overrides seed.
|
22
|
+
|
23
|
+
Notes
|
24
|
+
- Only replaces characters present in confusables.confusables_data with single-codepoint alternatives.
|
25
|
+
- Maintains determinism by shuffling candidates and sampling via the provided RNG.
|
26
|
+
"""
|
27
|
+
if rng is None:
|
28
|
+
rng = random.Random(seed)
|
29
|
+
|
30
|
+
if classes is None:
|
31
|
+
classes = ["LATIN", "GREEK", "CYRILLIC"]
|
32
|
+
|
33
|
+
target_chars = [char for char in text if char.isalnum()]
|
34
|
+
confusable_chars = [
|
35
|
+
char for char in target_chars if char in confusables.confusables_data
|
36
|
+
]
|
37
|
+
num_replacements = int(len(confusable_chars) * replacement_rate)
|
38
|
+
done = 0
|
39
|
+
rng.shuffle(confusable_chars)
|
40
|
+
for char in confusable_chars:
|
41
|
+
if done >= num_replacements:
|
42
|
+
break
|
43
|
+
options = [
|
44
|
+
o["c"] for o in confusables.confusables_data[char] if len(o["c"]) == 1
|
45
|
+
]
|
46
|
+
if classes != "all":
|
47
|
+
options = [opt for opt in options if confusables.alias(opt) in classes]
|
48
|
+
if not options:
|
49
|
+
continue
|
50
|
+
text = text.replace(char, rng.choice(options), 1)
|
51
|
+
done += 1
|
52
|
+
return text
|
53
|
+
|
54
|
+
|
55
|
+
class Mim1c(Glitchling):
|
56
|
+
"""Glitchling that swaps characters for visually similar homoglyphs."""
|
57
|
+
|
58
|
+
def __init__(
|
59
|
+
self,
|
60
|
+
*,
|
61
|
+
replacement_rate: float = 0.02,
|
62
|
+
classes: list[str] | Literal["all"] | None = None,
|
63
|
+
seed: int | None = None,
|
64
|
+
) -> None:
|
65
|
+
super().__init__(
|
66
|
+
name="Mim1c",
|
67
|
+
corruption_function=swap_homoglyphs,
|
68
|
+
scope=AttackWave.CHARACTER,
|
69
|
+
order=AttackOrder.LAST,
|
70
|
+
seed=seed,
|
71
|
+
replacement_rate=replacement_rate,
|
72
|
+
classes=classes,
|
73
|
+
)
|
74
|
+
|
75
|
+
|
76
|
+
mim1c = Mim1c()
|
77
|
+
|
78
|
+
|
79
|
+
__all__ = ["Mim1c", "mim1c"]
|
@@ -0,0 +1,128 @@
|
|
1
|
+
import re
|
2
|
+
import random
|
3
|
+
|
4
|
+
from .core import Glitchling, AttackWave
|
5
|
+
|
6
|
+
FULL_BLOCK = "█"
|
7
|
+
|
8
|
+
|
9
|
+
try:
|
10
|
+
from glitchlings._zoo_rust import redact_words as _redact_words_rust
|
11
|
+
except ImportError: # pragma: no cover - compiled extension not present
|
12
|
+
_redact_words_rust = None
|
13
|
+
|
14
|
+
|
15
|
+
def _python_redact_words(
|
16
|
+
text: str,
|
17
|
+
*,
|
18
|
+
replacement_char: str,
|
19
|
+
redaction_rate: float,
|
20
|
+
merge_adjacent: bool,
|
21
|
+
rng: random.Random,
|
22
|
+
) -> str:
|
23
|
+
"""Redact random words by replacing their characters.
|
24
|
+
|
25
|
+
Parameters
|
26
|
+
- text: Input text.
|
27
|
+
- replacement_char: The character to use for redaction (default FULL_BLOCK).
|
28
|
+
- redaction_rate: Max proportion of words to redact (default 0.05).
|
29
|
+
- merge_adjacent: If True, merges adjacent redactions across intervening non-word chars.
|
30
|
+
- seed: Seed used if `rng` not provided (default 151).
|
31
|
+
- rng: Optional RNG; overrides seed.
|
32
|
+
"""
|
33
|
+
# Preserve exact spacing and punctuation by using regex
|
34
|
+
tokens = re.split(r"(\s+)", text)
|
35
|
+
word_indices = [i for i, token in enumerate(tokens) if i % 2 == 0 and token.strip()]
|
36
|
+
if not word_indices:
|
37
|
+
raise ValueError("Cannot redact words because the input text contains no redactable words.")
|
38
|
+
num_to_redact = max(1, int(len(word_indices) * redaction_rate))
|
39
|
+
|
40
|
+
# Sample from the indices of actual words
|
41
|
+
indices_to_redact = rng.sample(word_indices, k=num_to_redact)
|
42
|
+
indices_to_redact.sort()
|
43
|
+
|
44
|
+
for i in indices_to_redact:
|
45
|
+
if i >= len(tokens):
|
46
|
+
break
|
47
|
+
|
48
|
+
word = tokens[i]
|
49
|
+
if not word or word.isspace(): # Skip empty or whitespace
|
50
|
+
continue
|
51
|
+
|
52
|
+
# Check if word has trailing punctuation
|
53
|
+
match = re.match(r"^(\W*)(.*?)(\W*)$", word)
|
54
|
+
if match:
|
55
|
+
prefix, core, suffix = match.groups()
|
56
|
+
tokens[i] = f"{prefix}{replacement_char * len(core)}{suffix}"
|
57
|
+
else:
|
58
|
+
tokens[i] = f"{replacement_char * len(word)}"
|
59
|
+
|
60
|
+
text = "".join(tokens)
|
61
|
+
|
62
|
+
if merge_adjacent:
|
63
|
+
text = re.sub(
|
64
|
+
rf"{replacement_char}\W+{replacement_char}",
|
65
|
+
lambda m: replacement_char * (len(m.group(0)) - 1),
|
66
|
+
text,
|
67
|
+
)
|
68
|
+
|
69
|
+
return text
|
70
|
+
|
71
|
+
|
72
|
+
def redact_words(
|
73
|
+
text: str,
|
74
|
+
replacement_char: str = FULL_BLOCK,
|
75
|
+
redaction_rate: float = 0.05,
|
76
|
+
merge_adjacent: bool = False,
|
77
|
+
seed: int = 151,
|
78
|
+
rng: random.Random | None = None,
|
79
|
+
) -> str:
|
80
|
+
"""Redact random words by replacing their characters."""
|
81
|
+
|
82
|
+
if rng is None:
|
83
|
+
rng = random.Random(seed)
|
84
|
+
|
85
|
+
if _redact_words_rust is not None:
|
86
|
+
return _redact_words_rust(
|
87
|
+
text,
|
88
|
+
replacement_char,
|
89
|
+
redaction_rate,
|
90
|
+
merge_adjacent,
|
91
|
+
rng,
|
92
|
+
)
|
93
|
+
|
94
|
+
return _python_redact_words(
|
95
|
+
text,
|
96
|
+
replacement_char=replacement_char,
|
97
|
+
redaction_rate=redaction_rate,
|
98
|
+
merge_adjacent=merge_adjacent,
|
99
|
+
rng=rng,
|
100
|
+
)
|
101
|
+
|
102
|
+
|
103
|
+
class Redactyl(Glitchling):
|
104
|
+
"""Glitchling that redacts words with block characters."""
|
105
|
+
|
106
|
+
def __init__(
|
107
|
+
self,
|
108
|
+
*,
|
109
|
+
replacement_char: str = FULL_BLOCK,
|
110
|
+
redaction_rate: float = 0.05,
|
111
|
+
merge_adjacent: bool = False,
|
112
|
+
seed: int = 151,
|
113
|
+
) -> None:
|
114
|
+
super().__init__(
|
115
|
+
name="Redactyl",
|
116
|
+
corruption_function=redact_words,
|
117
|
+
scope=AttackWave.WORD,
|
118
|
+
seed=seed,
|
119
|
+
replacement_char=replacement_char,
|
120
|
+
redaction_rate=redaction_rate,
|
121
|
+
merge_adjacent=merge_adjacent,
|
122
|
+
)
|
123
|
+
|
124
|
+
|
125
|
+
redactyl = Redactyl()
|
126
|
+
|
127
|
+
|
128
|
+
__all__ = ["Redactyl", "redactyl"]
|
@@ -0,0 +1,100 @@
|
|
1
|
+
import re
|
2
|
+
import random
|
3
|
+
|
4
|
+
from .core import Glitchling, AttackWave
|
5
|
+
|
6
|
+
try:
|
7
|
+
from glitchlings._zoo_rust import reduplicate_words as _reduplicate_words_rust
|
8
|
+
except ImportError: # pragma: no cover - compiled extension not present
|
9
|
+
_reduplicate_words_rust = None
|
10
|
+
|
11
|
+
|
12
|
+
def _python_reduplicate_words(
|
13
|
+
text: str,
|
14
|
+
*,
|
15
|
+
reduplication_rate: float,
|
16
|
+
rng: random.Random,
|
17
|
+
) -> str:
|
18
|
+
"""Randomly reduplicate words in the text.
|
19
|
+
|
20
|
+
Parameters
|
21
|
+
- text: Input text.
|
22
|
+
- reduplication_rate: Max proportion of words to reduplicate (default 0.05).
|
23
|
+
- seed: Optional seed if `rng` not provided.
|
24
|
+
- rng: Optional RNG; overrides seed.
|
25
|
+
|
26
|
+
Notes
|
27
|
+
- Preserves spacing and punctuation by tokenizing with separators.
|
28
|
+
- Deterministic when run with a fixed seed or via Gaggle.
|
29
|
+
"""
|
30
|
+
# Preserve exact spacing and punctuation by using regex
|
31
|
+
tokens = re.split(r"(\s+)", text) # Split but keep separators
|
32
|
+
|
33
|
+
for i in range(0, len(tokens), 2): # Every other token is a word
|
34
|
+
if i >= len(tokens):
|
35
|
+
break
|
36
|
+
|
37
|
+
word = tokens[i]
|
38
|
+
if not word or word.isspace(): # Skip empty or whitespace
|
39
|
+
continue
|
40
|
+
|
41
|
+
# Only consider actual words for reduplication
|
42
|
+
if rng.random() < reduplication_rate:
|
43
|
+
# Check if word has trailing punctuation
|
44
|
+
match = re.match(r"^(\W*)(.*?)(\W*)$", word)
|
45
|
+
if match:
|
46
|
+
prefix, core, suffix = match.groups()
|
47
|
+
# Reduplicate with a space: "word" -> "word word"
|
48
|
+
tokens[i] = f"{prefix}{core} {core}{suffix}"
|
49
|
+
else:
|
50
|
+
tokens[i] = f"{word} {word}"
|
51
|
+
return "".join(tokens)
|
52
|
+
|
53
|
+
|
54
|
+
def reduplicate_words(
|
55
|
+
text: str,
|
56
|
+
reduplication_rate: float = 0.05,
|
57
|
+
seed: int | None = None,
|
58
|
+
rng: random.Random | None = None,
|
59
|
+
) -> str:
|
60
|
+
"""Randomly reduplicate words in the text.
|
61
|
+
|
62
|
+
Falls back to the Python implementation when the optional Rust
|
63
|
+
extension is unavailable.
|
64
|
+
"""
|
65
|
+
|
66
|
+
if rng is None:
|
67
|
+
rng = random.Random(seed)
|
68
|
+
|
69
|
+
if _reduplicate_words_rust is not None:
|
70
|
+
return _reduplicate_words_rust(text, reduplication_rate, rng)
|
71
|
+
|
72
|
+
return _python_reduplicate_words(
|
73
|
+
text,
|
74
|
+
reduplication_rate=reduplication_rate,
|
75
|
+
rng=rng,
|
76
|
+
)
|
77
|
+
|
78
|
+
|
79
|
+
class Reduple(Glitchling):
|
80
|
+
"""Glitchling that repeats words to simulate stuttering speech."""
|
81
|
+
|
82
|
+
def __init__(
|
83
|
+
self,
|
84
|
+
*,
|
85
|
+
reduplication_rate: float = 0.05,
|
86
|
+
seed: int | None = None,
|
87
|
+
) -> None:
|
88
|
+
super().__init__(
|
89
|
+
name="Reduple",
|
90
|
+
corruption_function=reduplicate_words,
|
91
|
+
scope=AttackWave.WORD,
|
92
|
+
seed=seed,
|
93
|
+
reduplication_rate=reduplication_rate,
|
94
|
+
)
|
95
|
+
|
96
|
+
|
97
|
+
reduple = Reduple()
|
98
|
+
|
99
|
+
|
100
|
+
__all__ = ["Reduple", "reduple"]
|
@@ -0,0 +1,104 @@
|
|
1
|
+
import math
|
2
|
+
import random
|
3
|
+
import re
|
4
|
+
|
5
|
+
from .core import Glitchling, AttackWave
|
6
|
+
|
7
|
+
try:
|
8
|
+
from glitchlings._zoo_rust import delete_random_words as _delete_random_words_rust
|
9
|
+
except ImportError: # pragma: no cover - compiled extension not present
|
10
|
+
_delete_random_words_rust = None
|
11
|
+
|
12
|
+
|
13
|
+
def _python_delete_random_words(
|
14
|
+
text: str,
|
15
|
+
*,
|
16
|
+
max_deletion_rate: float,
|
17
|
+
rng: random.Random,
|
18
|
+
) -> str:
|
19
|
+
"""Delete random words from the input text while preserving whitespace."""
|
20
|
+
|
21
|
+
tokens = re.split(r"(\s+)", text) # Split but keep separators for later rejoin
|
22
|
+
|
23
|
+
candidate_indices: list[int] = []
|
24
|
+
for i in range(2, len(tokens), 2): # Every other token is a word, skip the first word
|
25
|
+
word = tokens[i]
|
26
|
+
if not word or word.isspace():
|
27
|
+
continue
|
28
|
+
|
29
|
+
candidate_indices.append(i)
|
30
|
+
|
31
|
+
allowed_deletions = min(
|
32
|
+
len(candidate_indices), math.floor(len(candidate_indices) * max_deletion_rate)
|
33
|
+
)
|
34
|
+
if allowed_deletions <= 0:
|
35
|
+
return text
|
36
|
+
|
37
|
+
deletions = 0
|
38
|
+
for i in candidate_indices:
|
39
|
+
if rng.random() < max_deletion_rate:
|
40
|
+
word = tokens[i]
|
41
|
+
match = re.match(r"^(\W*)(.*?)(\W*)$", word)
|
42
|
+
if match:
|
43
|
+
prefix, _, suffix = match.groups()
|
44
|
+
tokens[i] = f"{prefix.strip()}{suffix.strip()}"
|
45
|
+
else:
|
46
|
+
tokens[i] = ""
|
47
|
+
|
48
|
+
deletions += 1
|
49
|
+
if deletions >= allowed_deletions:
|
50
|
+
break
|
51
|
+
|
52
|
+
text = "".join(tokens)
|
53
|
+
text = re.sub(r"\s+([.,;:])", r"\1", text)
|
54
|
+
text = re.sub(r"\s{2,}", " ", text).strip()
|
55
|
+
|
56
|
+
return text
|
57
|
+
|
58
|
+
|
59
|
+
def delete_random_words(
|
60
|
+
text: str,
|
61
|
+
max_deletion_rate: float = 0.01,
|
62
|
+
seed: int | None = None,
|
63
|
+
rng: random.Random | None = None,
|
64
|
+
) -> str:
|
65
|
+
"""Delete random words from the input text.
|
66
|
+
|
67
|
+
Uses the optional Rust implementation when available.
|
68
|
+
"""
|
69
|
+
|
70
|
+
if rng is None:
|
71
|
+
rng = random.Random(seed)
|
72
|
+
|
73
|
+
if _delete_random_words_rust is not None:
|
74
|
+
return _delete_random_words_rust(text, max_deletion_rate, rng)
|
75
|
+
|
76
|
+
return _python_delete_random_words(
|
77
|
+
text,
|
78
|
+
max_deletion_rate=max_deletion_rate,
|
79
|
+
rng=rng,
|
80
|
+
)
|
81
|
+
|
82
|
+
|
83
|
+
class Rushmore(Glitchling):
|
84
|
+
"""Glitchling that deletes words to simulate missing information."""
|
85
|
+
|
86
|
+
def __init__(
|
87
|
+
self,
|
88
|
+
*,
|
89
|
+
max_deletion_rate: float = 0.01,
|
90
|
+
seed: int | None = None,
|
91
|
+
) -> None:
|
92
|
+
super().__init__(
|
93
|
+
name="Rushmore",
|
94
|
+
corruption_function=delete_random_words,
|
95
|
+
scope=AttackWave.WORD,
|
96
|
+
seed=seed,
|
97
|
+
max_deletion_rate=max_deletion_rate,
|
98
|
+
)
|
99
|
+
|
100
|
+
|
101
|
+
rushmore = Rushmore()
|
102
|
+
|
103
|
+
|
104
|
+
__all__ = ["Rushmore", "rushmore"]
|