<commit_before>package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"github.com/Kovensky/go-lastfm"
	"github.com/fluffle/goirc/client"
	"github.com/howeyc/fsnotify"
	"log"
	"os"
	"sort"
	"strings"
	"sync"
	"time"
)

var (
	requireAuth = flag.Bool("require-auth", true,
		`Requires that nicknames be authenticated for using the user-nick mapping. `+
			`Disable on networks that don't implement a NickServ, such as EFNet.`)

	saveNicks = flag.Bool("save-nicks", true, `Whether to persist the user-nick mappings.`)
	nickFile  = flag.String("nick-file", "", `JSON file where user-nick map is stored. If blank, {{server}}.nicks.json is used.`)
)

type NickMap struct {
	nickMap    map[string]string
	reverseMap map[string][]string
	beingSaved bool
	sync.Mutex
}

func NewNickMap() *NickMap {
	return &NickMap{
		nickMap:    make(map[string]string),
		reverseMap: make(map[string][]string)}
}

func loadNickMap() {
	if *saveNicks {
		path := *nickFile
		if path == "" {
			path = *server + ".nicks.json"
		}
		log.Println("Watching", path)
		decodeNickMap(path)
		watcher, err := fsnotify.NewWatcher()
		if err != nil {
			log.Fatalln(err)
		}
		err = watcher.Watch(path)
		if err != nil {
			log.Println(err)
		}
		go func() {
			var timer <-chan time.Time
			for {
				select {
				case <-watcher.Event:
					timer = time.After(time.Second)
				case err := <-watcher.Error:
					log.Println(err)
				case <-timer:
					if !nickMap.beingSaved {
						log.Println("Nick persistence file changed, reloading...")
						decodeNickMap(path)
					}
					nickMap.beingSaved = false
					timer = nil
				}
			}
		}()
	}
}

func decodeNickMap(path string) {
	fh, err := os.Open(path)
	if err != nil {
		log.Println("Error opening nick persistence file:", err)
	} else {
		nickMap.Lock()
		j := json.NewDecoder(fh)
		err = j.Decode(nickMap)
		nickMap.Unlock()
		if err != nil {
			log.Println("Error reading nick-user map:", err)
		}
		fh.Close()
	}
}

func saveNickMap() {
	if *saveNicks {
		nickMap.beingSaved = true
		path := *nickFile
		if path == "" {
			path = *server + ".nicks.json"
		}
		fh, err := os.Create(path)
		if err != nil {
			log.Println("Error creating nick persistence file:", err)
		} else {
			b, err := json.MarshalIndent(nickMap, "", "\t")
			if err != nil {
				log.Println("Error marshaling nick-user map:", err)
			}
			_, err = fh.Write(b)
			if err != nil {
				log.Println("Error writing persistence file:", err)
			}

			fh.Close()
		}
	}
}

func addNickHandlers(irc *client.Conn) {
	if !flag.Parsed() {
		flag.Parse()
	}

	irc.AddHandler("QUIT", dropIdentifiedCache)
	irc.AddHandler("307", isIdentified)
	irc.AddHandler("330", isIdentified)
	irc.AddHandler("318", isIdentified)
}

func (m *NickMap) MarshalJSON() (j []byte, err error) {
	data := make(map[string][]string)
	for _, rm := range m.reverseMap {
		if len(m.reverseMap) > 0 {
			user := m.nickMap[strings.ToLower(rm[0])]
			data[user] = rm
		}
	}
	return json.Marshal(data)
}

func (m *NickMap) UnmarshalJSON(j []byte) (err error) {
	data := make(map[string][]string)
	err = json.Unmarshal(j, &data)
	if err != nil {
		return
	}
	nickMap.nickMap = make(map[string]string)
	nickMap.reverseMap = make(map[string][]string)
	for user, nicks := range data {
		for _, nick := range nicks {
			m.setUser(nick, user)
		}
	}
	return
}
<commit_msg>Check the length of the right variable<commit_after>package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"github.com/Kovensky/go-lastfm"
	"github.com/fluffle/goirc/client"
	"github.com/howeyc/fsnotify"
	"log"
	"os"
	"sort"
	"strings"
	"sync"
	"time"
)

var (
	requireAuth = flag.Bool("require-auth", true,
		`Requires that nicknames be authenticated for using the user-nick mapping. `+
			`Disable on networks that don't implement a NickServ, such as EFNet.`)

	saveNicks = flag.Bool("save-nicks", true, `Whether to persist the user-nick mappings.`)
	nickFile  = flag.String("nick-file", "", `JSON file where user-nick map is stored. If blank, {{server}}.nicks.json is used.`)
)

type NickMap struct {
	nickMap    map[string]string
	reverseMap map[string][]string
	beingSaved bool
	sync.Mutex
}

func NewNickMap() *NickMap {
	return &NickMap{
		nickMap:    make(map[string]string),
		reverseMap: make(map[string][]string)}
}

func loadNickMap() {
	if *saveNicks {
		path := *nickFile
		if path == "" {
			path = *server + ".nicks.json"
		}
		log.Println("Watching", path)
		decodeNickMap(path)
		watcher, err := fsnotify.NewWatcher()
		if err != nil {
			log.Fatalln(err)
		}
		err = watcher.Watch(path)
		if err != nil {
			log.Println(err)
		}
		go func() {
			var timer <-chan time.Time
			for {
				select {
				case <-watcher.Event:
					timer = time.After(time.Second)
				case err := <-watcher.Error:
					log.Println(err)
				case <-timer:
					if !nickMap.beingSaved {
						log.Println("Nick persistence file changed, reloading...")
						decodeNickMap(path)
					}
					nickMap.beingSaved = false
					timer = nil
				}
			}
		}()
	}
}

func decodeNickMap(path string) {
	fh, err := os.Open(path)
	if err != nil {
		log.Println("Error opening nick persistence file:", err)
	} else {
		nickMap.Lock()
		j := json.NewDecoder(fh)
		err = j.Decode(nickMap)
		nickMap.Unlock()
		if err != nil {
			log.Println("Error reading nick-user map:", err)
		}
		fh.Close()
	}
}

func saveNickMap() {
	if *saveNicks {
		nickMap.beingSaved = true
		path := *nickFile
		if path == "" {
			path = *server + ".nicks.json"
		}
		fh, err := os.Create(path)
		if err != nil {
			log.Println("Error creating nick persistence file:", err)
		} else {
			b, err := json.MarshalIndent(nickMap, "", "\t")
			if err != nil {
				log.Println("Error marshaling nick-user map:", err)
			}
			_, err = fh.Write(b)
			if err != nil {
				log.Println("Error writing persistence file:", err)
			}

			fh.Close()
		}
	}
}

func addNickHandlers(irc *client.Conn) {
	if !flag.Parsed() {
		flag.Parse()
	}

	irc.AddHandler("QUIT", dropIdentifiedCache)
	irc.AddHandler("307", isIdentified)
	irc.AddHandler("330", isIdentified)
	irc.AddHandler("318", isIdentified)
}

func (m *NickMap) MarshalJSON() (j []byte, err error) {
	data := make(map[string][]string)
	for _, rm := range m.reverseMap {
		if len(rm) > 0 {
			user := m.nickMap[strings.ToLower(rm[0])]
			data[user] = rm
		}
	}
	return json.Marshal(data)
}

func (m *NickMap) UnmarshalJSON(j []byte) (err error) {
	data := make(map[string][]string)
	err = json.Unmarshal(j, &data)
	if err != nil {
		return
	}
	nickMap.nickMap = make(map[string]string)
	nickMap.reverseMap = make(map[string][]string)
	for user, nicks := range data {
		for _, nick := range nicks {
			m.setUser(nick, user)
		}
	}
	return
}

{'fraction_non_alphanumeric': 0.15612321562734785, 'fraction_numerical': 0.003305785123966942, 'mean_word_length': 3.322077922077922, 'pattern_counts': {'":': 0, '<': 11, '<?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': '6904342', 'n_tokens_mistral': 2852, 'n_tokens_neox': 2485, 'n_words': 582}