2022-11-15 09:19:08 +00:00
|
|
|
import re
|
|
|
|
|
|
|
|
from owoify.structures.word import Word
|
2022-08-27 04:48:44 +00:00
|
|
|
from owoify.utility.interleave_arrays import interleave_arrays
|
|
|
|
from owoify.utility.presets import *
|
|
|
|
|
|
|
|
import files.helpers.regex as help_re
|
|
|
|
import files.helpers.sanitize as sanitize
|
|
|
|
|
|
|
|
# Includes, excerpts, and modifies some functions from:
|
|
|
|
# https://github.com/deadshot465/owoify-py @ owoify/owoify.py
|
|
|
|
|
|
|
|
OWO_WORD_REGEX = re.compile(r'[^\s]+')
|
|
|
|
OWO_SPACE_REGEX = re.compile(r'\s+')
|
|
|
|
|
|
|
|
OWO_EXCLUDE_PATTERNS = [
|
|
|
|
re.compile(r'\]\('), # links []() and images ![]()
|
|
|
|
# NB: May not be effective when URL part contains literal spaces vs %20
|
|
|
|
# Also relies on owoify replacements currently not affecting symbols.
|
|
|
|
sanitize.url_re, # bare links
|
|
|
|
re.compile(r':[!#@a-z0-9_\-]+:', flags=re.I|re.A), # emoji
|
|
|
|
help_re.mention_regex, # mentions
|
2023-03-10 01:45:58 +00:00
|
|
|
help_re.group_mention_regex, #ping group mentions
|
2022-08-27 04:48:44 +00:00
|
|
|
help_re.poll_regex, # polls
|
|
|
|
help_re.choice_regex,
|
|
|
|
help_re.command_regex, # markup commands
|
2022-11-07 07:22:55 +00:00
|
|
|
re.compile(r'\bthe\b', flags=re.I|re.A), # exclude: 'the' ↦ 'teh'
|
2022-08-27 04:48:44 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def owoify(source: str) -> str:
|
|
|
|
word_matches = OWO_WORD_REGEX.findall(source)
|
|
|
|
space_matches = OWO_SPACE_REGEX.findall(source)
|
|
|
|
|
|
|
|
words = [Word(s) for s in word_matches]
|
|
|
|
spaces = [Word(s) for s in space_matches]
|
|
|
|
|
|
|
|
words = list(map(lambda w: owoify_map_token_custom(w), words))
|
|
|
|
|
|
|
|
result = interleave_arrays(words, spaces)
|
|
|
|
result_strings = list(map(lambda w: str(w), result))
|
|
|
|
return ''.join(result_strings)
|
|
|
|
|
|
|
|
def owoify_map_token_custom(token):
|
|
|
|
for pattern in OWO_EXCLUDE_PATTERNS:
|
|
|
|
# if pattern appears anywhere in token, do not owoify.
|
|
|
|
if pattern.search(token.word):
|
|
|
|
return token
|
|
|
|
|
|
|
|
# Original Owoification Logic (sans cases for higher owo levels)
|
|
|
|
for func in SPECIFIC_WORD_MAPPING_LIST:
|
|
|
|
token = func(token)
|
|
|
|
|
|
|
|
for func in OWO_MAPPING_LIST:
|
|
|
|
token = func(token)
|
|
|
|
# End Original Owoification Logic
|
|
|
|
|
|
|
|
return token
|