LeetTransformer :one::three::three::seven::robot:
Overview
Word camouflage is currently used to evade content moderation in Social Media. Therefore, this tool aims to counter new misinformation that emerges in social media platforms by providing a mechanism for simulating and generating leetspeak/word camouflaging data.
pyleetspeak2 provides the LeetSpeaker word camouflaging method.
LeetSpeaker: This module apply the canonical 'leetspeak' method of producing visually similar character strings by replacing alphabet characters with special symbols or numbers. There's many different ways you can use leet speak. Ranging from basic vowel substitutions to really advanced combinations of various punctuation marks and glyphs. Different leetspeak levels are included.
Installation
pip install pyleetspeak2
Word camouflaging
LeetSpeaker
Canonical leetspeak in which standard letters are often replaced by numerals or special characters that resemble the letters in appearance
Parameters
You can see an example of use in a Heroku App:
The only required argument that the user has to provide is the text_in argument which represent the casual text to transform to leetspeak. Nonetheless, there are other optional arguments that control the behaviour of the transformation:`
change_prbdetermines the probability of a transformation to take place (i.e, if it is equal 1 all the possible transformation will be applied).change_frqis affects how frequently a transformation will occur (i.e, if it is equal 1 all the letters of this transformation type will be changed).modecontrols the level of leetspeak transformation. Currently onlybasicmode is available. We are working on more modes. Stay tuned.seedcontrols the reproducibility of the results. By default no seed is applied.verbosecontrols the verbosity of the proccess.get_all_combsto obtain all the possible leetspeak versions of a casual textuniform_changedetermines if the same substitution character should be used in all the positions where the casual text will be modified.
Minor concerns about the package behaviour: accents are deleted before the substitutions are applied. This is important for languages like Spanish, where the word "melocotón" is preprocessed as "melocoton" and finally transformed to leetspeak.
Modes
There are several modes available:
basicintermediateadvancedcovid_basiccovid_intermediate
Basic Use
Let's see a simple working example:
from pyleetspeak2 import LeetSpeaker
text_in = "I speak leetspeak"
leeter = LeetSpeaker(
change_prb=0.8, change_frq=0.6, mode="basic", seed=None, verbose=False
)
leet_result = leeter.text2leet(text_in)
print(leet_result)
For the sake of reproducibility you can set a random seed:
from pyleetspeak2 import LeetSpeaker
leeter = LeetSpeaker(
change_prb=0.8,
change_frq=0.5,
mode="basic",
seed=42, # for reproducibility purposes
verbose=False,
)
leet_result = leeter.text2leet(text_in)
print(leet_result)
# "1 sp34k leetsp3ak"
Define your own changes
pyleetspeak2 is prepared to apply substitutions defined by the user. It is essential to highlight that these new user-defined changes have to follow two possible formats, dictionary or List of tuples. Here we show a toy example to add two new target characters from the original text to be replaced by two and one different characters, respectively:
-
Dictionary type:
{"target_chr_1": ["sub_chr_1", "sub_chr_1"], "target_chr_2": ["sub_chr_1"]}
-
List[Tuple] type:
[("target_chr_1", ["sub_chr_1", "sub_chr_1"]), (("target_chr_2", ["sub_chr_1"])]
You can add new user-defined substitutions:
from pyleetspeak2 import LeetSpeaker
text_in = "New changes Leetspeak"
letter = LeetSpeaker(
change_prb=1,
change_frq=0.8,
mode="basic",
seed=21,
verbose=False,
get_all_combs=False,
user_changes=[("a", "#"), ("s", "$")], # user-defined changes
)
print(letter.text2leet(text_in))
# N3w ch@ng3$ L33t$pe4k
Moreover, you can use only the user-defined substitutions:
from pyleetspeak2 import LeetSpeaker
text_in = "Only user changes: Leetspeak"
letter = LeetSpeaker(
change_prb=1,
change_frq=0.8,
mode=None, # None pre-defined changes will be applied
seed=21,
verbose=False,
get_all_combs=False,
user_changes = [("a", "#"), ("s", "$")], # user-defined changes
)
print(letter.text2leet(text_in))
# Only u$er ch#nge$: Leet$pe#k
Uniform substitutions
Usually, the same substitution character is used in all the matches for a specific substitution type. In other words, the same target character is usually replaced by the same substitution character. In order to reproduce this situation, pyleetspeak2 includes the uniform_change parameter that determines if all the matches of a target character are jointly or independently substituted. In the following example notice how the target character "e" is always replaced by "€" when uniform_changes is se to True.
from pyleetspeak2 import LeetSpeaker
text_in = "Leetspeak"
leeter = LeetSpeaker(
change_prb=1, # All subs type will occur
change_frq=1, # All matches of target chr will be changed
mode="basic",
seed=41,
user_changes=[
("e", ["3", "%", "€", "£"])
], # Add diferent subs characters for target chr "e"
uniform_change=True, # Use the same substitution chr for each target chr
)
print(leeter.text2leet(text_in))
# L€€tsp€4k
Get all changes
You can also obtain all the possible versions of a leetspeak text using the get_all_combs parameter like this:
from pyleetspeak2 import LeetSpeaker
text_in = "leetspeak"
leeter = LeetSpeaker(
mode="basic",
get_all_combs=True,
user_changes = [("e", "€"), ("s", "$")], # user-defined changes
)
leet_result = leeter.text2leet(text_in)
print(len(leet_result))
assert len(leet_result) == 162 # all possible combinations
leet_result[20]
# 162
# 'le3t$p34k'
If you are only interested in the combinations that apply the same substitution character for each target target, you can also set uniform_change to True.
from pyleetspeak2 import LeetSpeaker
text_in = "leetspeak"
leeter = LeetSpeaker(
mode="basic",
get_all_combs=True,
user_changes = [("e", "€"), ("s", "$")], # user-defined changes
uniform_change = True
)
leet_result = leeter.text2leet(text_in)
print(len(leet_result))
assert len(leet_result) == 90 # all possible combinations
leet_result[60]
# 90
# 'le3t$peak'
Metadata
Release files for pyleetspeak2 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pyleetspeak2-0.4.0.tar.gz | 19.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pyleetspeak2-0.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 37.2 kB
Release files / pyleetspeak2-0.4.0.tar.gz
| Download URL | pyleetspeak2-0.4.0.tar.gz |
|---|---|
| Size | 19.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
db34b32ab74c8c0f8007933e43275e130a8b13271ee98f00b447bc7773854451
|
|
BLAKE2b-256 checksum How to use checksums |
9467c1d64d0dd2a93c0e8d7306cb1bdaf74cc5f8d95bb4a21def4f4b622cb467
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Release files / pyleetspeak2-0.4.0-py3-none-any.whl
| Download URL | pyleetspeak2-0.4.0-py3-none-any.whl |
|---|---|
| Size | 17.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e337ea435877a2293afe6707dd91f9b1b5881fd638581580191f1821c4c052bd
|
|
BLAKE2b-256 checksum How to use checksums |
f37aebf8191eb69fdf10091f13f7ea062d5c318fe56da2bc2c71a3e18bb2a0ac
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|