<commit_before>from globibot.lib.plugin import Plugin
from globibot.lib.decorators import command

from globibot.lib.helpers import parsing as p
from globibot.lib.helpers.hooks import global_cooldown

from tornado.escape import json_decode
from tornado.httputil import url_concat
from tornado.httpclient import AsyncHTTPClient, HTTPRequest
from tornado.platform.asyncio import to_asyncio_future

from random import randint

from discord import Embed

def definition_embed(definitions, index):
    definition = definitions[index]

    embed = Embed(
        title       = 'Definition',
        description = definition['definition']
    )

    embed.set_author(
        name     = definition['word'],
        icon_url = 'http://www.urbandictionary.com/favicon.ico',
        url      = definition['permalink']
    )

    embed.add_field(
        name  = 'Example',
        value = '{}\n\n👍 {:,} 👎 {:,}'.format(
            definition['example'],
            definition['thumbs_up'],
            definition['thumbs_down'],
        )
    )

    embed.set_footer(text='definition {} / {}'.format(index + 1, len(definitions)))

    embed.color = randint(0, 0xffffff)

    return embed

class UrbanDictionary(Plugin):

    def load(self):
        self.api_key = self.config.get('mashape_key')

        self.client = AsyncHTTPClient()

        self.definitions_by_message = dict()

    async def on_reaction_add(self, reaction, user):
        if user == self.bot.user:
            return

        show_prev = (reaction.emoji == '⬅')
        show_next = (reaction.emoji == '➡')

        if not show_prev and not show_next:
            return

        message = reaction.message

        try:
            (definitions, index) = self.definitions_by_message[message.id]
        except KeyError:
            pass
        else:
            new_index = index + 1 if show_next else index - 1

            if new_index < 0 or new_index >= len(definitions):
                return

            new_embed = definition_embed(definitions, new_index)

            await self.bot.clear_reactions(message)
            await self.bot.edit_message(message, '', embed=new_embed)

            self.definitions_by_message[message.id] = (definitions, new_index)

            if new_index > 0:
                await self.bot.add_reaction(message, '⬅')
            if new_index < (len(definitions) - 1):
                await self.bot.add_reaction(message, '➡')

    @command(p.string('!define') + p.bind(p.word, 'term'), global_cooldown(30))
    async def define_word_command(self, message, term):
        definitions = await self.fetch_definitions(term)

        if definitions:
            by_thumbs_up = lambda d: d['thumbs_up']<commit_msg>[Urban dictionary] No longer restricting to one word terms<commit_after>from globibot.lib.plugin import Plugin
from globibot.lib.decorators import command

from globibot.lib.helpers import parsing as p
from globibot.lib.helpers.hooks import global_cooldown

from tornado.escape import json_decode
from tornado.httputil import url_concat
from tornado.httpclient import AsyncHTTPClient, HTTPRequest
from tornado.platform.asyncio import to_asyncio_future

from random import randint

from discord import Embed

def definition_embed(definitions, index):
    definition = definitions[index]

    embed = Embed(
        title       = 'Definition',
        description = definition['definition']
    )

    embed.set_author(
        name     = definition['word'],
        icon_url = 'http://www.urbandictionary.com/favicon.ico',
        url      = definition['permalink']
    )

    embed.add_field(
        name  = 'Example',
        value = '{}\n\n👍 {:,} 👎 {:,}'.format(
            definition['example'],
            definition['thumbs_up'],
            definition['thumbs_down'],
        )
    )

    embed.set_footer(text='definition {} / {}'.format(index + 1, len(definitions)))

    embed.color = randint(0, 0xffffff)

    return embed

class UrbanDictionary(Plugin):

    def load(self):
        self.api_key = self.config.get('mashape_key')

        self.client = AsyncHTTPClient()

        self.definitions_by_message = dict()

    async def on_reaction_add(self, reaction, user):
        if user == self.bot.user:
            return

        show_prev = (reaction.emoji == '⬅')
        show_next = (reaction.emoji == '➡')

        if not show_prev and not show_next:
            return

        message = reaction.message

        try:
            (definitions, index) = self.definitions_by_message[message.id]
        except KeyError:
            pass
        else:
            new_index = index + 1 if show_next else index - 1

            if new_index < 0 or new_index >= len(definitions):
                return

            new_embed = definition_embed(definitions, new_index)

            await self.bot.clear_reactions(message)
            await self.bot.edit_message(message, '', embed=new_embed)

            self.definitions_by_message[message.id] = (definitions, new_index)

            if new_index > 0:
                await self.bot.add_reaction(message, '⬅')
            if new_index < (len(definitions) - 1):
                await self.bot.add_reaction(message, '➡')

    @command(p.string('!define'), global_cooldown(30))
    async def define_word_command(self, message):
        term = message.clean_content[len('!define'):].strip()
        definitions = await self.fetch_definitions(term)

        if definitions:
            by_thumbs_up = lambda d: d['thumbs_up']
{'fraction_non_alphanumeric': 0.1050909090909091, 'fraction_numerical': 0.0036363636363636364, 'mean_word_length': 2.4728535353535355, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3417308', 'n_tokens_mistral': 1676, 'n_tokens_neox': 1590, 'n_words': 364}