Test performance improvement
This commit is contained in:
parent
058924da3b
commit
1a7e4f6c48
|
@ -1,5 +1,4 @@
|
|||
import random
|
||||
import re
|
||||
from .abstract_entity import AbstractEntity
|
||||
from src.utils import deep_get_attr
|
||||
from src.config import config
|
||||
|
@ -9,11 +8,14 @@ class Message(AbstractEntity):
|
|||
def __init__(self, chat, message):
|
||||
super(Message, self).__init__(chat=chat, message=message)
|
||||
|
||||
if self.has_text():
|
||||
self.text = message.text
|
||||
self.words = self.__get_words()
|
||||
else:
|
||||
self.text = ''
|
||||
self.text = message.text
|
||||
self._words = None
|
||||
|
||||
@property
|
||||
def words(self):
|
||||
if self._words is None:
|
||||
self._words = [] if not self.has_text() else self.__get_words()
|
||||
return self._words
|
||||
|
||||
def has_text(self):
|
||||
"""Returns True if the message has text.
|
||||
|
@ -59,23 +61,18 @@ class Message(AbstractEntity):
|
|||
return random.randint(0, 100) < getattr(self.chat, 'random_chance', config['bot']['default_chance'])
|
||||
|
||||
def __get_words(self):
|
||||
symbols = list(re.sub('\s', ' ', self.text))
|
||||
symbols = list(self.text)
|
||||
|
||||
def prettify(word):
|
||||
lowercase_word = word.lower().strip()
|
||||
last_symbol = lowercase_word[-1:]
|
||||
if last_symbol not in config['grammar']['end_sentence']:
|
||||
last_symbol = ''
|
||||
pretty_word = lowercase_word.strip(config['grammar']['all'])
|
||||
pretty_word = lowercase_word.strip(config['grammar']['all']) + last_symbol
|
||||
|
||||
if pretty_word != '' and len(pretty_word) > 2:
|
||||
return pretty_word + last_symbol
|
||||
elif lowercase_word in config['grammar']['all']:
|
||||
return None
|
||||
|
||||
return lowercase_word
|
||||
return pretty_word if pretty_word != '' and len(pretty_word) > 2 else lowercase_word
|
||||
|
||||
for entity in self.message.entities:
|
||||
symbols[entity.offset:entity.length] = ' ' * entity.length
|
||||
|
||||
return list(filter(None, map(prettify, ''.join(symbols).split(' '))))
|
||||
return list(filter(None, map(prettify, ''.join(symbols).split(' '))))
|
Loading…
Reference in New Issue