<commit_before>from IRCMessage import IRCMessage
from IRCResponse import IRCResponse, ResponseType
from Function import Function
import GlobalVars

import string
import sqlite3
import re

class Instantiate(Function):
	Help = "hugs [nick] -- How many hugs has this person given and received?"
	# hug_dict is : {"nick":[given, received]}
	
	def GetResponse(self, HubbeBot, message):
		if message.Type == "ACTION":
			filename = "data/data.db"
			hug_dict = {}
			with sqlite3.connect(filename) as conn:
                                c = conn.cursor()
                                for row in c.execute("SELECT * FROM hugs"):
                                        hug_dict[row[0]] = [row[1], row[2]]
			pattern = "hu+g|cuddle|snu+ggle|snu+g|squeeze|glomp"
			match = re.search(pattern, message.MessageList[0] , re.IGNORECASE)
			if match:
				receivers = []
				for nick in message.MessageList[1:]:
					if string.lower(nick) not in GlobalVars.commonWords:
						nick = string.rstrip(nick, "\x01")
						nick = string.rstrip(nick, ".")
						nick = string.rstrip(nick, "!")
						nick = string.rstrip(nick, ",")
						nick = string.lower(nick)
						regexPattern = r"^[^a-zA-Z0-9`_\[\]\{\}\|\^\\]+$"
						invalid = re.match(regexPattern, nick)
						if not invalid:
                                                        receivers.append(nick)
				for index in range(0, len(receivers)):
					giver = string.lower(message.User.Name)
					receiver = receivers[index]
					if giver not in hug_dict:
						hug_dict[giver] = [1,0]
						with sqlite3.connect(filename) as conn:
                                                        c = conn.cursor()
                                                        c.execute("INSERT INTO hugs VALUES (?,?,?)", (giver,1,0))
                                                        conn.commit()
					else:
						hug_dict[giver] = list(hug_dict[giver])
						hug_dict[giver][0] += 1
						with sqlite3.connect(filename) as conn:
                                                        c = conn.cursor()
                                                        c.execute("UPDATE hugs SET given=? WHERE nick=?", (hug_dict[giver][0], giver))
                                                        conn.commit()
					if receiver not in hug_dict:
						hug_dict[receiver] = [0,1]
						with sqlite3.connect(filename) as conn:
                                                        c = conn.cursor()
                                                        c.execute("INSERT INTO hugs VALUES (?,?,?)", (receiver,0,1))
                                                        conn.commit()
					else:
						hug_dict[receiver] = list(hug_dict[receiver])
						hug_dict[receiver][1] += 1
						with sqlite3.connect(filename) as conn:
                                                        c = conn.cursor()
                                                        c.execute("UPDATE hugs SET received=? WHERE nick=?", (hug_dict[receiver][1], receiver))
                                                        conn.commit()
			
		elif message.Type == "PRIVMSG":
			if message.Command == "hugs":
				filename = "data/data.db"
				hug_dict = {}
				with sqlite3.connect(filename) as conn:
                                        c = conn.cursor()
                                        for row in c.execute("SELECT * FROM hugs"):
                                                hug_dict[row[0]] = [row[1], row[2]]
				if len(message.ParameterList) == 0:
					target = message.User.Name
				else:
					target = message.ParameterList[0]
					
				hugData = [0,0]
				matches = []
				for (user, hugCounts) in hug_dict.items():
					try:
						match = re.search(target, user, re.IGNORECASE)
					except:
						match = False
					if match:
						matches.append(match.string)
						hugData[0] += hugCounts[0]
						hugData[1] += hugCounts[1]
						
				HugString = target + " has received " + str(hugData[1]) + " hugs and given " + str(hugData[0]) + " hugs."
				matchedNicksString = "Matches found: "
				numberOfMatches = 0
				for name in matches:
					if (numberOfMatches > 9):
						matchedNicksString += "..."
						break
					elif (matches.index(name) == len(matches)-1):
						matchedNicksString += name
						numberOfMatches += 1
					else:
						matchedNicksString = matchedNicksString + name + ", "
						numberOfMatches += 1
				if len(matchedNicksString) > 50:
                                        matchedNicksString = "Matches found: LOTS."
				return IRCResponse(ResponseType.Say, matchedNicksString, message.ReplyTo), IRCResponse(ResponseType.Say, HugString, message.ReplyTo)<commit_msg>In today's episode of GitHub, I am stupid.<commit_after>from IRCMessage import IRCMessage
from IRCResponse import IRCResponse, ResponseType
from Function import Function
import GlobalVars

import string
import sqlite3
import re

class Instantiate(Function):
	Help = "hugs [nick] -- How many hugs has this person given and received?"
	# hug_dict is : {"nick":[given, received]}
	
	def GetResponse(self, HubbeBot, message):
		if message.Type == "ACTION":
			filename = "data/data.db"
			hug_dict = {}
			with sqlite3.connect(filename) as conn:
                                c = conn.cursor()
                                for row in c.execute("SELECT * FROM hugs"):
                                        hug_dict[row[0]] = [row[1], row[2]]
			pattern = "hu+g|cuddle|snu+ggle|snu+g|squeeze|glomp"
			match = re.search(pattern, message.MessageList[0] , re.IGNORECASE)
			if match:
				receivers = []
				for nick in message.MessageList[1:]:
					if string.lower(nick) not in GlobalVars.commonWords:
						nick = string.rstrip(nick, "\x01")
						nick = string.rstrip(nick, ".")
						nick = string.rstrip(nick, "!")
						nick = string.rstrip(nick, ",")
						nick = string.lower(nick)
						regexPattern = r"^[^a-zA-Z0-9`_\[\]\{\}\|\^\\]+$"
						invalid = re.match(regexPattern, nick)
						if not invalid:
                                                        receivers.append(nick)
				for index in range(0, len(receivers)):
					giver = string.lower(message.User.Name)
					receiver = receivers[index]
					if giver not in hug_dict:
						hug_dict[giver] = [1,0]
						with sqlite3.connect(filename) as conn:
                                                        c = conn.cursor()
                                                        c.execute("INSERT INTO hugs VALUES (?,?,?)", (giver,1,0))
                                                        conn.commit()
					else:
						hug_dict[giver] = list(hug_dict[giver])
						hug_dict[giver][0] += 1
						with sqlite3.connect(filename) as conn:
                                                        c = conn.cursor()
                                                        c.execute("UPDATE hugs SET given=? WHERE nick=?", (hug_dict[giver][0], giver))
                                                        conn.commit()
					if receiver not in hug_dict:
						hug_dict[receiver] = [0,1]
						with sqlite3.connect(filename) as conn:
                                                        c = conn.cursor()
                                                        c.execute("INSERT INTO hugs VALUES (?,?,?)", (receiver,0,1))
                                                        conn.commit()
					else:
						hug_dict[receiver] = list(hug_dict[receiver])
						hug_dict[receiver][1] += 1
						with sqlite3.connect(filename) as conn:
                                                        c = conn.cursor()
                                                        c.execute("UPDATE hugs SET received=? WHERE nick=?", (hug_dict[receiver][1], receiver))
                                                        conn.commit()
			
		elif message.Type == "PRIVMSG":
			if message.Command == "hugs":
				filename = "data/data.db"
				hug_dict = {}
				with sqlite3.connect(filename) as conn:
                                        c = conn.cursor()
                                        for row in c.execute("SELECT * FROM hugs"):
                                                hug_dict[row[0]] = [row[1], row[2]]
				if len(message.ParameterList) == 0:
					target = message.User.Name
				else:
					target = message.ParameterList[0]
					
				hugData = [0,0]
				matches = []
				for (user, hugCounts) in hug_dict.items():
					try:
						match = re.search(target, user, re.IGNORECASE)
					except:
						match = False
					if match:
						matches.append(match.string)
						hugData[0] += hugCounts[0]
						hugData[1] += hugCounts[1]
						
				HugString = target + " has received " + str(hugData[1]) + " hugs and given " + str(hugData[0]) + " hugs."
				matchedNicksString = "Matches found: "
				numberOfMatches = 0
				for name in matches:
					if (numberOfMatches > 9):
						matchedNicksString += "..."
						break
					elif (matches.index(name) == len(matches)-1):
						matchedNicksString += name
						numberOfMatches += 1
					else:
						matchedNicksString = matchedNicksString + name + ", "
						numberOfMatches += 1
				if len(matches) > 30:
                                        matchedNicksString = "Matches found: LOTS."
				return IRCResponse(ResponseType.Say, matchedNicksString, message.ReplyTo), IRCResponse(ResponseType.Say, HugString, message.ReplyTo)
{'fraction_non_alphanumeric': 0.11529335634167386, 'fraction_numerical': 0.01100086281276963, 'mean_word_length': 1.532914504233816, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 72, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '62738', 'n_tokens_mistral': 3157, 'n_tokens_neox': 2513, 'n_words': 596}