2022-11-15 09:19:08 +00:00
|
|
|
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 *
|
|
|
|
|
2023-10-07 14:58:45 +00:00
|
|
|
from files.helpers.regex import *
|
2022-08-27 04:48:44 +00:00
|
|
|
|
|
|
|
# Includes, excerpts, and modifies some functions from:
|
|
|
|
# https://github.com/deadshot465/owoify-py @ owoify/owoify.py
|
|
|
|
|
|
|
|
|
|
|
|
OWO_EXCLUDE_PATTERNS = [
|
2023-10-07 14:58:45 +00:00
|
|
|
owo_ignore_links_images_regex, # links []() and images ![]()
|
2022-08-27 04:48:44 +00:00
|
|
|
# NB: May not be effective when URL part contains literal spaces vs %20
|
|
|
|
# Also relies on owoify replacements currently not affecting symbols.
|
2023-10-07 14:58:45 +00:00
|
|
|
owo_ignore_emojis_regex, #emojis
|
|
|
|
owo_ignore_the_Regex, # exclude: 'the' ↦ 'teh'
|
|
|
|
sanitize_url_regex, # bare links
|
|
|
|
mention_regex, # mentions
|
|
|
|
group_mention_regex, #ping group mentions
|
|
|
|
command_regex, # markup commands
|
2022-08-27 04:48:44 +00:00
|
|
|
]
|
|
|
|
|
2023-10-13 18:56:48 +00:00
|
|
|
def owoify(source, chud_phrase):
|
2023-08-15 20:16:51 +00:00
|
|
|
if '`' in source or '<pre>' in source or '<code>' in source:
|
|
|
|
return source
|
|
|
|
|
2023-10-07 14:58:45 +00:00
|
|
|
word_matches = owo_word_regex.findall(source)
|
|
|
|
space_matches = owo_space_regex.findall(source)
|
2022-08-27 04:48:44 +00:00
|
|
|
|
|
|
|
words = [Word(s) for s in word_matches]
|
|
|
|
spaces = [Word(s) for s in space_matches]
|
|
|
|
|
2024-02-14 17:11:41 +00:00
|
|
|
ignored_words = chud_phrase.split() if chud_phrase else []
|
2023-10-13 18:56:48 +00:00
|
|
|
|
2024-02-14 17:11:41 +00:00
|
|
|
for pattern in (poll_regex, choice_regex, bet_regex):
|
|
|
|
matches = [x.group(0) for x in pattern.finditer(source.lower())]
|
|
|
|
for match in matches:
|
|
|
|
ignored_words += match.split()
|
|
|
|
|
|
|
|
words = list(map(lambda w: owoify_map_token_custom(w, ignored_words), words))
|
2022-08-27 04:48:44 +00:00
|
|
|
|
|
|
|
result = interleave_arrays(words, spaces)
|
|
|
|
result_strings = list(map(lambda w: str(w), result))
|
|
|
|
return ''.join(result_strings)
|
|
|
|
|
2024-02-14 17:11:41 +00:00
|
|
|
def owoify_map_token_custom(token, ignored_words):
|
|
|
|
if token.word.lower() in ignored_words:
|
2023-10-13 18:56:48 +00:00
|
|
|
return token
|
|
|
|
|
2022-08-27 04:48:44 +00:00
|
|
|
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
|