forked from rDrama/rDrama
simplify marsify's code
parent
fc01eb6c1d
commit
14f109ea52
|
@ -925,9 +925,9 @@ marsey_mappings = {}
|
||||||
for marsey in marseys:
|
for marsey in marseys:
|
||||||
for tag in marsey.tags.split():
|
for tag in marsey.tags.split():
|
||||||
if tag in marsey_mappings:
|
if tag in marsey_mappings:
|
||||||
marsey_mappings[tag].append(f':{marsey.name}:')
|
marsey_mappings[tag].append(marsey.name)
|
||||||
else:
|
else:
|
||||||
marsey_mappings[tag] = [f':{marsey.name}:']
|
marsey_mappings[tag] = [marsey.name]
|
||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
SNAPPY_MARSEYS = []
|
SNAPPY_MARSEYS = []
|
||||||
|
|
|
@ -1,56 +1,11 @@
|
||||||
import random
|
|
||||||
import io
|
|
||||||
import json
|
|
||||||
import re
|
|
||||||
from .const import marsey_mappings
|
from .const import marsey_mappings
|
||||||
|
from random import choice
|
||||||
WORD_DELIMITER = " "
|
|
||||||
MAX_EMOJIS_PER_BLOCK = 2
|
|
||||||
BLOCK_REGEX = re.compile(r"\s*[^\s]*")
|
|
||||||
TRIM_REGEX = re.compile(r"^\W*|\W*$")
|
|
||||||
|
|
||||||
def split_into_blocks(text):
|
|
||||||
if text == "" or BLOCK_REGEX.search(text) is None:
|
|
||||||
return [text]
|
|
||||||
blocks = []
|
|
||||||
start = 0
|
|
||||||
while start < len(text):
|
|
||||||
block_match = BLOCK_REGEX.search(text, start)
|
|
||||||
blocks.append(block_match.group())
|
|
||||||
start = block_match.end()
|
|
||||||
return blocks
|
|
||||||
|
|
||||||
def trim_nonalphabetical_characters(text):
|
|
||||||
return TRIM_REGEX.sub("", text)
|
|
||||||
|
|
||||||
def marsify(text):
|
def marsify(text):
|
||||||
blocks = split_into_blocks(text)
|
new_text = ''
|
||||||
new_blocks = []
|
for x in text.split(' '):
|
||||||
for i, block in enumerate(blocks):
|
new_text += f'{x} '
|
||||||
new_blocks.append(block)
|
if 3 < len(x) < 10 and x in marsey_mappings:
|
||||||
emojis = generate_emojis_from(block)
|
marsey = choice(marsey_mappings[x])
|
||||||
if emojis:
|
new_text += f':{marsey}: '
|
||||||
new_blocks.append(" " + emojis)
|
return new_text
|
||||||
return "".join(new_blocks)
|
|
||||||
|
|
||||||
def generate_emojis_from(block):
|
|
||||||
trimmed_block = trim_nonalphabetical_characters(block)
|
|
||||||
matching_emojis = get_matching_emojis(trimmed_block)
|
|
||||||
emojis = []
|
|
||||||
if matching_emojis:
|
|
||||||
num_emojis = random.randint(0, MAX_EMOJIS_PER_BLOCK)
|
|
||||||
for x in range(num_emojis):
|
|
||||||
emojis.append(random.choice(matching_emojis))
|
|
||||||
return "".join(emojis)
|
|
||||||
|
|
||||||
def get_matching_emojis(trimmed_block):
|
|
||||||
key = get_alphanumeric_prefix(trimmed_block.lower())
|
|
||||||
if key in marsey_mappings:
|
|
||||||
return marsey_mappings[get_alphanumeric_prefix(key)]
|
|
||||||
return []
|
|
||||||
|
|
||||||
def get_alphanumeric_prefix(s):
|
|
||||||
i = 0
|
|
||||||
while i < len(s) and s[i].isalnum():
|
|
||||||
i += 1
|
|
||||||
return s[:i]
|
|
Loading…
Reference in New Issue