<commit_before>package command

import (
	"encoding/json"
	"log"
	"math/rand"
	"regexp"
)

const (
	ImageSearchUrl = "http://ajax.googleapis.com/ajax/services/search/images"
)

type ImageCommand struct {
	pattern *regexp.Regexp
}

func Image() ImageCommand {
	return ImageCommand{regexp.MustCompile(`(?i)(image|img)\s+([^\s].*)`)}
}

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

func (c ImageCommand) Run(query string) []string {
	return findImages(query, Params{})
}

type GIFCommand struct {
	pattern *regexp.Regexp
}

func GIF() GIFCommand {
	return GIFCommand{regexp.MustCompile(`(?i)(gif|animate)\s+([^\s].*)`)}
}

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

func (c GIFCommand) Run(query string) []string {
	return findImages(query, Params{"imgtype": "animated"})
}

type Params map[string]string

var defaultParams = Params{
	"v":    "1.0",
	"safe": "active",
	"rsz":  "8",
}

type ImageResult struct {
	UnescapedUrl string `json:"unescapedUrl"`
}

type ResponseData struct {
	Images []ImageResult `json:"results"`
}

type ImageResults struct {
	Data ResponseData `json:"responseData"`
}

func findImages(query string, params Params) []string {
	for k, v := range defaultParams {
		params[k] = v
	}
	params["q"] = query

	var imgUrl string

	if body, err := NewHTTPClient(ImageSearchUrl).With(params).Get(); err == nil {
		var result ImageResults
		json.Unmarshal(body, &result)

		if images := result.Data.Images; len(images) > 0 {
			selected := images[rand.Intn(len(images))]

			imgUrl = selected.UnescapedUrl
		}
	} else {
		log.Println("ERROR:", err)
	}

	var urls []string
	if imgUrl != "" {
		urls = append(urls, imgUrl)
	}
	return urls
}
<commit_msg>Inline struct for image search results.<commit_after>package command

import (
	"encoding/json"
	"log"
	"math/rand"
	"regexp"
)

const (
	ImageSearchUrl = "http://ajax.googleapis.com/ajax/services/search/images"
)

type ImageCommand struct {
	pattern *regexp.Regexp
}

func Image() ImageCommand {
	return ImageCommand{regexp.MustCompile(`(?i)(image|img)\s+([^\s].*)`)}
}

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

func (c ImageCommand) Run(query string) []string {
	return findImages(query, Params{})
}

type GIFCommand struct {
	pattern *regexp.Regexp
}

func GIF() GIFCommand {
	return GIFCommand{regexp.MustCompile(`(?i)(gif|animate)\s+([^\s].*)`)}
}

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

func (c GIFCommand) Run(query string) []string {
	return findImages(query, Params{"imgtype": "animated"})
}

type Params map[string]string

var defaultParams = Params{
	"v":    "1.0",
	"safe": "active",
	"rsz":  "8",
}

type imageResults struct {
	Data struct {
		Images []struct {
			UnescapedUrl string `json:"unescapedUrl"`
		} `json:"results"`
	} `json:"responseData"`
}

func findImages(query string, params Params) []string {
	for k, v := range defaultParams {
		params[k] = v
	}
	params["q"] = query

	var imgUrl string

	if body, err := NewHTTPClient(ImageSearchUrl).With(params).Get(); err == nil {
		var result imageResults
		json.Unmarshal(body, &result)

		if images := result.Data.Images; len(images) > 0 {
			selected := images[rand.Intn(len(images))]

			imgUrl = selected.UnescapedUrl
		}
	} else {
		log.Println("ERROR:", err)
	}

	var urls []string
	if imgUrl != "" {
		urls = append(urls, imgUrl)
	}
	return urls
}

{'fraction_non_alphanumeric': 0.15911764705882353, 'fraction_numerical': 0.002352941176470588, 'mean_word_length': 4.530081300813008, 'pattern_counts': {'":': 8, '<': 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': '3692551', 'n_tokens_mistral': 1282, 'n_tokens_neox': 1228, 'n_words': 287}