<commit_before>#!/usr/bin/env python3
#
# Google Translate Bot for Telegram
#
# @author  <NAME>
# @date    14.02.2017

import sys

if sys.version_info < (3, 6):
    raise Exception("This script requires Python 3.6 or higher.")

import telebot
import logging
import json
import html
import re
import os

from pathlib import Path
from google.cloud import translate

# The name of the bot on Telegram
BOT_NAME = 'gtranslatebot'

telegram_token_file = 'telegram_token'
google_app_credentials_file = 'google_app_credentials.json'

log = logging.getLogger('gtranslatebot.main')
# log.setLevel(logging.NOTSET)

if not Path(telegram_token_file).is_file():
    raise Exception(f"No {telegram_token_file} file found.")

with open('telegram_token') as f:
    global token
    token = f.read().strip()

if not Path(google_app_credentials_file).is_file():
    raise Exception(f"No {google_app_credentials_file} file found.")

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = google_app_credentials_file


# Compiled patterns
start_pattern = re.compile(rf'^\/start(@{BOT_NAME})?$', re.IGNORECASE)
help_pattern = re.compile(rf'^\/help(@{BOT_NAME})?$', re.IGNORECASE)
translation_with_arg_pattern = re.compile(rf'^\/translate(@{BOT_NAME})? (?P<text>.+)', re.IGNORECASE)
translation_this_pattern = re.compile(r'^translate this$', re.IGNORECASE)
translation_pattern = re.compile(rf'^\/translate(@{BOT_NAME})?$', re.IGNORECASE)
custom_translation_inline_pattern = re.compile(
        r'^(?P<source>\w{2,3}(-\w{2})?) (->|to) (?P<target>\w{2,3}(-\w{2})?):\s{1,2}(?P<text>[^$]+)',
        re.IGNORECASE)
custom_translation_pattern = re.compile(
        r'^(?P<source>\w{2,3}(-\w{2})?) (->|to) (?P<target>\w{2,3}(-\w{2})?)',
        re.IGNORECASE)
detect_lang_pattern = re.compile(r'^detect lang(uage)?$', re.IGNORECASE)
code_for_lang_pattern = re.compile(r'^code for (?P<language>\w+( [\(\)\w]+)?)$', re.IGNORECASE)
valid_lang_codes_pattern = re.compile(rf'^\/getvalidlangcodes(@{BOT_NAME})?$', re.IGNORECASE)


bot = telebot.TeleBot(token)
client = translate.Client()

log.info("Running Google Translate Bot")

def reply_message_has_text(message):
    return message.reply_to_message != None and \
            message.reply_to_message.text != None

def report_error(message, error):
    error_str = str(error)
    error_msg = "Error: "

    if "Bad language pair" in error_str:
        error_msg += "bad language pair."<commit_msg>Fix problem with a pattern<commit_after>#!/usr/bin/env python3
#
# Google Translate Bot for Telegram
#
# @author  <NAME>
# @date    14.02.2017

import sys

if sys.version_info < (3, 6):
    raise Exception("This script requires Python 3.6 or higher.")

import telebot
import logging
import json
import html
import re
import os

from pathlib import Path
from google.cloud import translate

# The name of the bot on Telegram
BOT_NAME = 'gtranslatebot'

telegram_token_file = 'telegram_token'
google_app_credentials_file = 'google_app_credentials.json'

log = logging.getLogger('gtranslatebot.main')
# log.setLevel(logging.NOTSET)

if not Path(telegram_token_file).is_file():
    raise Exception(f"No {telegram_token_file} file found.")

with open('telegram_token') as f:
    global token
    token = f.read().strip()

if not Path(google_app_credentials_file).is_file():
    raise Exception(f"No {google_app_credentials_file} file found.")

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = google_app_credentials_file


# Compiled patterns
start_pattern = re.compile(rf'^\/start(@{BOT_NAME})?$', re.IGNORECASE)
help_pattern = re.compile(rf'^\/help(@{BOT_NAME})?$', re.IGNORECASE)
translation_with_arg_pattern = re.compile(rf'^\/translate(@{BOT_NAME})? (?P<text>.+)', re.IGNORECASE)
translation_this_pattern = re.compile(r'^translate this$', re.IGNORECASE)
translation_pattern = re.compile(rf'^\/translate(@{BOT_NAME})?$', re.IGNORECASE)
custom_translation_inline_pattern = re.compile(
        r'^(?P<source>\w{2,3}(-\w{2})?) (->|to) (?P<target>\w{2,3}(-\w{2})?):\s{1,2}(?P<text>[^$]+)',
        re.IGNORECASE)
custom_translation_pattern = re.compile(
        r'^(?P<source>\w{2,3}(-\w{2})?) (->|to) (?P<target>\w{2,3}(-\w{2})?)$',
        re.IGNORECASE)
detect_lang_pattern = re.compile(r'^detect lang(uage)?$', re.IGNORECASE)
code_for_lang_pattern = re.compile(r'^code for (?P<language>\w+( [\(\)\w]+)?)$', re.IGNORECASE)
valid_lang_codes_pattern = re.compile(rf'^\/getvalidlangcodes(@{BOT_NAME})?$', re.IGNORECASE)


bot = telebot.TeleBot(token)
client = translate.Client()

log.info("Running Google Translate Bot")

def reply_message_has_text(message):
    return message.reply_to_message != None and \
            message.reply_to_message.text != None

def report_error(message, error):
    error_str = str(error)
    error_msg = "Error: "

    if "Bad language pair" in error_str:
        error_msg += "bad language pair."
{'fraction_non_alphanumeric': 0.16459627329192547, 'fraction_numerical': 0.011180124223602485, 'mean_word_length': 5.201540436456996, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, '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': '576536', 'n_tokens_mistral': 1792, 'n_tokens_neox': 1757, 'n_words': 331}