import torch
import torch.nn as nn
import torch.nn.functional as F
from .TopicDistQuant import TopicDistQuant
from .TSC import TSC

class TSCTM(nn.Module):
    '''
        Mitigating Data Sparsity for Short Text Topic Modeling by Topic-Semantic Contrastive Learning. EMNLP 2022

        Xiaobao Wu, Anh Tuan Luu, Xinshuai Dong.

        Note: This implementation does not include TSCTM with augmentations. For augmentations, see https://github.com/BobXWu/TSCTM.
    '''

    def __init__(self, vocab_size, num_topics=50, en_units=200, temperature=0.5, weight_contrast=1.0):
        super().__init__()

        self.fc11 = nn.Linear(vocab_size, en_units)
        self.fc12 = nn.Linear(en_units, en_units)
        self.fc21 = nn.Linear(en_units, num_topics)

        self.mean_bn = nn.BatchNorm1d(num_topics)
        self.mean_bn.weight.requires_grad = False
        self.decoder_bn = nn.BatchNorm1d(vocab_size)
        self.decoder_bn.weight.requires_grad = False

        self.fcd1 = nn.Linear(num_topics, vocab_size, bias=False)

        for m in self.modules():
            if isinstance(m, (nn.Conv2d, nn.Linear)):
                nn.init.xavier_uniform_(m.weight)
                if m.bias is not None:
                    nn.init.zeros_(m.bias)

        self.topic_dist_quant = TopicDistQuant(num_topics, num_topics)
        self.contrast_loss = TSC(temperature, weight_contrast)

    def get_beta(self):
        return self.fcd1.weight.T

    def encode(self, inputs):
        e1 = F.softplus(self.fc11(inputs))
        e1 = F.softplus(self.fc12(e1))
        return self.mean_bn(self.fc21(e1))

    def decode(self, theta):
        d1 = F.softmax(self.decoder_bn(self.fcd1(theta)), dim=1)
        return d1

    def get_theta(self, inputs):
        theta = self.encode(inputs)
        softmax_theta = F.softmax(theta, dim=1)
        return softmax_theta

    def forward(self, inputs):
        theta = self.encode(inputs)
        softmax_theta = F.softmax(theta, dim=1)

        quant_rst = self.topic_dist_quant(softmax_theta)

        recon = self.decode(quant_rst['quantized'])
        loss = self.loss_function(recon, inputs) + quant_rst['loss']

        features = torch.cat([F.normalize(theta, dim=1).unsqueeze(1)], dim=1)
        contrastive_loss = self.contrast_loss(features, quant_idx=quant_rst['encoding_indices'])
        loss += contrastive_loss

        return {'loss': loss, 'contrastive_loss': contrastive_loss}

    def loss_function(self, recon_x, x):
        loss = -(x * (recon_x).log()).sum(axis=1)
        loss = loss.mean()
        return loss


import re
import pandas as pd
import draughts
from draughts import *
from draughts.PDN import PDNReader, _PDNGame
import numpy as np
import torch
from torch import Tensor
import torch.nn as nn
import torch.nn.functional as F
import torchvision
import torch.utils.data
from torch.utils.data import *
import pickle

filepath = "/Users/benakkermans/Downloads/CheckersDB.txt"
#reader = PDNReader(filepath)
#print(reader.games[0].moves)

def PDN_parse(filepath):
    """
    Parse text at given filepath
    Pasres a PDN FIle as .txt into games

    Parameters
    ----------
    filepath : str
        Filepath for file to be parsed

    Returns
    -------
    data : pd.DataFrame
        Parsed data

    """
    re_startmove = "^1."
    re_movesplit = " "
    terminate_list = ['1-0','0-1','1/2-1/2']

    data = []
    with open(filepath, 'r') as file:
        # Flags to indicate where in the file structure we are
        movesflag = False
        validflag = True
        ## MOVE LIST FOR EACH GAME
        movelist = []
        game_result = ''

        lines = file.readlines()

        for line in lines:
            if re.search(re_startmove,line):
                # Change flag to indicate it is a move line
                movesflag = True

            # Parse line of moves
            if movesflag:
                moves = re.split(re_movesplit, line)

                for move in moves:
                    move = move.strip("\n")
                    ## COmment in game, just scrap as there is very few
                    if '{' in move:
                        validflag = False

                    # Check if ending condition
                    if move in terminate_list:
                        if move == '1-0':
                            game_result = 1
                        elif move == '0-1':
                            game_result = -1
                        else:
                            game_result = 0

                        movesflag = False
                        if validflag:
                            data.append((movelist,game_result))
                        movelist = []
                        validflag = True

                    elif re.search("\.",move) == None:
                        movelist.append(move)

    return data

def get_BoardState(moveslist, game_result=0):
    ## Pieces denoted in the board as follows
    ## -3: White K, -1: White Piece, 0: No Piece, 1: Black Piece, 3: Black King
    ## Function currently starts with Black at Square 1 (Index 0)
    ## Game result denoted as follows:
        # 0 = Draw
        # 1 = Player-to-move Wins
        #-1 = Opposing Player Wins
    ## The game result attached to the output will flip considering who is at move(flip flag integration)
    ###############################################################################
    # Index to Move Dict:
    index_dict = {1:[0,0],2:[0,1],3:[0,2],4:[0,3],
                  5:[1,0],6:[1,1],7:[1,2],8:[1,3],
                  9:[2,0],10:[2,1],11:[2,2],12:[2,3],
                  13:[3,0],14:[3,1],15:[3,2],16:[3,3],
                  17:[4,0],18:[4,1],19:[4,2],20:[4,3],
                  21:[5,0],22:[5,1],23:[5,2],24:[5,3],
                  25:[6,0],26:[6,1],27:[6,2],28:[6,3],
                  29:[7,0],30:[7,1],31:[7,2],32:[7,3]}

    # Board Initialzied to starting position of the board
    board = np.array([[1,1,1,1],
                      [1,1,1,1],
                      [1,1,1,1],
                      [0,0,0,0],
                      [0,0,0,0],
                      [-1,-1,-1,-1],
                      [-1,-1,-1,-1],
                      [-1,-1,-1,-1]])

    cols = ['num_moves','board_state','next_move']
    #master_list = pd.DataFrame(columns=cols)
    master_list = []
    num_moves = 0
    ## Flip the board if it is Black's move (Board currently set so player-to-move is at the bottom)
    flip_flag = False

    ## Next define the changes made to the board by each type of move
    for strmove in moveslist:
        # Breakdown the move first
        ## Basic Move
        if re.search("-",strmove) != None:
            ## Simple as we know there are only two moves, no capture so straight update
            move = strmove.split("-")
            start_id = index_dict[int(move[0])]
            end_id = index_dict[int(move[1])]
            piece = board[start_id[0],start_id[1]]

            ## Execute the Update using above info on the move
            board[start_id[0],start_id[1]] = 0
            ## KING CHECK
            if piece == 1 and end_id[0] == 7:
                board[end_id[0],end_id[1]] = 3
            elif piece == -1 and end_id[0] == 0:
                board[end_id[0],end_id[1]] = -3
            else:
                board[end_id[0],end_id[1]] = piece

        ## Capture Move / Jump --> x-number of jumps in the move
        else:
            move = strmove.split("x")
            x = len(move)
            start_id = index_dict[int(move[0])]

            visited_squares = []
            for i in range(x-1):
                idx = index_dict[int(move[i+1])]
                visited_squares.append(idx)

            #end_id = visited_squares[-1]
            piece = board[start_id[0],start_id[1]]

            ## Execute the Update using above info on the move
            board[start_id[0],start_id[1]] = 0

            current = start_id
            for target in visited_squares:
                ## First Check if current/target rows are EVEN or ODD
                ## A KING off of a jump can only occur in two scenarios ->
                ## [Odd-row moving down the board (5->7) BLACK King and Even-row moving up (2->0) WHITE King

                # EVEN ROW RULES
                if current[0] % 2 == 0:
                    if (current[0]<target[0] and current[1]>target[1]) or (current[0]>target[0] and current[1]>target[1]):
                        middle_row = (max([current[0],target[0]])) - 1
                        # Remove piece in middle row, starting column
                        board[middle_row,current[1]] = 0
                        ## KING CHECK (3 conditions: Last Jump, White Piece, Row=0), even row can only be a WHITE KING
                        if (visited_squares.index(target) == (len(visited_squares) - 1)) and (piece == -1) and (target[0] == 0):
                             board[target[0],target[1]] = -3
                        else:
                            board[target[0],target[1]] = piece

                        current = target

                    elif (current[0]<target[0] and current[1]<target[1]) or (current[0]>target[0] and current[1]<target[1]):
                        middle_row = (max([current[0],target[0]])) - 1
                        # Remove piece in middle row, target column
                        board[middle_row,target[1]] = 0
                        ## KING CHECK (3 conditions: Last Jump, White Piece, Row=0), even row can only be a WHITE KING
                        if (visited_squares.index(target) == (len(visited_squares) - 1)) and (piece == -1) and (target[0] == 0):
                             board[target[0],target[1]] = -3
                        else:
                            board[target[0],target[1]] = piece

                        current = target

                # ODD ROW RULES
                else:
                    if (current[0]<target[0] and current[1]>target[1]) or (current[0]>target[0] and current[1]>target[1]):
                        middle_row = (max([current[0],target[0]])) - 1
                        # Remove piece in middle row, starting column
                        board[middle_row,target[1]] = 0
                        ## KING CHECK (3 conditions: Last Jump, Black Piece, Row=7), odd row can only be a BLACK KING
                        if (visited_squares.index(target) == (len(visited_squares) - 1)) and (piece == 1) and (target[0] == 7):
                             board[target[0],target[1]] = 3
                        else:
                            board[target[0],target[1]] = piece

                        current = target

                    elif (current[0]<target[0] and current[1]<target[1]) or (current[0]>target[0] and current[1]<target[1]):
                        middle_row = (max([current[0],target[0]])) - 1
                        # Remove piece in middle row, target column
                        board[middle_row,current[1]] = 0
                        ## KING CHECK (3 conditions: Last Jump, Black Piece, Row=7), odd row can only be a BLACK KING
                        if (visited_squares.index(target) == (len(visited_squares) - 1)) and (piece == 1) and (target[0] == 7):
                             board[target[0],target[1]] = 3
                        else:
                            board[target[0],target[1]] = piece

                        current = target
        num_moves += 1
        ## Calculate whos move is next for the flip_flag
        if num_moves % 2 == 0:
            # Black's Turn
            flip_flag = True
        else:
            flip_flag = False

        ## Output with next move
        # Set board, flip if flip_flag
        c_board = board.copy()
        gc = game_result
        ## Black to move, game result fine
        if flip_flag:
            c_board = np.flip(c_board, axis=0)
            c_board = np.flip(c_board, axis=1)
            gc = game_result

        ## White to move, flip game result and pieces (white is standard negative, we want white positive)
        else:
            c_board *= -1
            gc = game_result*-1

        master_list.append((c_board,gc))

        #print("Move was: ",strmove)
        #print(board)

    return master_list, moveslist

def add_game_data(df,moves,games_master,labels_master):
    #game = np.array([])
    labels = []
    for z in df:
        board = z[0]
        label = z[1]
        #print(board)
        #b_tensor = torch.from_numpy(board.copy())
        #b_tensor = torch.flatten(b_tensor)
        board = board.flatten()
        #games_master = torch.cat((games_master,b_tensor),1)
        games_master = np.vstack([games_master,board])
        labels.append(label)
        #print(games_master)
        #print('\n')
    labels_master.append(labels)

    return games_master,labels_master

#games_master = torch.Tensor()
games_master = np.empty((0,32))
labels_master = []
data = PDN_parse(filepath)

i = 0
for game in data:
    i += 1
    print(i)
    df, moves = get_BoardState(game[0],game[1])
    games_master,labels_master = add_game_data(df,moves,games_master,labels_master)

games_master = torch.Tensor(games_master)

gamefile = open('boardDB','wb')
pickle.dump(games_master,gamefile)
gamefile.close()

labelfile = open('labelDB','wb')
pickle.dump(labels_master,labelfile)
labelfile.close()


from typing import Any, List
from uuid import UUID

from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session

from app import crud, models, schemas
from app.api import deps

router = APIRouter()

@router.get("/", response_model=List[schemas.Definition])
def read_definitions(
    db: Session = Depends(deps.get_db),
    skip: int = 0,
    limit: int = 100,
    current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
    """
    Retrieve definitions.
    """
    if crud.user.is_superuser(current_user):
        definitions = crud.definition.get_multi(db, skip=skip, limit=limit)
    else:
        definitions = crud.definition.get_multi_by_owner(
            db=db, owner_uuid=current_user.uuid, skip=skip, limit=limit
        )
    return definitions

@router.get("/{uuid}", response_model=schemas.Definition)
def read_definition(
    *,
    db: Session = Depends(deps.get_db),
    uuid: UUID,
) -> Any:
    """
    Get definition by ID.
    """
    definition = crud.definition.get_by_uuid(db=db, uuid=uuid)
    if not definition:
        raise HTTPException(status_code=404, detail="Definition not found")
    return definition

# @router.post("/{uuid}", response_model=schemas.Definition)
# def create_definition(
#     *,
#     db: Session = Depends(deps.get_db),
#     uuid: UUID,
#     definition_in: schemas.DefinitionCreate,
#     current_user: models.User = Depends(deps.get_current_active_user),
# ) -> Any:
#     """
#     Create new definition.
#     """
#     definition = crud.definition.create_with_owner(db=db, obj_in=definition_in, owner_uuid=current_user.uuid)
#     return definition

# @router.put("/{id}", response_model=schemas.Definition)
# def update_definition(
#     *,
#     db: Session = Depends(deps.get_db),
#     id: int,
#     definition_in: schemas.DefinitionUpdate,
#     current_user: models.User = Depends(deps.get_current_active_user),
# ) -> Any:
#     """
#     Update an definition.
#     """
#     definition = crud.definition.get(db=db, id=id)
#     if not definition:
#         raise HTTPException(status_code=404, detail="Definition not found")
#     if not crud.user.is_superuser(current_user) and (definition.owner_uuid != current_user.uuid):
#         raise HTTPException(status_code=400, detail="Not enough permissions")
#     definition = crud.definition.update(db=db, db_obj=definition, obj_in=definition_in)
#     return definition

# @router.delete("/{id}", response_model=schemas.Definition)
# def delete_definition(
#     *,
#     db: Session = Depends(deps.get_db),
#     id: int,
#     current_user: models.User = Depends(deps.get_current_active_user),
# ) -> Any:
#     """
#     Delete an definition.
#     """
#     definition = crud.definition.get(db=db, id=id)
#     if not definition:
#         raise HTTPException(status_code=404, detail="Definition not found")
#     if not crud.user.is_superuser(current_user) and (definition.owner_uuid != current_user.uuid):
#         raise HTTPException(status_code=400, detail="Not enough permissions")
#     definition = crud.definition.remove(db=db, id=id)
#     return definition


from django.shortcuts import render, get_object_or_404, redirect
import urllib
# Create your views here.
def oauth(request):
    code = request.GET['code']
    print('code= ' + str(code))

    client_id = '13839c3b390207f7c0c7eaa789139e34'
    redirect_uri = 'http://127.0.0.1:8000/account/login/kakao/callback'
    access_token_request_uri = "https://kauth.kakao.com/oauth/token?grant_type=authorization_code&"

    access_token_request_uri += "client_id=" + client_id
    access_token_request_uri += "&redirect_uri=" + redirect_uri
    access_token_request_uri += "&code=" + code
    print(access_token_request_uri)

    access_token_request_uri_data = requests.get(access_token_request_uri)
    json_data = access_token_request_uri_data.json()
    access_token = json_data['access_token']
    print(access_token)

    user_profile_info_uri = "https://kapi.kakao.com/v2/user/me?access_token="
    user_profile_info_uri += str(access_token)
    print(user_profile_info_uri)

    user_profile_info_uri_data = requests.get(user_profile_info_uri)
    user_json_data = user_profile_info_uri_data.json()
    user_nickname = user_json_data['properties']['nickname']
    print(user_nickname)

    return redirect('index')

def kakao_login(request):
    login_request_uri = 'https://kauth.kakao.com/oauth/authorize?'
    client_id = '13839c3b390207f7c0c7eaa789139e34'
    redirect_uri = 'http://127.0.0.1:8000/account/login/kakao/callback'

    login_request_uri += 'client_id=' + client_id
    login_request_uri += '&redirect_uri=' + redirect_uri
    login_request_uri += '&response_type=code'

    return redirect(login_request_uri)


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import math

import REDACTED
from absl import app
from absl import flags
from absl import logging
import tensorflow.compat.v2 as tf
from REDACTED.tf2_bert import optimization
from REDACTED.tf2_bert.bert import bert_models
from REDACTED.tf2_bert.bert import common_flags
from REDACTED.tf2_bert.bert import configs
from REDACTED.tf2_bert.bert import input_pipeline
from REDACTED.tf2_common.modeling import model_training_utils
from REDACTED.tf2_common.modeling import performance
from REDACTED.tf2_common.utils.misc import distribution_utils
from REDACTED.tf2_common.utils.mlp_log import mlp_log

flags.DEFINE_string('input_files', None,
                    'File path to retrieve training data for pre-training.')
# Model training specific flags.
flags.DEFINE_integer(
    'max_seq_length', 128,
    'The maximum total input sequence length after WordPiece tokenization. '
    'Sequences longer than this will be truncated, and sequences shorter '
    'than this will be padded.')
flags.DEFINE_integer('max_predictions_per_seq', 20,
                     'Maximum predictions per sequence_output.')
flags.DEFINE_integer('train_batch_size', 32, 'Total batch size for training.')
flags.DEFINE_integer('num_steps_per_epoch', 1000,
                     'Total number of training steps to run per epoch.')
flags.DEFINE_float('warmup_steps', 10000,
                   'Warmup steps for Adam weight decay optimizer.')
flags.DEFINE_integer('stop_steps', None,
                     'The number of steps to stop training.')
flags.DEFINE_bool('do_eval', False, 'Whether to run eval.')
flags.DEFINE_bool('device_warmup', False,
                  'Whether or not to enable device warmup.')
flags.DEFINE_integer('steps_between_eval', 10000,
                     'Steps between an eval. Is multiple of steps per loop.')
flags.DEFINE_integer('steps_before_eval_start', 0,
                     'Steps before starting eval.')
flags.DEFINE_integer('num_eval_samples', 10000, 'Number of eval samples.')
flags.DEFINE_integer('eval_batch_size', 32, 'Total batch size for training.')
flags.DEFINE_float('weight_decay_rate', 0.01,
                   'The weight_decay_rate value for the optimizer.')
flags.DEFINE_float('beta_1', 0.9, 'The beta_1 value for the optimizer.')
flags.DEFINE_float('beta_2', 0.999, 'The beta_2 value for the optimizer.')
flags.DEFINE_float('epsilon', 1e-6, 'The epsilon value for the optimizer.')
flags.DEFINE_integer('num_accumulation_steps', 1,
                     'number of steps to accumulate with large batch size.')
flags.DEFINE_float(
    'stop_threshold', default=0.712, help=('Stop threshold for MLPerf.'))
common_flags.define_common_bert_flags()

FLAGS = flags.FLAGS

def get_pretrain_dataset_fn(input_file_pattern, seq_length,
                            max_predictions_per_seq, global_batch_size,
                            is_training, use_synthetic, num_eval_samples):
  """Returns input dataset from input file string."""
  def _dataset_fn(ctx=None):
    """Returns tf.data.Dataset for distributed BERT pretraining."""
    if use_synthetic:
      input_patterns = ''
    else:
      input_patterns = input_file_pattern.split(',')
    batch_size = ctx.get_per_replica_batch_size(global_batch_size)

    train_dataset = input_pipeline.create_pretrain_dataset(
        input_patterns=input_patterns,
        seq_length=seq_length,
        max_predictions_per_seq=max_predictions_per_seq,
        batch_size=batch_size,
        is_training=is_training,
        use_synthetic=use_synthetic,
        input_pipeline_context=ctx,
        num_eval_samples=num_eval_samples)
    return train_dataset

  return _dataset_fn

def get_loss_fn(loss_factor=1.0):
  """Returns loss function for BERT pretraining."""

  def _bert_pretrain_loss_fn(unused_labels, losses, **unused_args):
    return tf.reduce_mean(losses) * loss_factor

  return _bert_pretrain_loss_fn

def clip_by_global_norm(grads_and_vars):
  grads, tvars = list(zip(*grads_and_vars))
  (grads, _) = tf.clip_by_global_norm(grads, clip_norm=1.0)
  return zip(grads, tvars)

def run_customized_training(
    strategy, optimizer_type, weight_decay_rate, beta_1, beta_2, epsilon,
    bert_config, max_seq_length, max_predictions_per_seq, model_dir,
    steps_per_epoch, steps_per_loop, epochs, initial_lr, warmup_steps,
    input_files, train_batch_size, do_eval, eval_batch_size, num_eval_samples,
    custom_callbacks, init_checkpoint, steps_between_eval,
    steps_before_eval_start, stop_threshold, explicit_allreduce,
    allreduce_bytes_per_pack, enable_checkpoint_and_summary, device_warmup):
  """Run BERT pretrain model training using low-level API."""
  mlp_log.mlperf_print('cache_clear', True)
  mlp_log.mlperf_print('init_start', None)
  mlp_log.mlperf_print('global_batch_size', train_batch_size)
  mlp_log.mlperf_print('max_sequence_length', max_seq_length)
  mlp_log.mlperf_print('max_predictions_per_seq', max_predictions_per_seq)
  mlp_log.mlperf_print('opt_base_learning_rate', initial_lr)
  mlp_log.mlperf_print('opt_lamb_weight_decay_rate', weight_decay_rate)
  mlp_log.mlperf_print('opt_lamb_beta_1', beta_1)
  mlp_log.mlperf_print('opt_lamb_beta_2', beta_2)
  mlp_log.mlperf_print('opt_gradient_accumulation_steps',
                       FLAGS.num_accumulation_steps)
  mlp_log.mlperf_print('opt_learning_rate_warmup_steps', warmup_steps)
  mlp_log.mlperf_print('num_warmup_steps', warmup_steps)
  mlp_log.mlperf_print('start_warmup_step', 0)
  mlp_log.mlperf_print('opt_epsilon', epsilon)
  mlp_log.mlperf_print('eval_samples', num_eval_samples)
  mlp_log.mlperf_print('opt_lamb_learning_rate_decay_poly_power', 1.0)
  mlp_log.mlperf_print('opt_learning_rate_training_steps',
                       steps_per_epoch * epochs)
  mlp_log.mlperf_print('train_samples',
                       train_batch_size * steps_per_epoch * epochs)
  train_input_fn = get_pretrain_dataset_fn(
      input_file_pattern=input_files,
      seq_length=max_seq_length,
      max_predictions_per_seq=max_predictions_per_seq,
      global_batch_size=train_batch_size,
      is_training=True,
      num_eval_samples=num_eval_samples,
      use_synthetic=False)
  eval_input_fn = None
  if do_eval:
    eval_input_fn = get_pretrain_dataset_fn(
        input_file_pattern=input_files,
        seq_length=max_seq_length,
        max_predictions_per_seq=max_predictions_per_seq,
        global_batch_size=eval_batch_size,
        is_training=False,
        num_eval_samples=num_eval_samples,
        use_synthetic=False)
  synthetic_train_input_fn = None
  if device_warmup:
    synthetic_train_input_fn = get_pretrain_dataset_fn(
        input_file_pattern=None,
        seq_length=max_seq_length,
        max_predictions_per_seq=max_predictions_per_seq,
        global_batch_size=train_batch_size,
        is_training=True,
        num_eval_samples=1,
        use_synthetic=True)

  def _get_pretrain_model():
    """Gets a pretraining model."""
    pretrain_model, core_model, core_pretrain_model = bert_models.pretrain_model(
        bert_config, max_seq_length, max_predictions_per_seq)
    optimizer = optimization.create_optimizer(initial_lr,
                                              steps_per_epoch * epochs,
                                              warmup_steps, optimizer_type,
                                              weight_decay_rate, beta_1, beta_2,
                                              epsilon)
    pretrain_model.optimizer = performance.configure_optimizer(
        optimizer,
        use_float16=common_flags.use_float16(),
        use_graph_rewrite=common_flags.use_graph_rewrite())
    return pretrain_model, core_model, core_pretrain_model

  trained_model, masked_lm_accuracy, run_steps = model_training_utils.run_customized_training_loop(
      strategy=strategy,
      model_fn=_get_pretrain_model,
      loss_fn=get_loss_fn(
          loss_factor=1.0 /
          strategy.num_replicas_in_sync if FLAGS.scale_loss else 1.0),
      model_dir=model_dir,
      train_input_fn=train_input_fn,
      steps_per_epoch=steps_per_epoch,
      steps_per_loop=steps_per_loop,
      epochs=epochs,
      eval_input_fn=eval_input_fn,
      eval_steps=math.ceil(num_eval_samples / eval_batch_size),
      steps_between_eval=steps_between_eval,
      steps_before_eval_start=steps_before_eval_start,
      sub_model_export_name='pretrained/bert_model',
      init_checkpoint=init_checkpoint,
      custom_callbacks=custom_callbacks,
      device_warmup=device_warmup,
      synthetic_train_input_fn=synthetic_train_input_fn,
      explicit_allreduce=explicit_allreduce,
      post_allreduce_callbacks=[clip_by_global_norm],
      allreduce_bytes_per_pack=allreduce_bytes_per_pack,
      enable_checkpoint_and_summary=enable_checkpoint_and_summary,
      num_accumulation_steps=FLAGS.num_accumulation_steps,
      stop_steps=FLAGS.stop_steps,
      stop_threshold=FLAGS.stop_threshold)

  return trained_model, masked_lm_accuracy, run_steps

def run_bert_pretrain(strategy, custom_callbacks=None):
  """Runs BERT pre-training."""
  bert_config = configs.BertConfig.from_json_file(FLAGS.bert_config_file)
  if not strategy:
    raise ValueError('Distribution strategy is not specified.')

  # Runs customized training loop.
  logging.info('Training using customized training loop TF 2.0 with distrubuted'
               'strategy.')

  performance.set_mixed_precision_policy(common_flags.dtype())

  _, masked_lm_accuracy, run_steps = run_customized_training(
      strategy=strategy,
      optimizer_type=FLAGS.optimizer_type,
      weight_decay_rate=FLAGS.weight_decay_rate,
      beta_1=FLAGS.beta_1,
      beta_2=FLAGS.beta_2,
      epsilon=FLAGS.epsilon,
      bert_config=bert_config,
      max_seq_length=FLAGS.max_seq_length,
      max_predictions_per_seq=FLAGS.max_predictions_per_seq,
      model_dir=FLAGS.model_dir,
      steps_per_epoch=FLAGS.num_steps_per_epoch,
      steps_per_loop=FLAGS.steps_per_loop,
      epochs=FLAGS.num_train_epochs,
      initial_lr=FLAGS.learning_rate,
      warmup_steps=FLAGS.warmup_steps,
      input_files=FLAGS.input_files,
      train_batch_size=FLAGS.train_batch_size,
      eval_batch_size=FLAGS.eval_batch_size,
      do_eval=FLAGS.do_eval,
      num_eval_samples=FLAGS.num_eval_samples,
      steps_between_eval=FLAGS.steps_between_eval,
      steps_before_eval_start=FLAGS.steps_before_eval_start,
      stop_threshold=FLAGS.stop_threshold,
      explicit_allreduce=FLAGS.explicit_allreduce,
      allreduce_bytes_per_pack=FLAGS.allreduce_bytes_per_pack,
      enable_checkpoint_and_summary=FLAGS.enable_checkpoint_and_summary,
      custom_callbacks=custom_callbacks,
      init_checkpoint=FLAGS.init_checkpoint,
      device_warmup=FLAGS.device_warmup)
  return masked_lm_accuracy, run_steps

# copybara:strip_begin
# Hparams wrapper should be REDACTED only, not open source.
def hparams_run_wrapper(hparams):
  """Convenience wrapper for running with hyperparameters."""
  print('HParams: ', hparams)

  if 'learning_rate' in hparams:
    print('Overwriting learning_rate with ', hparams.learning_rate)
    FLAGS.learning_rate = hparams.learning_rate
  if 'beta_1' in hparams:
    print('Overwriting beta_1 with ', hparams.beta_1)
    FLAGS.beta_1 = hparams.beta_1
  if 'beta_2' in hparams:
    print('Overwriting beta_2 with ', hparams.beta_2)
    FLAGS.beta_1 = hparams.beta_2
  if 'warmup_steps' in hparams:
    print('Overwriting warmup_steps with ', hparams.warmup_steps)
    FLAGS.warmup_steps = hparams.warmup_steps
  if 'num_steps_per_epoch' in hparams:
    print('Overwriting num_steps_per_epoch with ', hparams.num_steps_per_epoch)
    FLAGS.num_steps_per_epoch = hparams.num_steps_per_epoch
  if 'weight_decay_rate' in hparams:
    print('Overwriting weight_decay_rate with ', hparams.weight_decay_rate)
    FLAGS.weight_decay_rate = hparams.weight_decay_rate

  if not FLAGS.model_dir:
    FLAGS.model_dir = '/tmp/bert20/'
  strategy = distribution_utils.get_distribution_strategy(
      distribution_strategy=FLAGS.distribution_strategy,
      num_gpus=FLAGS.num_gpus,
      all_reduce_alg=FLAGS.all_reduce_alg,
      tpu_address=FLAGS.tpu)
  if strategy:
    print('***** Number of cores used : ', strategy.num_replicas_in_sync)

  return run_bert_pretrain(strategy)

# copybara:strip_end

def main(_):
  # Users should always run this script under TF 2.x
  tf.enable_v2_behavior()

  if not FLAGS.model_dir:
    FLAGS.model_dir = '/tmp/bert20/'
  strategy = distribution_utils.get_distribution_strategy(
      distribution_strategy=FLAGS.distribution_strategy,
      num_gpus=FLAGS.num_gpus,
      all_reduce_alg=FLAGS.all_reduce_alg,
      tpu_address=FLAGS.tpu)
  if strategy:
    print('***** Number of cores used : ', strategy.num_replicas_in_sync)

  run_bert_pretrain(strategy)

if __name__ == '__main__':
  app.run(main)


import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import time as ti
import pandas as pd
from scipy import stats
import random as rd
from matplotlib.font_manager import FontProperties
from scipy import interpolate
font = FontProperties(fname=r'C:/Users/user/Anaconda3/Lib/site-packages/matplotlib/mpl-data/fonts/ttf/DejaVuSans.ttc')

def linear_regression(x,y):
  x=np.concatenate((np.ones((x.shape[0],1)),x[:,np.newaxis]),axis=1)
  y=y[:,np.newaxis]
  beta=np.matmul(np.matmul(np.linalg.inv(np.matmul(x.T,x)),x.T),y)
  return beta
pass

path = 'well.csv'
file = pd.read_csv(path)
print(file['level'])
print(file['ELE'])
print(len(file['level']))

by_hand = linear_regression(file['ELE'], file['level'])
print(by_hand[0])
print(by_hand[1])
dis = by_hand[0]
m = by_hand[1]
str_dis=str(dis)
str_m=str(m)
xs = np.linspace(0,150, 5)
ys = dis+m*xs
plt.xlabel("地表高程(m)", fontproperties=font, fontsize=15)
plt.ylabel("水頭(m)", fontproperties=font, fontsize=15)
plt.plot(xs ,ys, color='green')
plt.scatter(file['ELE'], file['level'], color='red' )
plt.text(10, 100, "水頭=" + str_m + "高程" + str_dis + "(單位:公尺)" , fontproperties=font, fontsize=10)
plt.show()


import math
from typing import List, Optional

import numpy as np
import rospy

from soccer_common.camera import Camera
from soccer_common.transformation import Transformation
from soccer_common.utils import wrapToPi
from soccer_pycontrol import path
from soccer_strategy.ball import Ball
from soccer_strategy.obstacle import Obstacle
from soccer_strategy.robot import Robot
from soccer_strategy.robot_controlled import RobotControlled

class RobotControlled2D(RobotControlled):
    class ObservationConstants:
        FOV = Camera.HORIZONTAL_FOV
        VISION_RANGE = 3

    def __init__(
        self,
        robot_id=0,
        team=Robot.Team.UNKNOWN,
        role=Robot.Role.UNASSIGNED,
        status=Robot.Status.DISCONNECTED,
        position=None,
    ):
        super().__init__(robot_id=robot_id, team=team, role=role, status=status, position=position)

        self.active = True  # Used only for switching which active robot for strategy stuff
        self.robot_id = robot_id
        self.robot_name = "robot %d" % robot_id
        self.path_time = 0
        self.path = None
        self.trajectory_timeout = 0

    def __index__(self):
        return self.robot_id, self.team

    def __hash__(self):
        return hash((self.robot_id, self.team))

    def kick(self, kick_velocity):
        """
        Set's a kick velocity for the ball
        :param kick_velocity: Amount of strength for the kick
        :return:
        """
        self.status = Robot.Status.KICKING
        self.kick_velocity = kick_velocity

    def set_navigation_position(self, goal_position):
        super().set_navigation_position(goal_position)
        self.path = path.Path(Transformation(pos_theta=self.start_position), Transformation(pos_theta=self.goal_position))

    def get_opponent_net_position(self):
        if self.team == Robot.Team.FRIENDLY:
            opponent_goal = np.array([0, 4.5])
        else:
            opponent_goal = np.array([0, -4.5])
        return opponent_goal

    def observe_ball(self, ball: Optional[Ball]) -> bool:
        if ball is None or ball.position is None:
            return False
        theta = self.position[2]  # TODO change this to direction vector?
        ball_position = ball.position
        ball_diff = ball_position - self.position[0:2]
        angle = wrapToPi(theta - np.arctan2(ball_diff[1], ball_diff[0]))
        distance = np.linalg.norm(ball_diff)
        if abs(angle) < self.ObservationConstants.FOV / 2 and distance < self.ObservationConstants.VISION_RANGE:
            if self.observed_ball is None:
                self.observed_ball = Ball()
            self.observed_ball.position = ball_position
            self.observed_ball.last_observed_time_stamp = rospy.Time.now()
            self.robot_focused_on_ball_time = rospy.Time.now()

        # TODO can add noise here

    def observe_obstacles(self, robots: List[Robot]):
        self.observed_obstacles.clear()
        for robot in robots:
            if robot.robot_id != self.robot_id:
                theta = self.position[2]  # TODO change this to direction vector?
                arrow_len = 0.3
                arrow_end_x = math.cos(theta) * arrow_len
                arrow_end_y = math.sin(theta) * arrow_len
                robot_direction = np.array([arrow_end_x, arrow_end_y])
                obstacle_position = robot.position[0:2]
                obstacle_to_robot = np.array(obstacle_position) - np.array(self.position[0:2])
                angle = np.arccos(
                    np.dot(obstacle_to_robot[0:2], robot_direction) / (np.linalg.norm(obstacle_to_robot[0:2]) * np.linalg.norm(robot_direction))
                )
                distance = np.linalg.norm(obstacle_to_robot)
                if angle < self.ObservationConstants.FOV / 2 and distance < self.ObservationConstants.VISION_RANGE:
                    o = Obstacle()
                    o.position = robot.position[0:2]
                    o.team = robot.team
                    o.probability = (self.ObservationConstants.VISION_RANGE - distance) / self.ObservationConstants.VISION_RANGE
                    self.observed_obstacles.append(o)

    def get_robot_polygon(self):
        l = self.BODY_LENGTH
        w = self.BODY_WIDTH
        a = self.position[0:2]
        t = self.position[2]
        rotm = np.array([[np.cos(t), -np.sin(t)], [np.sin(t), np.cos(t)]])
        p1 = a + rotm @ np.array([l, w])
        p2 = a + rotm @ np.array([l, -w])
        p3 = a + rotm @ np.array([-l, -w])
        p4 = a + rotm @ np.array([-l, w])

        return [p1, p2, p3, p4]


from datetime import datetime
#Default spy details concept of classes is also used
class Spy:

  def __init__(self, name, salutation, age, rating):
    self.name = name
    self.salutation = salutation
    self.age = age
    self.rating = rating
    self.is_online = True
    self.chats = []
    self.current_status_message = None
    self.count=0

spy = Spy('Ishaan', 'Mr', 20, 5.0)

friend_one = Spy('Raja', 'Mr', 27, 4.9)
friend_two = Spy('rani', 'Ms', 21, 4.39)
friend_three = Spy('Barati', 'Dr', 37, 4.95)

friends = [friend_one, friend_two, friend_three]

#class ChatMessage is used to get the message sent by the user to his/her friends
class ChatMessage:

  def __init__(self, message, sent_by_me):
    self.message = message
    self.time = datetime.now()
    self.sent_by_me = sent_by_me


import ee
from ee_plugin import Map

#Tradicional print
print("Hola Mundo")

#Earth Engine object
print(ee.String('Hello World from Earth Engine!').getInfo())

#Area de estudio
Cordi_blanca = ee.FeatureCollection('users/mastergis01/Cordil_Blanca')
Map.addLayer(Cordi_blanca, {'color': 'red'}, 'Coordillera_Blanca')

#Funcion de fintrado de image collection
Id = ["LANDSAT/LT05/C01/T1_SR", "LANDSAT/LC08/C01/T1_SR"]
Date = ['2010-01-01','2011-01-01','2020-01-01','2021-01-01']
def fun_medTOA(id, date01, date02):
    img = ee.ImageCollection(id)\
            .filterBounds(Cordi_blanca)\
            .filterDate(date01, date02)\
            .filterMetadata('CLOUD_COVER', 'less_than', 10)\
            .median()\
            .clip(Cordi_blanca)

    return img

#Cargar las imagenes del 2010 y 2020
img_2010 = fun_medTOA(Id[0],Date[0], Date[1])
img_2020 = fun_medTOA(Id[1], Date[2], Date[3])
Map.addLayer(img_2010, {'min': 100, 'max': 2000, 'bands': ['B3', 'B2', 'B1']}, 'Img_2010')
Map.addLayer(img_2020, {'min': 100, 'max': 2000, 'bands': ['B4', 'B3', 'B2']}, 'Img_2020')

## Calculo del Indice NDVI para el año 2010 y 2020
#Calculo del NDVI
ndvi_2010 = img_2010.normalizedDifference(['B4', 'B3'])
ndvi_2020 = img_2020.normalizedDifference(['B5', 'B4'])

#Parametros de visualizacion ndvi
vis_parandvi ={
    'min':0,
    'max':0.6,
    'palette':["#051852", "#FFFFFF", "#C7B59B", "#A8B255", "#A3C020", "#76AD00","#429001", "#006400", "#003B00", "#000000"]
}
Map.addLayer(ndvi_2010, vis_parandvi, 'ndvi_2010')
Map.addLayer(ndvi_2020, vis_parandvi, 'ndvi_2020')

## Seleccion de las bandas térmicas 6 y 10 (con temperatura de brillo), sin cálculo
#Banda termica
thermal_2010 = img_2010.select('B6').multiply(0.1)
thermal_2020 = img_2020.select('B10').multiply(0.1)

#Parametros de visualizacion
vis_parathermal ={
    'min':291.918,
    'max':302.382,
    'palette':['042333', '2c3395', '744992', 'b15f82', 'eb7958', 'fbb43d', 'e8fa5b']
}

Map.addLayer(thermal_2010, vis_parathermal, 'thermal_2010')
Map.addLayer(thermal_2020, vis_parathermal, 'thermal_2020')

## Calculo de la Vegetacion fracturada (fv)
#Vegetacion fracturada del 2010 ((NDVI-minNDVI)/(maxNDVI-minNDVI))^2
fv_2010 = (ndvi_2010.subtract(ee.Number(-1)).divide(ee.Number(1).subtract(ee.Number(-1)))).pow(ee.Number(2)).rename('fv')
fv_2020 = (ndvi_2020.subtract(ee.Number(-1)).divide(ee.Number(1).subtract(ee.Number(-1)))).pow(ee.Number(2)).rename('fv')
#Parametros de visualizacion
para_fv ={
    'min':0,
    'max':0.6,
    'palette':['blue', 'white', 'green']
}
Map.addLayer(fv_2010, para_fv, 'fv_2010')
Map.addLayer(fv_2020, para_fv, 'fv_2020')

## Emissivity EM = Fv * 0.04 + 0.986
#Asignacion de valores
a= ee.Number(0.004)
b= ee.Number(0.986)

#Calculo de la emisibidad
EMM_2010 = fv_2010.multiply(a).add(b).rename('EMM')
EMM_2020 = fv_2020.multiply(a).add(b).rename('EMM')

#Parametros de visualizacion
Para_EMM = {
    'min':  0.9865619146722164,
    'max': 0.989699971371314,
    'palette':['181c43', '0c5ebe', '75aabe', 'f1eceb', 'd08b73', 'a52125', '3c0912']
}

#Visualizacion de resultados
Map.addLayer(EMM_2010, Para_EMM, 'EMM_2010')
Map.addLayer(EMM_2020, Para_EMM, 'EMM_2020')

############### Temperatura de la superficie terrestre(LST) en grados Celsius ################
# Land Surface Temperature
def fun_lst(thermal,EMM, Banda):
    lst = thermal.expression(
    '(Tb/(1 + (0.00115* (Tb / 1.438))*log(Ep)))-273.15', {
        'Tb': thermal.select(Banda),
        'Ep': EMM.select('EMM')
    }
    ).rename('LST')
    return lst
#Calculo
LST_2010 = fun_lst(thermal_2010, EMM_2010, 'B6' )
LST_2020 = fun_lst(thermal_2020, EMM_2020, 'B10' )

#Parametros de visualizacion
Para_LST = {
    'min':0,
    'max':10,
    'palette':[
                '040274', '040281', '0502a3', '0502b8', '0502ce', '0502e6',
                '0602ff', '235cb1', '307ef3', '269db1', '30c8e2', '32d3ef',
                '3be285', '3ff38f', '86e26f', '3ae237', 'b5e22e', 'd6e21f',
                'fff705', 'ffd611', 'ffb613', 'ff8b13', 'ff6e08', 'ff500d',
                'ff0000', 'de0101', 'c21301', 'a71001', '911003'
 ]
}

#Visualizacion de resultados LST
Map.addLayer(LST_2010, Para_LST, 'LST_2010')
Map.addLayer(LST_2020, Para_LST, 'LST_2020')


# coding: utf-8

# In[ ]:

from scipy import sparse
import numpy as np

train_text_tfidf=sparse.load_npz("./npz/train_apitext_valuetext.npz")
train_api_tfidf=sparse.load_npz("./npz/train_api_set_x.npz")
train_value_tfidf=sparse.load_npz("./npz/train_value_set_x.npz")
train_count_fea=np.load("./npy/train_count_fea_x.npy")

train_x=sparse.hstack((train_text_tfidf,train_api_tfidf))
train_x=sparse.hstack((train_x,train_value_tfidf))
train_x=sparse.hstack((train_x,train_count_fea))
print(train_x.shape)

# In[ ]:

test_text_tfidf=sparse.load_npz("./npz/test_apitext_valuetext.npz")
test_api_tfidf=sparse.load_npz("./npz/test_api_set_x.npz")
test_value_tfidf=sparse.load_npz("./npz/test_value_set_x.npz")
test_count_fea=np.load("./npy/test_count_fea_x.npy")

test_x=sparse.hstack((test_text_tfidf,test_api_tfidf))
test_x=sparse.hstack((test_x,test_value_tfidf))
test_x=sparse.hstack((test_x,test_count_fea))
print(test_x.shape)

# In[ ]:

import numpy as np
train_y=np.load("./npy/data1_train_y.npy")

# In[ ]:

from sklearn.model_selection import  StratifiedKFold
import lightgbm as lgb
from sklearn.metrics import  log_loss
nfold=10

kf=StratifiedKFold(nfold)
train_x=train_x.tocsr()
test_x=test_x.tocsr()

params = {
    'max_depth':-1,
    'boosting_type':'gbdt',
    'num_leaves': 31,
    'objective': 'multiclass',
    'min_data_in_leaf': 200,
    'learning_rate': 0.02,
    'max_bin':255,
    'feature_fraction': 0.64,
    'bagging_fraction': 0.8,
    'bagging_freq': 1,
    'metric': 'multi_logloss',
    'num_class':6,
    'num_threads': -1,
    'is_unbalance':True,
}
import numpy as np
oof_train=np.zeros((train_x.shape[0],6))
oof_test=np.zeros((test_x.shape[0],6))
oof_test_skf=np.zeros((nfold,test_x.shape[0],6))
logloss_csv=[]

for i,(train,test) in  enumerate(kf.split(train_x,train_y)):
    print("*******第 %d 折********"%i)

    x_tr=train_x[train]
    x_te=train_x[test]
    y_tr=train_y[train]
    y_te=train_y[test]

    lgb_train = lgb.Dataset(x_tr, y_tr)
    lgb_eval = lgb.Dataset(x_te,y_te)
    lgb_train = lgb.Dataset(x_tr, y_tr)
    lgb_eval = lgb.Dataset(x_te,y_te,reference=lgb_train)
    gbm = lgb.train(params,
                lgb_train,
                num_boost_round=1000,
                valid_sets=lgb_eval,
                early_stopping_rounds=10)
    eva_pre=gbm.predict(x_te,num_iteration=gbm.best_iteration)
    oof_train[test]=eva_pre
    logloss=log_loss(y_te,eva_pre)
    print("valid logloss %s"%logloss)
    logloss_csv.append(logloss)
    pre=gbm.predict(test_x,num_iteration=gbm.best_iteration)
    oof_test_skf[i,:]=pre

print("mean loss  %f"%np.mean(logloss_csv))

# In[ ]:

oof_test=oof_test_skf.mean(axis=0)
score=np.mean(logloss_csv)
print(score)

# In[ ]:

# import numpy as np
# def check_ans(result):
#     new_result=[]
#     for i in result:
#         m=i.sum()
#         for j in range(0,6):
#             i[j]=i[j]/m
#         if abs(i.sum()-1)>=1e-6:
#             print(i.sum()-1)
#         new_result.append(i)
#     return new_result

# In[ ]:

# import numpy as np

# res=check_ans(oof_test)

# In[ ]:

# #
# test_prob=pd.DataFrame(oof_test)
# test_prob.columns=['prob0','prob1','prob2','prob3','prob4','prob5']
# test_prob['file_id']=test_data['file_id']

# In[ ]:

# test_prob=test_prob[['file_id','prob0','prob1','prob2','prob3','prob4','prob5']]
# test_prob.head(20)

# In[ ]:

# test_prob.to_csv("lgb_submit_%f.csv"%np.mean(logloss_csv),index=False)

# In[ ]:

# test_prob.info()

# In[ ]:

#保存train_prob
import pandas as pd
train_num=1
train_prob=pd.DataFrame(oof_train)
train_prob.columns=['class_%s'%i for i  in   range(0,6)]
test_prob=pd.DataFrame(oof_test)
test_prob.columns=['class_%s'%i for i in range(0,6)]
train_prob.to_csv("./stacking_csv/cv10/train%d_lgb_train_prob_cv10_%f.csv"%(train_num,score),index=False)
test_prob.to_csv("./stacking_csv/cv10/train%d_lgb_test_prob_cv10_%f.csv"%(train_num,score),index=False)

# In[ ]:

np.mean(logloss_csv)

# In[ ]:

from sklearn.metrics import  classification_report
y_pr=[np.argmax(i) for i in eva_pre]
print(classification_report(y_te,y_pr))


import sqlite3
import pandas as pd

class App(tk.Frame):
    def __init__(self, master):
        super().__init__(master)
        self.pack()

        self.entrythingy = tk.Entry()
        self.entrythingy.pack()

        # Create the application variable.
        self.contents = tk.StringVar()
        # Set it to some value.
        self.contents.set("this is a variable")
        # Tell the entry widget to watch this variable.
        self.entrythingy["textvariable"] = self.contents

        # Define a callback for when the user hits return.
        # It prints the current value of the variable.
        self.entrythingy.bind('<Key-Return>',
                             self.print_contents)

    def print_contents(self, event):
        print("Hi. The current entry content is:",
              self.contents.get())

def read_from_db(entrada):

    cont = 0
    text = ''
    #print(data)
    for row in c.execute('SELECT Nombre, Sexo FROM CV'):
        cont+=1
        text += str(row)
        if cont == entrada:
            break
    return text

class MyWidget(BoxLayout):

    def __init__(self,entrada):
        super(MyWidget, self).__init__()
        for x in range(0,entrada):
            self.add_widget(campoBD())

class campoBD(BoxLayout):
    g = StringProperty()
    def __init__(self):
        super(campoBD, self).__init__()
        self.g = read_from_db(1)

class myApp(App):
    title = 'Plataforma'
    def build(self):
        return MyWidget(4)
    def on_pause(self):
        return True
    def on_resume(self): #¿Para que sirve esta función?
        pass

if __name__ == '__main__':

    table = "CV"
    path = "./cv_acosta.xlsx"

    xls = pd.ExcelFile(path)
    miConexion = sqlite3.connect('base')
    c = miConexion.cursor()
    df = pd.read_excel(path)

    df.to_sql(name = table, con = miConexion, if_exists = 'append', index = False)

    root = tk.Tk()
    myapp = App(root)
    myapp.mainloop()

    c.close
    miConexion.close


import pprint

import requests
from flask import Blueprint, render_template
from keys import KAKAO_REST_KEY

KAKAO_BASE_URL = 'https://dapi.kakao.com'
header = {'Authorization' : 'KakaoAK ' + KAKAO_REST_KEY}

news_blueprint = Blueprint('news', __name__)

@news_blueprint.route('/main')
def news_main():
    return 'welcome news {0}'.format("SOA news main")

@news_blueprint.route('/sports')
def news_sports():
    return 'welcome sports news {0}'.format("SOA Sports")

@news_blueprint.route('/images')
def books():
    res = requests.get(
        url=KAKAO_BASE_URL + '/v2/search/image?query=파이썬',
        headers=header
    )

    if res.status_code == 200:
        docs = res.json()
        images = []
        for image in docs['documents']:
            images.append(image['image_url'])
    else:
        print("Error : {0}".format(res.status_code))

    return render_template(
        'images.html',
        images=images,
        nav_menu='image'
    )

@news_blueprint.route('/books')
def images():
    res = requests.get(
        url=KAKAO_BASE_URL+'/v3/search/book?target=title&query=미움받을 용기',
        headers=header
    )

    if res.status_code == 200:
        docs = res.json()
        books = []
        for book in docs['documents']:
            books.append(book)
    else:
        print("Error : {0}".format(res.status_code))

    return render_template(
        'books.html',
        books=books,
        nav_menu='book'
    )


# Written by Seonwoo Min, Seoul National University (mswzeus@gmail.com)
# Some parts of the code were referenced from or inspired by below
# - Tristan Bepler's code (https://github.com/tbepler/protein-sequence-embedding-iclr2019)
# PLUS

""" P-ELMo model classes and functions """

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.utils.rnn import PackedSequence, pack_padded_sequence, pad_packed_sequence

from plus.model.plus_rnn import PLUS_RNN

class P_ELMo(PLUS_RNN):
    """ P-ELMo model """
    def __init__(self, cfg):
        super(PLUS_RNN, self).__init__()

        # LM hidden states -> EM input model (ELMo-style; use hidden states as input to EM model)
        self.fc_lm = nn.Linear(cfg.lm_dim, cfg.lm_proj_dim)
        self.x_embed = nn.Embedding(cfg.input_dim-1, cfg.lm_proj_dim, padding_idx=cfg.input_dim - 2)
        self.rnn = nn.LSTM(cfg.lm_proj_dim, cfg.hidden_dim, cfg.num_layers, bidirectional=True, batch_first=True)
        self.fc = nn.Linear(2 * cfg.hidden_dim, cfg.embedding_dim)

        # language modeling decoder
        self.decoder = nn.Linear(2 * cfg.hidden_dim, cfg.input_dim-1)

        # ordinal classification
        # initialize ordinal regression bias with 10 following ICLR19_Bepler
        bias_init = 10
        if cfg.num_classes is not None:
            self.ordinal_weight = nn.Parameter(torch.ones(1, cfg.num_classes))
            self.ordinal_bias = nn.Parameter(torch.zeros(cfg.num_classes) + bias_init)

    def forward(self, x_aa, x_lm, lengths):
        order = torch.argsort(lengths, descending=True)
        order_rev = torch.argsort(order)
        x_aa, x_lm, lengths = x_aa[order], x_lm[order], lengths[order]

        # language model + x embedding
        total_length = x_aa.shape[1] - 2
        for i in range(x_aa.size(0)): x_aa[i, 1:lengths[i] - 1] -= 1
        x_aa = pack_padded_sequence(x_aa[:, 1:-1], lengths - 2, batch_first=True)
        x_aa = PackedSequence(self.x_embed(x_aa.data), x_aa.batch_sizes)
        x_lm = pack_padded_sequence(x_lm, lengths - 2, batch_first=True)
        x_lm = PackedSequence(self.fc_lm(x_lm.data), x_lm.batch_sizes)
        h = PackedSequence(nn.ReLU()(x_aa.data + x_lm.data), x_aa.batch_sizes)

        # feed-forward bidirectional RNN
        self.rnn.flatten_parameters()
        h, _ = self.rnn(h)
        h = pad_packed_sequence(h, batch_first=True, total_length=total_length)[0][order_rev]

        # final projection
        z = self.fc(h)
        return z, h

    def em(self, h, lengths, cpu=False):
        # get representations with different lengths from the collated single matrix
        e = [None] * len(lengths)
        for i in range(len(lengths)):
            if cpu: e[i] = h[i, :lengths[i] - 2].cpu()
            else:   e[i] = h[i, :lengths[i] - 2]
        return e

class P_ELMo_lm(nn.Module):
    """ language model for P-ELMo """
    def __init__(self, cfg):
        super(P_ELMo_lm, self).__init__()
        self.embed = nn.Embedding(cfg.input_dim, cfg.input_dim-1, padding_idx=cfg.input_dim-1)

        input_dim = cfg.input_dim - 1
        layers = []
        for _ in range(cfg.num_layers):
            layers.append(nn.LSTM(input_dim, cfg.hidden_dim, 1, batch_first=True))
            input_dim = cfg.hidden_dim
        self.rnn = nn.ModuleList(layers)

        self.decoder = nn.Linear(cfg.hidden_dim, cfg.input_dim-1)

    def forward(self, x, lengths):
        x = self.embed(x)
        h_fwd, h_rvs = x[:, :-1], x[:, 1:]
        h_fwd, h_rvs = self.transform(h_fwd, self.reverse(h_rvs))

        b, l, d = h_fwd.size()
        h_fwd = self.decoder(h_fwd.contiguous().view(-1, d))
        h_rvs = self.decoder(h_rvs.contiguous().view(-1, d))

        zero = h_fwd.new_zeros((b, 1, h_fwd.size(1)))
        h_fwd = torch.cat((zero, h_fwd.view(b, l, -1)), 1)
        h_rvs = torch.cat((h_rvs.view(b, l, -1), zero), 1)
        logits = F.log_softmax(h_fwd + h_rvs, dim=2)

        return logits

    def load_weights(self, pretrained_model):
        # load pretrained_model weights
        state_dict = self.state_dict()
        for key, value in torch.load(pretrained_model, map_location=torch.device('cpu')).items():
            if key.startswith("module"): key = key[7:]
            if key in state_dict and value.shape == state_dict[key].shape: state_dict[key] = value
        self.load_state_dict(state_dict)

    def encode(self, x, lengths):
        order = torch.argsort(lengths, descending=True)
        order_rev = torch.argsort(order)
        x, lengths = x[order], lengths[order]

        with torch.no_grad():
            x = self.embed(x)
            h_fwd, h_rvs = x[:, :-1], x[:, 1:]
            total_length = x.shape[1] - 1
            h_fwd = pack_padded_sequence(h_fwd, lengths - 1, batch_first=True)
            h_rvs = pack_padded_sequence(h_rvs, lengths - 1, batch_first=True)
            h = self.transform(h_fwd, self.reverse(h_rvs, total_length), total_length, last_only=False)[order_rev]

        return h

    def reverse(self, h, total_length=None):
        # reverse given input tensor for reverse direction RNN
        if isinstance(h, PackedSequence):
            h, lengths = pad_packed_sequence(h, batch_first=True, total_length=total_length)
            h_rvs = h.clone().zero_()
            for i in range(h.size(0)):
                n = lengths[i]
                idx = [j for j in range(n - 1, -1, -1)]
                idx = torch.LongTensor(idx).to(h.device)
                h_rvs[i, :n] = h[i].index_select(0, idx)
            # repack h_rvs
            h_rvs = pack_padded_sequence(h_rvs, lengths, batch_first=True)
        else:
            idx = [i for i in range(h.size(1) - 1, -1, -1)]
            idx = torch.LongTensor(idx).to(h.device)
            h_rvs = h.index_select(1, idx)
        return h_rvs

    def transform(self, h_fwd, h_rvs, total_length=None, last_only=True):
        # transform into forward and reverse rnn hidden states
        h = []
        for l in range(len(self.rnn)):
            self.rnn[l].flatten_parameters()
            h_fwd, _ = self.rnn[l](h_fwd)
            h_rvs, _ = self.rnn[l](h_rvs)
            if not last_only:
                h.append(pad_packed_sequence(h_fwd, batch_first=True, total_length=total_length)[0][:, :-1])
                h.append(pad_packed_sequence(self.reverse(h_rvs), batch_first=True, total_length=total_length)[0][:, 1:])

        if not last_only:
            return torch.cat(h, 2)
        else:
            return h_fwd, self.reverse(h_rvs)


import cv2
import numpy as np
import mediapipe as mp
from src import string_constants, var, landmark_condition
from pynput.keyboard import Controller, Key

keyboard = Controller()
mp_hands = mp.solutions.hands
hands = mp_hands.Hands()
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles

def put_text_in_middle(image, text):
    text_size = cv2.getTextSize(text, var.font, var.scale_default, var.thickness_double)[0]

    text_x = (image.shape[1] - text_size[0]) // 2
    text_y = (image.shape[0] + text_size[1]) // 2

    cv2.putText(image, text, (text_x, text_y), var.font, var.scale_default, var.white_color, var.thickness_double)
    return image

def format_number_lead_zero(num):
    return str(num) if num >= 10 else '0' + str(num)

def add_normal_text_to_right_bottom(image, text):
    x = 10
    y = image.shape[0] - 10
    cv2.putText(image, text, (x, y), var.font, var.scale_half, var.white_color, var.thickness_double)

def add_warning_text_to_bottom_left(image, text):
    x = 10
    y = image.shape[0] - 30
    cv2.putText(image, text, (x, y), var.font, var.scale_half, var.white_color, var.thickness_double)

def draw_hand_bounding_box(frame, hand_landmarks, player_name):
    image_height, image_width, _ = frame.shape
    landmark_list = []
    for landmark in hand_landmarks.landmark:
        x = int(landmark.x * image_width)
        y = int(landmark.y * image_height)
        landmark_list.append((x, y))

    x, y, w, h = cv2.boundingRect(np.array(landmark_list))
    cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

    text_size = cv2.getTextSize(player_name,
                                var.font, var.scale_half,
                                var.thickness_double
                                )[0]

    cv2.rectangle(frame, (x - 1, y - text_size[1] - var.space_between),
                  (x + text_size[0], y + text_size[1] - 2 * var.space_between - 1),
                  var.green_color, var.border_fill)
    cv2.putText(frame, player_name,
                (x, y - var.space_between),
                var.font, var.scale_half,
                var.white_color, var.thickness_double)

def get_hand_move(hand_landmarks):
    landmarks = hand_landmarks.landmark
    if all([landmarks[i].y < landmarks[i + 3].y for i in range(9, 20, 4)]):
        return string_constants.ROCK
    elif landmarks[13].y < landmarks[16].y and landmarks[17].y < landmarks[20].y:
        return string_constants.SCISSORS
    else:
        return string_constants.PAPER

def draw_multiline_text(image, text):
    lines = text.split('\n')
    total_text_height = len(lines) * cv2.getTextSize(lines[0], var.font, var.scale_half, var.thickness_double)[0][1]
    start_y = (image.shape[0] - total_text_height) // 2
    try:
        margin_x = cv2.getTextSize(lines[1], var.font, var.scale_half, var.thickness_double)[0][0]
    except Exception as e:
        margin_x = cv2.getTextSize(lines[0], var.font, var.scale_half, var.thickness_double)[0][0]
    start_x = (image.shape[1] - margin_x) // 2
    for line in lines:
        text_size = cv2.getTextSize(line, var.font, var.scale_half, var.thickness_double)[0]
        # start_x = (image.shape[1] - text_size[0]) // 2
        cv2.putText(image, line, (start_x, start_y), var.font, var.scale_half, var.white_color, var.thickness_double)
        start_y = int(start_y + text_size[1] * var.line_height)
    return image

def check_winner(player1, player2):
    if player1 == player2:
        return string_constants.TIED
    elif player1 == string_constants.PAPER and player2 == string_constants.ROCK:
        return string_constants.PLAYER1_WIN
    elif player1 == string_constants.ROCK and player2 == string_constants.SCISSORS:
        return string_constants.PLAYER1_WIN
    elif player1 == string_constants.SCISSORS and player2 == string_constants.PAPER:
        return string_constants.PLAYER1_WIN
    else:
        return string_constants.PLAYER2_WIN

def draw_landmarks(frame, hand_landmarks):
    mp_drawing.draw_landmarks(
        frame, hand_landmarks, mp_hands.HAND_CONNECTIONS,
        mp_drawing_styles.get_default_hand_landmarks_style(),
        mp_drawing_styles.get_default_hand_connections_style()
    )

def game1_running(clock, success, game_text, player1, player2, results):
    if 0 <= clock <= 20:
        success = True
        game_text = string_constants.start
    elif clock < 50:
        game_text = f"{5 - clock // 10}..."
    # elif clock < 40:
    #     game_text = "2..."
    # elif clock < 50:
    #     game_text = "1..."
    elif clock < 60:
        game_text = string_constants.process
    elif clock == 60:
        hand_landmarks = results.multi_hand_landmarks
        if hand_landmarks and len(hand_landmarks) == 2:
            player1 = get_hand_move(hand_landmarks[0])
            player2 = get_hand_move(hand_landmarks[1])
        else:
            success = False
            game_text = string_constants.FAIL
    elif clock < var.MAX_TIME:
        if success:
            game_text = f"{string_constants.PLAYER1} {player1}.\n{string_constants.PLAYER2} {player2}.\n"
            game_text += check_winner(player1, player2)
            print(game_text)
    return clock, success, game_text, player1, player2

def get_current_option_and_warming(frame, text_frame):
    results = hands.process(frame)
    if results.multi_hand_landmarks is None:
        return None
    if len(results.multi_hand_landmarks) == 1:
        hand_landmarks = results.multi_hand_landmarks[0]
        draw_hand_bounding_box(text_frame, hand_landmarks, string_constants.PLAYER1)
        if landmark_condition.is_five(hand_landmarks):
            return 5
        if landmark_condition.is_one(hand_landmarks):
            return 1
        if landmark_condition.is_three(hand_landmarks):
            return 3
        if landmark_condition.is_two(hand_landmarks):
            return 2
        else:
            return None
    elif len(results.multi_hand_landmarks) == 2:
        add_warning_text_to_bottom_left(text_frame, string_constants.warn_one_hand)
    return None

def detect_rock_gestures(hand_landmarks):
    if (hand_landmarks.landmark[8].y > hand_landmarks.landmark[5].y) \
            and (hand_landmarks.landmark[12].y > hand_landmarks.landmark[9].y) \
            and (hand_landmarks.landmark[16].y > hand_landmarks.landmark[13].y):
        return string_constants.ROCK

def put_text_horizontal(image, is_horizontal=False):
    cv2.putText(image, string_constants.waiting_to_exit if is_horizontal else string_constants.tutor_to_exit,
                var.position_horizontal_text,
                var.font,
                var.scale_half, var.white_color, var.thickness_double)

def is_quite_game(is_quit, flag_game, option, game_clock):
    if is_quit:
        return False, False, None, var.MIN_TIME, var.MIN_TIME
    return is_quit, flag_game, option, var.MIN_TIME, game_clock

def change_video_capture_size(video_capture):
    video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, var.new_width)
    video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, var.new_height)
    pass


from skimage import feature
import numpy as np
from PIL import Image

class LocalBinaryPatterns:

    def __init__(self, numPoints, radius):
        # store the number of points and radius
        self.numPoints = numPoints
        self.radius = radius

    def describe(self, image, eps=1e-7):
        # compute the Local Binary Pattern representation
        # of the image, and then use the LBP representation
        # to build the histogram of patterns
        lbp = feature.local_binary_pattern(image, self.numPoints,
                                           self.radius, method="uniform")
        (hist, _) = np.histogram(lbp.ravel(),
                                 bins=np.arange(0, self.numPoints + 3),
                                 range=(0, self.numPoints + 2))

        # normalize the histogram
        hist = hist.astype("float")
        hist /= (hist.sum() + eps)

        # return the histogram of Local Binary Patterns
        return hist

    def get_lbp_feature(self, image_path):
        img = Image.open(image_path).convert('L')
        return self.describe(img)

def get_LBP_feature_vector(imagePath):
    image = cv2.imread(imagePath)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    hist = desc.describe(gray)

    # extract the label from the image path, then update the
    # label and data lists
    data.append(hist)
    return data

desc = LocalBinaryPatterns(24, 8)
# data = []
# imagePath = "cover.jpgs"
# data = get_LBP_feature_vector(imagePath)
# print(data)
print(desc.get_lbp_feature('cover.jpg'))


import socket, cv2, pickle, struct

fuente1 = "rtsp://192.168.226.202:554"

host_ip = '192.168.226.100'
print('HOST IP:',host_ip)
port = 9999
socket_address = (host_ip,port)

# Socket Create
server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server_socket.bind(socket_address)	# Socket Bind
server_socket.listen(5) 			# Socket Listen
print("LISTENING AT:",socket_address)

client_socket,addr = server_socket.accept()
if client_socket:
	print('GOT CONNECTION FROM:',addr)
	rov_cam1 = cv2.VideoCapture(fuente1)

	while(rov_cam1.isOpened()):
		ret,frame = rov_cam1.read()

		if ret == True:
			# Pack size of frame + frame
			#frame = cv2.resize(frame,(176,144))
			a = pickle.dumps(frame)
			message = struct.pack("Q",len(a))+a
			client_socket.sendall(message)


import requests as r
import config as cfg
from collections import deque
import sys
import time
import os

tree = dict()  #
d = dict()  #
q = deque()  #
used = dict()  #
pred = dict()  #
fout = open("order.txt", "w")

def nameFromID(id):
    response = r.get(
        "https://api.vk.com/method/users.get?access_token={0}&user_ids={1}&v=5.75".format(cfg.token, id))
    name = response.json()["response"][0]["first_name"]
    surname = response.json()["response"][0]["last_name"]
    return name + " " + surname

maxD, ids, idf = sys.argv[1:]
maxD = int(maxD)

start = time.clock()

response = r.get(
    "https://api.vk.com/method/users.get?access_token={0}&user_ids={1}&v=5.75".format(cfg.token, ids))
ids = response.json()["response"][0]["id"]
response = r.get(
    "https://api.vk.com/method/users.get?access_token={0}&user_ids={1}&v=5.75".format(cfg.token, idf))
idf = response.json()["response"][0]["id"]

q.append(ids)
d[ids] = 0
pred[ids] = ids
used[ids] = True
curD = 0
found = False
count = 0

while len(q) > 0 and curD <= maxD and not found:
    start = time.clock()
    v = q.popleft()
    curD = d[v]
    if v == idf:
        found = True
    response = None
    while response is None:
        try:
            response = r.get(
                "https://api.vk.com/method/friends.get?access_token={0}&user_id={1}&v=5.75".format(cfg.token, v), timeout=5)
        except r.exceptions.BaseHTTPError as e:
            print("Connection error, trying again in 2s")
            print()
            time.sleep(2)
            continue
    items = response.json().get("response", {"items": []})["items"]
    for item in items:
        if used.get(item, False):
            continue
        used[item] = True
        d[item] = d[v] + 1
        pred[item] = v
        if item == idf:
            found = True
            break
        q.append(item)
        count += 1

    sys.stdout.write("\rTotal people checked: " + str(count))
    sys.stdout.flush()
    time.sleep(0.1)

print()
if found:
    if not os.path.exists("img"):
        os.makedirs("img")
    id = idf
    order = []
    while d[id] > 0:
        order.append(nameFromID(id))
        order.append(str(id))
        response = r.get(
            "https://api.vk.com/method/users.get?access_token={0}&fields=photo_max&user_ids={1}&v=5.75".format(cfg.token, id))
        imgR = r.get(response.json()["response"][0]["photo_max"])
        with open("img/" + str(id) + ".jpg", "wb") as img:
            img.write(imgR.content)
            img.close()
        id = pred[id]
    response = r.get(
        "https://api.vk.com/method/users.get?access_token={0}&fields=photo_max&user_ids={1}&v=5.75".format(cfg.token, id))
    imgR = r.get(response.json()["response"][0]["photo_max"])
    with open("img/" + str(id) + ".jpg", "wb") as img:
        img.write(imgR.content)
        img.close()
    order.append(nameFromID(id))
    order.append(str(id))
    order.reverse()
    print(len(order) // 2, file=fout)
    for id in order:
        print(id, file=fout)
    fout.close()
else:
    print("Sorry, finish id is out of max range")


"""
Module that reduces scenario_trees and create  a graph structure
"""

import time

import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
import pandas as pd

# tree = pd.read_csv('test.csv', delimiter = ';', decimal = ',', header = 0)
# tree = np.array(tree)

# plt.plot(tree)
# n_scen = len(tree[0])
# print(n_scen)

# print('tree_prob', tree_prob)
# print(tree)

class Scenario_builder():
    """Main class, se référer à l'algorithme tel que présenté dans la biblio
    """

    def __init__(self):
        pass

    def data_loader_csv(self, file, header=0):
        """
        Load csv
        """
        tree = pd.read_csv(file, delimiter=';', decimal=',', header=header)
        self.tree = np.array(tree)
        self.tree_prob = np.ones(self.n_scen) / self.n_scen
        # plt.plot(self.tree, color = 'b')
        # plt.show()

    def data_loader_df(self, df):
        """
        Load Data
        """
        tree_with_head = np.array(df)
        tree = tree_with_head[1:, :-1]
        self.tree = tree
        self.tree_prob = np.ones(self.n_scen) / self.n_scen
        plt.plot(self.tree)
        plt.xlabel('nombre d étapes')
        plt.ylabel('Prix en euro')
        plt.title('Arbres de scénarios')
        plt.show()

    def distance_scen_pair(self, i, j, t_max):
        return sum(abs(self.tree.transpose()[
                   i][:t_max] - self.tree.transpose()[j][:t_max]))

    n_scen = property(lambda self: len(self.tree[0]))
    T = property(lambda self: len(self.tree))

    def mat_distance_scen(self, T_max):
        index = {i for i in range(self.n_scen)}
        C_K_J = []
        for k in range(self.n_scen):
            C_J = []
            for j in range(self.n_scen):
                C_J.append(self.distance_scen_pair(k, j, T_max))
            C_K_J.append(C_J)
        C_K_J = np.array(C_K_J)
        self.C_K_J = C_K_J

    def backward_reduction(self, n_deletion, T_max=False):
        if not T_max:
            T_max = self.T
        else:
            pass
        index = {i for i in range(self.n_scen)}
        J = set()
        C_LL = np.zeros(self.n_scen)
        self.mat_distance_scen(T_max)
        for l in range(self.n_scen):
            C_LL[l] = min(self.C_K_J[l][list(index.difference(J | {l}))])
        z_L = self.tree_prob * C_LL
        l = np.argmin(z_L)
        J = J | {l}
        # Redondance avec l'initialisation à voir
        while len(J) < n_deletion:
            Z_L = []
            for l in index.difference(J):
                C_KL = []
                for k in (J | {l}):
                    c_kl = min(self.C_K_J[k][list(index.difference(J | {l}))])
                    C_KL.append(c_kl)
                C_KL = np.array(C_KL)
                z_l = sum(self.tree_prob[np.array(list(J | {l}))] * C_KL)
                # FAire des test sur la taille des listes qui sont crées
                Z_L.append(z_l)
            l = list(index.difference(J))[np.argmin(Z_L)]
            J = J | {l}
        return J

    def backward_reduction_iter(self, index, T_max=False):

        if not T_max:
            T_max = self.T
        else:
            pass
        J = set()
        C_LL = np.zeros(self.n_scen)
        self.mat_distance_scen(T_max)
        while len(J) < self.n_scen:
            Z_L = []
            for l in index.difference(J):
                C_KL = []
                for k in (J | {l}):
                    c_kl = min(self.C_K_J[k][list(index.difference(J | {l}))])
                    C_KL.append(c_kl)
                C_KL = np.array(C_KL)
                z_l = sum(self.tree_prob[np.array(list(J | {l}))] * C_KL)
                # FAire des test sur la taille des listes qui sont crées
                Z_L.append(z_l)
            l = list(index.difference(J))[np.argmin(Z_L)]
            J = J | {l}
            yield J, min(Z_L)

    def scenario_deletion(self, n_deletion):
        index = {i for i in range(self.n_scen)}
        tree_prob = self.tree_prob
        b = sum(self.tree_prob)
        J = self.backward_reduction(n_deletion)
        for i in J:
            j = np.argmin(self.C_K_J[i][list(index.difference(J))])
            j = list(index.difference(J))[j]
            # assert not j in J : A passer en test
            tree_prob[j] += tree_prob[i]

        tree_t = self.tree.transpose()[list(index.difference(J))]
        self.tree = tree_t.transpose()
        self.tree_prob = tree_prob[list(index.difference(J))]
        a = sum(self.tree_prob)
        print(a - b)

    def plot_tree(self):
        plt.plot(self.tree)
        plt.xlabel('nombre d étapes')
        plt.ylabel('Prix en euro')
        plt.title('Arbres de scénarios')
        plt.show()

    def scenario_tree_construction(self, epsilon):
        index = {i for i in range(self.n_scen)}
        boundings = {}
        for pas in range(1, self.T):
            # Reduction

            iter_J = self.backward_reduction_iter(index, self.T - pas + 1)
            J, Z = next(iter_J)
            while Z < epsilon:
                J, Z = next(iter_J)
            # print('k',pas)
            # print('I',index)
            # print('J',J)

            # scenario bundling
            for j in J:
                i = np.argmin(self.C_K_J[j][list(index.difference(J))])
                i = list(index.difference(J))[i]
                self.tree_prob[i] += self.tree_prob[j]
                self.tree[:self.T - pas + 1,
                          j] = self.tree[:self.T - pas + 1, i]
                transfer_bound = [j]
                if j in boundings.keys():
                    bounds = boundings[j]
                    for q in bounds:
                        self.tree[:self.T - pas + 1,
                                  q] = self.tree[:self.T - pas + 1, i]
                    transfer_bound.append(j)
                if i in boundings.keys():
                    boundings[i] = boundings[i] + transfer_bound
                else:
                    boundings[i] = transfer_bound
            # plt.plot(self.tree)
            # plt.show()
            for i in range(2, self.n_scen):
                pass
            index = index.difference(J)
            if len(index) <= 1:
                break

    def nx_graph_builder(self):
        G = nx.DiGraph()
        n, m = self.tree.shape
        G.add_node('root', price=self.tree[0, 0], probability=1)
        for i in range(m):
            last_node = 'root'
            for j in range(1, n):
                G.add_node(
                    f'node{i}_{j}', price=self .tree[j, i], probability=1 / self.n_scen)
                G.add_edge(last_node, f'node{i}_{j}')
                last_node = f'node{i}_{j}'
        self.graph = G

    def plot_graph(self):
        nx.draw_shell(self.graph)
        plt.show()

# builder = Scenario_builder()
# plt.show()
# builder.data_loader('test.csv')
# builder.scenario_tree_construction(7)

# builder.plot_tree()


from django.http import HttpResponse
from django.shortcuts import render

def home(request):
    return HttpResponse("Hello!")

def blog(request):
    return HttpResponse("Welcome to my Blog! ENJOY!")

def about2(request):
    pangalan = 'Rachel'
    apelyido = 'Paras'
    edad = '20'

    return render(request, "about2.html", {'fname':pangalan, 'lname':apelyido, 'age':edad})

def about(request):
    pangalan = 'Mae'
    apelyido = 'De Mesa'
    edad = '20'
    kasabihan = 'Live life to the fullest'
    snumber = 'A20-29378'
    info = 'Hi, I am currently a 3rd year BSIS with specialization in Business Analytics, I wanted to become a data analyst someday \
        or a project manager, But I hate programming subject'

    return render(request, "about.html", {'fname':pangalan, 'lname':apelyido, 'age':edad, 'motto':kasabihan, 'snumber':snumber, 'aboutme': info,})

def Score(request):

    quiz1 = '78'
    quiz2 = '85'
    quiz3 = '90'
    Fullname = 'Mae Dador De Mesa'

    return render(request, 'quiz.html', {'quiz1' : quiz1, 'quiz2' : quiz2, 'quiz3': quiz3, 'fnme' : Fullname,})


# -*- coding: utf-8 -*-
__author__ = 'Tao Kong'
import logging
import os
import time

from common import globalvariable

PATH = lambda path: os.path.abspath(
    os.path.join(
        os.path.dirname(__file__), path
    )
)

class BasePage:
    def __init__(self, appium_driver):
        self.__driver = appium_driver
        self.logger = logging.getLogger(globalvariable.get_value("LOGGER_NAME"))

    @property
    def driver(self):
        return self.__driver

    @property
    def window_width(self):
        width = self.driver.get_window_size()['width']
        return width

    @property
    def window_height(self):
        height = self.driver.get_window_size()['height']
        return height

    @property
    def window_size(self):
        size = self.driver.get_window_size()
        return {'width': size['width'], 'height': size['height']}

    def back(self):
        self.driver.back()

    def press_key(self, code):
        self.driver.press_keycode(code)

    def switch_to_web_view(self, view_name):
        self.driver.switch_to.context(view_name)

    def set_value_by_keys(self, value):
        letterToCodeHashMap = {}
        lettersStr = "a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z @ . + _ 0 1 2 3 4 5 6 7 8 9".split()
        androidCodes = [29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
                        53, 54, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
                        51, 52, 53, 54, 77, 56, 81, 69, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
        for index in range(len(lettersStr)):
            letterToCodeHashMap[lettersStr[index]] = androidCodes[index]
        for i in range(len(value)):
            code = letterToCodeHashMap.get(value[i])
            self.pressKey(code)
            time.sleep(0.5)

    # 屏幕向上滑动
    def swipe_up(self, t=1000):
        x1 = int(self.window_width * 0.5)  # x坐标
        y1 = int(self.window_height * 0.75)  # 起始y坐标
        y2 = int(self.window_height * 0.25)  # 终点y坐标
        self.driver.swipe(x1, y1, x1, y2, t)
        time.sleep(1.5)

    # 屏幕向下滑动
    def swipe_down(self, t=1000):
        x1 = int(self.window_width * 0.5)  # x坐标
        y1 = int(self.window_height * 0.25)  # 起始y坐标
        y2 = int(self.window_height * 0.75)  # 终点y坐标
        self.driver.swipe(x1, y1, x1, y2, t)
        time.sleep(1.5)

    # 屏幕向左滑动
    def swipe_left(self, t=1000):
        x1 = int(self.window_width * 0.75)
        y1 = int(self.window_height * 0.5)
        x2 = int(self.window_width * 0.05)
        self.driver.swipe(x1, y1, x2, y1, t)
        time.sleep(1.5)

    # 屏幕向右滑动
    def swipe_right(self, t=1000):
        x1 = int(self.window_width * 0.05)
        y1 = int(self.window_height * 0.5)
        x2 = int(self.window_width * 0.75)
        self.driver.swipe(x1, y1, x2, y1, t)
        time.sleep(1.5)

    # savePngName:生成图片的名称
    def save_png_name(self, name):
        """
        name：自定义图片的名称
        """
        day = time.strftime('%Y-%m-%d', time.localtime(time.time()))
        fp = os.path.join(".." + os.sep + ".." + os.sep, "result", day, "image", day)
        tm = self.save_time()
        img_type = ".png"
        if os.path.exists(PATH(fp)):
            filename = os.path.join(PATH(fp), tm + "_" + name + img_type)
            print(PATH(filename))
            # print "True"
            return PATH(filename)
        else:
            os.makedirs(PATH(fp))
            filename = os.path.join(PATH(fp), tm + "_" + name + img_type)
            print(PATH(filename))
            # print "True"
            return PATH(filename)

    # 获取系统当前时间
    def save_time(self):
        """
        返回当前系统时间以括号中（2014-08-29-15_21_55）展示
        """
        return time.strftime('%Y-%m-%d-%H_%M_%S', time.localtime(time.time()))

    # saveScreenshot:通过图片名称，进行截图保存
    def save_screenshot(self, name):
        """
        快照截图
        name:图片名称
        """
        # 获取当前路径
        # print os.getcwd()
        image = self.driver.save_screenshot(self.save_png_name(name))
        return image


from functools import partial
import logging
import math
from typing import Tuple, List, Callable, Any, Dict, Sequence, Optional

import torch
import torch.nn as nn

@torch.jit.script
def get_padding_size(length, dap_size):
    return (int(length / dap_size) + int((length % dap_size)!=0)) * dap_size - length

@torch.jit.script
def padding_feat(feat:torch.Tensor, ps1:torch.Tensor, ps2:torch.Tensor, is_mask:bool=False):
    if ps1 == 0 and ps2 == 0:
        return feat
    else:
        if is_mask:
            feat = torch.nn.functional.pad(feat, (0, int(ps1), 0, int(ps2)))
        else:
            feat = torch.nn.functional.pad(feat, (0, 0, 0, int(ps1), 0, int(ps2)))
        return feat

@torch.jit.script
def split_feat(feat, ps1, ps2, has_batch=False):
    if ps1 == 0 and ps2 == 0:
        return feat
    else:
        if has_batch:
            if ps1 != 0:
                feat = feat[:, :-ps1]
            if ps2 != 0:
                feat = feat[:, :, :-ps2]
        else:
            if ps1 != 0:
                feat = feat[:-ps1]
            if ps2 != 0:
                feat = feat[:, :-ps2]
        return feat

def permute_final_dims(tensor, inds):
    zero_index = -1 * len(inds)
    first_inds = [i for i in range(len(tensor.shape[:zero_index]))]
    return tensor.permute(first_inds + [zero_index + i for i in inds])

def flatten_final_dims(t, no_dims):
    return t.reshape(t.shape[:-no_dims] + (-1,))

def one_hot(x, num_classes, dtype=torch.float32):
    x_one_hot = torch.zeros(*x.shape, num_classes, dtype=dtype, device=x.device)
    x_one_hot.scatter_(-1, x.long().unsqueeze(-1), 1)
    return x_one_hot

def _fetch_dims(tree):
    shapes = []
    tree_type = type(tree)
    if tree_type is dict:
        for v in tree.values():
            shapes.extend(_fetch_dims(v))
    elif tree_type is list or tree_type is tuple:
        for t in tree:
            shapes.extend(_fetch_dims(t))
    elif tree_type is torch.Tensor:
        shapes.append(tree.shape)
    elif tree_type is torch.nn.parameter.Parameter:
        shapes.append(tree.shape)
    else:
        raise ValueError(f"{tree_type} Not supported")

    return shapes

def dict_map(fn, dic, leaf_type):
    new_dict = {}
    for k, v in dic.items():
        if type(v) is dict:
            new_dict[k] = dict_map(fn, v, leaf_type)
        else:
            new_dict[k] = tree_map(fn, v, leaf_type)

    return new_dict

def tree_map(fn, tree, leaf_type):
    if isinstance(tree, dict):
        return dict_map(fn, tree, leaf_type)
    elif isinstance(tree, list):
        return [tree_map(fn, x, leaf_type) for x in tree]
    elif isinstance(tree, tuple):
        return tuple([tree_map(fn, x, leaf_type) for x in tree])
    elif isinstance(tree, leaf_type):
        return fn(tree)
    else:
        print(type(tree))
        raise ValueError("Not supported")

tensor_tree_map = partial(tree_map, leaf_type=torch.Tensor)

@torch.jit.ignore
def _flat_idx_to_idx(
    flat_idx: int,
    dims: Tuple[int],
) -> Tuple[int]:
    idx = []
    for d in reversed(dims):
        idx.append(flat_idx % d)
        flat_idx = flat_idx // d

    return tuple(reversed(idx))

@torch.jit.ignore
def _get_minimal_slice_set(
    start: Sequence[int],
    end: Sequence[int],
    dims: int,
    start_edges: Optional[Sequence[bool]] = None,
    end_edges: Optional[Sequence[bool]] = None,
) -> Sequence[Tuple[int]]:
    """
        Produces an ordered sequence of tensor slices that, when used in
        sequence on a tensor with shape dims, yields tensors that contain every
        leaf in the contiguous range [start, end]. Care is taken to yield a
        short sequence of slices, and perhaps even the shortest possible (I'm
        pretty sure it's the latter).

        end is INCLUSIVE.
    """
    # start_edges and end_edges both indicate whether, starting from any given
    # dimension, the start/end index is at the top/bottom edge of the
    # corresponding tensor, modeled as a tree
    def reduce_edge_list(l):
        tally = 1
        for i in range(len(l)):
            reversed_idx = -1 * (i + 1)
            l[reversed_idx] *= tally
            tally = l[reversed_idx]

    if(start_edges is None):
        start_edges = [s == 0 for s in start]
        reduce_edge_list(start_edges)
    if(end_edges is None):
        end_edges = [e == (d - 1) for e,d in zip(end, dims)]
        reduce_edge_list(end_edges)

    # Base cases. Either start/end are empty and we're done, or the final,
    # one-dimensional tensor can be simply sliced
    if(len(start) == 0):
        return [tuple()]
    elif(len(start) == 1):
        return [(slice(start[0], end[0] + 1),)]

    slices = []
    path = []

    # Dimensions common to start and end can be selected directly
    for s,e in zip(start, end):
        if(s == e):
            path.append(slice(s, s + 1))
        else:
            break

    path = tuple(path)
    divergence_idx = len(path)

    # start == end, and we're done
    if(divergence_idx == len(dims)):
        return [tuple(path)]

    def upper():
        sdi = start[divergence_idx]
        return [
            path + (slice(sdi, sdi + 1),) + s for s in
            _get_minimal_slice_set(
                start[divergence_idx + 1:],
                [d - 1 for d in dims[divergence_idx + 1:]],
                dims[divergence_idx + 1:],
                start_edges=start_edges[divergence_idx + 1:],
                end_edges=[1 for _ in end_edges[divergence_idx + 1:]]
            )
        ]

    def lower():
        edi = end[divergence_idx]
        return [
            path + (slice(edi, edi + 1),) + s for s in
            _get_minimal_slice_set(
                [0 for _ in start[divergence_idx + 1:]],
                end[divergence_idx + 1:],
                dims[divergence_idx + 1:],
                start_edges=[1 for _ in start_edges[divergence_idx + 1:]],
                end_edges=end_edges[divergence_idx + 1:],
            )
        ]

    # If both start and end are at the edges of the subtree rooted at
    # divergence_idx, we can just select the whole subtree at once
    if(start_edges[divergence_idx] and end_edges[divergence_idx]):
        slices.append(
            path + (slice(start[divergence_idx], end[divergence_idx] + 1),)
        )
    # If just start is at the edge, we can grab almost all of the subtree,
    # treating only the ragged bottom edge as an edge case
    elif(start_edges[divergence_idx]):
        slices.append(
            path + (slice(start[divergence_idx], end[divergence_idx]),)
        )
        slices.extend(lower())
    # Analogous to the previous case, but the top is ragged this time
    elif(end_edges[divergence_idx]):
        slices.extend(upper())
        slices.append(
            path + (slice(start[divergence_idx] + 1, end[divergence_idx] + 1),)
        )
    # If both sides of the range are ragged, we need to handle both sides
    # separately. If there's contiguous meat in between them, we can index it
    # in one big chunk
    else:
        slices.extend(upper())
        middle_ground = end[divergence_idx] - start[divergence_idx]
        if(middle_ground > 1):
            slices.append(
                path + (slice(start[divergence_idx] + 1, end[divergence_idx]),)
            )
        slices.extend(lower())

    return [tuple(s) for s in slices]

@torch.jit.ignore
def _chunk_slice(
    t: torch.Tensor,
    flat_start: int,
    flat_end: int,
    no_batch_dims: int,
) -> torch.Tensor:
    """
        Equivalent to

            t.reshape((-1,) + t.shape[no_batch_dims:])[flat_start:flat_end]

        but without the need for the initial reshape call, which can be
        memory-intensive in certain situations. The only reshape operations
        in this function are performed on sub-tensors that scale with
        (flat_end - flat_start), the chunk size.
    """

    batch_dims = t.shape[:no_batch_dims]
    start_idx = list(_flat_idx_to_idx(flat_start, batch_dims))
    # _get_minimal_slice_set is inclusive
    end_idx = list(_flat_idx_to_idx(flat_end - 1, batch_dims))

    # Get an ordered list of slices to perform
    slices = _get_minimal_slice_set(
        start_idx,
        end_idx,
        batch_dims,
    )

    sliced_tensors = [t[s] for s in slices]

    return torch.cat(
        [s.view((-1,) + t.shape[no_batch_dims:]) for s in sliced_tensors]
    )

def chunk_layer(
    layer: Callable,
    inputs: Dict[str, Any],
    chunk_size: int,
    no_batch_dims: int,
    low_mem: bool = False,
    _out: Any = None,
    _add_into_out: bool = False,
) -> Any:
    """
    Implements the "chunking" procedure described in section 1.11.8.

    Layer outputs and inputs are assumed to be simple "pytrees,"
    consisting only of (arbitrarily nested) lists, tuples, and dicts with
    torch.Tensor leaves.

    Args:
        layer:
            The layer to be applied chunk-wise
        inputs:
            A (non-nested) dictionary of keyworded inputs. All leaves must
            be tensors and must share the same batch dimensions.
        chunk_size:
            The number of sub-batches per chunk. If multiple batch
            dimensions are specified, a "sub-batch" is defined as a single
            indexing of all batch dimensions simultaneously (s.t. the
            number of sub-batches is the product of the batch dimensions).
        no_batch_dims:
            How many of the initial dimensions of each input tensor can
            be considered batch dimensions.
        low_mem:
            Avoids flattening potentially large input tensors. Unnecessary
            in most cases, and is ever so slightly slower than the default
            setting.
    Returns:
        The reassembled output of the layer on the inputs.
    """
    if not (len(inputs) > 0):
        raise ValueError("Must provide at least one input")

    initial_dims = [shape[:no_batch_dims] for shape in _fetch_dims(inputs)]
    orig_batch_dims = tuple([max(s) for s in zip(*initial_dims)])

    def _prep_inputs(t):
        if(not low_mem):
            if not sum(t.shape[:no_batch_dims]) == no_batch_dims:
                t = t.expand(orig_batch_dims + t.shape[no_batch_dims:])
            t = t.reshape(-1, *t.shape[no_batch_dims:])
        else:
            t = t.expand(orig_batch_dims + t.shape[no_batch_dims:])
        return t

    prepped_inputs = tensor_tree_map(_prep_inputs, inputs)
    prepped_outputs = None
    if(_out is not None):
        reshape_fn = lambda t: t.view([-1] + list(t.shape[no_batch_dims:]))
        prepped_outputs = tensor_tree_map(reshape_fn, _out)

    flat_batch_dim = 1
    for d in orig_batch_dims:
        flat_batch_dim *= d

    no_chunks = flat_batch_dim // chunk_size + (
        flat_batch_dim % chunk_size != 0
    )

    i = 0
    out = prepped_outputs
    for _ in range(no_chunks):
        # Chunk the input
        if(not low_mem):
            select_chunk = (
                lambda t: t[i : i + chunk_size] if t.shape[0] != 1 else t
            )
        else:
            select_chunk = (
                partial(
                    _chunk_slice,
                    flat_start=i,
                    flat_end=min(flat_batch_dim, i + chunk_size),
                    no_batch_dims=len(orig_batch_dims)
                )
            )

        chunks = tensor_tree_map(select_chunk, prepped_inputs)

        # Run the layer on the chunk
        output_chunk = layer(**chunks)

        # Allocate space for the output
        if out is None:
            allocate = lambda t: t.new_zeros((flat_batch_dim,) + t.shape[1:])
            out = tensor_tree_map(allocate, output_chunk)

        # Put the chunk in its pre-allocated space
        out_type = type(output_chunk)
        if out_type is dict:
            def assign(d1, d2):
                for k, v in d1.items():
                    if type(v) is dict:
                        assign(v, d2[k])
                    else:
                        if(_add_into_out):
                            v[i: i + chunk_size] += d2[k]
                        else:
                            v[i: i + chunk_size] = d2[k]

            assign(out, output_chunk)
        elif out_type is tuple:
            for x1, x2 in zip(out, output_chunk):
                if(_add_into_out):
                    x1[i: i + chunk_size] += x2
                else:
                    x1[i : i + chunk_size] = x2
        elif out_type is torch.Tensor:
            if(_add_into_out):
                out[i: i + chunk_size] += output_chunk
            else:
                out[i: i + chunk_size] = output_chunk
        else:
            raise ValueError("Not supported")

        i += chunk_size

    reshape = lambda t: t.view(orig_batch_dims + t.shape[1:])
    out = tensor_tree_map(reshape, out)

    return out


import tensorflow as tf
import numpy as np
from typing import Tuple, Optional, Union
from models.Subpixel import Subpixel

class SSDCAE(tf.keras.models.Model):
    def __init__(
            self,
            filters:Tuple=(16, 16),
            pre_filters:Tuple=(8, 8),
            shape:Tuple[int, int, int]=(128, 128, 3),
            use_prelu:bool=False,
            dropout:Optional[Union[bool, float]]=.2
            ) -> None:
        """
        Subpixel Stacked-Denoising Convolutional Autoencoder
        This module is automatically trained when in model.training is True.
        Args:
            filters: The tuple of filters which is required as an argument for Conv layers
            pre_filters: The tuple of pre-filters which is required as an argument for Conv layers that lay before Downsample layers length of it much equal to `filters`
            shape: The tuple of image shape that use as a sample for the network
            use_prelu: Use PReLU as activation after down/up sample or just use normal ReLU (PReLU will make more parameters)
            dropout: Dropout probability for dropout2d layer
        """
        super(SSDCAE, self).__init__(name='SSDCAE')
        assert len(filters) == len(pre_filters)
        self.filters = filters
        self.pre_filters = pre_filters
        self.shape = shape
        encoder_layers = [ tf.keras.layers.Input(shape=self.shape) ]
        for i, (f, p) in enumerate(zip(self.filters, self.pre_filters)):
            encoder_layers += [
                tf.keras.layers.Conv2D(p, kernel_size=(3, 3), strides=1, padding='same', name=f'Presampling{i+1}'),
                tf.keras.layers.Conv2D(f, kernel_size=(3, 3), strides=2, padding='same', name=f'Downsample{i+1}'),
                tf.keras.layers.BatchNormalization(axis=-1)
            ]
            if use_prelu:
                encoder_layers += [tf.keras.layers.PReLU()]
            else:
                encoder_layers += [tf.keras.layers.Activation('relu')]
            if dropout:
                encoder_layers += [tf.keras.layers.Dropout(dropout)]
        else:
            encoder_layers += [tf.keras.layers.Flatten()]
        self.encoder = tf.keras.Sequential(encoder_layers, name='encoder')
        self.encoder_shape = tf.keras.backend.int_shape(self.encoder.layers[-2].output)

        decoder_layers = [
            tf.keras.layers.Reshape(
            self.encoder_shape[1:],
            input_shape=(np.prod(self.encoder_shape[1:]),))
        ]
        for i, f in enumerate(self.filters[::-1]):
            decoder_layers += [
                Subpixel(filters=f, kernel_size=(3,3), r=2, padding='same', name=f'Upsample{len(self.filters)-i}'),
                tf.keras.layers.BatchNormalization(axis=-1)
            ]
            if use_prelu:
                decoder_layers += [tf.keras.layers.PReLU()]
            else:
                decoder_layers += [tf.keras.layers.Activation('relu')]
            if dropout:
                decoder_layers += [tf.keras.layers.Dropout(dropout)]
            if i == len(self.filters)-1:
                decoder_layers += [
                    tf.keras.layers.Conv2DTranspose(self.shape[2], (3, 3), padding='same'),
                    tf.keras.layers.Activation('sigmoid')
                ]
                break
        self.decoder = tf.keras.Sequential(decoder_layers, name='decoder')

    def encode_decode_summary(self) -> None:
        self.encoder.summary()
        self.decoder.summary()

    def encode(self, x):
        return self.encoder(x)

    def decode(self, x):
        return self.decoder(x)

    @tf.function
    def call(self, x):
        encoded = self.encode(x)
        decoded = self.decode(encoded)
        return decoded

    def get_config(self):
        return {
            "name": "SSDCAE",
            "encoder": self.encoder,
            "decoder": self.decoder,
        }


"""
中文文本情感分析
pip3 install snownlp
"""
from snownlp import SnowNLP

text = '愿上天赐予我:接受无法改变之事物的平静，改变可改变之事物的勇气，以及区分，这两者之不同的永恒智慧 --沉思录 奥勒留 '
s = SnowNLP(text)
# 分词
print(s.words)
# 词性标注
tags = [x for x in s.tags]
print(tags)
# 断句
print(s.sentences)
# 拼音
print(s.pinyin)

# 情绪判断，返回值为正面情绪的概率，越接近1表示正面情绪，越接近0表示负面情绪
text1 = '这部电影是真心棒'
text2 = '这部电影剧情烂透了'
s1 = SnowNLP(text1)
s2 = SnowNLP(text2)
print(text1, s1.sentiments)
print(text2, s2.sentiments)

# 关键字抽取
text3 = '请允许我尘埃落定，用沉默埋葬过去，满身风雨我从海上来，\
    才隐居到着沙漠，该隐瞒的事总是清晰，千言万语只能无语，爱是天时地利的迷信，\
    喔 原来你也在这里'
s3 = SnowNLP(text3)
print(s3.keywords(limit=5))
print(s3.summary(limit=4))