Skip to content

Commit

Permalink
refactor unique methods into single dynamic method
Browse files Browse the repository at this point in the history
Signed-off-by: Bradley Reynolds <[email protected]>
  • Loading branch information
shenanigansd committed Feb 5, 2023
1 parent f684f32 commit c088b65
Showing 1 changed file with 21 additions and 80 deletions.
101 changes: 21 additions & 80 deletions src/imsosorry/uwuification.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import random
import re
from functools import partial
from typing import Callable

WORD_REPLACE = {
"small": "smol",
Expand Down Expand Up @@ -55,80 +55,15 @@
]
"""A list of emojis/emoticons to add."""

REGEX_WORD_REPLACE = re.compile(r"(?<!w)[lr](?!w)")
"""A wegex that to detect certain characters to change to "w"s."""

REGEX_PUNCTUATION = re.compile(r"[.!?\r\n\t]")
"""A regex to detect certain punctuation characters to emotify /(^•ω•^)"""
def _randomly(strength: float, fn: Callable[[str], str]):
def replace(match: re.Match[str]) -> str:
if random.random() < strength:
return fn(match[0])
else:
return match[0]

REGEX_TILDE = re.compile(r"(?![^ ])(?<!\B)")
"""A regex to find places to add tildes (~) to."""

REGEX_STUTTER = re.compile(r"(\s)([a-zA-Z])")
"""A regex to find words to stutter."""
SUBSTITUTE_STUTTER = r"\g<1>\g<2>-\g<2>"
"""A regex to add st-stuttering to strings."""

REGEX_NYA = re.compile(r"n([aeou][^aeiou])")
"""A regex to detect words with an n before a vowel to nyaify."""
SUBSTITUTE_NYA = r"ny\1"
"""A regex to to nyaify words."""


def word_replace(text: str) -> str:
"""Replaces words that are keys in the word replacement hash to the values specified."""
for word, replacement in WORD_REPLACE.items():
text = text.replace(word, replacement)
return text


def char_replace(text: str) -> str:
"""Replace certain characters with 'w'."""
return REGEX_WORD_REPLACE.sub("w", text)


def stutter_replace(match: re.Match, strength: float = 0.0) -> str:
"""Replaces a single character with a stuttered character."""
match_string = match.group()
if random.random() < strength:
return f"{match_string}-{match_string[-1]}" # Stutter the last character
return match_string


def stutter(text: str, strength: float) -> str:
"""Adds stuttering to a string."""
return REGEX_STUTTER.sub(partial(stutter_replace, strength=strength), text, 0)


def nyaify(text: str) -> str:
"""Nyaifies a string by adding a 'y' between an 'n' and a vowel."""
return REGEX_NYA.sub(SUBSTITUTE_NYA, text, 0)


def emoji_replace(match: re.Match, strength: float = 0.0) -> str:
"""Replaces a punctuation character with an emoticon."""
match_string = match.group()
if random.random() < strength:
return f" {random.choice(EMOJIS)} "
return match_string


def emoji(text: str, strength: float) -> str:
"""Replaces some punctuation with emoticons."""
return REGEX_PUNCTUATION.sub(partial(emoji_replace, strength=strength), text, 0)


def tildes(match: re.Match, strength: float = 0.0):
"""Adds some tildes to spaces."""
match_string = match.group()
if random.random() < strength:
return "~"
return match_string


def tildify(text: str, strength: float) -> str:
"""Adds some tildes to spaces."""
return REGEX_TILDE.sub(partial(tildes, strength=strength), text, 0)
return replace


def uwuify(
Expand All @@ -138,12 +73,18 @@ def uwuify(
emoji_strength: float = 0.1,
tilde_strength: float = 0.1,
) -> str:
"""Takes a string and returns an uwuified version of it."""
text = text.lower()
text = word_replace(text)
text = nyaify(text)
text = char_replace(text)
text = stutter(text, stutter_strength)
text = emoji(text, emoji_strength)
text = tildify(text, tilde_strength)

for word, replacement in WORD_REPLACE.items():
text = text.replace(word, replacement)

for pattern, replacer in [
(r"n([aeou][^aeiou])", r"ny\1"),
(r"(?<!w)[lr](?!w)", "w"),
(r"(\s)([a-zA-Z])", _randomly(stutter_strength, lambda s: f"{s}-{s[-1]}")),
(r"[.!?\r\n\t]", _randomly(emoji_strength, lambda _: f" {random.choice(EMOJIS)} ")),
(r"(?![^ ])(?<!\B)", _randomly(tilde_strength, lambda _: "~")),
]:
text = re.sub(pattern, replacer, text)

return text

0 comments on commit c088b65

Please sign in to comment.