<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-


import urllib2
import json

def ud(bot,msg):
    if msg.msg.startswith('!ud'):
        split = msg.msg.split(' ')
        if len(split) < 2:
            return

        word = split[1]
        definition = None
        idx = 0

        try:
            if len(split) > 2:
                idx = int(split[2]) - 1
            url = 'http://api.urbandictionary.com/v0/define?term=%s' % word
            resp = urllib2.urlopen(url)
            #get encoding
            enc = resp.headers['content-type'].split('charset=')[-1]
            content = unicode(resp.read(),enc)
            js = json.loads(content)
            definition = js['list'][idx]['definition']
        except Exception as ex:
            pass

        if not definition:
            answer = 'No results for "%s" :(' % word
        else:
            answer = '"%s": %s' % (word, definition)
        bot.sendChannelMessage(msg.replyTo, answer)

def init(bot):
    bot.events.register('channelMessage',ud)

def close(bot):
    bot.events.unregister('channelMessage',ud)<commit_msg>don't respond to messages by the bot itself<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-


import urllib2
import json

def ud(bot,msg):
    if not msg.msg.startswith('!ud') or msg.user == bot.nick:
        return

    split = msg.msg.split(' ')
    if len(split) < 2:
        return

    word = split[1]
    definition = None
    idx = 0

    try:
        if len(split) > 2:
            idx = int(split[2]) - 1
        url = 'http://api.urbandictionary.com/v0/define?term=%s' % word
        resp = urllib2.urlopen(url)
        #get encoding
        enc = resp.headers['content-type'].split('charset=')[-1]
        content = unicode(resp.read(),enc)
        js = json.loads(content)
        definition = js['list'][idx]['definition']
    except Exception as ex:
        pass

    if not definition:
        answer = 'No results for "%s" :(' % word
    else:
        answer = '"%s": %s' % (word, definition)
    bot.sendChannelMessage(msg.replyTo, answer)

def init(bot):
    bot.events.register('channelMessage',ud)

def close(bot):
    bot.events.unregister('channelMessage',ud)
{'fraction_non_alphanumeric': 0.14995442114858706, 'fraction_numerical': 0.010027347310847767, 'mean_word_length': 2.095909732016925, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6397049', 'n_tokens_mistral': 709, 'n_tokens_neox': 688, 'n_words': 157}