From 14f109ea528289dca91b4a3bf2fdd7902a5c7b9f Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Tue, 30 Aug 2022 14:11:05 +0200 Subject: [PATCH] simplify marsify's code --- files/helpers/const.py | 4 +-- files/helpers/marsify.py | 61 ++++++---------------------------------- 2 files changed, 10 insertions(+), 55 deletions(-) diff --git a/files/helpers/const.py b/files/helpers/const.py index adc8e2ea9..293202ca8 100644 --- a/files/helpers/const.py +++ b/files/helpers/const.py @@ -925,9 +925,9 @@ marsey_mappings = {} for marsey in marseys: for tag in marsey.tags.split(): if tag in marsey_mappings: - marsey_mappings[tag].append(f':{marsey.name}:') + marsey_mappings[tag].append(marsey.name) else: - marsey_mappings[tag] = [f':{marsey.name}:'] + marsey_mappings[tag] = [marsey.name] db.close() SNAPPY_MARSEYS = [] diff --git a/files/helpers/marsify.py b/files/helpers/marsify.py index 770ce83fc..67aef9d19 100644 --- a/files/helpers/marsify.py +++ b/files/helpers/marsify.py @@ -1,56 +1,11 @@ -import random -import io -import json -import re from .const import marsey_mappings - -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) +from random import choice def marsify(text): - blocks = split_into_blocks(text) - new_blocks = [] - for i, block in enumerate(blocks): - new_blocks.append(block) - emojis = generate_emojis_from(block) - if emojis: - new_blocks.append(" " + emojis) - 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] \ No newline at end of file + new_text = '' + for x in text.split(' '): + new_text += f'{x} ' + if 3 < len(x) < 10 and x in marsey_mappings: + marsey = choice(marsey_mappings[x]) + new_text += f':{marsey}: ' + return new_text \ No newline at end of file