Skip to content

Commit

Permalink
Manually revert the previous 'optimisation' which turned out to be a …
Browse files Browse the repository at this point in the history
…regression. regex is the best for longer strings, as opposed to splitting on whitespace, strippnig punctuation and converting to lower.
  • Loading branch information
PaulSonOfLars committed Sep 16, 2017
1 parent 32f1368 commit 1bfa4ff
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions tg_bot/modules/cust_filters.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

import telegram
from telegram.ext import CommandHandler, BaseFilter, MessageHandler, DispatcherHandlerStop, run_async

Expand All @@ -11,13 +13,14 @@
class CustSearcher(BaseFilter):
def __init__(self, chat_id, keyword):
super().__init__()
self.keyword = keyword.lower()
self.keyword = keyword
self.pattern = "( |^|[^\w])" + re.escape(self.keyword) + "( |$|[^\w])"
self.chat_id = chat_id

def filter(self, message):
return bool(message.text
and message.chat_id == self.chat_id
and self.keyword in message.text.lower())
and re.search(self.pattern, message.text, flags=re.IGNORECASE))

def __eq__(self, other):
return other == (self.keyword, self.chat_id)
Expand All @@ -26,7 +29,7 @@ def __str__(self):
return self.keyword

def __repr__(self):
return "<RegexSearcher for {} in chat {}>".format(self.keyword, self.chat_id)
return "<RegexSearcher for {} by {} in chat {}>".format(self.keyword, self.pattern, self.chat_id)


def load_filters():
Expand Down

0 comments on commit 1bfa4ff

Please sign in to comment.