def query(self, query_id, query_string, from_id): from wallebot.app import rds binding_key = self.USER_GROUP_BINDING_KEY % from_id chat_id = rds.get(binding_key) fts = FullTextSearch(str(chat_id)) params = query_string.split(u' ') #params = (convert(x) for x in params) params = (u'NOT %s' % x[1:] if x.startswith('-') else x for x in params) keyword = u' '.join(params) log.info(u'%s: Inline querying keyword %s...' % (chat_id, keyword)) (total, tags) = fts.search(keyword, limit=self.TAGS_DISPLAY_MAX) results = [] if tags: for tag in tags: t = u'#{}'.format(tag) results.append(InlineQueryResultArticle( id=t, title=t, input_message_content=dict(message_text=t) )) return results
def message(self, msg): '''Message handler. Store all the tags to full text search engine.''' from wallebot.app import rds text = msg['text'] if not text and (text.startswith('#') or ' #' in text): return chat_id = msg['chat']['id'] user_id = msg['from']['id'] # bind user to latest chat group for the inline bot binding_key = self.USER_GROUP_BINDING_KEY % user_id rds.set(binding_key, chat_id) fts = FullTextSearch(str(chat_id)) text = msg['text'].replace('\n', ' ').replace(' ', ' ') text = ' %s ' % text tags = re.findall(r' #.+? ', text) if tags: tags = map(lambda x: x.strip('# '), tags) for tag in tags: (total, results) = fts.search(tag, limit=1) if total == 0: fts.add_document(tag) log.info("{}: Added tag: {}".format(chat_id, tag.encode('utf-8'))) log.info("{}: Added tags: {}".format(chat_id, ', '.join(tags).encode('utf-8')))
def command(self, msg, params): '''Command handler. Implements `/t` command.''' chat_id = msg['chat']['id'] fts = FullTextSearch(str(chat_id)) #params = (convert(x) for x in params) params = (u'NOT %s' % x[1:] if x.startswith('-') else x for x in params) keyword = u' '.join(params) log.info(u'%s: Querying keyword %s...' % (chat_id, keyword)) (total, tags) = fts.search(keyword, limit=None) if tags: more_msg = u'' if len(tags) > self.TAGS_DISPLAY_MAX: more_msg = "\n...and %d tags more" % (len(tags) - self.TAGS_DISPLAY_MAX) tags = random.sample(tags, self.TAGS_DISPLAY_MAX) text = '\n'.join(u'#' + tag for tag in tags) if more_msg: text += more_msg self.bot.sendMessage(chat_id=chat_id, text=text)
# -*- coding: utf-8 -*- from wallebot.fulltext_search import FullTextSearch if __name__ == '__main__': f = FullTextSearch() #f.add_document(u'羡慕霍师傅赞') #f.add_document(u'羡慕陈总') results = f.search(u'羡') for result in results: print result