<commit_before># -*- coding: utf-8 -*-

__module_name__ = "Xchat Translator"
__module_version__ = "-.-"
__module_description__ = "Performs translations from one language to another"
__module_author__ = "<NAME>"

import xchat
import json
import socket
import select

DEFAULT_LANG = "fr"

SERVER_IP = "127.0.0.1"
SERVER_PORT = 4242
BUFFER_SIZE = 1024
TIMER = 100

LANGUAGES = {
  'AFRIKAANS' : 'af',
  'ALBANIAN' : 'sq',
  'AMHARIC' : 'am',
  'ARABIC' : 'ar',
  'ARMENIAN' : 'hy',
  'AZERBAIJANI' : 'az',
  'BASQUE' : 'eu',
  'BELARUSIAN' : 'be',
  'BENGALI' : 'bn',
  'BIHARI' : 'bh',
  'BULGARIAN' : 'bg',
  'BURMESE' : 'my',
  'CATALAN' : 'ca',
  'CHEROKEE' : 'chr',
  'CHINESE' : 'zh',
  'CHINESE_SIMPLIFIED' : 'zh-CN',
  'CHINESE_TRADITIONAL' : 'zh-TW',
  'CROATIAN' : 'hr',
  'CZECH' : 'cs',
  'DANISH' : 'da',
  'DHIVEHI' : 'dv',
  'DUTCH': 'nl',
  'ENGLISH' : 'en',
  'ESPERANTO' : 'eo',
  'ESTONIAN' : 'et',
  'FILIPINO' : 'tl',
  'FINNISH' : 'fi',
  'FRENCH' : 'fr',
  'GALICIAN' : 'gl',
  'GEORGIAN' : 'ka',
  'GERMAN' : 'de',
  'GREEK' : 'el',
  'GUARANI' : 'gn',
  'GUJARATI' : 'gu',
  'HEBREW' : 'iw',
  'HINDI' : 'hi',
  'HUNGARIAN' : 'hu',
  'ICELANDIC' : 'is',
  'INDONESIAN' : 'id',
  'INUKTITUT' : 'iu',
  'IRISH' : 'ga',
  'ITALIAN' : 'it',
  'JAPANESE' : 'ja',
  'KANNADA' : 'kn',
  'KAZAKH' : 'kk',
  'KHMER' : 'km',
  'KOREAN' : 'ko',
  'KURDISH': 'ku',
  'KYRGYZ': 'ky',
  'LAOTHIAN': 'lo',
  'LATVIAN' : 'lv',
  'LITHUANIAN' : 'lt',
  'MACEDONIAN' : 'mk',
  'MALAY' : 'ms',
  'MALAYALAM' : 'ml',
  'MALTESE' : 'mt',
  'MARATHI' : 'mr',
  'MONGOLIAN' : 'mn',
  'NEPALI' : 'ne',
  'NORWEGIAN' : 'no',
  'ORIYA' : 'or',
  'PASHTO' : 'ps',
  'PERSIAN' : 'fa',
  'POLISH' : 'pl',
  'PORTUGUESE' : 'pt-PT',
  'PUNJABI' : 'pa',
  'ROMANIAN' : 'ro',
  'RUSSIAN' : 'ru',
  'SANSKRIT' : 'sa',
  'SERBIAN' : 'sr',
  'SINDHI' : 'sd',
  'SINHALESE' : 'si',
  'SLOVAK' : 'sk',
  'SLOVENIAN' : 'sl',
  'SPANISH' : 'es',
  'SWAHILI' : 'sw',
  'SWEDISH' : 'sv',
  'TAJIK' : 'tg',
  'TAMIL' : 'ta',
  'TAGALOG' : 'tl',
  'TELUGU' : 'te',
  'THAI' : 'th',
  'TIBETAN' : 'bo',
  'TURKISH' : 'tr',
  'UKRAINIAN' : 'uk',
  'URDU' : 'ur',
  'UZBEK' : 'uz',
  'UIGHUR' : 'ug',
  'VIETNAMESE' : 'vi',
  'WELSH' : 'cy',
  'YIDDISH' : 'yi'
}

WATCHLIST = {}
ACTIVE_JOBS = 0
TIMEOUT_HOOK = None
CONN = None

class Translator:
	def translate(cls, channel, user, text, tgtLang = DEFAULT_LANG, outgoing = False, srcLang = "auto", tgtTxt = None, echoTxt = None, echo = False, kill = False, read = False):
		global CONN

		request = dict(Outgoing = outgoing, Channel = channel, User = user, Srctxt = text, Srclang = srcLang, Tgttxt = tgtTxt, Tgtlang = tgtLang, EchoTxt = echoTxt, Echo = echo, Kill = kill, Read = read)

		cls.connectToServer()
		
		jsonStr = json.dumps(request).encode("utf-8")
		CONN.send(jsonStr)

		return None
	translate = classmethod(translate)

	def readResults(cls, userdata = None):
		global TIMEOUT_HOOK
		global ACTIVE_JOBS
		global BUFFER

		request = dict(Outgoing = None, Channel = None, User = None, Srctxt = None, Srclang = None, Tgttxt = None, Tgtlang = None, EchoTxt = None, Echo = False, Kill = False, Read = True)
		jsonStr = json.dumps(request).encode("utf-8")

		CONN.send(jsonStr)
		result = json.loads(CONN.recv(BUFFER_SIZE).decode("utf-8"))

		if type(result) == dict:
			if result["Outgoing"]:
				xchat.command("say " + result["Tgttxt"])
			elif result["Srctxt"] != result["Tgttxt"]:
				context = xchat.find_context(channel=result["Channel"])
				str = result["Tgttxt"]
				context.emit_print("Channel Message", "_[%s]" %(result["User"]), str)
				
			ACTIVE_JOBS -= 1
			
		if ACTIVE_JOBS <= 0:
			xchat.unhook(TIMEOUT_HOOK)
			TIMEOUT_HOOK = None
			
			cls.closeConnection()

		return None
	readResults = classmethod(readResults)

	def connectToServer(cls, ip = SERVER_IP, port = SERVER_PORT):
		global CONN

		if CONN is None:
			CONN = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
			CONN.connect((ip, port))

		return None
	connectToServer = classmethod(connectToServer)

	def checkConnection(cls, status):
		r, w, e = select.select([CONN,], [CONN,], [CONN,], 0)

		if status == "r":<commit_msg>Encoded information sent to XChat/Hexchat.<commit_after># -*- coding: utf-8 -*-

__module_name__ = "Xchat Translator"
__module_version__ = "-.-"
__module_description__ = "Performs translations from one language to another"
__module_author__ = "<NAME>"

import xchat
import json
import socket
import select

DEFAULT_LANG = "fr"

SERVER_IP = "127.0.0.1"
SERVER_PORT = 4242
BUFFER_SIZE = 1024
TIMER = 100

LANGUAGES = {
  'AFRIKAANS' : 'af',
  'ALBANIAN' : 'sq',
  'AMHARIC' : 'am',
  'ARABIC' : 'ar',
  'ARMENIAN' : 'hy',
  'AZERBAIJANI' : 'az',
  'BASQUE' : 'eu',
  'BELARUSIAN' : 'be',
  'BENGALI' : 'bn',
  'BIHARI' : 'bh',
  'BULGARIAN' : 'bg',
  'BURMESE' : 'my',
  'CATALAN' : 'ca',
  'CHEROKEE' : 'chr',
  'CHINESE' : 'zh',
  'CHINESE_SIMPLIFIED' : 'zh-CN',
  'CHINESE_TRADITIONAL' : 'zh-TW',
  'CROATIAN' : 'hr',
  'CZECH' : 'cs',
  'DANISH' : 'da',
  'DHIVEHI' : 'dv',
  'DUTCH': 'nl',
  'ENGLISH' : 'en',
  'ESPERANTO' : 'eo',
  'ESTONIAN' : 'et',
  'FILIPINO' : 'tl',
  'FINNISH' : 'fi',
  'FRENCH' : 'fr',
  'GALICIAN' : 'gl',
  'GEORGIAN' : 'ka',
  'GERMAN' : 'de',
  'GREEK' : 'el',
  'GUARANI' : 'gn',
  'GUJARATI' : 'gu',
  'HEBREW' : 'iw',
  'HINDI' : 'hi',
  'HUNGARIAN' : 'hu',
  'ICELANDIC' : 'is',
  'INDONESIAN' : 'id',
  'INUKTITUT' : 'iu',
  'IRISH' : 'ga',
  'ITALIAN' : 'it',
  'JAPANESE' : 'ja',
  'KANNADA' : 'kn',
  'KAZAKH' : 'kk',
  'KHMER' : 'km',
  'KOREAN' : 'ko',
  'KURDISH': 'ku',
  'KYRGYZ': 'ky',
  'LAOTHIAN': 'lo',
  'LATVIAN' : 'lv',
  'LITHUANIAN' : 'lt',
  'MACEDONIAN' : 'mk',
  'MALAY' : 'ms',
  'MALAYALAM' : 'ml',
  'MALTESE' : 'mt',
  'MARATHI' : 'mr',
  'MONGOLIAN' : 'mn',
  'NEPALI' : 'ne',
  'NORWEGIAN' : 'no',
  'ORIYA' : 'or',
  'PASHTO' : 'ps',
  'PERSIAN' : 'fa',
  'POLISH' : 'pl',
  'PORTUGUESE' : 'pt-PT',
  'PUNJABI' : 'pa',
  'ROMANIAN' : 'ro',
  'RUSSIAN' : 'ru',
  'SANSKRIT' : 'sa',
  'SERBIAN' : 'sr',
  'SINDHI' : 'sd',
  'SINHALESE' : 'si',
  'SLOVAK' : 'sk',
  'SLOVENIAN' : 'sl',
  'SPANISH' : 'es',
  'SWAHILI' : 'sw',
  'SWEDISH' : 'sv',
  'TAJIK' : 'tg',
  'TAMIL' : 'ta',
  'TAGALOG' : 'tl',
  'TELUGU' : 'te',
  'THAI' : 'th',
  'TIBETAN' : 'bo',
  'TURKISH' : 'tr',
  'UKRAINIAN' : 'uk',
  'URDU' : 'ur',
  'UZBEK' : 'uz',
  'UIGHUR' : 'ug',
  'VIETNAMESE' : 'vi',
  'WELSH' : 'cy',
  'YIDDISH' : 'yi'
}

WATCHLIST = {}
ACTIVE_JOBS = 0
TIMEOUT_HOOK = None
CONN = None

class Translator:
	def translate(cls, channel, user, text, tgtLang = DEFAULT_LANG, outgoing = False, srcLang = "auto", tgtTxt = None, echoTxt = None, echo = False, kill = False, read = False):
		global CONN

		request = dict(Outgoing = outgoing, Channel = channel, User = user, Srctxt = text, Srclang = srcLang, Tgttxt = tgtTxt, Tgtlang = tgtLang, EchoTxt = echoTxt, Echo = echo, Kill = kill, Read = read)

		cls.connectToServer()
		
		jsonStr = json.dumps(request).encode("utf-8")
		CONN.send(jsonStr)

		return None
	translate = classmethod(translate)

	def readResults(cls, userdata = None):
		global TIMEOUT_HOOK
		global ACTIVE_JOBS
		global BUFFER

		request = dict(Outgoing = None, Channel = None, User = None, Srctxt = None, Srclang = None, Tgttxt = None, Tgtlang = None, EchoTxt = None, Echo = False, Kill = False, Read = True)
		jsonStr = json.dumps(request).encode("utf-8")

		CONN.send(jsonStr)
		result = json.loads(CONN.recv(BUFFER_SIZE).decode("utf-8"))

		if type(result) == dict:
			if result["Outgoing"]:
				xchat.command("say " + result["Tgttxt"].encode("utf-8"))
			elif result["Srctxt"] != result["Tgttxt"]:
				context = xchat.find_context(channel=result["Channel"])
				str = result["Tgttxt"].encode("utf-8")
				context.emit_print("Channel Message", "_[%s]" %(result["User"]), str)
				
			ACTIVE_JOBS -= 1
			
		if ACTIVE_JOBS <= 0:
			xchat.unhook(TIMEOUT_HOOK)
			TIMEOUT_HOOK = None
			
			cls.closeConnection()

		return None
	readResults = classmethod(readResults)

	def connectToServer(cls, ip = SERVER_IP, port = SERVER_PORT):
		global CONN

		if CONN is None:
			CONN = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
			CONN.connect((ip, port))

		return None
	connectToServer = classmethod(connectToServer)

	def checkConnection(cls, status):
		r, w, e = select.select([CONN,], [CONN,], [CONN,], 0)

		if status == "r":
{'fraction_non_alphanumeric': 0.20573072773048795, 'fraction_numerical': 0.006234264476681453, 'mean_word_length': 3.6629401900503074, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '3002860', 'n_tokens_mistral': 4046, 'n_tokens_neox': 3733, 'n_words': 773}