<commit_before>package command

import (
	"encoding/json"
	"fmt"
	"log"
	"math/rand"
	"regexp"
	"strconv"
	"strings"
)

const (
	XKCDLatestUrl = "http://xkcd.com/info.0.json"
	XKCDNumberUrl = "http://xkcd.com/%d/info.0.json"
)

var numRegexp = regexp.MustCompile(`(^\d+$)`)

type XKCDResult struct {
	Num        int    `json:"num"`
	Day        string `json:"day"`
	Month      string `json:"month"`
	Year       string `json:"year"`
	Img        string `json:"img"`
	Link       string `json:"link"`
	SafeTitle  string `json:"safe_title"`
	Title      string `json:"title"`
	Alt        string `json:"alt"`
	Transcript string `json:"transcript"`
	News       string `json:"news"`
}

type XKCDCommand struct {
	pattern *regexp.Regexp
}

func XKCD() XKCDCommand {
	return XKCDCommand{regexp.MustCompile(`(?i)xkcd\s*(.*)`)}
}

func (c XKCDCommand) Pattern() *regexp.Regexp {
	return c.pattern
}

func (c XKCDCommand) Run(query string) []string {
	q := strings.Trim(query, " ")
	current := loadComic(XKCDLatestUrl)
	comic := current

	if q == "random" {
		comic = loadComic(fmt.Sprintf(XKCDNumberUrl, rand.Intn(current.Num)))
	} else if match := numRegexp.FindStringSubmatch(q); len(match) > 1 {
		if number, err := strconv.Atoi(match[1]); err == nil {
			if number == 404 {
				return []string{"smart ass :)"}
			}

			comic = loadComic(fmt.Sprintf(XKCDNumberUrl, number))
		}
	}

	return []string{
		comic.Img,
		comic.Title,
		comic.Alt,
	}
}

func loadComic(url string) *XKCDResult {
	if body, err := NewHTTPClient(url).Get(); err == nil {
		var result XKCDResult
		json.Unmarshal(body, &result)

		return &result
	} else {
		log.Println("ERROR:", err)
	}
	return nil
}<commit_msg>Optimize XKCD command.<commit_after>package command

import (
	"encoding/json"
	"fmt"
	"log"
	"math/rand"
	"regexp"
	"strconv"
	"strings"
)

const (
	XKCDLatestUrl = "http://xkcd.com/info.0.json"
	XKCDNumberUrl = "http://xkcd.com/%d/info.0.json"
)

var numRegexp = regexp.MustCompile(`(^\d+$)`)

type XKCDResult struct {
	Num        int    `json:"num"`
	Day        string `json:"day"`
	Month      string `json:"month"`
	Year       string `json:"year"`
	Img        string `json:"img"`
	Link       string `json:"link"`
	SafeTitle  string `json:"safe_title"`
	Title      string `json:"title"`
	Alt        string `json:"alt"`
	Transcript string `json:"transcript"`
	News       string `json:"news"`
}

type XKCDCommand struct {
	pattern *regexp.Regexp
}

func XKCD() XKCDCommand {
	return XKCDCommand{regexp.MustCompile(`(?i)xkcd\s*(.*)`)}
}

func (c XKCDCommand) Pattern() *regexp.Regexp {
	return c.pattern
}

func (c XKCDCommand) Run(query string) []string {
	q := strings.Trim(query, " ")

	if match := numRegexp.FindStringSubmatch(q); len(match) > 1 {
		if number, err := strconv.Atoi(match[1]); err == nil {
			if number == 404 {
				return []string{"smart ass :)"}
			}

			return render(loadComic(fmt.Sprintf(XKCDNumberUrl, number)))
		}
	}

	current := loadComic(XKCDLatestUrl)
	comic := current

	if q == "random" {
		comic = loadComic(fmt.Sprintf(XKCDNumberUrl, rand.Intn(current.Num)))
	}

	return render(comic)
}

func loadComic(url string) *XKCDResult {
	if body, err := NewHTTPClient(url).Get(); err == nil {
		var result XKCDResult
		json.Unmarshal(body, &result)

		return &result
	} else {
		log.Println("ERROR:", err)
	}
	return nil
}

func render(comic *XKCDResult) []string {
	return []string{
		comic.Img,
		comic.Title,
		comic.Alt,
	}
}
{'fraction_non_alphanumeric': 0.16185476815398075, 'fraction_numerical': 0.004082822980460776, 'mean_word_length': 3.654002713704206, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3775810', 'n_tokens_mistral': 1388, 'n_tokens_neox': 1315, 'n_words': 281}