<jupyter_start><jupyter_text><jupyter_code>#<NAME>

import nltk
import random
import json
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.linear_model import LogisticRegression, SGDClassifier
from sklearn.model_selection import train_test_split
with open('/content/BIG_CONFIG_.json', 'r') as f:
    BOT_CONFIG = json.load(f)

def match(text, example):#гибкая функция сравнения
    return nltk.edit_distance(text, example) / len(example) < 0.3 if len(example) >0 else False

def cleaner(text):#Функция чистки текста
    cleaned_text = ''
    for ch in text.lower():
        if ch in 'абвгдеёжзийклмнопрстуфхцчшщъыьэюя abcdefghijklmnopqrstuvwxyz':
            cleaned_text = cleaned_text + ch
    return cleaned_text

def get_intent(text):#Функция определения интента текста
    for intent in BOT_CONFIG['intents']:
      if 'examples' in BOT_CONFIG['intents'][intent]:
        for example in BOT_CONFIG['intents'][intent]['examples']:
            if match(cleaner(text), cleaner(example)):
                return intent
        
X=[]
y=[]
for intent in BOT_CONFIG['intents']:
    if 'examples' in BOT_CONFIG['intents'][intent]:
        X += BOT_CONFIG['intents'][intent]['examples']
        y += [intent for i in range(len(BOT_CONFIG['intents'][intent]['examples']))]
#Создаем обучающую выборку для ML-модели
vectorizer = CountVectorizer(preprocessor=cleaner, ngram_range=(1,3), stop_words = ['а','и'])
#Создаем векторайзер - объект для превращения текста в вектора
vectorizer.fit(X)
X_vect = vectorizer.transform(X)
#Обучаем векторайзер на нашей выборке
X_train_vect, X_test_vect, y_train, y_test = train_test_split(X_vect, y, test_size = 0.3)
#Разбиваем выборку на train и на test
sgd = SGDClassifier() #Создаем модель
sgd.fit(X_vect, y)
sgd.score(X_vect, y) #Смотрим качество классификации
def get_intent_by_model(text):#Функция, определяющая интент текста с помощью ML-модели
    return sgd.predict(vectorizer.transform([text]))[0]
def bot(text):#Функция бота
    intent=get_intent(text)    #1. Попытаться понять намерения сравнением по Левинштейну
    if intent is None:
        intent = get_intent_by_model(text)   #2. Понять намерения с помощью ML-модели
    return random.choice(BOT_CONFIG['intents'][intent]['responses']) 
#question = ''
#while question not in ['выход', 'выключайся', 'хватит!']:
      #question=input()
      #print("Бот: " + bot(question))
#!pip install python-telegram-bot --upgrade
import logging

from telegram import Update
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext

# Enable logging
logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
)

logger = logging.getLogger(__name__)


# Define a few command handlers. These usually take the two arguments update and
# context. Error handlers also receive the raised TelegramError object in error.
def start(update: Update, _: CallbackContext) -> None:
    """Send a message when the command /start is issued."""
    update.message.reply_text('Hi!')


def help_command(update: Update, _: CallbackContext) -> None:
    """Send a message when the command /help is issued."""
    update.message.reply_text('Help!')


def echo(update: Update, _: CallbackContext) -> None:
    """Echo the user message."""

    update.message.reply_text(bot(update.message.text))


def main() -> None:
    """Start the bot."""
    # Create the Updater and pass it your bot's token.
    updater = Updater("1796743771:AAGxgsPzCc2dT6GIsNJAnaN2fVzB8l4bQGg")

    # Get the dispatcher to register handlers
    dispatcher = updater.dispatcher

    # on different commands - answer in Telegram
    dispatcher.add_handler(CommandHandler("start", start))
    dispatcher.add_handler(CommandHandler("help", help_command))

    # on noncommand i.e message - echo the message on Telegram
    dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, echo))

    # Start the Bot
    updater.start_polling()

    # Run the bot until you press Ctrl-C or the process receives SIGINT,
    # SIGTERM or SIGABRT. This should be used most of the time, since
    # start_polling() is non-blocking and will stop the bot gracefully.
    updater.idle()
main()<jupyter_output>2021-03-28 13:15:22,484 - apscheduler.scheduler - INFO - Scheduler started
2021-03-28 13:29:25,269 - telegram.ext.updater - INFO - Received signal 2 (SIGINT), stopping...
2021-03-28 13:29:25,275 - apscheduler.scheduler - INFO - Scheduler has been shut down
{'fraction_non_alphanumeric': 0.1118026228050678, 'fraction_numerical': 0.017114914425427872, 'mean_word_length': 4.461165048543689, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 10, '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}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '653775', 'license': 'MIT', 'n_tokens_mistral': 1591, 'n_tokens_neox': 1601, 'n_words': 400, 'path': 'ChatBot.ipynb', 'repo_name': 'zalikvl25/github-slideshow', 'size': '4503'}