<commit_before>/** Created on May 6, 2014
 *  author: MrPoxipol
 */

var http = require('http');
var util = require('util');
// Templates module
var Mustache = require('mustache');
var S = require('string');

var debug = require('../nibo/debug');

const COMMAND_NAME = 'ud';
// pattern for util.format()
const TERM_API_URL_PATTERN = 'http://api.urbandictionary.com/v0/define?term=%s';
const RANDOM_API_URL = 'http://api.urbandictionary.com/v0/random';

const DESCRIPTION_MAX_LEN = 250;

exports.meta = {
	name: 'urban-dictionary',
	commandName: COMMAND_NAME,
	description: 'Polls UrbanDictionary for given phrase. Returns random definition when the term is not specified'
};

function shorten(str) {
	/** Shortens string*/
	if (str.length < DESCRIPTION_MAX_LEN)
		return str;

	str = str.substr(0, DESCRIPTION_MAX_LEN);
	var wordWrap = str.lastIndexOf(' ');
	if (wordWrap == -1) wordWrap = DESCRIPTION_MAX_LEN;

	str = str.substr(0, wordWrap) + '..';
	return str;
}

function getDefFromJson(data) {
	var parsedJson;
	try {
		parsedJson = JSON.parse(data);
	} catch (e) {
		debug.debug('JSON parsing error');
		return;
	}

	if (parsedJson.list.length < 1) {
		return;
	}

	var first = parsedJson.list[0];

	var term = {};
	term.id = first.defid;
	term.link = first.permalink;
	term.word = first.word;
	term.definition = shorten(first.definition);
	term.example = shorten(first.example);
	term.upvotes = first.thumbs_up;
	term.downvotes = first.thumbs_down;

	var pattern = '[#{{&id}}|{{&word}}] {{&upvotes}}/{{&downvotes}} - {{&definition}}|Example: {{&example}} ({{&link}})';
	var output = Mustache.render(pattern, term);
	// Remove \r\n
	output = S(output).collapseWhitespace().s;

	return output;<commit_msg>Move short function to utiles module<commit_after>/** Created on May 6, 2014
 *  author: MrPoxipol
 */

var http = require('http');
var util = require('util');
// Templates module
var Mustache = require('mustache');
var S = require('string');

// Bot utiles
var ut = require('../nibo/ut');
var debug = require('../nibo/debug');

const COMMAND_NAME = 'ud';
// pattern for util.format()
const TERM_API_URL_PATTERN = 'http://api.urbandictionary.com/v0/define?term=%s';
const RANDOM_API_URL = 'http://api.urbandictionary.com/v0/random';

const DESCRIPTION_MAX_LEN = 250;

exports.meta = {
	name: 'urban-dictionary',
	commandName: COMMAND_NAME,
	description: 'Polls UrbanDictionary for given phrase. Returns random definition when the term is not specified'
};

function getDefFromJson(data) {
	var parsedJson;
	try {
		parsedJson = JSON.parse(data);
	} catch (e) {
		debug.debug('JSON parsing error');
		return;
	}

	if (parsedJson.list.length < 1) {
		return;
	}

	var first = parsedJson.list[0];

	var term = {};
	term.id = first.defid;
	term.link = first.permalink;
	term.word = first.word;
	term.definition = ut.short(first.definition, DESCRIPTION_MAX_LEN);
	term.example = ut.short(first.example, DESCRIPTION_MAX_LEN);
	term.upvotes = first.thumbs_up;
	term.downvotes = first.thumbs_down;

	var pattern = '[#{{&id}}|{{&word}}] {{&upvotes}}/{{&downvotes}} - {{&definition}}|Example: {{&example}} ({{&link}})';
	var output = Mustache.render(pattern, term);
	// Remove \r\n
	output = S(output).collapseWhitespace().s;

	return output;
{'fraction_non_alphanumeric': 0.16594694632942628, 'fraction_numerical': 0.008328192473781616, 'mean_word_length': 4.562607204116638, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 3, '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': '7430369', 'n_tokens_mistral': 1213, 'n_tokens_neox': 1132, 'n_words': 262}