glitchlings 0.1.0__py3-none-any.whl → 0.1.2__py3-none-any.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.
@@ -1,244 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: glitchlings
3
- Version: 0.1.0
4
- Summary: Monsters for your language games.
5
- Project-URL: Homepage, https://github.com/osoleve/glitchlings
6
- Project-URL: Repository, https://github.com/osoleve/glitchlings.git
7
- Project-URL: Issues, https://github.com/osoleve/glitchlings/issues
8
- Project-URL: Changelog, https://github.com/osoleve/glitchlings/releases
9
- Author: osoleve
10
- License: MIT License
11
-
12
- Copyright (c) 2025 osoleve
13
-
14
- Permission is hereby granted, free of charge, to any person obtaining a copy
15
- of this software and associated documentation files (the "Software"), to deal
16
- in the Software without restriction, including without limitation the rights
17
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
- copies of the Software, and to permit persons to whom the Software is
19
- furnished to do so, subject to the following conditions:
20
-
21
- The above copyright notice and this permission notice shall be included in all
22
- copies or substantial portions of the Software.
23
-
24
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
- SOFTWARE.
31
- License-File: LICENSE
32
- Keywords: adversarial,data,evaluation,glitch,nlp,text
33
- Classifier: Development Status :: 3 - Alpha
34
- Classifier: Intended Audience :: Developers
35
- Classifier: License :: OSI Approved :: MIT License
36
- Classifier: Operating System :: OS Independent
37
- Classifier: Programming Language :: Python
38
- Classifier: Programming Language :: Python :: 3
39
- Classifier: Programming Language :: Python :: 3.12
40
- Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
41
- Classifier: Topic :: Software Development :: Testing
42
- Requires-Python: >=3.12
43
- Requires-Dist: confusable-homoglyphs>=3.3.1
44
- Requires-Dist: datasets>=4.0.0
45
- Requires-Dist: jellyfish>=1.2.0
46
- Requires-Dist: nltk>=3.9.1
47
- Provides-Extra: dev
48
- Requires-Dist: pytest>=8.0.0; extra == 'dev'
49
- Provides-Extra: prime
50
- Requires-Dist: verifiers>=0.1.3.post0; extra == 'prime'
51
- Description-Content-Type: text/markdown
52
-
53
- #
54
-
55
- ```plaintext
56
- .─') _ .─') _
57
- ( OO) ) ( OO ) )
58
- ░██████ ░██ ░██ ░██ ░██ ░██ ░██
59
- ░██ ░██ ░██ ░██ ░██ ░██
60
- ░██ ░██ ░██░████████ ░███████ ░████████ ░██ ░██░████████ ░████████ ░███████
61
- ░██ █████ ░██ ░██ ░██ ░██('─.░██ ░██ ░██ ░██ ░██░██ ░██ ░██.─')░██ ░██
62
- ░██ ██ ░██ ░██ ░██ ░██( OO ) ╱░██ ░██ ░██ ░██░██ ░██ ░██(OO)░██ ░███████
63
- ░██ ░███ ░██ ░██ ░██ ░██ ░██ ░██ ░██ ░██ ░██░██ ░██ ░██ o░███ ░██
64
- ░█████░█ ░██ ░██ ░████ ░███████ ░██ ░██ ░██ ░██░██ ░██ ░█████░██ ░███████
65
- ░██
66
- ░███████
67
-
68
- Every language game breeds monsters.
69
- ```
70
-
71
- `Glitchlings` are **utilities for corrupting the text inputs to your language models in deterministic, _linguistically principled_** ways.
72
- Each embodies a different way that documents can be compromised in the wild.
73
-
74
- If reinforcement learning environments are games, then `Glitchling`s are enemies to breathe new life into old challenges.
75
-
76
- They do this by breaking surface patterns in the input while keeping the target output intact.
77
-
78
- Some `Glitchling`s are petty nuisances. Some `Glitchling`s are eldritch horrors.
79
- Together, they create truly nightmarish scenarios for your language models.
80
-
81
- After all, what good is general intelligence if it can't handle a little chaos?
82
-
83
- -_The Curator_
84
-
85
- ## Quickstart
86
-
87
- ```python
88
- from glitchlings import summon, SAMPLE_TEXT
89
-
90
- gaggle = summon(["reduple", "mim1c", "typogre", "rushmore"])
91
- gaggle(SAMPLE_TEXT)
92
- ```
93
-
94
- > Onҽ m‎ھ‎rning, wһen Gregor Samƽa woke from trouble𝐝 𝑑reams, he found himself transformed in his bed into a horrible vermin‎٠‎ He l lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightlh domed and divided by arches ino stiff sections. The bedding was adly able to cover it and and seemed ready to slide off any moment. His many legxs, pitifully thin compared with the size of the the rest of him, waved about helplessly ashe looked looked.
95
-
96
- ## Motivation
97
-
98
- If your model performs well on a particular task, but not when `Glitchling`s are present, it's a sign that it hasn't actually generalized to the problem.
99
-
100
- Conversely, training a model to perform well in the presence of the types of perturbations introduced by `Glitchling`s should help it generalize better.
101
-
102
- ## Your First Battle
103
-
104
- Summon your chosen `Glitchling` (_or a few, if ya nasty_) and call it on your text or slot it into `Dataset.map(...)`, supplying a seed if desired.
105
- Some `Glitchling`s may have additional keyword arguments but they will always be optional with what I decide are "reasonable defaults".
106
- Seed defaults to 151, obviously.
107
-
108
- Calling a `Glitchling` on a `str` transparently calls `.corrupt(str, ...) -> str`.
109
- This means that as long as your glitchlings get along logically, they play nicely with one another.
110
-
111
- When summoned as a `Gaggle`, the `Glitchling`s will automatically order themselves into attack waves, based on the scope of the change they make:
112
-
113
- 1. Document
114
- 2. Paragraph
115
- 3. Sentence
116
- 4. Word
117
- 5. Character
118
-
119
- They're horrible little gremlins, but they're not _unreasonable_.
120
-
121
- ## Starter 'lings
122
-
123
- For maintainability reasons, all `Glitchling` have consented to be given nicknames once they're in your care. See the [Monster Manual](MONSTER_MANUAL.md) for a complete bestiary.
124
-
125
- ### Typogre
126
-
127
- _What a nice word, would be a shame if something happened to it._
128
-
129
- > _**Fatfinger.**_ Typogre introduces character-level errors (duplicating, dropping, adding, or swapping) based on the layout of a (currently QWERTY) keyboard.
130
- >
131
- > Args
132
- >
133
- > - `max_change_rate (float)`: The maximum number of edits to make as a percentage of the length (default: 0.02, 2%).
134
- > - `preserve_first_last (bool)`: Avoid editing the first and last character of a word (default: False).
135
- > - `keyboard (str)`: Keyboard layout key-neighbor map to use (default: "CURATOR_QWERTY").
136
- > - `seed (int)`: The random seed for reproducibility (default: 151).
137
-
138
- ### Mim1c
139
-
140
- _Wait, was that...?_
141
-
142
- > _**Confusion.**_ Mim1c replaces non-space characters with Unicode Confusables, characters that are distinct but would not usually confuse a human reader.
143
- >
144
- > Args
145
- >
146
- > - `replacement_rate (float)`: The maximum proportion of characters to replace (default: 0.02, 2%).
147
- > - `classes (list[str] | "all")`: Restrict replacements to these Unicode script classes (default: ["LATIN", "GREEK", "CYRILLIC"]).
148
- > - `seed (int)`: The random seed for reproducibility (default: 151).
149
-
150
- ### Scannequin
151
-
152
- _How can a computer need reading glasses?_
153
-
154
- > _**OCR Artifacts.**_ Scannequin mimics optical character recognition errors by swapping visually similar character sequences (like rn↔m, cl↔d, O↔0, l/I/1).
155
- >
156
- > Args
157
- >
158
- > - `error_rate (float)`: The maximum proportion of eligible confusion spans to replace (default: 0.02, 2%).
159
- > - `seed (int)`: The random seed for reproducibility (default: 151).
160
-
161
- ### Jargoyle
162
-
163
- _Uh oh. The worst person you know just bought a thesaurus._
164
-
165
- > _**Sesquipedalianism.**_ Jargoyle, the insufferable `Glitchling`, replaces nouns with synonyms at random, without regard for connotational or denotational differences.
166
- >
167
- > Args
168
- >
169
- > - `replacement_rate (float)`: The maximum proportion of words to replace (default: 0.1, 10%).
170
- > - `part_of_speech`: The WordNet part of speech to target (default: nouns). Accepts `wn.NOUN`, `wn.VERB`, `wn.ADJ`, or `wn.ADV`.
171
- > - `seed (int)`: The random seed for reproducibility (default: 151).
172
-
173
- ### Reduple
174
-
175
- _Did you say that or did I?_
176
-
177
- > _**Broken Record.**_ Reduple stutters through text by randomly reduplicating words. Like a nervous speaker, it creates natural repetitions that test a model's ability to handle redundancy without losing the thread.
178
- >
179
- > Args
180
- >
181
- > - `reduplication_rate (float)`: The maximum proportion of words to reduplicate (default: 0.05, 5%).
182
- > - `seed (int)`: The random seed for reproducibility (default: 151).
183
-
184
- ### Rushmore
185
-
186
- _I accidentally an entire word._
187
-
188
- > _**Hasty Omission.**_ The evil (?) twin of `reduple`, Rushmore moves with such frantic speed that it causes words to simply vanish from existence as it passes.
189
- >
190
- > Args
191
- >
192
- > - `max_deletion_rate (float)`: The maximum proportion of words to delete (default: 0.01, 1%).
193
- > - `seed (int)`: The random seed for reproducibility (default: 151).
194
-
195
- ### Redactyl
196
-
197
- _Oops, that was my black highlighter._
198
-
199
- > _**FOIA Reply.**_ Redactyl obscures random words in your document like an NSA analyst with a bad sense of humor.
200
- >
201
- > ### Args
202
- >
203
- > - `replacement_char (str)`: The character to use for redaction (default: █).
204
- > - `redaction_rate (float)`: The maximum proportion of words to redact (default: 0.05, 5%).
205
- > - `merge_adjacent (bool)`: Whether to redact the space between adjacent redacted words (default: False).
206
- > - `seed (int)`: The random seed for reproducibility (default: 151).
207
-
208
- ## Field Report: Uncontained Specimens
209
-
210
- ### _Containment procedures pending_
211
-
212
- - `ekkokin` substitutes words with homophones (phonetic equivalents).
213
- - `nylingual` backtranslates portions of text.
214
- - `glothopper` introduces code-switching effects, blending languages or dialects.
215
- - `palimpsest` rewrites, but leaves accidental traces of the past.
216
- - `vesuvius` is an apocryphal `Glitchling` with ties to _[Nosy, aren't we? -The Curator]_
217
-
218
- ## Apocrypha
219
-
220
- Cave paintings and oral tradition contain many depictions of strange, otherworldly `Glitchling`s.
221
- These _Apocryphal `Glitchling`_ are said to possess unique abilities or behaviors.
222
- If you encounter one of these elusive beings, please document your findings and share them with _The Curator_.
223
-
224
- ### Reproducible Corruption
225
-
226
- Every `Glitchling` owns its own independent `random.Random` instance. That means:
227
-
228
- - No `random.seed(...)` calls touch Python's global RNG.
229
- - Supplying a `seed` when you construct a `Glitchling` (or when you `summon(...)`) makes its behavior reproducible.
230
- - Re-running a `Gaggle` with the same master seed and the same input text (_and same external data!_) yields identical corruption output.
231
- - Corruption functions are written to accept an `rng` parameter internally so that all randomness is centralized and testable.
232
-
233
- #### Caveats
234
-
235
- - If you mutate a glitchling's parameters after you've used it (e.g. `typogre.set_param(...)`) the outputs may not be the same as before the change. So don't do that.
236
-
237
- #### At Wits' End?
238
-
239
- If you're trying to add a new glitchling and can't seem to make it deterministic, here are some places to look for determinism-breaking code:
240
-
241
- 1. Search for any direct calls to `random.choice`, `random.shuffle`, or `set(...)` ordering without going through the provided `rng`.
242
- 2. Ensure you sort collections before shuffling or sampling.
243
- 3. Make sure indices are chosen from a stable reference (e.g., original text) when applying length‑changing edits.
244
- 4. Make sure there are enough sort keys to maintain stability.
@@ -1,28 +0,0 @@
1
- ./.gitignore,sha256=dGf2ypTSG-e9eVdXEkBKda_1JikOGVtVJO983675dt8,133
2
- ./LICENSE,sha256=LXni4cDLub1De97hk0PQj8-RAZnlvGvOo6ILVur9MOI,1085
3
- ./MONSTER_MANUAL.md,sha256=vdoVnUl-Zdl3gH6E51fUGlSnhxdxxIbIXVNorReJAng,9344
4
- ./PKG-INFO,sha256=9sx6KkIZ64BksLaif6yOt3ga6fLpmtpGVP9EiKs6jnc,12383
5
- ./README.md,sha256=ivQ4MCqX-y-GOUKe3AnelkSvuxtVaE8eIcealdnxq3k,10089
6
- ./RELEASE.md,sha256=U7spXkNrGboPzLsK7uyqAwjm4gaaPLyryrAV7RwFcBE,1552
7
- ./__init__.py,sha256=dOvJSFqrFUQvYNUHne5CmSUUwzME8rdyL-lXGNWKoL0,2365
8
- ./main.py,sha256=HZveNbsuHiHJFGhB31I_u4EK2aI7KOCevcI2ZP9u5Lw,89
9
- ./pyproject.toml,sha256=masaVBWpUqKG_4FXupQRS7k-rg-ws_yDhe81ZgO2X6s,1645
10
- ./.github/workflows/publish.yml,sha256=0N02QrLlOaj3y9PGfl3Ns9wVgP-EaCizmcospjGGMco,1118
11
- ./.github/workflows/testpypi.yml,sha256=HdC0QPPG0tEk9VCUk0_7HIrCezHfCDO623yFKVeAcc8,945
12
- ./dlc/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
13
- ./dlc/prime.py,sha256=FunhqcEXGUMm49WadYtWbH7MrMcYuqcmk-LPEZEEZeI,1382
14
- ./util/__init__.py,sha256=C7VPB225Uro-9MzpMpofIyTH86gYUpiYbGDxk_Jtvaw,2190
15
- ./zoo/__init__.py,sha256=vOfvSfvAZegCrJ_xEyQBfDQLVXLz6OXFepA1cmT9X58,1218
16
- ./zoo/core.py,sha256=jBigckZRNOKhg4Bnki0iTfgp8z6yI43K5SkqMBcChv4,4776
17
- ./zoo/jargoyle.py,sha256=CbhprejTP54KEaI9tWJqG-mKxCOcqKgtJvuuLb_zCZM,2972
18
- ./zoo/mim1c.py,sha256=i0K2NjeM_pLVXOmTTUPelSIMhyTRwT5zMwXJ5XFLip4,2118
19
- ./zoo/redactyl.py,sha256=V9-4XNLKzAQhqFqZ8pE6zFJatVKYdxOvBS3E8JVtoqo,2211
20
- ./zoo/reduple.py,sha256=ZWa08nE9S7eTAzl0bQk2q5tsnkttfo2G_cjKJZpaB-A,1675
21
- ./zoo/rushmore.py,sha256=u_tbvFVKCJyYDtbzraA519_p5t0I0RoKmHF9mvnnaDM,1580
22
- ./zoo/scannequin.py,sha256=-dcykPGWFrnF5nDyHQLfW9Olj-uxV1e-6DVDXgdN8rg,3765
23
- ./zoo/typogre.py,sha256=kw_lvD5a6JjYU9mwrOyicmQz3HuqxtvUVAdrGf32WrA,7872
24
- glitchlings-0.1.0.dist-info/METADATA,sha256=9sx6KkIZ64BksLaif6yOt3ga6fLpmtpGVP9EiKs6jnc,12383
25
- glitchlings-0.1.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
26
- glitchlings-0.1.0.dist-info/entry_points.txt,sha256=kGOwuAsjFDLtztLisaXtOouq9wFVMOJg5FzaAkg-Hto,54
27
- glitchlings-0.1.0.dist-info/licenses/LICENSE,sha256=LXni4cDLub1De97hk0PQj8-RAZnlvGvOo6ILVur9MOI,1085
28
- glitchlings-0.1.0.dist-info/RECORD,,
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2025 osoleve
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
main.py DELETED
@@ -1,6 +0,0 @@
1
- def main():
2
- print("Hello from glitchlings!")
3
-
4
-
5
- if __name__ == "__main__":
6
- main()
pyproject.toml DELETED
@@ -1,74 +0,0 @@
1
- [project]
2
- name = "glitchlings"
3
- version = "0.1.0"
4
- description = "Monsters for your language games."
5
- readme = "README.md"
6
- requires-python = ">=3.12"
7
- dependencies = [
8
- "confusable-homoglyphs>=3.3.1",
9
- "datasets>=4.0.0",
10
- "jellyfish>=1.2.0",
11
- "nltk>=3.9.1",
12
- ]
13
-
14
- authors = [
15
- { name = "osoleve" }
16
- ]
17
- keywords = ["nlp", "text", "adversarial", "data", "evaluation", "glitch"]
18
- classifiers = [
19
- "Development Status :: 3 - Alpha",
20
- "Intended Audience :: Developers",
21
- "License :: OSI Approved :: MIT License",
22
- "Programming Language :: Python",
23
- "Programming Language :: Python :: 3",
24
- "Programming Language :: Python :: 3.12",
25
- "Operating System :: OS Independent",
26
- "Topic :: Scientific/Engineering :: Artificial Intelligence",
27
- "Topic :: Software Development :: Testing",
28
- ]
29
-
30
- [project.license]
31
- file = "LICENSE"
32
-
33
- [project.urls]
34
- Homepage = "https://github.com/osoleve/glitchlings"
35
- Repository = "https://github.com/osoleve/glitchlings.git"
36
- Issues = "https://github.com/osoleve/glitchlings/issues"
37
- Changelog = "https://github.com/osoleve/glitchlings/releases"
38
-
39
- [project.scripts]
40
- glitchlings = "glitchlings.main:main"
41
-
42
- [project.optional-dependencies]
43
- prime = [
44
- "verifiers>=0.1.3.post0",
45
- ]
46
- dev = [
47
- "pytest>=8.0.0",
48
- ]
49
-
50
- [build-system]
51
- requires = ["hatchling>=1.18"]
52
- build-backend = "hatchling.build"
53
-
54
- [tool.hatch.build]
55
- exclude = [
56
- "**/__pycache__/**",
57
- ".git/**",
58
- ".venv/**",
59
- ]
60
-
61
- [tool.hatch.build.targets.sdist]
62
- exclude = [
63
- "**/__pycache__/**",
64
- ".git/**",
65
- ".venv/**",
66
- ]
67
-
68
- [tool.hatch.build.targets.wheel]
69
- packages = ["."]
70
- exclude = [
71
- "tests/**",
72
- "tests",
73
- "**/__pycache__/**",
74
- ]
util/__init__.py DELETED
@@ -1,73 +0,0 @@
1
- import difflib
2
-
3
- SAMPLE_TEXT = "One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin. He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided by arches into stiff sections. The bedding was hardly able to cover it and seemed ready to slide off any moment. His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked."
4
-
5
-
6
- def string_diffs(a: str, b: str):
7
- """
8
- Compare two strings using SequenceMatcher and return
9
- grouped adjacent opcodes (excluding 'equal' tags).
10
-
11
- Each element is a tuple: (tag, a_text, b_text).
12
- """
13
- sm = difflib.SequenceMatcher(None, a, b)
14
- ops = []
15
- buffer = []
16
-
17
- for tag, i1, i2, j1, j2 in sm.get_opcodes():
18
- if tag == "equal":
19
- # flush any buffered operations before skipping
20
- if buffer:
21
- ops.append(buffer)
22
- buffer = []
23
- continue
24
-
25
- # append operation to buffer
26
- buffer.append((tag, a[i1:i2], b[j1:j2]))
27
-
28
- # flush trailing buffer
29
- if buffer:
30
- ops.append(buffer)
31
-
32
- return ops
33
-
34
-
35
- _KEYNEIGHBORS = {
36
- "CURATOR_QWERTY": {
37
- "a": [*"qwsz"],
38
- "b": [*"vghn "],
39
- "c": [*"xdfv "],
40
- "d": [*"serfcx"],
41
- "e": [*"wsdrf34"],
42
- "f": [*"drtgvc"],
43
- "g": [*"ftyhbv"],
44
- "h": [*"gyujnb"],
45
- "i": [*"ujko89"],
46
- "j": [*"huikmn"],
47
- "k": [*"jilom,"],
48
- "l": [*"kop;.,"],
49
- "m": [*"njk, "],
50
- "n": [*"bhjm "],
51
- "o": [*"iklp90"],
52
- "p": [*"o0-[;l"],
53
- "q": [*"was 12"],
54
- "r": [*"edft45"],
55
- "s": [*"awedxz"],
56
- "t": [*"r56ygf"],
57
- "u": [*"y78ijh"],
58
- "v": [*"cfgb "],
59
- "w": [*"q23esa"],
60
- "x": [*"zsdc "],
61
- "y": [*"t67uhg"],
62
- "z": [*"asx"],
63
- }
64
- }
65
-
66
-
67
- class KeyNeighbors:
68
- def __init__(self):
69
- for layout_name, layout in _KEYNEIGHBORS.items():
70
- setattr(self, layout_name, layout)
71
-
72
-
73
- KEYNEIGHBORS = KeyNeighbors()
zoo/jargoyle.py DELETED
@@ -1,89 +0,0 @@
1
- import random
2
- from typing import Literal, Any, cast
3
- import nltk
4
- import re
5
- from nltk.corpus import wordnet as wn
6
- from .core import Glitchling, AttackWave
7
-
8
- nltk.download("wordnet", quiet=True)
9
-
10
-
11
- def substitute_random_synonyms(
12
- text: str,
13
- replacement_rate: float = 0.1,
14
- part_of_speech: Literal["n", "v", "a", "r"] = wn.NOUN,
15
- seed: int | None = None,
16
- rng: random.Random | None = None,
17
- ) -> str:
18
- """Replace words with random WordNet synonyms.
19
-
20
- Parameters
21
- - text: Input text.
22
- - replacement_rate: Max proportion of candidate words to replace (default 0.1).
23
- - part_of_speech: WordNet POS to target. One of wn.NOUN (default), wn.VERB, wn.ADJ, wn.ADV.
24
- - rng: Optional RNG instance used for deterministic sampling.
25
- - seed: Optional seed if `rng` not provided.
26
-
27
- Determinism
28
- - Candidates collected in left-to-right order; no set() reordering.
29
- - Replacement positions chosen via rng.sample.
30
- - Synonyms sorted before rng.choice to fix ordering.
31
- - Only first synset is used for stability.
32
- """
33
- if rng is None and seed is not None:
34
- rng = random.Random(seed)
35
- elif rng is None:
36
- rng = random.Random()
37
-
38
- # Split but keep whitespace separators so we can rebuild easily
39
- tokens = re.split(r"(\s+)", text)
40
-
41
- # Collect indices of candidate tokens (even positions 0,2,.. are words given our split design)
42
- candidate_indices: list[int] = []
43
- for idx, tok in enumerate(tokens):
44
- if idx % 2 == 0 and tok and not tok.isspace():
45
- if wn.synsets(tok, pos=part_of_speech):
46
- candidate_indices.append(idx)
47
-
48
- if not candidate_indices:
49
- return text
50
-
51
- max_replacements = int(len(candidate_indices) * replacement_rate)
52
- if max_replacements <= 0:
53
- return text
54
-
55
- # Choose which positions to replace deterministically via rng.sample
56
- replace_positions = rng.sample(candidate_indices, k=max_replacements)
57
- # Process in ascending order to avoid affecting later indices
58
- replace_positions.sort()
59
-
60
- for pos in replace_positions:
61
- word = tokens[pos]
62
- synsets = wn.synsets(word, pos=part_of_speech)
63
- if not synsets:
64
- continue
65
- synset0: Any = synsets[0]
66
- lemmas_list = [lemma.name() for lemma in cast(Any, synset0).lemmas()]
67
- if not lemmas_list:
68
- continue
69
- # Normalize & dedupe deterministically
70
- synonyms = sorted(
71
- {
72
- lemma_str.replace("_", " ")
73
- for lemma_str in lemmas_list
74
- if lemma_str.lower() != word.lower()
75
- }
76
- )
77
- if not synonyms:
78
- continue
79
- replacement = rng.choice(synonyms)
80
- tokens[pos] = replacement
81
-
82
- return "".join(tokens)
83
-
84
-
85
- jargoyle = Glitchling(
86
- name="Jargoyle",
87
- corruption_function=substitute_random_synonyms,
88
- scope=AttackWave.WORD,
89
- )
File without changes