import argparse
from typing import List, Any, TYPE_CHECKING
import asyncio
import sys

if TYPE_CHECKING:
    from management_commands import Command

def underscore(s: str) -> str:
    return ''.join(['_' + c.lower() if c.isupper() else c for c in s]).lstrip('_')

def initialize_argument_parser(
    commands: List['Command'],
    handler_argument_name: str,
    **kwargs,
) -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(**kwargs)
    subparsers = parser.add_subparsers(title='commands')

    for command_obj in commands:
        command_obj.add_subparser(subparsers, handler_argument_name=handler_argument_name)

    return parser

def run_command(parser: argparse.ArgumentParser, handler_argument_name: str, args: List[str]) -> Any:
    arguments = vars(parser.parse_args(args=args))

    if handler_argument_name not in arguments:
        parser.print_usage()
        sys.exit(1)

    handler = arguments.pop(handler_argument_name)

    if asyncio.iscoroutinefunction(handler):
        return asyncio.run(handler(**arguments))
    else:
        return handler(**arguments)


# Вариант 27
# Из исходного текстового файла (Dostoevsky.txt) выбрать блок информации
# за 1857  год и поместить ее в новый текстовый файл.

import re

inp = open('Dostoevsky.txt', 'r', encoding="utf-8")
out = open('output.txt', 'w', encoding="utf-8")
all = inp.read()

block = re.search('1857 год[а-яА-ЯёЁ\W]*Христа.', all)[0]
out.write(block)
out.close()


# this file normalize the pure data
import pandas as pd
import numpy as np
import math

def normalize(srcName, targetName, mode):
    """
    normalize the pure data
    :param srcName: pure csv name
    :param targetName: norm csv name
    :param mode: the mode of normalize
    :return: None
    """
    dataDf = pd.read_csv(srcName, index_col=0)
    dataArr = np.array(dataDf)
    dataList = dataArr.tolist()
    dataNormList = []
    #  Nose – 0, Neck – 1, Right Shoulder – 2, Right Elbow – 3, Right Wrist – 4, Left Shoulder – 5, Left Elbow – 6,
    #  Left Wrist – 7, Right Hip – 8, Right Knee – 9, Right Ankle – 10, Left Hip – 11, Left Knee – 12, LAnkle – 13,
    #  Right Eye – 14, Left Eye – 15, Right Ear – 16, Left Ear – 17
    if mode == "angle":
        for line in dataList:
            normal_line = []
            # aspect ratio of torso
            min_x = min(line[2], line[4], line[10], line[16], line[18], line[22], line[24])
            max_x = max(line[2], line[4], line[10], line[16], line[18], line[22], line[24])
            min_y = min(line[3], line[5], line[11], line[17], line[19], line[23], line[25])
            max_y = max(line[3], line[5], line[11], line[17], line[19], line[23], line[25])
            aspect_ratio = (max_x - min_x) / (max_y - min_y)
            normal_line.append(aspect_ratio)
            # angle of knee-neck and the vertical line
            angle_left = math.degrees(math.atan2(line[24]-line[2], line[25]-line[3]))
            angle_right = math.degrees(math.atan2(line[18]-line[2], line[19]-line[3]))
            normal_line.append(angle_left)
            normal_line.append(angle_right)
            dataNormList.append(normal_line)
        dataNormDf = pd.DataFrame(dataNormList, columns=['ratio', 'LAngle', 'RAngle'])
        dataNormDf.to_csv(targetName)
    else:
        for line in dataList:
            if mode == "thigh_len":
                # the length of right thigh
                thigh = math.sqrt((line[16]-line[18])**2 + (line[17]-line[19])**2)
                if thigh == 0:
                    thigh = 0.01
                centerX = line[2]
                centerY = line[3]
                for i in range(len(line)):
                    if i % 2 == 0:
                        line[i] = (line[i]-centerX) / thigh
                    else:
                        line[i] = (line[i]-centerY) / thigh
            elif mode == "torso_box":
                # min_x, min_y, max_x, max_y of torso
                min_x = min(line[4], line[10], line[16], line[18], line[22], line[24])
                max_x = max(line[4], line[10], line[16], line[18], line[22], line[24])
                min_y = min(line[5], line[11], line[17], line[19], line[23], line[25])
                max_y = max(line[5], line[11], line[17], line[19], line[23], line[25])
                centerX = line[2]
                centerY = line[3]
                # calculate the normalize index
                x_diff = max_x - min_x
                y_diff = max_y - min_y
                if x_diff == 0:
                    x_diff = 0.01
                if y_diff == 0:
                    y_diff = 0.01
                for i in range(len(line)):
                    if i % 2 == 0:
                        line[i] = (line[i]-centerX) / x_diff
                    else:
                        line[i] = (line[i]-centerY) / y_diff
            elif mode == "none":
                centerX = line[2]
                centerY = line[3]
                for i in range(len(line)):
                    if i % 2 == 0:
                        line[i] = line[i] - centerX
                    else:
                        line[i] = line[i] - centerY
            dataNormList.append(line)

        dataNormDf = pd.DataFrame(dataNormList, columns=['0x', '0y', '1x', '1y', '2x', '2y', '3x', '3y', '4x', '4y',
                                                         '5x', '5y', '6x', '6y', '7x', '7y', '8x', '8y', '9x', '9y',
                                                         '10x', '10y', '11x', '11y', '12x', '12y', '13x', '13y'])
        dataNormDf.to_csv(targetName)

normalize("data/ADLPure.csv", "data/normalized/ADLNormAngle.csv", mode="angle")
normalize("data/ADLPure.csv", "data/normalized/ADLNormThigh.csv", mode="thigh_len")
normalize("data/ADLPure.csv", "data/normalized/ADLNormTorso.csv", mode="torso_box")
normalize("data/ADLPure.csv", "data/normalized/ADLNormNone.csv", mode="none")

normalize("data/FallPure.csv", "data/normalized/FallNormAngle.csv", mode="angle")
normalize("data/FallPure.csv", "data/normalized/FallNormThigh.csv", mode="thigh_len")
normalize("data/FallPure.csv", "data/normalized/FallNormTorso.csv", mode="torso_box")
normalize("data/FallPure.csv", "data/normalized/FallNormNone.csv", mode="none")

normalize("data/ADLPureTest.csv", "data/normalized/ADLNormAngleTest.csv", mode="angle")
normalize("data/ADLPureTest.csv", "data/normalized/ADLNormThighTest.csv", mode="thigh_len")
normalize("data/ADLPureTest.csv", "data/normalized/ADLNormTorsoTest.csv", mode="torso_box")
normalize("data/ADLPureTest.csv", "data/normalized/ADLNormNoneTest.csv", mode="none")

normalize("data/FallPureTest.csv", "data/normalized/FallNormAngleTest.csv", mode="angle")
normalize("data/FallPureTest.csv", "data/normalized/FallNormThighTest.csv", mode="thigh_len")
normalize("data/FallPureTest.csv", "data/normalized/FallNormTorsoTest.csv", mode="torso_box")
normalize("data/FallPureTest.csv", "data/normalized/FallNormNoneTest.csv", mode="none")


import os
from glob import glob
import sys
sys.path.insert(0, '../data/')
import matplotlib.pyplot as plt
from datacleaner import LightCurve
from results import BestLightCurve, MCMCResults, plot_star
import batman
import numpy as np

#results_dir = '/astro/store/scratch/tmp/bmmorris/stsp/kepler17/window229/run002/'

window_ind, run_ind = sys.argv[-2:]
results_dir = ('/astro/store/scratch/tmp/bmmorris/stsp/kepler17/window{0:03d}/run{1:03d}/'
               .format(int(window_ind), int(run_ind)))
#if not os.path.exists(results_dir):

print('Results from: {0}'.format(results_dir))
#results_dir = '/astro/store/scratch/tmp/bmmorris/stsp/kepler17/window101/run009/'
#results_dir = os.path.abspath('../condor/tmp/')

files_in_dir = glob(os.path.join(results_dir, '*.txt'))

for output_file in files_in_dir:
    if output_file.endswith('_errstsp.txt'):
        error_path = output_file
    elif output_file.endswith('_finalparam.txt'):
        final_params_path = output_file
    elif output_file.endswith('_lcbest.txt'):
        best_lc_path = output_file
    elif output_file.endswith('_mcmc.txt'):
        mcmc_path = output_file
    elif output_file.endswith('_parambest.txt'):

        best_params_path = output_file

window_dir = os.sep.join(results_dir.split(os.sep)[:-2])
mcmc_paths = glob(window_dir + '/run???/*_mcmc.txt')

try:
    print(best_lc_path)
except NameError:
    raise ValueError("{0} doesn't exist.".format(results_dir))

def get_basic_kepler17_params():
    # http://exoplanets.org/detail/HAT-P-11_b
    params = batman.TransitParams()
    params.t0 = 2455185.678035                       #time of inferior conjunction
    params.per = 1.4857108                     #orbital period
    params.rp = 0.13413993                      #planet radius (in units of stellar radii)
    b = 0.1045441
    #inclination = 88.94560#np.arccos(b/params.a)
    params.inc = 88.94560 #orbital inclination (in degrees)
    inclination = np.radians(params.inc)
    params.inclination = params.inc
    params.a = b/np.cos(inclination)                       #semi-major axis (in units of stellar radii)
    params.ecc = 0.                      #eccentricity
    params.w = 90.                       #longitude of periastron (in degrees)
    params.u = [0.1, 0.3]                #limb darkening coefficients
    params.limb_dark = "quadratic"       #limb darkening model
    params.duration = params.per/np.pi*np.arcsin(np.sqrt((1+params.rp)**2 + b**2)
                                                 / np.sin(inclination)/params.a)
    return params

transit_params = get_basic_kepler17_params()
blc = BestLightCurve(best_lc_path, transit_params=transit_params)
blc.plot_whole_lc()
blc.plot_transits()
#plt.show()

mcmc = MCMCResults(mcmc_paths)
mcmc.plot_chi2()
mcmc.plot_chains()
#mcmc.plot_star()
#mcmc.plot_corner()
#mcmc.plot_each_spot()
plt.show()


from django.urls import path
from rest_framework_simplejwt.views import token_obtain_pair

from .views import *

urlpatterns = [
    path('companies/', CompanyListAPIView.as_view()),
    path('companies/<int:pk>/', CompanyDetailAPIView.as_view()),
    path('companies/<int:pk>/vacancies/', company_vacancies),

    path('vacancies/', VacancyListAPIView.as_view()),
    path('vacancies/<int:pk>/', VacancyDetailAPIView.as_view()),
    path('vacancies/top_ten', vacanciesTopTen),

    path('login/', token_obtain_pair)
]


import numpy as np
import cv2
import gym

IMG_HEIGHT = 84
IMG_WIDTH = 64
STACK_SIZE = 16

env = gym.make('BreakoutNoFrameskip-v4')

print(env.action_space.n)

for D in range(10):
    env.reset()

    frames = [env.step(env.action_space.sample())[0] for _ in range(STACK_SIZE)]

    def preprocess_input(frames):
        observed = np.zeros(shape=(IMG_HEIGHT, IMG_WIDTH, STACK_SIZE))
        for ind, obs in enumerate(frames):
            # to gray and to 0-1
            obs = cv2.cvtColor(obs, cv2.COLOR_RGB2GRAY)
            obs = cv2.resize(obs, (IMG_WIDTH, IMG_HEIGHT))
            observed[..., ind] += obs
        return observed.astype(np.float32)

    state = preprocess_input(frames)

    [cv2.imwrite('%d-%d.png' % (D, i), state[..., i]) for i in range(STACK_SIZE)]


import random as rd
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from src.util import config
from scipy.sparse import vstack, coo_matrix, csr_matrix
from src.util.eval_implicit import eval_implicit, eval_implicit_NGCF

class NGCF(nn.Module):
    def __init__(self, n_user, n_item, dataset, norm_adj):
        super(NGCF, self).__init__()
        self.n_user = n_user
        self.n_item = n_item
        #self.device = config.device
        self.device = "cuda" if torch.cuda.is_available() else "cpu"
        self.emb_size = config.latent_dim
        self.batch_size = config.batch_size
        self.node_dropout = config.dropout
        self.mess_dropout = config.mess_dropout

        self.norm_adj = norm_adj

        self.layers = config.layers
        self.reg = config.regs
        self.bpr_decay = config.bpr_decay
        self.embedding_dict, self.weight_dict = self.init_weight()
        self.sparse_norm_adj = self._convert_sp_mat_to_sp_tensor(self.norm_adj).to(self.device)

        self.optimizer = config.optimizer(self.parameters(), lr=config.lr, weight_decay=self.reg)
        self.dataset = dataset

        self.to(self.device)

    def init_weight(self):
        # xavier init
        initializer = nn.init.xavier_uniform_

        embedding_dict = nn.ParameterDict({
            'user_emb': nn.Parameter(initializer(torch.empty(self.n_user,
                                                 self.emb_size))),
            'item_emb': nn.Parameter(initializer(torch.empty(self.n_item,
                                                 self.emb_size)))
        })

        weight_dict = nn.ParameterDict()
        layers = [self.emb_size] + self.layers #임베딩 [64] + 레이어 [64,64,64]
        for k in range(len(self.layers)): #w1, w2 두개
            weight_dict.update({'W_gc_%d'%k: nn.Parameter(initializer(torch.empty(layers[k],
                                                                      layers[k+1])))})
            weight_dict.update({'b_gc_%d'%k: nn.Parameter(initializer(torch.empty(1, layers[k+1])))})

            weight_dict.update({'W_bi_%d'%k: nn.Parameter(initializer(torch.empty(layers[k],
                                                                      layers[k+1])))})
            weight_dict.update({'b_bi_%d'%k: nn.Parameter(initializer(torch.empty(1, layers[k+1])))})

        return embedding_dict, weight_dict

    def _convert_sp_mat_to_sp_tensor(self, X):
        coo = X.tocoo()
        i = torch.LongTensor([coo.row, coo.col]) #0이 아닌 값이 있는 위치
        v = torch.from_numpy(coo.data).float() #0이 아닌 값들
        return torch.sparse.FloatTensor(i, v, coo.shape)

    def sparse_dropout(self, x, rate, noise_shape): #rate에 node dropout 들어감=0.1, noise shape은 0이 아닌 갯수..
        random_tensor = 1 - rate
        random_tensor += torch.rand(noise_shape).to(x.device)
        dropout_mask = torch.floor(random_tensor).type(torch.bool)
        i = x._indices()
        v = x._values()

        i = i[:, dropout_mask]
        v = v[dropout_mask] #boolean

        out = torch.sparse.FloatTensor(i, v, x.shape).to(x.device)
        return out * (1. / (1 - rate)) #숫자를 크게만들기... 그래디언트가 작게되니까 시그널 크기..올려줌....

    def create_bpr_loss(self, users, pos_items, neg_items): #bayesian personalized ranking
        pos_scores = torch.sum(torch.mul(users, pos_items), axis=1)
        neg_scores = torch.sum(torch.mul(users, neg_items), axis=1)

        maxi = nn.LogSigmoid()(pos_scores - neg_scores)

        mf_loss = -1 * torch.mean(maxi)

        # cul regularizer 같음
        regularizer = (torch.norm(users) ** 2
                       + torch.norm(pos_items) ** 2
                       + torch.norm(neg_items) ** 2) / 2

        emb_loss = self.bpr_decay * regularizer / self.batch_size

        return mf_loss + emb_loss, mf_loss, emb_loss

    def rating(self, u_g_embeddings, pos_i_g_embeddings):
        return torch.matmul(u_g_embeddings, pos_i_g_embeddings.t()) #matmul은 벡터와 메트릭스 자유롭게 계산

    def forward(self, users, pos_items, neg_items, drop_flag=False):
        #dropout한 adjacency matrix 엣지 유무
        A_hat = self.sparse_dropout(self.sparse_norm_adj,
                                    self.node_dropout,
                                    self.sparse_norm_adj._nnz()) if drop_flag else self.sparse_norm_adj #nnz는 행렬에 포함된 0이 아닌 요소의 갯수

        ego_embeddings = torch.cat([self.embedding_dict['user_emb'],
                                    self.embedding_dict['item_emb']], 0)

        all_embeddings = [ego_embeddings]

        for k in range(len(self.layers)):
            side_embeddings = torch.sparse.mm(A_hat, ego_embeddings)  #이웃정보 가져옴 = ei

            # transformed sum messages of neighbors.
            sum_embeddings = torch.matmul(side_embeddings, self.weight_dict['W_gc_%d' % k]) \
                                             + self.weight_dict['b_gc_%d' % k] #웨이트곱함 W1

            # bi messages of neighbors.
            # element-wise product
            bi_embeddings = torch.mul(ego_embeddings, side_embeddings) #원소별곱셈 그냥 아무것도 안한 자기 자신과 이웃정보 받은 인풋이랑...
            # transformed bi messages of neighbors.
            bi_embeddings = torch.matmul(bi_embeddings, self.weight_dict['W_bi_%d' % k]) \
                                            + self.weight_dict['b_bi_%d' % k] #W2

            # non-linear activation.
            ego_embeddings = nn.LeakyReLU(negative_slope=0.2)(sum_embeddings + bi_embeddings) #더한 값을 활성화 함수에 넣어주고

            # message dropout.
            ego_embeddings = nn.Dropout(self.mess_dropout[k])(ego_embeddings) #드랍아웃 함번 해주고

            # normalize the distribution of embeddings.
            norm_embeddings = F.normalize(ego_embeddings, p=2, dim=1)

            all_embeddings += [norm_embeddings] #각층마다 나온 결과 모아모아

        all_embeddings = torch.cat(all_embeddings, 1) #다 컨캣해버려 여프 = 피쳐의 종류가 늘어난 거지
        u_g_embeddings = all_embeddings[:self.n_user, :] #위에는 유저꺼~
        i_g_embeddings = all_embeddings[self.n_user:, :] #밑엥는 아이템꺼~

        """
        *********************************************************
        look up.
        """
        #print('i_g_embeddings - ' + str(len(i_g_embeddings)))
        #print('pos items - ' + str(pos_items))

        #print('user embeddings - ' + str(len(u_g_embeddings)))

        u_g_embeddings = u_g_embeddings[users, :]
        pos_i_g_embeddings = i_g_embeddings[pos_items, :]
        neg_i_g_embeddings = i_g_embeddings[neg_items, :]

        return u_g_embeddings, pos_i_g_embeddings, neg_i_g_embeddings

    def train_model_per_batch(self, users, pos_items, neg_items, users_in_owner):
        self.optimizer.zero_grad()
        u_g_embeddings, pos_i_g_embeddings, neg_i_g_embeddings = self.forward(users, pos_items, neg_items)
        batch_loss, batch_mf_loss, batch_emb_loss = self.create_bpr_loss(u_g_embeddings,
                                                                         pos_i_g_embeddings,
                                                                         neg_i_g_embeddings,
                                                                        )
        batch_loss.backward()
        self.optimizer.step()

        return batch_loss

    def fit(self):
        user_idx = np.arange(self.n_user)
        patience = 0
        best_score = 0.

        for epoch in range(config.epochs_local):
            epoch_loss = 0.
            self.train()

            np.random.RandomState(1234).shuffle(user_idx)
            batch_num = len(user_idx) // self.batch_size + 1

            for batch_idx in range(batch_num):
                try:
                    batch_users = user_idx[batch_idx*self.batch_size:(batch_idx+1)*self.batch_size]
                except:
                    batch_users = user_idx[batch_idx*self.batch_size:]

                batch_users, batch_pos_items, batch_neg_items, users_in_owner= \
                    self.sample_pair(batch_users) #Some users are empty because of test setting
                batch_loss = self.train_model_per_batch(batch_users, batch_pos_items, batch_neg_items, users_in_owner)

                if torch.isnan(batch_loss):
                    print('Loss NaN. Train finish.')
                    break

                epoch_loss += batch_loss

            '''
            if epoch % config.test_every == 0:
                with torch.no_grad():
                    self.eval()
                    top_k = config.top_k
                    #error!
                    ndcg, prec, recall = eval_implicit_NGCF(self, self, self.dataset["train"], self.dataset["valid"], top_k)

                    if epoch % config.print_every == 0:
                        print("[NGCF] epoch %d, loss: %f"%(epoch, epoch_loss))
                        print(f"(NGCF) prec@{top_k} {prec}, recall@{top_k} {recall}, ndcg@{top_k} {ndcg}")
                    self.train()

                    if config.early_stopping_policy == "recall":
                        if recall > best_score:
                            best_score = recall
                            patience = 0
                        else:
                            patience += 1
                    elif config.early_stopping_policy == "ndcg":
                        if ndcg > best_score:
                            best_score = ndcg
                            patience = 0
                        else:
                            patience += 1

            if config.use_early_stopping and patience > config.early_stopping_patience:
                #print(f"Early Stopping at epoch {epoch}")
                break
                '''

    def sample_pair(self, users):
        csr_adj_mat = csr_matrix(self.norm_adj)
        def sample_pos_items_for_u(u, num):
            # sample num pos items for u-th user
            # pos_items = self.train_items[u]
            pos_items = csr_adj_mat[u].nonzero()[1]
            n_pos_items = len(pos_items)
            pos_batch = []
            while True:
                if len(pos_batch) == num:
                    break
                pos_id = np.random.randint(low=0, high=n_pos_items, size=1)[0]
                pos_i_id = pos_items[pos_id] - self.n_user

                if pos_i_id not in pos_batch:
                    pos_batch.append(pos_i_id)
            return pos_batch

        def sample_neg_items_for_u(u, num):
            # sample num neg items for u-th user
            neg_items = []
            while True:
                if len(neg_items) == num:
                    break
                neg_id = np.random.randint(low=0, high=self.n_item,size=1)[0]
                if neg_id not in csr_adj_mat[u].nonzero()[1] and neg_id not in neg_items:
                    neg_items.append(neg_id)
            return neg_items

        sampled_users, pos_items, neg_items = [], [], []
        for u in users:
            try:
                new_pos = sample_pos_items_for_u(u, 1)
                sampled_users.append(u)
            except:
                new_pos = [0]
            new_neg = sample_neg_items_for_u(u, 1)
            # try:
            #     new_pos = sample_neg_items_for_u(u, 1)
            #     new_neg = sample_neg_items_for_u(u, 1)
            #     sampled_users.append(u)
            # except:
            #     new_pos = 0
            #     new_neg = 0
            pos_items += new_pos
            neg_items += new_neg
            # pos_items += sample_pos_items_for_u(u, 1)
            # neg_items += sample_neg_items_for_u(u, 1)
        return users, pos_items, neg_items, sampled_users
        #return users, pos_items, neg_items

    def predict(self, user_ids, item_ids):
        with torch.no_grad():
            u_g_embeddings, pos_embeddings, _ = self.forward(user_ids, 0, 0)
            scores = self.rating(u_g_embeddings, pos_embeddings)
            predict_ = scores.detach().cpu().numpy()

            #print('predict len - ' + str(len(predict_)))
            #print('item_ids len - ' + str(len(item_ids)))

            return predict_


from botbuilder.dialogs import (
    ComponentDialog,
    WaterfallDialog,
    WaterfallStepContext,
    DialogTurnResult,
)
from botbuilder.dialogs.prompts import TextPrompt, PromptOptions
from botbuilder.core import (
    MessageFactory,
    TurnContext,
    BotTelemetryClient,
    NullTelemetryClient,
)
from botbuilder.schema import InputHints

from booking_details import BookingDetails
from flight_booking_recognizer import FlightBookingRecognizer
from helpers.luis_helper import LuisHelper, Intent
from .booking_dialog import BookingDialog

class MainDialog(ComponentDialog):
    def __init__(
        self,
        luis_recognizer: FlightBookingRecognizer,
        booking_dialog: BookingDialog,
        telemetry_client: BotTelemetryClient = None,
    ):
        super(MainDialog, self).__init__(MainDialog.__name__)
        self.telemetry_client = telemetry_client or NullTelemetryClient()

        text_prompt = TextPrompt(TextPrompt.__name__)
        text_prompt.telemetry_client = self.telemetry_client

        booking_dialog.telemetry_client = self.telemetry_client

        wf_dialog = WaterfallDialog(
            "WFDialog", [self.intro_step, self.act_step, self.final_step]
        )
        wf_dialog.telemetry_client = self.telemetry_client

        self._luis_recognizer = luis_recognizer
        self._booking_dialog_id = booking_dialog.id

        self.add_dialog(text_prompt)
        self.add_dialog(booking_dialog)
        self.add_dialog(wf_dialog)

        self.initial_dialog_id = "WFDialog"

    async def intro_step(self, step_context: WaterfallStepContext) -> DialogTurnResult:
        if not self._luis_recognizer.is_configured:
            await step_context.context.send_activity(
                MessageFactory.text(
                    "NOTE: LUIS is not configured. To enable all capabilities, add 'LuisAppId', 'LuisAPIKey' and "
                    "'LuisAPIHostName' to the appsettings.json file.",
                    input_hint=InputHints.ignoring_input,
                )
            )

            return await step_context.next(None)
        message_text = (
            str(step_context.options)
            if step_context.options
            else "Can you tell me about your trip ?"
        )
        prompt_message = MessageFactory.text(
            message_text, message_text, InputHints.expecting_input
        )

        return await step_context.prompt(
            TextPrompt.__name__, PromptOptions(prompt=prompt_message)
        )

    async def act_step(self, step_context: WaterfallStepContext) -> DialogTurnResult:
        if not self._luis_recognizer.is_configured:
            # LUIS is not configured, we just run the BookingDialog path with an empty BookingDetailsInstance.
            return await step_context.begin_dialog(
                self._booking_dialog_id, BookingDetails()
            )

        # Call LUIS and gather any potential booking details. (Note the TurnContext has the response to the prompt.)
        intent, luis_result = await LuisHelper.execute_luis_query(
            self._luis_recognizer, step_context.context
        )

        if intent == Intent.BOOK_FLIGHT.value and luis_result:
            # Show a warning for Origin and Destination if we can't resolve them.
            await MainDialog._show_warning_for_unsupported_cities(
                step_context.context, luis_result
            )

            # Run the BookingDialog giving it whatever details we have from the LUIS call.
            return await step_context.begin_dialog(self._booking_dialog_id, luis_result)

        elif intent == Intent.GREETING.value:
            greeting_text = (
                "Hi !"
            )
            greeting_message = MessageFactory.text(
                greeting_text, greeting_text, InputHints.ignoring_input
            )
            await step_context.context.send_activity(greeting_message)
            prompt_message = "Can you tell me about your trip ?"
            return await step_context.replace_dialog(self.id, prompt_message)

        elif intent == Intent.GOODBYE.value:
            goodbye_text = (
                "Have a nice day !"
            )
            goodbye_message = MessageFactory.text(
                goodbye_text, goodbye_text, InputHints.ignoring_input
            )
            await step_context.context.send_activity(goodbye_message)

            close_text = (
                "This conversation is over. Retype something or refresh to restart."
            )
            close_message = MessageFactory.text(
                close_text, close_text, InputHints.ignoring_input
            )
            await step_context.context.send_activity(close_message)
            return await inner_dc.cancel_all_dialogs()

        elif intent == Intent.THANKYOU.value:
            thank_text = (
                "You're welcome."
            )
            thank_message = MessageFactory.text(
                thank_text, thank_text, InputHints.ignoring_input
            )
            await step_context.context.send_activity(thank_message)
            prompt_message = "Can you tell me about your trip ?"
            return await step_context.replace_dialog(self.id, prompt_message)

        else:
            # didnt_understand_text = (
            #     "Sorry, I didn't get that. Please try asking in a different way."
            # )
            # didnt_understand_message = MessageFactory.text(
            #     didnt_understand_text, didnt_understand_text, InputHints.ignoring_input
            # )
            # await step_context.context.send_activity(didnt_understand_message)
            prompt_message = "Sorry, I didn't get that. Please try asking in a different way."
            return await step_context.replace_dialog(self.id, prompt_message)

        return await step_context.next(None)

    async def final_step(self, step_context: WaterfallStepContext) -> DialogTurnResult:
        # If the child dialog ("BookingDialog") was cancelled or the user failed to confirm,
        # the Result here will be null.
        if step_context.result is not None:
            if (step_context.result):
                msg_txt = "This is (imaginary) booked !"
                message = MessageFactory.text(msg_txt, msg_txt, InputHints.ignoring_input)
                await step_context.context.send_activity(message)
                prompt_message = "Is there something else I can do for you?"
                return await step_context.replace_dialog(self.id, prompt_message)
            else:
                msg_txt = "Sorry that I didn't understand your request."
                message = MessageFactory.text(msg_txt, msg_txt, InputHints.ignoring_input)
                await step_context.context.send_activity(message)
                msg_txt = "I'm just a bot and i'm still in early development..."
                message = MessageFactory.text(msg_txt, msg_txt, InputHints.ignoring_input)
                await step_context.context.send_activity(message)
                prompt_message = "Can you explain me again your trip, like I'm a 3yo child ?"
                return await step_context.replace_dialog(self.id, prompt_message)

        return await step_context.end_dialog()

    @staticmethod
    async def _show_warning_for_unsupported_cities(
        context: TurnContext, luis_result: BookingDetails
    ) -> None:
        """
        Shows a warning if the requested From or To cities are recognized as entities but they are not in the Airport entity list.
        In some cases LUIS will recognize the From and To composite entities as a valid cities but the From and To Airport values
        will be empty if those entity values can't be mapped to a canonical item in the Airport.
        """
        if luis_result.unsupported_airports:
            message_text = (
                f"Sorry but the following airports are not supported:"
                f" {', '.join(luis_result.unsupported_airports)}"
            )
            message = MessageFactory.text(
                message_text, message_text, InputHints.ignoring_input
            )
            await context.send_activity(message)


N,M=4,1
L=[1,2,3,4]

ans = 0
nums = L
T=[]
# def search(T,i):
#     if i >= N-1:
#         return 0
#     if T[i] <= T[i+1]+M:
#         return 1 + DFS(i+1)

for i in range(N):
    T.append(nums.pop(nums(i)))
    ans += search(T,i)

print(ans)


import logging

from django.db.models import Sum, Case, When, IntegerField

from confession.models import AdminApprovedConfession
from confession.utils import DateTimeService, slack_notify
from rule.models import Rule

logger = logging.getLogger(__name__)

class SetPublishTimeService:
    @classmethod
    def check_instance(cls):
        approved_limit_rule = int(Rule.objects.get(slug='approve-confession').value)
        instances = AdminApprovedConfession.objects.filter(publish_date__isnull=True, user_approved=False).annotate(
            approved_count=Sum(
                Case(
                    When(confessionuserapprovement__vote=True, then=1),
                    When(confessionuserapprovement__vote=False, then=-1),
                    When(confessionuserapprovement=None, then=0),
                    output_field=IntegerField()
                )
            )
        ).filter(approved_count__gte=approved_limit_rule).order_by('-approved_count')

        logger.debug(f'Filtered confession queryset is: {instances}')

        return instances

    @classmethod
    def update(cls, instances, *args, **kwargs):
        if instances.exists():
            for confession in instances:
                publish_time = DateTimeService.get_random_time()
                confession.publish_date = publish_time
                confession.save()
                message = f'Confession with id: {confession.id} and approved votes: {confession.approved_count} updated with publish_time of {publish_time}'
                slack_notify(message=message)
                continue
            return f'Task executed with confessions: {instances}'
        else:
            message = 'There is no confession to publish'
            slack_notify(message=message)

            return f'Task executed with message: {message}'


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 26 04:30:52 2019

@author: dan
"""

from Colonel_Blotto_game import *
g = Game(5,3,100000)
#print(g.actions)
#print(g.get_utility(-5,-5))

a= time.time()
g.train()
training_result = g.history
b = time.time()

#p2.action_count.sum()

#plt.scatter([i for i in range(1,g.nr_actions+1)],p2.action_count)
#plt.scatter([i for i in range(1,g.nr_actions+1)],p1.action_count)

#plt.hist(p2.last_regret)
#plt.hist(p1.last_regret)


import json

from modules.job_sync.model.job_logger import consumer_logger
from modules.job_sync.model.job_worker import JobWorker

class AnalysisSectorWorker(JobWorker):
    @property
    def job_id(self):
        return 'analysis.sector'

    def handle(self, ch, method, properties, body):
        consumer_logger.info(f"[worker][analysis.sector] ____ start ____")
        data: dict = json.loads(body.decode("utf-8"))

        if not isinstance(data, dict):
            consumer_logger.error(f"[worker][analysis.sector] wrong message format")
            return

        payload = data.get('payload')

        if not isinstance(payload, dict):
            consumer_logger.error(f"[worker][analysis.sector] wrong payload format")
            return

        # KHÔNG CHỈ LÀM VỚI GP, MÀ PHẢI LÀM VỚI TOÀN BỘ CÁC CHỈ SỐ KHÁC CẦN TÍNH THEO KIỂU NÀY
        # Với mỗi một cổ phiếu trong sector, tính ra tỷ suất lợi nhuận gộp biên
        # 1. đi từng năm và tính bình quân của cả sector trong năm đó
        # 2. tính độ lệch chuẩn của tỷ suất này qua các năm

        sector_data = payload.get('sectorData')

        sector_data_result = [
            # Tổ chức dữ liệu của sector, cái này sẽ save xuống result {} bao gồm key: all_sector, theo từng mã
        ]

        sector: dict = sector_data.get('sector')
        infos: dict = sector_data.get('infos')

        if not isinstance(sector_data, dict) or not isinstance(sector, dict) or not isinstance(infos, dict):
            consumer_logger.error(f"[worker][analysis.sector] wrong sector_data format")
            return

        consumer_logger.info(f'{sector.get("industryName3")}')

        # # stockFi là list fi-indicator của cổ phiếu qua các năm
        # for code, stockFi in fiSector.items():
        #     if not isinstance(stockFi, list):
        #         break
        #
        #     listGPM = [
        #         fi['grossProfitMargin'] for fi in stockFi if
        #         isinstance(fi.get('grossProfitMargin'), str) and fi.get('grossProfitMargin').isnumeric()]
        #
        #     # Tính độ lệch chuẩn của list gross profit margin
        #
        #     for fi in stockFi:
        #         if not isinstance(fi, dict):
        #             break
        #
        #         # Lấy list các bản ghi indicator của ngành trong năm này
        #         # Tìm xem trong gpSectorData đã có giá trị trung bình của ngành ở năm này chưa,
        #         # Nếu chưa có thì: Tính giá trị trung bình của ngành trong năm này và lưu vào gpSectorData
        #         year = fi.get('year')


from math import exp
import json
import os

# Add all log probabilites of each word by eachother for each document
# for both spam and nonspam, then classify that document spam or nonspam
# based on which has the higher probability
def classify(folder, vocab, nonspam_word_probabilites, spam_word_probabilities):
    false_positives = 0
    true_positives = 0
    false_negatives = 0
    true_negatives = 0

    for filename in os.listdir('data/' + folder + '-test'):
        spam_probability = 0
        nonspam_probability = 0

        for line in open('data/'+ folder +'-test/' + filename):
            for token in line.split():
                if token in vocab:
                    spam_probability += spam_word_probabilities[token]
                    nonspam_probability += nonspam_word_probabilites[token]

        if (spam_probability > nonspam_probability):
            if (folder == 'spam'):
                true_positives += 1
            else:
                false_positives += 1
        else:
            if (folder == 'nonspam'):
                true_negatives += 1
            else:
                false_negatives += 1

    return false_positives, true_positives, false_negatives, true_negatives

# Load classifier data from json files and call classify on spam and nonspam test documents
# to get number of fp's, fn's, tp's, tn's and print results
if __name__ == '__main__':
    nonspam_word_probabilites = json.loads(open('nonspam-word-probabilities.json').read())
    spam_word_probabilities = json.loads(open('spam-word-probabilities.json').read())
    vocab = json.loads(open('vocab.json').read())

    nonspam_false_positives, nonspam_true_positives, nonspam_false_negatives, nonspam_true_negatives = classify('nonspam', vocab, nonspam_word_probabilites, spam_word_probabilities)
    spam_false_positives, spam_true_positives, spam_false_negatives, spam_true_negatives = classify('spam', vocab, nonspam_word_probabilites, spam_word_probabilities)

    false_positives = nonspam_false_positives + spam_false_positives
    true_positives = nonspam_true_positives + spam_true_positives
    false_negatives = nonspam_false_negatives + spam_false_negatives
    true_negatives = nonspam_true_negatives + spam_true_negatives

    print('\n        |  correct  |  not correct')
    print('   spam |   ', true_positives, '   |     ', false_positives)
    print('nonspam |   ', true_negatives, '   |     ', false_negatives)
    print('\nPrecision: ' + str(true_positives/(true_positives + false_positives)))
    print('Recall: ' + str(true_positives/(true_positives + false_negatives)))


#!/usr/bin/env python
""" Helper script to facilitate downloading data from LOSC

Usage: To download the GW150914 data from https://losc.ligo.org/events/

$ python get_LOSC_event_data -e GW150914

"""

import numpy as np
import os
import argparse

parser = argparse.ArgumentParser(description='Script to download LOSC data.')
parser.add_argument('-e', '--event', metavar='event', type=str)
parser.add_argument('-o', '--outdir', metavar='outdir',
                    default='tutorial_data')

args = parser.parse_args()

url_dictionary = dict(
    GW150914="https://losc.ligo.org/s/events/GW150914/{}-{}1_LOSC_4_V2-1126259446-32.txt.gz",
    LVT151012="https://losc.ligo.org/s/events/LVT151012/{}-{}1_LOSC_4_V2-1128678884-32.txt.gz",
    GW151226="https://losc.ligo.org/s/events/GW151226/{}-{}1_LOSC_4_V2-1135136334-32.txt.gz",
    GW170104="https://losc.ligo.org/s/events/GW170104/{}-{}1_LOSC_4_V1-1167559920-32.txt.gz",
    GW170608="https://losc.ligo.org/s/events/GW170608/{}-{}1_LOSC_CLN_4_V1-1180922478-32.txt.gz",
    GW170814="https://dcc.ligo.org/public/0146/P1700341/001/{}-{}1_LOSC_CLN_4_V1-1186741845-32.txt.gz",
    GW170817="https://dcc.ligo.org/public/0146/P1700349/001/{}-{}1_LOSC_CLN_4_V1-1187007040-2048.txt.gz")

outdir = 'tutorial_data'

data = []
for det, in ['H', 'L']:
    url = url_dictionary[args.event].format(det, det)
    filename = os.path.basename(url)
    if os.path.isfile(filename.rstrip('.gz')) is False:
        print("Downloading data from {}".format(url))
        os.system("wget {} ".format(url))
        os.system("gunzip {}".format(filename))
        filename = filename.rstrip('.gz')
    data.append(np.loadtxt(filename))
    with open(filename, 'r') as f:
        header = f.readlines()[:3]
        event = header[0].split(' ')[5]
        detector = header[0].split(' ')[7]
        sampling_frequency = header[1].split(' ')[4]
        starttime = header[2].split(' ')[3]
        duration = header[2].split(' ')[5]
        print('Loaded data for event={}, detector={}, sampling_frequency={}'
              ', starttime={}, duration={}'.format(
                  event, detector, sampling_frequency, starttime, duration))
    os.remove(filename)

time = np.arange(0, int(duration), 1 / int(sampling_frequency)) + int(starttime)
arr = [time] + data

outfile = '{}/{}_strain_data.npy'.format(args.outdir, args.event)
np.save(outfile, arr)
print("Saved data to {}".format(outfile))


import pandas as pd
import sqlite3

# OBS: O DATASET FOI APAGA DEVIDO A UTILIZAÇÃO DO BANCO DE DADOS POSTGRES SENDO POSSÍVEL UTILIZAR O PRÓPRIO BANCO
# COMO DATASET
df_winedata = pd.read_csv('C:/Users/ACER/PycharmProjects/Bravinhos/Bravinhos_Pi/base/static/WineData.csv', sep=';')

wineDetails = df_winedata[['fixed acidity', 'volatile acidity', 'citric acid', 'residual sugar', 'chlorides',
                           'free sulfur dioxide', 'total sulfur dioxide', 'density', 'pH', 'sulphates', 'alcohol',
                           'quality']]

conn = sqlite3.connect('C:/Users/ACER/PycharmProjects/Bravinhos/Bravinhos_Pi/db.sqlite3')

cursor = conn.cursor()

for value, index in enumerate(wineDetails.values):
    cursor.execute(
        f'''INSERT INTO Wine_general_winedetails VALUES ({value+1}, {index[0]}, {index[1]}, {index[2]}, {index[3]}, {index[4]}, {index[5]},
         {index[6]}, {index[7]}, {index[8]}, {index[9]}, {index[10]}, {index[11]})''')
    conn.commit()

cursor.close()
conn.close()


# Databricks notebook source
# MAGIC %run ../../../bootstrap/bootstrap_base

# COMMAND ----------

import os
import IPython

# pylint: disable=wrong-import-position

if "DAIPE_BOOTSTRAPPED" not in os.environ:
    os.environ["DAIPE_BOOTSTRAPPED"] = "1"

if os.environ["APP_ENV"] == "dev":
    IPython.get_ipython().run_line_magic("load_ext", "autoreload")
    IPython.get_ipython().run_line_magic("autoreload", "2")

# COMMAND ----------

from daipecore.decorator.ContainerManager import ContainerManager
from daipecore.bootstrap.container_factory import create_container

def init_container():
    ContainerManager.set_container(create_container(os.environ["APP_ENV"]))

init_container()

# COMMAND ----------

from databricksbundle.notebook.helpers import get_notebook_path
from daipecore.container import container_refresher

if os.environ["APP_ENV"] == "dev" and "watcher_thread" not in globals():
    configs_dir = "/Workspace" + "/".join(get_notebook_path().split("/")[:6]) + "/_config"

    watcher_logger, watcher_thread = container_refresher.watch_configs(
        configs_dir, init_container
    )
    watcher_logger.print()


from utils.Database import Database
from config.State import State

class FindAnswer:

    # Database 인스턴스 객체로 생성
    def __init__(self, db):
        self.db = db

    def select_state(self, user_id):
        sql = 'SELECT * FROM members WHERE id=%s' % user_id
        print("sql:",  sql)
        return self.db.select_one(sql)

    def update_state(self, user_id, state):
        # SQL
        sql='''
            UPDATE members set m_state=%d WHERE id=%s
        ''' % (state, user_id)
        print("sql:", sql)

        # 쿼리 실행
        with self.db.cursor() as cursor:
            result = cursor.execute(sql)
            # DML의 execute() return값은 몇 개의 행을 변경했는지에 대한 총 count (정수)임
        print(result, '개 row update 성공!')

    # 답변 검색
    def search(self, intent_name=None, ner_tags=None):
        print("search까지 들어왔따")
        # 의도명, 개체명으로 답변 검색
        sql = self._make_query(intent_1=intent_name, ner_tags=ner_tags)
        answer = self.db.select_one(sql)
        print("답변 테이블 :", answer)
        print("❤intent_name:", intent_name)
        print("🧡ner_tags:", ner_tags)
        print("💛sql:", sql)
        print("💚answer:", answer)

        if intent_name == "리스트불러오기":
            sql = self.call_list(intent_name, None)
            answer = self.db.select_one(sql)

        # 검색되는 답변이 없으면 의도명만 검색
        elif answer is None:
            sql = self._make_query(intent_name, None)
            answer = self.db.select_one(sql)

        return (answer['answer'], answer['answer_contents'])

    # 답변 검색
    def reco_search(self, intent_1=None, intent_2=None, ner_tags=None):

        # 추천 2번문제 ~ 4번 문제
        if intent_1 == None:
            # locations 담을 리스트 변수 선언
            location_li = []
            sql = self._make_query(intent_1=None, intent_2=intent_2)
            answer = self.db.select_one(sql)

            # 추천 답변
            if intent_2 == 4:
                # location DB 검색 SQL
                print("State.q:", State.q)
                sql_table = self._make_location(State.q)
                print("sql_table:", sql_table)
                answer_locations = self.db.select_all(sql_table)

                # answer_locations type : dict in list
                for loca in answer_locations:
                    location_li.append([loca['metro'], loca['location']])

            return (answer['answer'], location_li)

        # 1번 문제
        sql = self._make_query(intent_1, intent_2)
        answer = self.db.select_one(sql)

        # print("FindAnswer sql:", sql)
        # print("FindAnswer answer:", answer)

        return (answer['answer'], answer['answer_contents'])

    # 검색 쿼리 생성
    def _make_query(self, intent_1=None, intent_2=None, ner_tags=None):
        sql = "select * from answer"

        # 추천 1번 문제
        if intent_1 != None and intent_2 != None and ner_tags == None:
            sql = sql + " where intent_1 ='{}' and intent_2='{}' ".format(intent_1, intent_2)
            print("_make_query sql:", sql)

        # 추천 2번 문제 ~ 4번 문제
        elif intent_1 == None and intent_2 != None and ner_tags == None:
            sql = sql + " where intent_2='{}' ".format(intent_2)
            print("_make_query sql:", sql)

        # intent_name 만 주어진 경우
        elif intent_1 != None and intent_2 != None and ner_tags == None:
            pass

        # 도움말, 리스트뽑기, 리스트 삭제
        elif intent_1 != None and intent_2 == None and ner_tags == None:
            print('make_query:', intent_1)
            sql = sql + " where intent_1='{}' ".format(intent_1)
            print("_make_query sql:", sql)
            return sql

        # intent_name 과 개체명도 주어진 경우
        elif intent_1 != None and ner_tags != None:
            where = ' where intent_1="%s" ' % intent_1

            if intent_1 == '길찾기':
                where += " ner like '%{}%' or ".format(ner_tags[1])
                where = where[:-3] + ')'

            elif (len(ner_tags) > 0):
                where += 'and ('
                for ne in ner_tags:
                    where += " ner like '%{}%' or ".format(ne)
                where = where[:-3] + ')'

            sql = sql + where
            print("_make_query sql:", sql)

        # 동일한 답변이 2개 이상인 경우, 랜덤으로 선택
        sql = sql + " order by rand() limit 1"

        return sql

    def _make_location(self, q):
        sql = "select * from location"
        sql += " where q = '{}' ".format(q)
        sql += " order by rand() limit 3"
        return sql

    # NER 태그를 실제 입력된 단어로 변환
    def tag_to_word(self, ner_predicts, answer):
        loc_list=[]

        for word, tag in ner_predicts:
            if "가는 길" in answer:
                loc_list.append(word)
            # 변환해야하는 태그가 있는 경우 추가
            elif tag == 'B_location' or tag == 'B_highway':
                answer = answer.replace(tag, word)  # 태그를 입력된 단어로 변환

        if "가는 길" in answer:
            answer = answer.replace("B_location", loc_list[1])
            print('loc_list: ',loc_list)
            print('loc_list[1]: ',loc_list[1])
            print('answer: 희지희즈히지흐지희짖 ',answer)
        answer = answer.replace('{', '')
        answer = answer.replace('}', '')

        print("tag_to_word answer : ", answer)

        return answer

    # 리스트 불러오기 쿼리문 생성
    def call_list(self, intent_1=None, intent_2=None, ner_tags=None):
        sql = "select * from member_location where m_idx = 1;"
        return sql

    # 리스트 삭제하기 쿼리문 생성
    def call_delete(self, intent_1=None, intent_2=None, ner_tags=None):
        sql = "DELETE FROM member_location WHERE m_idx = 1 AND location_list = '경기도';"
        return sql

    # 추천 태그 없애기
    def reco_to_word(self, reconame, answer):

        if reconame == '차':
            answer = answer.replace('way', reconame)
        if reconame=='뚜벅이':
            answer = "걷는 거 좋지! 어느 계절에 여행갈 계획이야?"
        elif reconame == '봄' or reconame == '여름' or reconame == '가을' or reconame == '겨울':
            answer = answer.replace('season', reconame)
        if reconame == '도시' or reconame == '자연':
            answer = answer.replace('city_nature', reconame)

        answer = answer.replace('{', '')
        answer = answer.replace('}', '')

        print("reco_to_word answer : ", answer)

        return answer


import telebot
from telebot import types
from select_db import get_db

bot = telebot.TeleBot('6083360054:AAF4bHUzjdkWyXNxWTFmTmMCDOx1l0ipY2M')

@bot.message_handler(commands=['start'])
def start(message):

    markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
    btn1 = types.KeyboardButton("👋 Поздороваться")
    markup.add(btn1)
    bot.send_message(message.from_user.id, "👋 Привет! Я твой бот-помошник!", reply_markup=markup)

@bot.message_handler(content_types=['text'])
def get_text_messages(message):

    if message.text == '👋 Поздороваться':
        markup = types.ReplyKeyboardMarkup(resize_keyboard=True) #создание новых кнопок
        btn1 = types.KeyboardButton('от 1000')
        btn2 = types.KeyboardButton('от 1000')
        btn3 = types.KeyboardButton('от 2000')
        btn4 = types.KeyboardButton('от 3000')
        markup.add(btn1, btn2, btn3, btn4)
        bot.send_message(message.from_user.id, '❓ Выберете сложность задания', reply_markup=markup) # ответ бота

    elif message.text == 'от 0':
        db = get_db(0)
        for i in range(10):
            bot.send_message(message.from_user.id, f"{db[i]}", parse_mode='Markdown')

    elif message.text == 'от 1000':
        db = get_db(1000)
        for i in range(10):
            bot.send_message(message.from_user.id, f"{db[i]}", parse_mode='Markdown')

    elif message.text == 'от 2000':
        db = get_db(2000)
        for i in range(10):
            bot.send_message(message.from_user.id, f"{db[i]}", parse_mode='Markdown')

    elif message.text == 'от 3000':
        db = get_db(3000)
        for i in range(10):
            bot.send_message(message.from_user.id, f"{db[i]}", parse_mode='Markdown')

bot.polling(none_stop=True, interval=0) #обязательная для работы бота часть


from math import floor
T = int(input())
score = ['A+','A0','A-','B+','B0','B-','C+','C0','C-','D']
c= {}
sum = 0
for i in range(T):
    a, b = map(int,input().split())
    for j in range(a):
        d = list(map(int,input().split()))
        c[j+1] = (d[0]*35+d[1]*45+d[2]*20)/100
    d = sorted(c.values(),reverse=True)
    for k in range(len(d)):
        if d[k] == c[b]:
            print(f"#{i+1} {score[floor(k/(a/10))]}")

3. 알고리즘/파이썬/2주차 모의고사/문제5.py


import pandas as pd
import numpy as np
import nltk

from .data_processing import  clean_tweet
from my_libs.twitter_api import search_more_than_100_tweet, search_tweets
from trainingBase.models import TrainingBase, TrainingBaseAdvanced
#from myLibs.training_base import training_base_advanced, training_base_simple

list_stopwords_portuguese = nltk.corpus.stopwords.words('portuguese')
np.transpose(list_stopwords_portuguese)

nltk.download('stopwords')
nltk.download('rslp')

# Função para remover stop words (palavras não significativas para a análise)
def remove_stop_words(texto):
    frases = []
    for palavras in texto:
        # Criamos uma list compreheension para extrair apenas as palavras que não estão na lista_Stop
        semStop = [ p for p in palavras.split() if p not in list_stopwords_portuguese]
        # Inserindo as frases com os Labels (sentimento) já tratadas pela Lista_Stop
        frases.append(semStop)
    return frases

# Diminui a palavra somente para o seu radical (EX: Livro, Livros, Livrinho, Livrão -> Livr)
#também já remover as stop words... A função especifica de remover stop words, no momento não esta sendo usada.
def apply_stmmer(texto):
    stemmer = nltk.stem.RSLPStemmer()
    # Escolhido o RSLPS pois é especifico da lingua portuguesa.
    frases_sem_Stemming = []
    for (palavras, sentimento) in texto:
        com_Stemming = [str(stemmer.stem(p)) for p in palavras.split() if p not in list_stopwords_portuguese]
        frases_sem_Stemming.append((com_Stemming, sentimento))
    return frases_sem_Stemming

# Retorna lista com todas as plavras.
def search_words(frases):
    todas_Palavras = []
    for (palavras, sentimento) in frases:
        todas_Palavras.extend(palavras)
    return todas_Palavras

# Busca a frequencia em que as palavras ocorrem.
def frequency_words(palavras):
    palavras = nltk.FreqDist(palavras)
    return palavras

# Remove a repetição das palavras, deixando somente uma palavra aparecer uma vez
def unique_words(frequencia):
    freq = frequencia.keys()
    return freq

def word_extractor(documento):
    # Utilizado set() para associar a variavel doc com o parâmetro que esta chegando
    global palavras_unicas_treinamento
    doc = set(documento)
    caracteristicas = {}
    for palavras in palavras_unicas_treinamento:
        caracteristicas['%s' % palavras] = (palavras in doc)
    return caracteristicas

# Função para analisar o tweet e retornar o provavel sentimento e suar porcentagem de precisão.
def analyze_tweet(tweet, classificador):
    try:
        testeStemming = []
        stemmer = nltk.stem.RSLPStemmer()

        for (palavras_treinamento) in tweet.split():
            comStem = [palavra for palavra in palavras_treinamento.split()]
            testeStemming.append(str(stemmer.stem(comStem[0])))

        novo = word_extractor(testeStemming)

        distribuicao = classificador.prob_classify(novo)

        probable_feeling_bigger = 0

        for classe in distribuicao.samples():
            probable_feeling = distribuicao.prob(classe)
            if probable_feeling > probable_feeling_bigger:
                probable_feeling_bigger = probable_feeling
                classe_feeling = classe

        felling = classe_feeling, probable_feeling_bigger
        return felling
    except:
        print('deu erro na analise')

# Função que retorna o classificador que sera utilizado para fazer as analises.
# O Classificador é montado com a base de treinamento.
def inicialize(option):
    global palavras_unicas_treinamento

    # if/else utilizado para utilizar o banco de dados da base de treinamento a avançada ou simples
    if option == 'simple': queryset = TrainingBase.objects.all()
    else: queryset = TrainingBaseAdvanced.objects.all()

    querysetvalues = queryset.values('texto', 'sentimento')
    training_base_list = list(querysetvalues)
    training_base_list2 = []
    for dict in training_base_list:
        training_base_list2.append(dict.values())

    frases_com_Stem_treinamento = apply_stmmer(training_base_list2)

    palavras_treinamento = search_words(frases_com_Stem_treinamento)
    frequencia_treinamento = frequency_words(palavras_treinamento)
    palavras_unicas_treinamento = unique_words(frequencia_treinamento)
    base_completa_treinamento = nltk.classify.apply_features(word_extractor, frases_com_Stem_treinamento)
    classificador = nltk.NaiveBayesClassifier.train(base_completa_treinamento)
    return classificador

def create_dict_training(user, options):
    # retorna o classificador que foi criado com base na base de treinamento
    # e sera utilizado como parametro para a função analyze_tweet
    classificador = inicialize(options['type_of_analysis'])

    # Coleta os tweets e os adiciona em uma lista de dicionarios
    if int(options['number_of_tweets']) <= 100:
        all_tweets, locations = search_tweets(user, options)
    else:
        all_tweets, locations = search_more_than_100_tweet(user, options)

    # Adiciona tweet_clean e tweet_analise ao dicionario feito na coleta de tweets.
    for tweet in all_tweets:
        try:
            tweet_clean = clean_tweet(tweet['tweet_text'])
            analise = analyze_tweet(tweet_clean, classificador)
            tweet['tweet_clean'] = tweet_clean
            tweet['tweet_analise'] = analise
        except:
            print('Error dict')

    return all_tweets, locations

# Função para digitar uma frase e analisar seu sentimento (Não coleta do twitter).
def analyze_test_phrase(phrase, option):

    classificador = inicialize(option)
    sentiment = analyze_tweet(phrase, classificador)

    return sentiment


with open("input","r") as file: input = [row for row in file.read().strip().split('\n\n')]

pairs = [eval(list_pair) for list_pairs in input for list_pair in list_pairs.split('\n')]
div1, div2 = [[2]], [[6]]
pairs.extend((div1, div2))

#Returns 0 if equal, -n if lower and +n if greater
def right_order(n1,n2):
    if type(n1) == int and type(n2) == int: return n2-n1
    if type(n1) != list : n1=[n1]
    if type(n2) != list : n2=[n2]
    for nn1,nn2 in zip(n1,n2):
        comp = right_order(nn1,nn2)
        if comp != 0 : return comp
    if len(n1) < len(n2) : return 1
    if len(n1) > len(n2) : return -1
    return 0

for i in range(len(pairs),0,-1):
    for j in range(i-1):
        if right_order(pairs[j],pairs[j+1]) < 0 :
            pairs[j], pairs[j+1] = pairs[j+1], pairs[j]

print("Decoder key for the distress signal:",(pairs.index(div1)+1)*(pairs.index(div2)+1))


"""
circle of words list

>>> q = queue107()
>>> q.enqueue('b')
>>> q.enqueue('a')
>>> q.enqueue('c')
>>> print(q)
['b', 'a', 'c']
>>> cowl(q)
['b', 'a', 'c']
['a', 'c', 'b']
['c', 'b', 'a']

"""

from queue107 import *

def cowl(q) -> list:
  assert type(q) == type(queue107())
  if q == q.empty():
    print(q)
  else:
    for i in range(q.len()):
      print(q)
      q.enqueue(q.front())
      q.dequeue()

if __name__ == "__main__":
    import doctest
    doctest.testmod()
    print("doctests completed")


import os
import time
import sys
import seiscomp3.System
import seiscomp3.Client

def collectParams(container):
    params = {}
    for i in range(container.groupCount()):
        params.update(collectParams(container.group(i)))
    for i in range(container.structureCount()):
        params.update(collectParams(container.structure(i)))
    for i in range(container.parameterCount()):
        p = container.parameter(i)
        if p.symbol.stage == seiscomp3.System.Environment.CS_UNDEFINED:
            continue
        params[p.variableName] = ",".join(p.symbol.values)

    return params

def collect(idset, paramSetID):
    paramSet = seiscomp3.DataModel.ParameterSet.Find(paramSetID)
    if not paramSet:
        return
    idset[paramSet.publicID()] = 1
    if not paramSet.baseID():
        return
    collect(idset, paramSet.baseID())

def sync(paramSet, params):
    obsoleteParams = []
    seenParams = {}
    i = 0
    while i < paramSet.parameterCount():
        p = paramSet.parameter(i)
        if p.name() in params:
            if p.name() in seenParams:
                # Multiple parameter definitions with same name
                sys.stderr.write(
                    "- %s:%s / duplicate parameter name\n" % (p.publicID(), p.name()))
                p.detach()
                continue
            seenParams[p.name()] = 1
            val = params[p.name()]
            if val != p.value():
                p.setValue(val)
                p.update()
        else:
            obsoleteParams.append(p)
        i = i + 1

    for p in obsoleteParams:
        p.detach()

    for key, val in list(params.items()):
        if key in seenParams:
            continue
        p = seiscomp3.DataModel.Parameter.Create()
        p.setName(key)
        p.setValue(val)
        paramSet.add(p)

class ConfigDBUpdater(seiscomp3.Client.Application):
    def __init__(self, argc, argv):
        seiscomp3.Client.Application.__init__(self, argc, argv)
        self.setLoggingToStdErr(True)
        self.setMessagingEnabled(True)
        self.setDatabaseEnabled(True, True)
        self.setAutoApplyNotifierEnabled(False)
        self.setInterpretNotifierEnabled(False)
        self.setMessagingUsername("_sccfgupd_")
        self.setLoadConfigModuleEnabled(True)
        # Load all configuration modules
        self.setConfigModuleName("")
        self.setPrimaryMessagingGroup(
            seiscomp3.Communication.Protocol.LISTENER_GROUP)

        self._outputFile = ""
        self._keyDir = None

    def createCommandLineDescription(self):
        self.commandline().addGroup("Input")
        self.commandline().addStringOption("Input", "key-dir",
                                           "Overrides the location of the default key directory ($SEISCOMP_ROOT/etc/key)")
        self.commandline().addGroup("Output")
        self.commandline().addStringOption("Output", "output,o",
                                           "If given, an output XML file is generated")

    def validateParameters(self):
        try:
            self._outputFile = self.commandline().optionString("output")
            # Switch to offline mode
            self.setMessagingEnabled(False)
            self.setDatabaseEnabled(False, False)
        except:
            pass

        try:
            self._keyDir = self.commandline().optionString("key-dir")
        except:
            pass

        return True

    def init(self):
        if not seiscomp3.Client.Application.init(self):
            return False

        # Initialize the basic directories
        filebase = seiscomp3.System.Environment.Instance().installDir()
        descdir = os.path.join(filebase, "etc", "descriptions")

        # Load definitions of the configuration schema
        defs = seiscomp3.System.SchemaDefinitions()
        if not defs.load(descdir):
            sys.stderr.write("Error: could not read descriptions\n")
            return False

        if defs.moduleCount() == 0:
            sys.stderr.write("Warning: no modules defined, nothing to do\n")
            return False

        # Create a model from the schema and read its configuration including
        # all bindings.
        model = seiscomp3.System.Model()
        if self._keyDir:
            model.keyDirOverride = self._keyDir
        model.create(defs)
        model.readConfig()

        # Find all binding mods for trunk. Bindings of modules where standalone
        # is set to true are ignored. They are supposed to handle their bindings
        # on their own.
        self.bindingMods = []
        for i in range(defs.moduleCount()):
            mod = defs.module(i)
            # Ignore stand alone modules (eg seedlink, slarchive, ...) as they
            # are not using the trunk libraries and don't need database
            # configurations
            if mod.isStandalone():
                continue
            self.bindingMods.append(mod.name)

        if len(self.bindingMods) == 0:
            sys.stderr.write(
                "Warning: no usable modules found, nothing to do\n")
            return False

        self.stationSetups = {}

        # Read bindings
        for m in self.bindingMods:
            mod = model.module(m)
            if not mod:
                sys.stderr.write("Warning: module %s not assigned\n" % m)
                continue
            if len(mod.bindings) == 0:
                continue

            # Rename global to default for being compatible with older
            # releases
            if m == "global":
                m = "default"

            sys.stderr.write("+ %s\n" % m)
            for staid in list(mod.bindings.keys()):
                binding = mod.getBinding(staid)
                if not binding:
                    continue
                # sys.stderr.write("  + %s.%s\n" % (staid.networkCode, staid.stationCode))
                params = {}
                for i in range(binding.sectionCount()):
                    params.update(collectParams(binding.section(i)))
                key = (staid.networkCode, staid.stationCode)
                if not key in self.stationSetups:
                    self.stationSetups[key] = {}
                self.stationSetups[key][m] = params
            sys.stderr.write("  + read %d stations\n" %
                             len(list(mod.bindings.keys())))

        return True

    def send(self, *args):
        '''
        A simple wrapper that sends a message and tries to resend it in case of
        an error.
        '''
        while not self.connection().send(*args):
            sys.stderr.write("Warning: sending failed, retrying\n")
            time.sleep(1)

    def run(self):
        '''
        Reimplements the main loop of the application. This methods collects
        all bindings and updates the database. It searches for already existing
        objects and updates them or creates new objects. Objects that is didn't
        touched are removed. This tool is the only one that should writes the
        configuration into the database and thus manages the content.
        '''
        config = seiscomp3.Client.ConfigDB.Instance().config()
        if config is None:
            config = seiscomp3.DataModel.Config()

        configMod = None
        obsoleteConfigMods = []

        if not self._outputFile:
            moduleName = self.name()
            seiscomp3.DataModel.Notifier.Enable()
        else:
            moduleName = "trunk"

        configID = "Config/%s" % moduleName

        for i in range(config.configModuleCount()):
            if config.configModule(i).publicID() != configID:
                obsoleteConfigMods.append(config.configModule(i))
            else:
                configMod = config.configModule(i)

        # Remove obsolete config modules
        for cm in obsoleteConfigMods:
            sys.stderr.write(
                "- %s / obsolete module configuration\n" % cm.name())
            ps = seiscomp3.DataModel.ParameterSet.Find(cm.parameterSetID())
            if not ps is None:
                ps.detach()
            cm.detach()
        del obsoleteConfigMods

        if not configMod:
            configMod = seiscomp3.DataModel.ConfigModule.Find(configID)
            if configMod is None:
                configMod = seiscomp3.DataModel.ConfigModule.Create(configID)
                config.add(configMod)
            else:
                if configMod.name() != moduleName:
                    configMod.update()
                if not configMod.enabled():
                    configMod.update()

            configMod.setName(moduleName)
            configMod.setEnabled(True)
        else:
            if configMod.name() != moduleName:
                configMod.setName(moduleName)
                configMod.update()
            paramSet = seiscomp3.DataModel.ParameterSet.Find(
                configMod.parameterSetID())
            if configMod.parameterSetID():
                configMod.setParameterSetID("")
                configMod.update()

            if not paramSet is None:
                paramSet.detach()

        stationConfigs = {}
        obsoleteStationConfigs = []

        for i in range(configMod.configStationCount()):
            cs = configMod.configStation(i)
            if (cs.networkCode(), cs.stationCode()) in self.stationSetups:
                stationConfigs[(cs.networkCode(), cs.stationCode())] = cs
            else:
                obsoleteStationConfigs.append(cs)

        for cs in obsoleteStationConfigs:
            sys.stderr.write("- %s/%s/%s / obsolete station configuration\n" %
                             (configMod.name(), cs.networkCode(), cs.stationCode()))
            cs.detach()
        del obsoleteStationConfigs

        for staid, setups in list(self.stationSetups.items()):
            try:
                cs = stationConfigs[staid]
            except:
                cs = seiscomp3.DataModel.ConfigStation.Find(
                    "Config/%s/%s/%s" % (configMod.name(), staid[0], staid[1]))
                if not cs:
                    cs = seiscomp3.DataModel.ConfigStation.Create(
                        "Config/%s/%s/%s" % (configMod.name(), staid[0], staid[1]))
                    configMod.add(cs)
                cs.setNetworkCode(staid[0])
                cs.setStationCode(staid[1])
                cs.setEnabled(True)

                ci = seiscomp3.DataModel.CreationInfo()
                ci.setCreationTime(seiscomp3.Core.Time.GMT())
                ci.setAgencyID(self.agencyID())
                ci.setAuthor(self.name())
                cs.setCreationInfo(ci)

            stationSetups = {}
            obsoleteSetups = []
            for i in range(cs.setupCount()):
                setup = cs.setup(i)
                if setup.name() in setups:
                    stationSetups[setup.name()] = setup
                else:
                    obsoleteSetups.append(setup)

            for s in obsoleteSetups:
                sys.stderr.write("- %s/%s/%s/%s / obsolete station setup\n" %
                                 (configMod.name(), cs.networkCode(), cs.stationCode(), setup.name()))
                ps = seiscomp3.DataModel.ParameterSet.Find(s.parameterSetID())
                if ps:
                    ps.detach()
                s.detach()
            del obsoleteSetups

            newParamSets = {}
            globalSet = ""
            for mod, params in list(setups.items()):
                try:
                    setup = stationSetups[mod]
                except:
                    setup = seiscomp3.DataModel.Setup()
                    setup.setName(mod)
                    setup.setEnabled(True)
                    cs.add(setup)

                paramSet = seiscomp3.DataModel.ParameterSet.Find(
                    setup.parameterSetID())
                if not paramSet:
                    paramSet = seiscomp3.DataModel.ParameterSet.Find("ParameterSet/%s/Station/%s/%s/%s" % (
                        configMod.name(), cs.networkCode(), cs.stationCode(), setup.name()))
                    if not paramSet:
                        paramSet = seiscomp3.DataModel.ParameterSet.Create(
                            "ParameterSet/%s/Station/%s/%s/%s" % (configMod.name(), cs.networkCode(), cs.stationCode(), setup.name()))
                        config.add(paramSet)
                    paramSet.setModuleID(configMod.publicID())
                    paramSet.setCreated(seiscomp3.Core.Time.GMT())
                    newParamSets[paramSet.publicID()] = 1
                    setup.setParameterSetID(paramSet.publicID())
                    if mod in stationSetups:
                        setup.update()
                elif paramSet.moduleID() != configMod.publicID():
                    paramSet.setModuleID(configMod.publicID())
                    paramSet.update()

                # Synchronize existing parameterset with the new parameters
                sync(paramSet, params)

                if setup.name() == "default":
                    globalSet = paramSet.publicID()

            for i in range(cs.setupCount()):
                setup = cs.setup(i)
                paramSet = seiscomp3.DataModel.ParameterSet.Find(
                    setup.parameterSetID())
                if not paramSet:
                    continue

                if paramSet.publicID() != globalSet and paramSet.baseID() != globalSet:
                    paramSet.setBaseID(globalSet)
                    if not paramSet.publicID() in newParamSets:
                        paramSet.update()

        # Collect unused ParameterSets
        usedSets = {}
        for i in range(config.configModuleCount()):
            configMod = config.configModule(i)
            for j in range(configMod.configStationCount()):
                cs = configMod.configStation(j)
                for k in range(cs.setupCount()):
                    setup = cs.setup(k)
                    collect(usedSets, setup.parameterSetID())

        # Delete unused ParameterSets
        i = 0
        while i < config.parameterSetCount():
            paramSet = config.parameterSet(i)
            if not paramSet.publicID() in usedSets:
                sys.stderr.write("- %s / obsolete parameter set\n" %
                                 paramSet.publicID())
                paramSet.detach()
            else:
                i = i + 1

        # Generate output file and exit if configured
        if self._outputFile:
            ar = seiscomp3.IO.XMLArchive()
            if not ar.create(self._outputFile):
                sys.stderr.write(
                    "Failed to created output file: %s" % self._outputFile)
                return False

            ar.setFormattedOutput(True)
            ar.writeObject(config)
            ar.close()
            return True

        ncount = seiscomp3.DataModel.Notifier.Size()
        if ncount > 0:
            sys.stderr.write("+ synchronize %d change" % ncount)
            if ncount > 1:
                sys.stderr.write("s")
            sys.stderr.write("\n")
        else:
            sys.stderr.write("- database is already up-to-date\n")
            return True

        cfgmsg = seiscomp3.DataModel.ConfigSyncMessage(False)
        cfgmsg.setCreationInfo(seiscomp3.DataModel.CreationInfo())
        cfgmsg.creationInfo().setCreationTime(seiscomp3.Core.Time.GMT())
        cfgmsg.creationInfo().setAuthor(self.author())
        cfgmsg.creationInfo().setAgencyID(self.agencyID())
        self.send(seiscomp3.Communication.Protocol.STATUS_GROUP, cfgmsg)

        # Send messages in a batch of 100 notifiers to not exceed the
        # maximum allowed message size of ~300kb.
        msg = seiscomp3.DataModel.NotifierMessage()
        nmsg = seiscomp3.DataModel.Notifier.GetMessage(False)
        count = 0
        sys.stderr.write("\r  + sending notifiers: %d%%" %
                         (count * 100 / ncount))
        sys.stderr.flush()
        while nmsg:
            for o in nmsg:
                n = seiscomp3.DataModel.Notifier.Cast(o)
                if n:
                    msg.attach(n)

            if msg.size() >= 100:
                count += msg.size()
                self.send("CONFIG", msg)
                msg.clear()
                self.sync()
                sys.stderr.write("\r  + sending notifiers: %d%%" %
                                 (count * 100 / ncount))
                sys.stderr.flush()

            nmsg = seiscomp3.DataModel.Notifier.GetMessage(False)

        if msg.size() > 0:
            count += msg.size()
            self.send("CONFIG", msg)
            msg.clear()
            self.sync()
            sys.stderr.write("\r  + sending notifiers: %d%%" %
                             (count * 100 / ncount))
            sys.stderr.flush()

        sys.stderr.write("\n")

        # Notify about end of synchronization
        cfgmsg.creationInfo().setCreationTime(seiscomp3.Core.Time.GMT())
        cfgmsg.isFinished = True
        self.send(seiscomp3.Communication.Protocol.STATUS_GROUP, cfgmsg)

        return True

def main():
    app = ConfigDBUpdater(len(sys.argv), sys.argv)
    return app()


#!/usr/bin/env python
# coding: utf-8

# In[2]:

def read_file(file_path):
    with open(file_path) as f:
        str_text = f.read()

    return str_text

# In[3]:

import spacy

# In[4]:

nlp = spacy.load('en', disable = ['parser', 'tagger', 'ner'])

# In[5]:

nlp.max_length=1198623

# In[6]:

def separate_punct(doc_text):
    return [token.text.lower() for token in nlp(doc_text) if token.text not in '\n\n \n\n\n!"-#$%&()--.*+,-/:;<=>?@[\\]^_`{|}~\t\n ']

# In[7]:

d = read_file('moby_dick_four_chapters.txt')
tokens = separate_punct(d)
tokens

# In[8]:

len(tokens)

# In[22]:

#pass in 25 words --> network predict #26

# In[12]:

train_len = 25 + 1
text_sequences = []

for i in range(train_len,len(tokens)):
    seq = tokens[i-train_len:i]
    text_sequences.append(seq) #input text_sequence of 26 token

# In[17]:

from keras.preprocessing.text import Tokenizer

# In[18]:

tokenizer = Tokenizer()
tokenizer.fit_on_texts(text_sequences)

# In[19]:

sequences = tokenizer.texts_to_sequences(text_sequences) #create unique indexes as id for each token in text_sequences

# In[24]:

#for i in sequences[0]:
#    print(f"{i} : {tokenizer.index_word[i]}")
#print(tokenizer.word_counts)

# In[28]:

vocab_size = len(tokenizer.word_counts) #unique token count

# In[31]:

import numpy as np
sequences = np.array(sequences)

# In[32]:

from keras.utils import to_categorical

# In[35]:

X = sequences[:,:-1]

# In[36]:

y = sequences[:,-1]

# In[37]:

y = to_categorical(y, num_classes=vocab_size+1)

# In[40]:

seq_len = X.shape[1]

# In[43]:

from keras.models import Sequential
from keras.layers import Dense, LSTM, Embedding

# In[47]:

def create_model(vocab_size, seq_len):
    model = Sequential()
    model.add(Embedding(vocab_size, seq_len, input_length=seq_len))
    model.add(LSTM(150, return_sequences=True))
    model.add(LSTM(150))
    model.add(Dense(150, activation='relu'))

    model.add(Dense(vocab_size, activation='softmax'))

    model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])

    model.summary()

    return model

# In[48]:

model = create_model(vocab_size+1, seq_len)

# In[49]:

from pickle import dump,load

# In[50]:

model.fit(X,y,batch_size=128,epochs=2,verbose=1)

# In[51]:

model.save('my_firstKeras_model.h5')
dump(tokenizer, open('dumped_tokenizer', 'wb'))

# In[52]:

############## text generation ############################################################

# In[53]:

from keras.preprocessing.sequence import pad_sequences

# In[70]:

def generate_text(model,tokenizer,seq_len,seed_text,num_gen_words):
    output_text = []

    input_text = seed_text
    for i in range(num_gen_words):
       # print(input_text)
       # print('----------------------')
        encoded_text = tokenizer.texts_to_sequences([input_text])[0]
       #  print(encoded_text)
       #  print('----------------------')
        pad_encoded = pad_sequences([encoded_text],maxlen=seq_len,truncating='pre')
        predicted_word_ind = model.predict_classes(pad_encoded, verbose=0)[0]
        predicted_word = tokenizer.index_word[predicted_word_ind]
      #   print(predicted_word)
        input_text += ' '+predicted_word

        output_text.append(predicted_word)
    return ' '.join(output_text)

# In[55]:

import random
random.seed(101)
random_pick = random.randint(0,len(text_sequences))
random_seed_text = text_sequences[random_pick]
print(random_seed_text)

# In[60]:

seed_text = ' '.join(random_seed_text)
print(seed_text)

# In[66]:

generate_text(model,tokenizer,seq_len,seed_text=seed_text,num_gen_words=25)

# In[69]:

from keras.models import load_model

# In[71]:

model = load_model('epochBIG.h5')
tokenizer = load(open('epochBIG','rb'))

# In[72]:

generate_text(model,tokenizer,seq_len,seed_text=seed_text,num_gen_words=25)


from django.contrib import admin
from .models import Company, Device, Employee, DeviceAssignment

admin.site.site_header = "Asset Tracker API"
admin.site.site_title = "Corporate Asset Tracker Django REST API"
admin.site.index_title = "Welcome Admin"

@admin.register(Company)
class CompanyAdmin(admin.ModelAdmin):
    list_display = ('name', 'location', 'phone_number')
    search_fields = ('name',)

@admin.register(Device)
class DeviceAdmin(admin.ModelAdmin):
    list_display = ('name', 'serial_number', 'condition', 'company', 'checked_out')
    search_fields = ('name', 'serial_number')

@admin.register(Employee)
class EmployeeAdmin(admin.ModelAdmin):
    search_fields = ('company', 'devices')

@admin.register(DeviceAssignment)
class DeviceAssignmentAdmin(admin.ModelAdmin):
    list_display = ('device', 'employee', 'assigned_date', 'return_date', 'updated_at')
    search_fields = ('device', 'employee__name')


import os

import pyben

def filter_empty(args, meta, info):
    """
    Remove dictionary keys with empty values.

    Parameters
    ----------
    args : `dict`
        Editable metafile properties from user.
    meta : `dict`
        Metafile data dictionary.
    info : `dict`
        Metafile info dictionary.
    """
    for key, val in list(args.items()):
        if val is None:
            del args[key]
            continue

        if val == "":
            if key in meta:
                del meta[key]
            elif key in info:
                del info[key]
            del args[key]

def edit_torrent(metafile, args):
    """
    Edit the properties and values in a torrent meta file.

    Parameters
    ----------
    metafile : `str`
        path to the torrent meta file.
    args : `dict`
        key value pairs of the properties to be edited.
    """
    meta = pyben.load(metafile)
    info = meta["info"]
    filter_empty(args, meta, info)

    if "comment" in args:
        info["comment"] = args["comment"]

    if "source" in args:
        info["source"] = args["source"]

    if "private" in args:
        info["private"] = 1

    if "announce" in args:
        val = args.get("announce", None)
        if isinstance(val, str):
            vallist = val.split()
            meta["announce"] = vallist[0]
            meta["announce list"] = [vallist]
        elif isinstance(val, list):
            meta["announce"] = val[0]
            meta["announce list"] = [val]

    if "url-list" in args:
        val = args.get("url-list")
        if isinstance(val, str):
            meta["url-list"] = val.split()
        elif isinstance(val, list):
            meta["url-list"] = val

    meta["info"] = info
    os.remove(metafile)
    pyben.dump(meta, metafile)
    return meta


import numpy as np
from sklearn import datasets
from numpy.linalg import inv
from math import sqrt,pi
import math
import matplotlib.pyplot as plt
from scipy.optimize import minimize
from sklearn.svm import SVC
from sklearn import preprocessing
from scipy.io import loadmat

def linear_reg(Xtrain,Ytrain):
    w = np.dot(inv(np.dot(np.transpose(Xtrain),Xtrain)),np.dot(np.transpose(Xtrain),Ytrain))
    return w

def RMSE_cal(w,Xtest,ytest):

    y_est = np.dot(Xtest,w)
    error = y_est - ytest

    mse = np.dot(np.transpose(error),error)/(error.shape[0])
    rmse = sqrt(mse)
    return rmse

diabetes = datasets.load_diabetes()
#print(type(diabetes))
Xtrain =  diabetes.data[:242]
Xtest = diabetes.data[242:]

Ytrain = diabetes.target[:242]
Ytest = diabetes.target[242:]

#print(len(Xtrain))

# intercept
Xtrain_i = np.concatenate((np.ones((Xtrain.shape[0],1)), Xtrain), axis=1)
Xtest_i = np.concatenate((np.ones((Xtest.shape[0],1)), Xtest), axis=1)
print('Problem 1 ')

print ('Training Data ')
w = linear_reg(Xtrain,Ytrain)
rmse = RMSE_cal(w,Xtrain,Ytrain)

w_i = linear_reg(Xtrain_i,Ytrain)
rmse_i = RMSE_cal(w_i,Xtrain_i,Ytrain)

print('RMSE without intercept '+str(rmse))
print('RMSE with intercept '+str(rmse_i))

print ('Test Data ')
w2 = linear_reg(Xtrain,Ytrain)
rmse2 = RMSE_cal(w2,Xtest,Ytest)

w_i2 = linear_reg(Xtrain_i,Ytrain)
rmse_i2 = RMSE_cal(w_i2,Xtest_i,Ytest)

print('RMSE without intercept '+str(rmse2))
print('RMSE with intercept '+str(rmse_i2))


### CS13
### 소프트웨어 전공 2016015878 김인영

### [2점] 1. 독서 퀴즈 (컴퓨터과학이 여는 세계)
### O 또는 X로 답하시오.
### 문제 번호 앞의 O/X 중에서 아닌 것을 지우세요.
### O (1) 메모리는 스위치만 이용해서도 구현가능하다.
### O (2) 컴퓨터로 문제를 푸는 알고리즘을 고안할 때 통밥(hueristic)과 무작위(randomness)를 동원하면
###         효과적인 경우가 있다.
### O (3) 튜링과 처치가 각각 독립적으로 정의한 튜링기계와 람다계산법은 능력이 동일하다.
###         튜링기계로 정의해서 돌릴 수 있는 것은 모두 람다계산법으로 정의해서 계산할 수 있고 그 반대도 성립한다.
### X (4) 사람이 새로운 지식을 만드는 방식으로 디덕(deduction, 반드시이끌기)이외에
###         앱덕(abduction, 원인짐작하기)과 인덕(induction, 짐작해서 이끌기)이 있고,
###         기계학습(machine learning)은 앱덕과 디덕을 자동화하는 기술이다.

### [2점] 2. 윤년 출력 프로시저
### 시작년도와 종료년도를 정수 인수로 주면 해당 기간동안의 윤년을 모두 한 줄에 하나씩 프린트하는 프로시저
### print_leap_year를 주어진 아래 뼈대 코드에 맞추어 작성하시오.
### 시작년도와 종료년도 모두 계산에 포함해야 합니다.
### 주어진 윤년 확인 함수 is_leap_year는 활용해도 좋습니다.
### 예를 들어 print_leap_year(1990,2004)를 호출하면,
### 다음과 같이 윤년을 한 줄에 하나씩 화면에 프린트 해야 합니다.
# 1990 ~ 2004 기간 중에서 윤년은 다음과 같습니다.
# 1992
# 1996
# 2000
# 2004

def is_leap_year(year):
    return year % 400 == 0 or year % 4 == 0 and year % 100 != 0

def print_leap_year(yearfrom, yearto):
    print(yearfrom, "~", yearto, "기간 중에서 윤년은 다음과 같습니다.")
    for i in range(yearfrom, yearto+1):
        if is_leap_year(i):
            print(i)

### test code
# print_leap_year(1990,2004) # prints 1992 1996 2000 2004, one in each line
# print_leap_year(2005,2014) # prints 2008 2012, one in each line
# print_leap_year(2094,2106) # prints 2096 2104, one in each line

### [3점] 3. ISBN-10 코드 검사 함수
### ISBN은 국제 표준 도서 번호로서, ISBN 1-2345-6789-X 또는 ISBN 1-4133-0454-0 같은 형식으로 표현합니다.
### 하이픈(-)을 제외한 10자리 중에서 앞 9자리는 모두 0~9 사이의 숫자로 구성합니다.
### 마지막 자리는 0~9 또는 X인데, 코드가 유효한지를 확인하는 자리수입니다.
### 이 자리수의 코드는 다음 공식에 의해서 결정합니다.
### d9 = (d0*1 + d1*2 + d2*3 + d3*4 + d4*5 + d5*6 + d6*7 + d7*8 + d8*9) % 11
### 마지막 자리수 d9은 이 공식에 의하면 0~10사이의 수가 되는데, 10인 경우에는 숫자 대신 X를 넣습니다.
### 따라서 ISBN 1-2345-6789-X 와 ISBN 1-4133-0454-0 모두 유효한 코드입니다.
### 이 코드의 앞 ISBN과 빈칸, 하이픈을 제외한 나머지 10자리 문자열을 인수로 받아
### 코드가 유효한지 True 또는 False로 확인해주는 함수 isbn10을 작성하시오.

def isbn10(s):
    d = []
    for i in range(9):
        d.append(int(s[i])*(i+1))
    x = sum(d)%11
    if x<10:
        return s[-1]!="X"  and x == int(s[-1])
    else:
        return s[-1] == "X"

### test code
# print(isbn10("123456789X")) # True
# print(isbn10("1234567890")) # False
# print(isbn10("1413304540")) # True
# print(isbn10("141330454X")) # False

### [3점] 4. 오른 뿔 그리기
### 자연수를 인수로 받아서 아래와 같은 모양으로 프린트하는 프로시저 hornright를 작성하시오.

### hornright(1)을 호출하면 화면에 다음과 같이 프린트한다.
# 1

### hornright(2)를 호출하면 화면에 다음과 같이 프린트한다.
# 1
# 2 2
# 1

### hornright(6)을 호출하면 화면에 다음과 같이 프린트한다.
# 1
# 2 2
# 3 3 3
# 4 4 4 4
# 5 5 5 5 5
# 6 6 6 6 6 6
# 5 5 5 5 5
# 4 4 4 4
# 3 3 3
# 2 2
# 1

### hornright(12)를 호출하면 화면에 다음과 같이 프린트한다.
# 1
# 2 2
# 3 3 3
# 4 4 4 4
# 5 5 5 5 5
# 6 6 6 6 6 6
# 7 7 7 7 7 7 7
# 8 8 8 8 8 8 8 8
# 9 9 9 9 9 9 9 9 9
# 0 0 0 0 0 0 0 0 0 0
# 1 1 1 1 1 1 1 1 1 1 1
# 2 2 2 2 2 2 2 2 2 2 2 2
# 1 1 1 1 1 1 1 1 1 1 1
# 0 0 0 0 0 0 0 0 0 0
# 9 9 9 9 9 9 9 9 9
# 8 8 8 8 8 8 8 8
# 7 7 7 7 7 7 7
# 6 6 6 6 6 6
# 5 5 5 5 5
# 4 4 4 4
# 3 3 3
# 2 2
# 1

### 0이하의 정수에 대해서는 아무 것도 프린트하지 말아야 한다.

def hornright(n):
    if n>0:
        x=-1
        for i in range(1,n+1):
            for j in range(i):
                print(str(i%10) + " ", end="")
            print()
            x = i
        for i in range(x-1, 0, -1):
            for j in range(i):
                print(str(i % 10) + " ", end="")
            print()
        print()

### test code
# hornright(-3)
# hornright(0)
# hornright(1)
# hornright(2)
# hornright(6)
# hornright(12)

### [4점] 5. 아래 급수를 계산하는 함수 sumofnumovernext를
### (1) 재귀 함수, (2) 꼬리재귀 함수, (3) while 루프 함수, (4) for 루프 형태로 차례로 작성하시오.
###    m(i) = 1/2 + 2/3 + ... + i/(i+1)

def sumofnumovernext(n):
    if n > 0:
        return n/(n+1) + sumofnumovernext(n-1)
    else:
        return 0

def sumofnumovernext(n):
    def loop(n,sum):
        if n > 0:
            return loop(n-1, sum+(n/(n+1)))
        else:
            return sum
    return loop(n,0)

def sumofnumovernext(n):
    sum = 0
    while n > 0:
        sum += n/(n+1)
        n-=1
    return sum

def sumofnumovernext(n):
    sum = 0
    for i in range(1, n+1):
        sum += i/(i+1)
    return sum

### test code
# print(sumofnumovernext(0)) # 0
# print(sumofnumovernext(1)) # 0.5
# print(sumofnumovernext(3)) # 1.9166666666666665
# print(sumofnumovernext(5)) # 3.5500000000000003
# print(sumofnumovernext(10)) # 7.980122655122655
# print(sumofnumovernext(100)) # 95.80272149226131

### [3점] 6. 로또 6/45
### 로또 6/45 번호를 아래와 같은 형식으로 자동으로 무작위 생성해주는 프로시저 lotto645를 작성하시오.
### 로또 번호는 1부터 45 사이의 수 6개로 구성합니다.
# 02 03 06 10 28 35
# 04 11 21 26 27 40
# 09 24 31 35 36 44
# 12 13 16 18 36 41
# 10 12 18 20 37 39
### 한 행에 같은 번호가 중복해서 나오면 안됩니다.
### 주의: 한 자리수는 앞을 0으로 채워서 번호의 줄을 모두 맞추어야 합니다.
### 힌트: random 모듈의 randrange 함수를 사용한다.
### 힌트: 리스트 sort() 메소드는 사용해도 좋다.

def lotto645():
    import random
    r=[]
    for i in range(6):
        r.append(random.randrange(1,46))
    r.sort()
    for i in r:
        if i<10:
            print("0"+str(i), end=" ")
        else:
            print(i, end=" ")
    print()

# ## test code
# for _ in range(5):
# 	lotto645()

### [3점] 7. 중복 골라내기
### 문자열 인수를 받아서 2번 이상 나온 문자만 나열한 리스트를 내주는 함수 collect_dups를 작성하시오.

def collect_dups(s):
    singles = ""
    duplicates = []
    for i in s:
        if i not in singles:
            singles += str(i)
        elif i in singles:
            duplicates.append(i)
    duplicates = list(set(duplicates))

    return duplicates

### test code
# print(collect_dups("sophisticated"))
# # ['s', 'i', 't']
# print(collect_dups("I'm no angel."))
# # [' ', 'n']
# print(collect_dups("Stay Hungry. Stay Foolish."))
# # ['y', ' ', 'S', 't', 'a', 'o', '.']
# print(collect_dups("Your time is limited, so don’t waste it living someone else’s life."))
# # [' ', 'i', 'm', 't', 'e', 's', 'o', 'd', 'l', 'n', '’']

### [3점] 8. 희소 리스트 (sparse list) 만들기
### 원소의 대부분이 0이고, 0이 아닌 원소가 소수인 리스트를 희소 리스트(sparse list)라고 한다.
### 리스트의 사이즈가 큰 경우, 0으로 리스트의 대부분을 채우는 건 저장공간의 낭비이다.
### 0이 아닌 원소만 인덱스를 키로하여 사전(dictionary)으로 저장하면 공간을 절약할 수 있다.
### 예를 들어, [0,0,3,0,0,0,0,0,0,7,0,0]은 {2:3,9:7}과 같이 사전으로 표현할 수 있다.
### 리스트를 이와 같은 사전 형태로 변환해주는 함수 sparse를 작성하시오.

def sparse(ns):
    dic = {}
    for i in range(len(ns)):
        if ns[i] != 0:
            dic[i] = ns[i]
    return dic

### test code
# print(sparse([])) # {}
# print(sparse([0,0,3,0,0,0,0,0,0,7,0,0])) # {2: 3, 9: 7}
# print(sparse([1,0,2,0,0,0,9,0,0])) # {0: 1, 2: 2, 6: 9}

### [3점] 9. 두 희소 리스트 덧셈
### 8번에서 사전 형태로 표현한 희소 리스트 2개 인수로 받아서 더하는 함수 sparse_add를 작성하시오.
### 예를 들어, {2: 3, 9: 7}와 {0: 1, 2: 2, 6: 9}를 더하면,
### {2: 5, 9: 7, 0: 1, 6: 9}를 내주어야 한다.
###

def sparse_add(ms,ns):
    for i in ms.keys():
        if i not in ns.keys():
            ns[i] = ms[i]
        else:
            ns[i] += ms[i]
    return ns

### test code
# print(sparse_add({},{})) # {}
# print(sparse_add({2: 3, 9: 7},{})) # {2: 3, 9: 7}
# print(sparse_add({},{0: 1, 2: 2, 6: 9})) # {0: 1, 2: 2, 6: 9}
# print(sparse_add({2: 3, 9: 7},{0: 1, 2: 2, 6: 9})) # {2: 5, 9: 7, 0: 1, 6: 9}

### [4점] 10. 완전수 (perfect number)
### 자연수 중에서 자신을 제외한 모든 약수의 합이 자신과 같은 수를 완전수라고 한다.
### 예를 들어, 6 = 3 + 2 + 1 이므로 6은 완전수이다.
### 28 = 14 + 7 + 4 + 2 + 1 이므로 28도 완전수이다.
### 10,000이하에서 완전수는 모두 4개이다.
### 자연수를 인수로 받아 그 수까지의 완전수를 모두 찾는 함수를 find_perfects_upto를 작성하시오.
### 즉, find_perfects_upto(10000)을 호출하면,
### 완전수의 리스트인 [6, 28, 496, 8128]를 내주어야 한다.

def find_perfects_upto(n):
    perfects = []
    for k in range(1,n+1):
        g=[]
        for i in range(1, k//2):
            if k%i==0:
                if i not in g:
                    g.append(i)
                    if i!=k//i:
                        g.append(k//i)
        if sum(g)-k == k:
            perfects.append(k)
    return perfects

# print(find_perfects_upto(30)) # [6, 28]
# print(find_perfects_upto(10000)) # [6, 28, 496, 8128]

### [5점] 11. 4-Queen 만족 여부를 확인하는 함수
### 체스(chess) 보드에서 Queen 말은 같은 행, 열, 대각선에 있는 말을 잡을 수 있다.
### 4x4 체스 보드에서 4개의 Queen 말을 서로 잡히지 않게 놓은 방법은 다음의 2가지가 있다.
### Queen 말은 Q로, 빈칸은 -으로 표시한다.
###    - Q - -      - - Q -
###    - - - Q      Q - - -
###    Q - - -      - - - Q
###    - - Q -      - Q - -
### 4x4 체스 보드는 아래와 같이 중첩리스트로 표현할 수 있다.
### 빈칸은 0으로, Queen 말은 1로 표시하기로 한다.
### 아래에서 board1과 board2는 Queen 말이 서로 잡히지 않은 배치이고,
### board3과 board4는 서로 잡힐 수 있는 Queen 말이 있는 배치의 사례이다.
### 이와 같은 형식의 4개의 Queen 말이 놓인 체스보드를 인수로 받아서
### 서로 잡히지 않는 배치이면 True를, 그렇지 않으면 False를 내주는
### 함수 check_4queen을 작성하시오.
### 힌트: 주어진 transpose, appears_once, appears_more_than_once 함수를 활용하면 편하다.

board1 = [[0, 1, 0, 0],
          [0, 0, 0, 1],
          [1, 0, 0, 0],
          [0, 0, 1, 0]]

board2 = [[0, 0, 1, 0],
          [1, 0, 0, 0],
          [0, 0, 0, 1],
          [0, 1, 0, 0]]

board3 = [[1, 0, 0, 0],
          [0, 0, 1, 0],
          [0, 0, 0, 1],
          [0, 1, 0, 0]]

board4 = [[0, 0, 1, 0],
          [1, 0, 0, 0],
          [0, 0, 0, 1],
          [0, 0, 0, 1]]

def transpose(mat):
    no_of_columns = len(mat[0])
    transposed = []
    for _ in range(no_of_columns):
        transposed.append([])
    for row in mat:
        i = 0
        for entry in row:
            transposed[i].append(entry)
            i += 1
    return transposed

def appears_once(ns): # 1 appears only once
    count = 0
    for n in ns:
        if n == 1:
            count += 1
    return count == 1

def appears_more_than_once(ns): # 1 appears more than once
    count = 0
    for n in ns:
        if n == 1:
            count += 1
    return count > 1

def check_4queen(board):
    for i in range(len(board)):
        if appears_more_than_once(board[i]):
            return False
    x = transpose(board)
    for i in range(len(x)):
        if appears_more_than_once(x[i]):
            return False
    y = []
    for i in range(len(board)):
        y.append([])
        for j in range(len(board)):
            if x[i][j]!=1 and board[i][j]!=1:
                y[i].append(x[i][j]+board[i][j])
            else:
                y[i].append(1)
    s=0
    for i in range(len(y)):
        s+=sum(y[i])

    if s!=8: # dup
        return False
    return True

# print(check_4queen(board1))
# print(check_4queen(board2))
# print(check_4queen(board3))
# print(check_4queen(board4))


import csv
from sklearn.datasets import fetch_20newsgroups
from sklearn.naive_bayes import MultinomialNB
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.feature_extraction.text import CountVectorizer

trainName = 'information_train.csv'
testName = 'information_test.csv'
# Row 1: Abstract (text)
# Row 2: Article title
# Row 3: Author
# Row 4: pmid
# Row 5: Publication Date
# Row 6: Set

train_data = []
test_data = []
with open(trainName, 'rb') as file:
    train_data = [line.decode('utf-8').strip().split('\t') for line in file.readlines()]
with open(testName, 'rb') as file:
    test_data = [line.decode('utf-8').strip().split('\t') for line in file.readlines()]

test_data = [test_data[index] for index in range(0,len(test_data)) if test_data[index] != ['"']]
test_data = [test_data[index] for index in range(0,len(test_data)) if test_data[index] != ['']]
#print(len(test_data))

# Define test data (text)
input_data = [test_data[index][0] for index in range(0,len(test_data))]

# Define the category map
category_map = {'talk.politics.misc': 'Politics', 'rec.autos': 'Autos',
                'rec.sport.hockey': 'Hockey', 'sci.electronics': 'Electronics',
                'sci.med': 'Medicine'}

#training set
training_data = fetch_20newsgroups(subset='train',
                                   categories=category_map.keys(),shuffle=True,
                                   random_state=5)

# Build a count vectorizer and extract term counts
count_vectorizer = CountVectorizer()
train_tc = count_vectorizer.fit_transform(training_data)

# Create the tf-idf transformer
tfidf = TfidfTransformer()
train_tfidf = tfidf.fit_transform(train_tc)

# Train a Multinomial Naive Bayes classifier
classifier = MultinomialNB().fit(train_tfidf, training_data.target)

# Transform input data using count vectorizer
input_tc = count_vectorizer.transform(input_data)

# Transform vectorized data using tfidf transformer
input_tfidf = tfidf.transform(input_tc)

# Predict the output categories
predictions = classifier.predict(input_tfidf)

# Print the outputs
count = 0
for sent, category in zip(input_data, predictions):
    print('\nInput:', sent, '\nPredicted category:', \
          category_map[training_data.target_names[category]])
    count += 1
    if count == 5:
        break