import warnings

warnings.filterwarnings('ignore', category=DeprecationWarning)

import os

os.environ['MKL_SERVICE_FORCE_INTEL'] = '1'
os.environ['MUJOCO_GL'] = 'egl'

from pathlib import Path
import hydra
import numpy as np
import torch
from dm_env import specs

import dmc
import utils
from logger import Logger
from numpy_replay_buffer import EfficientReplayBuffer
from video import TrainVideoRecorder, VideoRecorder
from utils import load_offline_dataset_into_buffer

torch.backends.cudnn.benchmark = True

def make_agent(obs_spec, action_spec, cfg):
    cfg.obs_shape = obs_spec.shape
    cfg.action_shape = action_spec.shape
    return hydra.utils.instantiate(cfg)

class Workspace:
    def __init__(self, cfg):
        self.work_dir = Path.cwd()
        print(f'workspace: {self.work_dir}')

        self.cfg = cfg
        utils.set_seed_everywhere(cfg.seed)
        self.device = torch.device(cfg.device)
        self.setup()

        self.agent = make_agent(self.train_env.observation_spec(),
                                self.train_env.action_spec(),
                                self.cfg.agent)
        self.timer = utils.Timer()
        self._global_step = 0
        self._global_episode = 0

    def setup(self):
        # create logger
        self.logger = Logger(self.work_dir, use_tb=self.cfg.use_tb, offline=self.cfg.offline,
                             distracting_eval=self.cfg.eval_on_distracting, multitask_eval=self.cfg.eval_on_multitask)
        # create envs
        self.train_env = dmc.make(self.cfg.task_name, self.cfg.frame_stack,
                                  self.cfg.action_repeat, self.cfg.seed, self.cfg.distracting_mode)
        self.eval_env = dmc.make(self.cfg.task_name, self.cfg.frame_stack,
                                 self.cfg.action_repeat, self.cfg.seed, self.cfg.distracting_mode)
        # create replay buffer
        data_specs = (self.train_env.observation_spec(),
                      self.train_env.action_spec(),
                      specs.Array((1,), np.float32, 'reward'),
                      specs.Array((1,), np.float32, 'discount'))

        self.replay_buffer = EfficientReplayBuffer(self.cfg.replay_buffer_size,
                                                   self.cfg.batch_size,
                                                   self.cfg.nstep,
                                                   self.cfg.discount,
                                                   self.cfg.frame_stack,
                                                   data_specs)

        self.video_recorder = VideoRecorder(
            self.work_dir if self.cfg.save_video else None)
        self.train_video_recorder = TrainVideoRecorder(
            self.work_dir if self.cfg.save_train_video else None)

        self.eval_on_distracting = self.cfg.eval_on_distracting
        self.eval_on_multitask = self.cfg.eval_on_multitask

    @property
    def global_step(self):
        return self._global_step

    @property
    def global_episode(self):
        return self._global_episode

    @property
    def global_frame(self):
        return self.global_step * self.cfg.action_repeat

    def eval(self):
        step, episode, total_reward = 0, 0, 0
        eval_until_episode = utils.Until(self.cfg.num_eval_episodes)

        while eval_until_episode(episode):
            time_step = self.eval_env.reset()
            self.video_recorder.init(self.eval_env, enabled=(episode == 0))
            while not time_step.last():
                with torch.no_grad(), utils.eval_mode(self.agent):
                    action = self.agent.act(time_step.observation,
                                            self.global_step,
                                            eval_mode=True)
                time_step = self.eval_env.step(action)
                self.video_recorder.record(self.eval_env)
                total_reward += time_step.reward
                step += 1

            episode += 1
            self.video_recorder.save(f'{self.global_frame}.mp4')

        with self.logger.log_and_dump_ctx(self.global_frame, ty='eval') as log:
            log('episode_reward', total_reward / episode)
            log('episode_length', step * self.cfg.action_repeat / episode)
            log('episode', self.global_episode)
            log('step', self.global_step)

    def eval_distracting(self, record_video):
        distraction_modes = ['easy', 'medium', 'hard', 'fixed_easy', 'fixed_medium', 'fixed_hard']
        if not hasattr(self, 'distracting_envs'):
            self.distracting_envs = []
            for distraction_mode in distraction_modes:
                env = dmc.make(self.cfg.task_name, self.cfg.frame_stack,
                               self.cfg.action_repeat, self.cfg.seed, distracting_mode=distraction_mode)
                self.distracting_envs.append(env)
        for env, env_name in zip(self.distracting_envs, distraction_modes):
            self.eval_single_env(env, env_name, record_video)

    def eval_multitask(self, record_video):
        multitask_modes = [f'len_{i}' for i in range(1, 11, 1)]
        if not hasattr(self, 'multitask_envs'):
            self.multitask_envs = []
            for multitask_mode in multitask_modes:
                env = dmc.make(self.cfg.task_name, self.cfg.frame_stack,
                               self.cfg.action_repeat, self.cfg.seed, multitask_mode=multitask_mode)
                self.multitask_envs.append(env)
        for env, env_name in zip(self.multitask_envs, multitask_modes):
            self.eval_single_env(env, env_name, record_video)

    def eval_single_env(self, env, env_name, save_video):
        step, episode, total_reward = 0, 0, 0
        eval_until_episode = utils.Until(self.cfg.num_eval_episodes)

        while eval_until_episode(episode):
            time_step = env.reset()
            self.video_recorder.init(env, enabled=((episode == 0) and save_video))
            while not time_step.last():
                with torch.no_grad(), utils.eval_mode(self.agent):
                    action = self.agent.act(time_step.observation,
                                            self.global_step,
                                            eval_mode=True)
                time_step = env.step(action)
                self.video_recorder.record(env)
                total_reward += time_step.reward
                step += 1

            episode += 1
            self.video_recorder.save(f'{env_name}_{self.global_frame}.mp4')

        self.logger.log(f'eval/{env_name}_episode_reward', total_reward / episode, self.global_frame)

    def train(self):
        # predicates
        train_until_step = utils.Until(self.cfg.num_train_frames,
                                       self.cfg.action_repeat)
        seed_until_step = utils.Until(self.cfg.num_seed_frames,
                                      self.cfg.action_repeat)
        eval_every_step = utils.Every(self.cfg.eval_every_frames,
                                      self.cfg.action_repeat)
        # only in distracting evaluation mode
        eval_save_vid_every_step = utils.Every(self.cfg.eval_save_vid_every_step,
                                               self.cfg.action_repeat)

        episode_step, episode_reward = 0, 0
        time_step = self.train_env.reset()
        self.replay_storage.add(time_step)
        self.train_video_recorder.init(time_step.observation)
        metrics = None
        while train_until_step(self.global_step):
            if time_step.last():
                self._global_episode += 1
                self.train_video_recorder.save(f'{self.global_frame}.mp4')
                # wait until all the metrics schema is populated
                if metrics is not None:
                    # log stats
                    elapsed_time, total_time = self.timer.reset()
                    episode_frame = episode_step * self.cfg.action_repeat
                    with self.logger.log_and_dump_ctx(self.global_frame,
                                                      ty='train') as log:
                        log('fps', episode_frame / elapsed_time)
                        log('total_time', total_time)
                        log('episode_reward', episode_reward)
                        log('episode_length', episode_frame)
                        log('episode', self.global_episode)
                        log('buffer_size', len(self.replay_storage))
                        log('step', self.global_step)

                # reset env
                time_step = self.train_env.reset()
                self.replay_storage.add(time_step)
                self.train_video_recorder.init(time_step.observation)
                # try to save snapshot
                if self.cfg.save_snapshot:
                    self.save_snapshot()
                episode_step = 0
                episode_reward = 0

            # try to evaluate
            if eval_every_step(self.global_step):
                self.logger.log('eval_total_time', self.timer.total_time(),
                                self.global_frame)
                if self.eval_on_distracting:
                    self.eval_distracting(eval_save_vid_every_step(self.global_step))
                if self.eval_on_multitask:
                    self.eval_multitask(eval_save_vid_every_step(self.global_step))
                self.eval()

            # sample action
            with torch.no_grad(), utils.eval_mode(self.agent):
                action = self.agent.act(time_step.observation,
                                        self.global_step,
                                        eval_mode=False)

            # try to update the agent
            if not seed_until_step(self.global_step):
                metrics = self.agent.update(self.replay_iter, self.global_step)
                self.logger.log_metrics(metrics, self.global_frame, ty='train')

            # take env step
            time_step = self.train_env.step(action)
            episode_reward += time_step.reward
            self.replay_storage.add(time_step)
            self.train_video_recorder.record(time_step.observation)
            episode_step += 1
            self._global_step += 1

    def train_offline(self, offline_dir):
        # Open dataset, load as memory buffer
        load_offline_dataset_into_buffer(Path(offline_dir), self.replay_buffer, self.cfg.frame_stack,
                                         self.cfg.replay_buffer_size)

        if self.replay_buffer.index == -1:
            raise ValueError('No offline data loaded, check directory.')

        # predicates
        train_until_step = utils.Until(self.cfg.num_train_frames, 1)
        eval_every_step = utils.Every(self.cfg.eval_every_frames, 1)
        show_train_stats_every_step = utils.Every(self.cfg.show_train_stats_every_frames, 1)
        # only in distracting evaluation mode
        eval_save_vid_every_step = utils.Every(self.cfg.eval_save_vid_every_step,
                                               self.cfg.action_repeat)

        metrics = None
        step = 0
        while train_until_step(self.global_step):
            if show_train_stats_every_step(self.global_step):
                # wait until all the metrics schema is populated
                if metrics is not None:
                    # log stats
                    elapsed_time, total_time = self.timer.reset()
                    with self.logger.log_and_dump_ctx(self.global_frame,
                                                      ty='train') as log:
                        log('fps', step / elapsed_time)
                        log('total_time', total_time)
                        log('buffer_size', len(self.replay_buffer))
                        log('step', self.global_step)
                    step = 0
                # try to save snapshot
                if self.cfg.save_snapshot:
                    self.save_snapshot()
            step += 1
            # try to evaluate
            if eval_every_step(self.global_step):
                self.logger.log('eval_total_time', self.timer.total_time(),
                                self.global_frame)
                if self.eval_on_distracting:
                    self.eval_distracting(eval_save_vid_every_step(self.global_step))
                if self.eval_on_multitask:
                    self.eval_multitask(eval_save_vid_every_step(self.global_step))
                self.eval()

            # try to update the agent
            metrics = self.agent.update(self.replay_buffer, self.global_step)
            if show_train_stats_every_step(self.global_step):
                self.logger.log_metrics(metrics, self.global_frame, ty='train')

            self._global_step += 1

    def save_snapshot(self):
        snapshot = self.work_dir / 'snapshot.pt'
        keys_to_save = ['agent', 'timer', '_global_step', '_global_episode']
        payload = {k: self.__dict__[k] for k in keys_to_save}
        with snapshot.open('wb') as f:
            torch.save(payload, f)

    def load_snapshot(self):
        snapshot = self.work_dir / 'snapshot.pt'
        with snapshot.open('rb') as f:
            payload = torch.load(f)
        for k, v in payload.items():
            self.__dict__[k] = v

@hydra.main(config_path='cfgs', config_name='config')
def main(cfg):
    from train import Workspace as W
    root_dir = Path.cwd()
    workspace = W(cfg)
    print(cfg)
    snapshot = root_dir / 'snapshot.pt'
    if snapshot.exists():
        print(f'resuming: {snapshot}')
        workspace.load_snapshot()
    if cfg.offline:
        workspace.train_offline(cfg.offline_dir)
    else:
        workspace.train()

if __name__ == '__main__':
    main()


class Solution(object):
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head is None:
            return head

        prevStack = []
        curr = head
        while curr.next is not None:
            prevStack.append(curr)
            curr = curr.next

        result = curr
        while curr is not None:
            if len(prevStack) > 0:
                curr.next = prevStack.pop()
            else:
                curr.next = None
            curr = curr.next
        return result


from gensim.models import Word2Vec
from gensim.models.word2vec import LineSentence
import multiprocessing
import logging
from setting import news_file, word2vec_model_path, word2vec_vectors_path

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(lineno)d -  %(message)s')

def word2vec_train():
    """
    词向量训练
    :param train_file:
    :param save_model_file:
    :param save_vector_file:
    :return:
    """
    f_wiki = open(news_file, "r", encoding="utf-8")
    sentences = LineSentence(f_wiki)
    model = Word2Vec(sentences, size = 100, window = 5, min_count = 7, workers = multiprocessing.cpu_count())
    model.save(word2vec_model_path)
    model.wv.save_word2vec_format(word2vec_vectors_path, binary = False)

def load_model(fname):
    """
    加载模型
    :param fname:
    :return:
    """
    return Word2Vec.load(fname)

if __name__ == "__main__":
    # 训练
    word2vec_train()
    # 导入模型
    model = Word2Vec.load(word2vec_model_path)
    # 词向量
    # print("江西", word2vec_model['江西']) # 获得词向量
    print(model.most_similar('江西'))


'''
This problem was recently asked by Facebook:

Given a list of numbers, where every number shows up twice except for one number, find that one number.

Example:
Input: [4, 3, 2, 4, 1, 3, 2]
Output: 1

Time: O(n)
'''

def singleNumber(nums):
    dup = 0
    for num in nums:
        dup = dup ^ num
    return dup

print(singleNumber([4, 3, 2, 4, 1, 3, 2]))
# 1


import argparse
import json
import logging
import os
import random
import math
from pprint import pprint

logger = logging.getLogger()
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)-7s - [%(funcName)s] %(message)s')
# uncomment for submission
# logger.disabled = True

ACTIONS = {
	-1: 'DoNothing',
	1: 'MoveUp',
	2: 'MoveLeft',
	3: 'MoveRight',
	4: 'MoveDown',
	5: 'PlaceBomb',
	6: 'TriggerBomb',
}

def main(player_key, output_path):

	logger.info('Player key: {}'.format(player_key))
	logger.info('Output path: {}'.format(output_path))

	with open(os.path.join(output_path, 'state.json'), 'r') as f:
		state = json.load(f)
		# logger.info('State: {}'.format(state))

	# Constants for json path
	PLAYER_ENTITY = "RegisteredPlayerEntities"
	GAME_BLOCKS = "GameBlocks"

	# Constants for data about map
	MAP_HEIGHT = state["MapHeight"]
	MAP_WIDTH = state["MapWidth"]
	CURRENT_ROUND = state["CurrentRound"]

	# Constants for entity type
	WALL = "Domain.Entities.IndestructibleWallEntity, Domain"
	OBSTACLE = "Domain.Entities.DestructibleWallEntity, Domain"
	PLAYER = "Domain.Entities.PlayerEntity, Domain"
	SUPER_POWER_UP = "Domain.Entities.PowerUps.SuperPowerUp, Domain"
	POWER_UP_BOMBBAG = "Domain.Entities.PowerUps.BombBagPowerUpEntity, Domain"
	POWER_UP_BOMBRADIUS = "Domain.Entities.PowerUps.BombRaduisPowerUpEntity, Domain" # emang typo dari sananya kok :(
	TOTAL_PLAYER = len(state[PLAYER_ENTITY])

	# Class queue
	class Queue:
		def __init__(self):
			self.items = []

		def isEmpty(self):
			return self.items == []

		def enqueue(self, item):
			self.items.insert(0,item)

		def dequeue(self):
			return self.items.pop()

		def size(self):
			return len(self.items)

	# Functions and Procedures

	def Player_Name(index):
	# Getter untuk nama bot
		return(state[PLAYER_ENTITY][index]["Name"])

	def Player_Index(key):
	# Getter untuk indeks bot
		i = 0
		while ((i < TOTAL_PLAYER) and (Player_Key(i) != key)):
			i += 1
		return (i)

	def Player_Key(index):
	# Getter untuk key bot
		return (state[PLAYER_ENTITY][index]["Key"])

	def Player_Points(index):
	# Getter untuk jumlah point yang dimiliki bot
		return (state[PLAYER_ENTITY][index]["Points"])

	def Player_Killed(index):
	# Getter untuk status nyawa musuh
		return(state[PLAYER_ENTITY][index]["Killed"])

	def Player_BombBag(index):
	# Getter untuk jumlah bomb bag yang dimiliki bot
		return(state[PLAYER_ENTITY][index]["BombBag"])

	def Player_BombRadius(index):
	# Getter untuk blast radius yang dimiliki bot
		return(state[PLAYER_ENTITY][index]["BombRadius"])

	def Player_X(index):
	# Getter untuk absis bot
		return(state[PLAYER_ENTITY][index]["Location"]["X"])

	def Player_Y(index):
	# Getter untuk ordinat bot
		return(state[PLAYER_ENTITY][index]["Location"]["Y"])

	def Map_Entity(x, y):
	# Getter untuk entitas yang berada pada petak (x, y)
		if (state[GAME_BLOCKS][x-1][y-1]["Entity"] == None):
			return(None)
		elif (state[GAME_BLOCKS][x-1][y-1]["Entity"]["$type"] == PLAYER):
			return(state[GAME_BLOCKS][x-1][y-1]["Entity"]["Key"])
		else:
			return(state[GAME_BLOCKS][x-1][y-1]["Entity"]["$type"])

	def Map_Bomb(x, y):
	# Bernilai true apabila ada bom pada petak (x, y)
		if (state[GAME_BLOCKS][x-1][y-1]["Bomb"] == None):
			return(False)
		else:
			return(True)

	def Map_Bomb_Key(x, y):
	# Getter untuk pemilik bom pada petak (x, y)
		return(state[GAME_BLOCKS][x-1][y-1]["Bomb"]["Owner"]["Key"])

	def Map_Bomb_Radius(x, y):
	# Getter untuk blast radius bom yang terletak pada petak (x, y)
		return(state[GAME_BLOCKS][x-1][y-1]["Bomb"]["BombRadius"])

	def Map_Bomb_Timer(x, y):
	# Getter untuk timer bom yang terletak pada petak (x, y)
		return(state[GAME_BLOCKS][x-1][y-1]["Bomb"]["BombTimer"])

	def Map_PowerUp(x, y):
	# Getter untuk power up pada petak (x, y)
		if (state[GAME_BLOCKS][x-1][y-1]["PowerUp"] == None):
			return(None)
		else:
			return(state[GAME_BLOCKS][x-1][y-1]["PowerUp"]["$type"])

	def Map_Exploding(x, y):
	# Getter untuk status peledakan petak (x, y)
		return(state[GAME_BLOCKS][x-1][y-1]["Exploding"])

	def HasPlacedBomb():
	# Memberikan nilai true apabila bot kita telah meletakkan bom dan timernya > 2
		found = False
		y = 0
		while ((y < MAP_HEIGHT) and (not found)):
			x = 0
			while ((x < MAP_WIDTH) and (not found)):
				if ((Map_Bomb(x, y)) and (Map_Bomb_Key(x, y) == player_key) and (Map_Bomb_Timer(x, y) > 2)):
					found = True
				x += 1
			y += 1
		return (found)

	def InDanger (x, y):
	# Memberi nilai true apabila bot kita berada dalam zona bahaya
	# Zona bahaya: dapat terkena ledakan bom
		danger = False
		# Left check
		x_left = x
		while ((x_left >= 0) and (Map_Entity(x_left, y) != WALL) and (Map_Entity(x_left, y) != OBSTACLE) and (not danger)):
			if (Map_Bomb(x_left, y)) and (Map_Bomb_Radius(x_left, y) >= abs(x_left - x)):
				danger = True
			else:
				x_left -= 1
		# Right check
		x_right = x + 1
		while ((x_right <= MAP_WIDTH) and (Map_Entity(x_right, y) != WALL) and (Map_Entity(x_right, y) != OBSTACLE) and (not danger)):
			if (Map_Bomb(x_right, y)) and (Map_Bomb_Radius(x_right, y) >= abs(x_right - x)):
				danger = True
			else:
				x_right += 1
		# Up check
		y_up = y - 1
		while ((y_up >= 0) and (Map_Entity(x, y_up) != WALL) and (Map_Entity(x, y_up) != OBSTACLE) and (not danger)):
			if (Map_Bomb(x, y_up)) and (Map_Bomb_Radius(x, y_up) >= abs(y_up - y)):
				danger = True
			else:
				y_up -= 1
		# Down check
		y_down = y + 1
		while ((y_down <= MAP_HEIGHT) and (Map_Entity(x, y_down) != WALL) and (Map_Entity(x, y_down) != OBSTACLE) and (not danger)):
			if (Map_Bomb(x, y_down)) and (Map_Bomb_Radius(x, y_down) >= abs(y_down - y)):
				danger = True
			else:
				y_down += 1
		# Return
		return (danger)

	def DangerCounter(x, y):
	# Mengembalikan timer bomb yang paling kecil yang dapat membahayakan bila bot berada di posisi x, y
		most_urgent_timer = 99
		# Left check
		x_left = x
		while ((x_left >= 0) and (Map_Entity(x_left, y) != WALL) and (Map_Entity(x_left, y) != OBSTACLE)):
			if ((Map_Bomb(x_left, y)) and (Map_Bomb_Radius(x_left, y) >= abs(x_left - x)) and (most_urgent_timer > Map_Bomb_Timer(x_left, y))):
				most_urgent_timer = Map_Bomb_Timer(x_left, y)
			x_left -= 1
		# Right check
		x_right = x + 1
		while ((x_right <= MAP_WIDTH) and (Map_Entity(x_right, y) != WALL) and (Map_Entity(x_right, y) != OBSTACLE)):
			if ((Map_Bomb(x_right, y)) and (Map_Bomb_Radius(x_right, y) >= abs(x_right - x)) and (most_urgent_timer > Map_Bomb_Timer(x_right, y))):
				most_urgent_timer = Map_Bomb_Timer(x_right, y)
			x_right += 1
		# Up check
		y_up = y - 1
		while ((y_up >= 0) and (Map_Entity(x, y_up) != WALL) and (Map_Entity(x, y_up) != OBSTACLE)):
			if ((Map_Bomb(x, y_up)) and (Map_Bomb_Radius(x, y_up) >= abs(y_up - y)) and (most_urgent_timer > Map_Bomb_Timer(x, y_up))):
				most_urgent_timer = Map_Bomb_Timer(x, y_up)
			y_up -= 1
		# Down check
		y_down = y + 1
		while ((y_down <= MAP_HEIGHT) and (Map_Entity(x, y_down) != WALL) and (Map_Entity(x, y_down) != OBSTACLE)):
			if ((Map_Bomb(x, y_down)) and (Map_Bomb_Radius(x, y_down) >= abs(y_down - y)) and (most_urgent_timer > Map_Bomb_Timer(x, y_down))):
				most_urgent_timer = Map_Bomb_Timer(x, y_down)
			y_down += 1
		# Return
		return(most_urgent_timer)

	def Distance (x1, y1, x2, y2):
	# Mengembalikan banyak petak yang harus dilalui apabila ingin berpindah dari (x1, y1) ke (x2, y2)
		return (abs(x1 - x2) + abs(y1 - y2))

	def PythagorasPow (x1, y1, x2, y2):
	# Mengembalikan kuadrat jarak Euclidean dari (x1, y1) ke (x2, y2)
		return ((x1-x2)**2 + (y1-y2)**2)

	def IsPowerUpInRange (x, y, radius):
	# Mengembalikan nilai true apabila terdapat power up dalam radius tertentu dari titik (x, y)
		# Mencegah x keluar batas map
		x_end = x + radius
		if (x_end > MAP_WIDTH):
			x_end = MAP_WIDTH
		# Mencegah y keluar batas map
		y_start = y - radius
		if (y_start < 1):
			y_start = 1
		y_end = y + radius
		if (y_end > MAP_HEIGHT):
			y_end = MAP_HEIGHT
		found = False # Inisialisasi awal
		# Pencarian power up per ordinat
		while ((y_start <= y_end) and (not found)):
			# Mencegah x keluar batas map
			x_start = x - radius
			if (x_start < 1):
				x_start = 1
			# Melakukan iterasi per absis
			while ((x_start <= x_end) and (not found)):
				if (Map_PowerUp(x_start, y_start) != None):
					found = True
				else:
					x_start += 1
			y_start += 1
		# Return
		return (found)

	def IsEnemyInRange (player_index,radius):
	# Mengembalikan indeks musuh (yang masih hidup) yang berada dalam radius tertentu dari bot
	# Bernilai -1 apabila tidak ada musuh yang berada dalam radius
		enemy_index = 0
		found = False
		# Pencarian musuh dalam radius tertentu
		while ((enemy_index < TOTAL_PLAYER-1) and (not found)):
			if ((enemy_index != player_index) and (not Player_Killed(enemy_index)) and (radius >= Distance(Player_X(player_index), Player_Y(player_index), Player_X(enemy_index), Player_Y(enemy_index)))):
				found = True
			else:
				enemy_index += 1
		if (found):
			return(enemy_index)
		else:
			return(-1)

	def SOS(index):
	# Menghasilkan aksi yang harus bot lakukan untuk melarikan diri dari zona bahaya
		goal = 0
		X = Queue() # Queue yang digunakan untuk menyimpan absis (x)
		Y = Queue() # Queue yang digunakan untuk menyimpan ordinat (y)
		M = Queue() # Queue yang digunakan untuk menyimpan aksi (move)
		X.enqueue(Player_X(index)) # Insialisasi awal dengan absis bot saat ini
		Y.enqueue(Player_Y(index)) # Inisialisasi awal dengan ordinat bot saat ini
		M.enqueue([]) # Inisialisasi awal dengan list kosong
		# Melakukan iterasi selama queue absis tidak kosong dan belum menemukan jalan keluar
		while ((not X.isEmpty()) and (goal == 0)):
			i = X.dequeue()
			j = Y.dequeue()
			move = M.dequeue()
			valid = False # valid adalah penentu apakah jalan tersebut dapat dilalui atau tidak
			if ((Map_Entity(i,j) == None) or (Map_Entity(i,j) == player_key)): # Kosong (tidak ada halangan)
				if (Map_Bomb(i,j)): # Ada bom
					if ((Player_X(index) == i) and (Player_Y(index) == j)): # Posisi bom = posisi bot
						valid = True
				else: # Tidak ada bom
					valid = True
			count = DangerCounter(i,j)-len(move)
			# Menentukan apakah sempat melarikan diri dengan pergerakan tersebut
			if ((count == 0) or (count == 1)):
				valid = False
			if (valid):
				if (not InDanger(i,j)):
					goal = move[0]
				elif (len(move) < 10):
					temp = TargetPos(i,j,math.floor(MAP_WIDTH/2),1)
					if (temp == -1):
						temp = TargetPos(i,j,math.floor(MAP_WIDTH/2),2)
					x_target = GetTargetX(temp)
					y_target = GetTargetY(temp)
					dist = []
					dist.append(Distance(i,j-1,x_target,y_target)) # Memasukkan jarak antar tetangga atas (i, j) ke koordinat target
					dist.append(Distance(i-1,j,x_target,y_target)) # Memasukkan jarak antar tetangga kiri (i, j) ke koordinat target
					dist.append(Distance(i+1,j,x_target,y_target)) # Memasukkan jarak antar tetangga kanan (i, j) ke koordinat target
					dist.append(Distance(i,j+1,x_target,y_target)) # Memasukkan jarak antar tetangga bawah (i, j) ke koordinat target
					X.enqueue(i)
					Y.enqueue(j)
					M.enqueue(move + [-1])
					for q in range(0,4):
						shortest = 0
						for w in range(1,4):
							if (dist[w] < dist[shortest]):
								shortest = w
						if (shortest == 0):
							X.enqueue(i)
							Y.enqueue(j-1)
							M.enqueue(move + [1])
						elif (shortest == 1):
							X.enqueue(i-1)
							Y.enqueue(j)
							M.enqueue(move + [2])
						elif (shortest == 2):
							X.enqueue(i+1)
							Y.enqueue(j)
							M.enqueue(move + [3])
						elif (shortest == 3):
							X.enqueue(i)
							Y.enqueue(j+1)
							M.enqueue(move + [4])
						dist[shortest] = 100000		#big number
		if (goal == 0): # Tidak ada jalan keluar
			return (-1)
		else:
			return(goal)

	def TargetPos(x,y,radius,search):
	# Terdiri dari 2 jenis search
	# Search 1: Mengembalikan nilai yang mengandung koordinat target
	# Search 2: Mengembalikan nilai yang mengandung indeks musuh
		x_end = x + radius # Menjaga agar x tidak keluar batas
		if (x_end > MAP_WIDTH):
			x_end = MAP_WIDTH
		y_start = y - radius # Menjaga agar y tidak keluar batas
		if (y_start < 1):
			y_start = 1
		y_end = y + radius # Menjaga agar y tidak keluar batas
		if (y_end > MAP_HEIGHT):
			y_end = MAP_HEIGHT
		x_start = x - radius # Menjaga agar x tidak keluar batas
		if (x_start < 1):
			x_start = 1
		# Insialisasi awal
		found_x = -1
		found_y = -1
		# Melakukan pencarian
		for i in range(x_start, x_end):
			for j in range(y_start, y_end):
				# Search 1
				if (search == 1):
					if (Map_PowerUp(i, j) != None):
						if (found_x == -1):
							found_x = i
							found_y = j
						else:
							if (Distance(x,y,i,j) < Distance(x,y,found_x,found_y)):
								found_x = i
								found_y = j
				# Search 2
				elif (search == 2):
					player_index = Player_Index(player_key)
					enemy_index = IsEnemyInRange(player_index,radius)
					if ((enemy_index != player_index) and (not Player_Killed(enemy_index)) and (i == Player_X(enemy_index)) and (j == Player_Y(enemy_index)) and (Distance(x, y, i, j) <= radius)):
							if (found_x == -1):
								found_x = i
								found_y = j
							else:
								if (Distance(x,y,i,j) < Distance(x,y,found_x,found_y)):
									found_x = i
									found_y = j
		if (found_x == -1): # Tidak ketemu
			return -1
		else:
			if (search == 1): # Search 1
				return (found_x*(10**(math.floor(math.log(MAP_HEIGHT,10))+1))+found_y) # Return value adalah koordinat target (data dimanipulasi)
			elif (search == 2): # Search 2
				# return(enemy_index*(10**(2*math.floor(math.log(MAP_HEIGHT,10))+1))+found_x*(10**(math.floor(math.log(MAP_HEIGHT,10))+1))+found_y) # Return value adalah indeks musuh (data dimanipulasi)
				return(enemy_index)

	def GetEnemyIndex(val):
	# Mengekstrak indeks musuh dari manipulasi data yang telah dilakukan
		# return (math.floor(val/(10**(2*math.floor(math.log(MAP_HEIGHT,10))))))
		return(val)

	def GetTargetX(val):
	# Mengekstrak absis target dari manipulasi data yang telah dilakukan
		return (math.floor(val/(10**(math.floor(math.log(MAP_HEIGHT,10))+1))))

	def GetTargetY(val):
	# Mengekstrak ordinat target dari manipulasi data yang telah dilakukan
		return (val % (10**(math.floor(math.log(MAP_HEIGHT,10))+1)))

	def GoToTarget(x,y,radius,index,search):
	# Menghasilkan aksi yang harus dilakukan untuk bergerak mendekati target (Search 1) atau musuh (Search 2)
	# Menggunakan Greedy Best-First Search
		if (search == 1): # Search 1: mencari power up
			temp = TargetPos(x,y,radius,1) # Koordinat target berupa manipulasi data
			smin = 9999 # Insialisasi awal
			move = -1 # Inisialisasi awal
			# Perbandingan nilai heuristik (kuadrat pythagoras) dari keempat tetangganya
			if (Map_Entity(Player_X(index), Player_Y(index)-1) == None) and (not InDanger(Player_X(index), Player_Y(index)-1)):
				sup = PythagorasPow(GetTargetX(temp),GetTargetY(temp),x,y-1) # Atas
				if (smin > sup) :
					smin = sup
					move = 1
			if (Map_Entity(Player_X(index)-1, Player_Y(index)) == None) and (not InDanger(Player_X(index)-1, Player_Y(index))):
				sleft = PythagorasPow(GetTargetX(temp),GetTargetY(temp),x-1,y) # Kiri
				if (smin > sleft) :
					smin = sleft
					move = 2
			if (Map_Entity(Player_X(index)+1, Player_Y(index)) == None) and (not InDanger(Player_X(index)+1, Player_Y(index))):
				sright = PythagorasPow(GetTargetX(temp),GetTargetY(temp),x+1,y) # Kanan
				if (smin > sright) :
					smin = sright
					move = 3
			if (Map_Entity(Player_X(index), Player_Y(index)+1) == None) and (not InDanger(Player_X(index), Player_Y(index)+1)):
				sdown = PythagorasPow(GetTargetX(temp),GetTargetY(temp),x,y+1) # Bawah
				if (smin > sdown) :
					smin = sdown
					move = 4
			# Mengembalikan aksi terbaik yang didapatkan
			return move
		else: # Search 2 dan 3: mencari musuh
			if (search == 2): # Dalam radius tertentu
				temp = TargetPos(x,y,radius,2) # Indeks musuh berupa manipulasi data
				enemy_index = GetEnemyIndex(temp) # Inisialisasi dengan indeks musuh sesungguhnya
			else: # Mengincar musuh yang masih hidup di manapun mereka berada
				found = False
				searchingindex = 0
				while (searchingindex <= TOTAL_PLAYER-1) and (not found):
					if (Player_Key(searchingindex) != player_key) and (not Player_Killed(searchingindex)):
						found = True
						enemy_index = searchingindex
					else:
						searchingindex += 1
			# Apabila jarak musuh <= blast radius bom bot dan musuh masih hidup
			if ((not Player_Killed(enemy_index)) and Distance(Player_X(index),Player_Y(index),Player_X(enemy_index),Player_Y(enemy_index)),Player_BombRadius(index)):
				time_to_attack = False
				# Horizontal check
				if (Player_X(enemy_index) == Player_X(index)):
					# Left check
					x_left = x
					while ((x_left >= 0) and (Map_Entity(x_left, y) != WALL) and (Map_Entity(x_left, y) != OBSTACLE) and (not time_to_attack)):
						if (Map_Entity(x_left, y) == Player_Key(enemy_index)):
							time_to_attack = True
						else:
							x_left -= 1
					# Right check
					x_right = x + 1
					while ((x_right <= MAP_WIDTH) and (Map_Entity(x_right, y) != WALL) and (Map_Entity(x_right, y) != OBSTACLE) and (not time_to_attack)):
						if (Map_Entity(x_right, y) == Player_Key(enemy_index)):
							time_to_attack = True
						else:
							x_right += 1
				# Vertical check
				elif (Player_Y(enemy_index) == Player_Y(index)):
					# Up check
					y_up = y - 1
					while ((y_up >= 0) and (Map_Entity(x, y_up) != WALL) and (Map_Entity(x, y_up) != OBSTACLE) and (not time_to_attack)):
						if (Map_Entity(x, y_up) == Player_Key(enemy_index)):
							time_to_attack = True
						else:
							y_up -= 1
					# Down check
					y_down = y + 1
					while ((y_down <= MAP_HEIGHT) and (Map_Entity(x, y_down) != WALL) and (Map_Entity(x, y_down) != OBSTACLE) and (not time_to_attack)):
						if (Map_Entity(x, y_down) == Player_Key(enemy_index)):
							time_to_attack = True
						else:
							y_down += 1
			# Ada kemungkinan dapat meledakkan musuh
			if (time_to_attack):
				return(5)
			else: # not time_to_attack
				smin = 9999 # Inisialisasi awal
				move = -1 # Inisialisasi awal
				# Perbandingan nilai heuristik (kuadrat pythagoras) dari keempat tetangganya
				if (Map_Entity(Player_X(index), Player_Y(index)-1) == None) and (not InDanger(Player_X(index), Player_Y(index)-1)):
					sup = PythagorasPow(Player_X(enemy_index),Player_Y(enemy_index),Player_X(index),Player_Y(index)-1)
					if (smin > sup) :
						smin = sup
						move = 1
				if (Map_Entity(Player_X(index)-1, Player_Y(index)) == None) and (not InDanger(Player_X(index)-1, Player_Y(index))):
					sleft = PythagorasPow(Player_X(enemy_index),Player_Y(enemy_index),Player_X(index)-1,Player_Y(index))
					if (smin > sleft) :
						smin = sleft
						move = 2
				if (Map_Entity(Player_X(index)+1, Player_Y(index)) == None) and (not InDanger(Player_X(index)+1, Player_Y(index))):
					sright = PythagorasPow(Player_X(enemy_index),Player_Y(enemy_index),Player_X(index)+1,Player_Y(index))
					if (smin > sright) :
						smin = sright
						move = 3
				if (Map_Entity(Player_X(index), Player_Y(index)+1) == None) and (not InDanger(Player_X(index), Player_Y(index)+1)):
					sdown = PythagorasPow(Player_X(enemy_index),Player_Y(enemy_index),Player_X(index),Player_Y(index)+1)
					if (smin > sdown) :
						smin = sdown
						move = 4
			# Mengembalikan aksi terbaik yang didapatkan
			return move

	def Choice(index):
	# Menentukan aksi yang dilakukan dengan berdasarkan urutan prioritas bot
		# Prioritas pertama: kabur dari zona bahaya
		if (InDanger(Player_X(index), Player_Y(index))):
			return(SOS(index))
		# Prioritas kedua: memicu ledakan bom yang sudah bot letakkan apabila sudah tidak berada di zona bahaya
		elif (HasPlacedBomb()):
			return(6)
		# Prioritas ketiga: meledakkan obstacle yang ada di sebelah bot
		elif ((Map_Entity(Player_X(index)-1, Player_Y(index)) == OBSTACLE) or (Map_Entity(Player_X(index)+1, Player_Y(index)) == OBSTACLE) or (Map_Entity(Player_X(index), Player_Y(index)-1) == OBSTACLE) or (Map_Entity(Player_X(index), Player_Y(index)+1) == OBSTACLE)):
			return(5)
		# Prioritas keempat: mengejar power up sebagai target
		elif (IsPowerUpInRange(Player_X(index),Player_Y(index), math.floor(MAP_WIDTH/2))):
			return(GoToTarget(Player_X(index),Player_Y(index),MAP_WIDTH,index,1))
		# Prioritas kelima: mengejar musuh dalam radius tertentu
		elif (IsEnemyInRange(index,math.floor(MAP_WIDTH)) != -1):
			enemy_key = Player_Key(IsEnemyInRange(index,math.floor(MAP_WIDTH)))
			if (((Map_Entity(Player_X(index)-1, Player_Y(index)) == enemy_key) or (Map_Entity(Player_X(index)+1, Player_Y(index)) == enemy_key) or (Map_Entity(Player_X(index), Player_Y(index)-1) == enemy_key) or (Map_Entity(Player_X(index), Player_Y(index)+1) == enemy_key)) and SOS(Player_Index(enemy_key))):
				return(5)
			else:
				return(GoToTarget(Player_X(index),Player_Y(index),MAP_WIDTH,index,2))
		# Prioritas keenam: berdiam diri apabila telah terpojok
		elif ((InDanger(Player_X(index)-1,Player_Y(index))) and (InDanger(Player_X(index),Player_Y(index)+1)) and (InDanger(Player_X(index),Player_Y(index)-1)) and (InDanger(Player_X(index),Player_Y(index)+1))):
			return(-1)
		# Prioritas ketujuh: hunting down the enemy
		else:
			return(GoToTarget(Player_X(index),Player_Y(index),MAP_WIDTH,index,3))

	action = Choice(Player_Index(player_key))
	logger.info('Action: {}'.format(ACTIONS[action]))

	with open(os.path.join(output_path, 'move.txt'), 'w') as f:
		f.write('{}\n'.format(action))

if __name__ == '__main__':
	parser = argparse.ArgumentParser()
	parser.add_argument('player_key', nargs='?')
	parser.add_argument('output_path', nargs='?', default=os.getcwd())
	args = parser.parse_args()

	assert(os.path.isdir(args.output_path))

	main(args.player_key, args.output_path)


import cv2
import numpy as np
import math
import os
import pygame #play music
from tkinter.filedialog import askdirectory
from tkinter import *

root=Tk()
root.configure(background='grey')
root.minsize(300,300)
listofsongs = []
total=3
index = total-1#of list
def nextsong(event):
    global index
    if(index==0):
        index=total-1
    else:
        index-=1
    pygame.mixer.music.load(listofsongs[index])
    pygame.mixer.music.play()

def stopsong(event):
     pygame.mixer.music.stop()

def directorychooser():
    directory = askdirectory()
    os.chdir(directory)#change directory

    for files in os.listdir(directory):
        if files.endswith(".mp3"):
            listofsongs.append(files)

    print(listofsongs)

    pygame.mixer.init()#initialise mixer module
def nextsong():

    global index
    if(index==0):
        index=total-1
    else:
        index-=1
    pygame.mixer.music.load(listofsongs[index])
    pygame.mixer.music.play()
def stopsong():
    pygame.mixer.music.stop()
def prevsong():
    global index
    index+=1
    index=index%total
    pygame.mixer.music.load(listofsongs[index])
    pygame.mixer.music.play()

def playsong():
    pygame.mixer.music.load(listofsongs[index])
    pygame.mixer.music.play()
directorychooser()
#listofsongs.reverse()
label=Label(root, text='Music Player',font=('times', 10, 'bold'), bg='grey')
label.pack()
listbox = Listbox(root,font=('times', 10, 'bold'),width=25, bg='white')
listbox.pack()
for items in listofsongs:
    listbox.insert(0,items)
nextbutton=Button(root,activebackground='white',activeforeground='blue',font=('times', 9),text='Next Song',width=10)
nextbutton.pack()
previousbutton=Button(root,activebackground='white',activeforeground='blue',font=('times', 9),text='Previous Song',width=10)
previousbutton.pack()
stopbutton=Button(root,activebackground='white',activeforeground='blue',font=('times', 9),text='Stop',width=10)
stopbutton.pack()
playbutton=Button(root,activebackground='white',activeforeground='blue',font=('times', 9),text='Play',width=10)
playbutton.pack()

nextbutton.bind("<Button-1>",nextsong)#<Button-1> left button
#<Button-2> wheel
#3 is right
previousbutton.bind("<Button-1>",prevsong)
stopbutton.bind("<Button-1>",stopsong)
playbutton.bind("<Button-1>",playsong)

cap=cv2.VideoCapture(0)
while(cap.isOpened()):
    ret,img=cap.read()

    cv2.rectangle(img,(0,0),(350,350),(0,255,),0)
    crop=img[0:350,0:350]#roi

    grey=cv2.cvtColor(crop,cv2.COLOR_BGR2GRAY)

    value=(35,35)
    blur=cv2.GaussianBlur(grey,value,0)
    ret1,thresh=cv2.threshold(blur,127,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
    contours, hierarchy = cv2.findContours(thresh.copy(),cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)#cv2.CHAIN_APPROX_SIMPLE
    drawing = np.zeros(crop.shape,np.uint8)

    cnt = max(contours, key = lambda x: cv2.contourArea(x))

    hull=cv2.convexHull(cnt)
    areahull = cv2.contourArea(hull)
    areacnt = cv2.contourArea(cnt)
    arearatio=((areahull-areacnt)/areacnt)*100

    cv2.drawContours(drawing,[cnt],0,(0,255,0),0)
    cv2.drawContours(drawing,[hull],0,(0,0,255),0)

    hull=cv2.convexHull(cnt, returnPoints=False)

    defects = cv2.convexityDefects(cnt, hull)
    count_defects = 0
    cv2.drawContours(thresh, contours, -1, (0, 255, 0), 3)#3 is width
    for i in range(defects.shape[0]):
        s,e,f,d = defects[i,0]#[start point, end point, farthest point, approximate distance to farthest point ].
        start = tuple(cnt[s][0])
        end = tuple(cnt[e][0])
        far = tuple(cnt[f][0])
    # find length of all sides of triangle

        a = math.sqrt((end[0] - start[0])**2 + (end[1] - start[1])**2)

        b = math.sqrt((far[0] - start[0])**2 + (far[1] - start[1])**2)

        c = math.sqrt((end[0] - far[0])**2 + (end[1] - far[1])**2)

        # apply cosine rule here

        angle = math.acos((b**2 + c**2 - a**2)/(2*b*c)) * 57

        # ignore angles > 90 and highlight rest with red dots

        if angle <= 90:

            count_defects += 1

            cv2.circle(crop, far, 1, [0,0,255], -1)

    # define actions required

    if count_defects == 1:

        cv2.putText(img,"2", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,(0,0,255), 2)

    elif count_defects == 2:
        str = "2"
        cv2.putText(img, "3", (5, 50), cv2.FONT_HERSHEY_SIMPLEX, 1,(0,0,255), 2)

    elif count_defects == 3:

        cv2.putText(img,"4", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,(0,0,255), 2)
        playsong()

    elif count_defects == 4:

        cv2.putText(img,"5", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,(0,0,255), 2)
        stopsong()

    else:
        if areacnt<2000:
            cv2.putText(img,'Nothing',(50,50),cv2.FONT_HERSHEY_SIMPLEX,2,(0,0,255),1,cv2.LINE_AA)
        else:
            if arearatio<12:
                cv2.putText(img,'0',(50,50), cv2.FONT_HERSHEY_SIMPLEX, 2, (0,0,255), 3, cv2.LINE_AA)

            else:
                cv2.putText(img,'1',(50,50), cv2.FONT_HERSHEY_SIMPLEX, 2, (0,0,255), 3, cv2.LINE_AA)

    cv2.imshow('thresh',thresh)
    cv2.imshow('frame',img)
    k = cv2.waitKey(10)

    if k == 27:
        break

cap.release()
cv2.destroyAllWindows()


# @Time    : 2023/4/2 22:49
# @Author  : tk
# @FileName: infer
import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__),'..')))

import torch
from deep_training.data_helper import ModelArguments
from transformers import HfArgumentParser

from data_utils import train_info_args, NN_DataHelper, get_deepspeed_config,build_template
from aigc_zoo.model_zoo.rwkv4.llm_model import MyTransformer, RwkvConfig,set_model_profile
from aigc_zoo.utils.rwkv4_generate import Generate

deep_config = get_deepspeed_config()

if __name__ == '__main__':
    parser = HfArgumentParser((ModelArguments,))
    (model_args,)  = parser.parse_dict(train_info_args, allow_extra_keys=True)

    # 可以自行修改 RWKV_T_MAX  推理最大长度
    set_model_profile(RWKV_T_MAX=2048, RWKV_FLOAT_MODE='')

    dataHelper = NN_DataHelper(model_args)
    tokenizer, _, _,_= dataHelper.load_tokenizer_and_config()

    config = RwkvConfig.from_pretrained('./best_ckpt')
    pl_model = MyTransformer(config=config, model_args=model_args,torch_dtype=torch.float16)

    # deepspeed 权重使用转换脚本命令
    # 一般根据时间排序选最新的权重文件夹
    # cd best_ckpt/last
    # python zero_to_fp32.py . ../last.ckpt

    train_weight = './best_ckpt/last.ckpt'
    pl_model.load_sft_weight(train_weight,strict=True)

    # 保存hf权重
    # config.save_pretrained('convert/')

    # 保存sft p-tuning-v2 权重
    #  pl_model.save_sft_weight('convert/pytorch_model_sft_ptv2.bin')

    # 保存sft权重
    # pl_model.save_sft_weight('convert/pytorch_model_sft.bin')

    model = pl_model.get_llm_model()

    model.eval().half().cuda()

    text_list = ["写一个诗歌，关于冬天",
                 "晚上睡不着应该怎么办",
                 "从南京到上海的路线",
                 ]
    for input in text_list:
        query = build_template(input)
        response = Generate.generate(model, query=query, tokenizer=tokenizer, max_length=512,
                                     eos_token_id=config.eos_token_id,
                                     pad_token_id=config.eos_token_id,
                                     do_sample=True, top_p=0.85, temperature=1.0, )
        print('input',input)
        print('output',response)


import csv
import sys
import getopt
import numpy as np
import pandas as pd
import nltk

def get_dataframe(filename):
    return pd.read_table(filename)

def get_hfw():
    word_file = open('./picked/pacifier.txt', 'r')
    res = list()
    for word in word_file.readlines():
        word = word.split(" ")[0]
        res.append(word)
    return res

def get_hfw_weight():
    word_file = open('./picked/pacifier.txt', 'r')
    res = list()
    for word in word_file.readlines():
        for weigth in word.split(" "):
            try:
                res.append(float(weigth))
            except ValueError:
                pass
    return res

def get_adj():
    word_file = open('./picked/pacifier_a.txt', 'r')
    res_p = list()
    res_n = list()
    for word in word_file.readlines():
        is_positive = False
        if "1" in word.split(" ") or  "1\n"  in word.split(" "):
            is_positive = True
        word = word.split(" ")[0]
        if is_positive:
            res_p.append(word)
        else:
            res_n.append(word)
    return (res_p, res_n)

def get_brands():
    table = get_dataframe("/source_file_path/Problem_C_Data/pacifier.tsv")
    product_titles = table[table['helpful_votes']!=0].product_title.tolist()
    count = {}
    for t in product_titles:
        count[t] = count.get(t, 0) + 1
    res = list()
    for title in count:
        if count[title] > 5:
            res.append(title)
    return res

def get_staratings(product_title):
    table = get_dataframe("/source_file_path/Problem_C_Data/pacifier.tsv")
    product_stars = table[table['product_title']==product_title].star_rating.tolist()
    product_votes = table[table['product_title']==product_title].helpful_votes.tolist()
    res = 0.0
    count = 0
    for i in range(len(product_stars)):
        res += product_stars[i] * product_votes[i]
        count += product_votes[i]
    return res/count

def get_sentence(product_title):
    table = get_dataframe("/source_file_path/Problem_C_Data/pacifier.tsv")
    product_reviews = table[table['product_title']==product_title].review_body.tolist()
    product_review_titles = table[table['product_title']==product_title].review_headline.tolist()
    '''
    product_reviews = table.review_body.tolist()
    product_review_titles = table.review_headline.tolist()
    '''
    product_reviews.extend(product_review_titles)
    tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
    sentences = list()
    for paragraph in product_reviews:
        try:
            sentences.extend(tokenizer.tokenize(paragraph))
        except:
            continue
        finally:
            pass
    return sentences

def get_pairs(product_title):
    # print("----------"+product_title+"----------")
    hfw = get_hfw()
    product_reviews = get_sentence(product_title)
    counts = {}
    for rw in product_reviews:
        tokens = nltk.word_tokenize(rw)
        for hf_word in hfw:
            if hf_word in tokens:
                pos_tags = nltk.pos_tag(tokens)
                last_token = ""
                for token, pos in pos_tags:
                    if pos == "JJ" or pos == "JJS" or pos == "JJR":
                        tmp_pair=(hf_word.lower(), token.lower())
                        if last_token != "not" and last_token != "barely" and last_token != "hardly":
                            counts[tmp_pair] = counts.get(tmp_pair, 0) + 1
                last_token = token
    return counts

def compute_vector(brandname):
    adjs = get_adj()
    positive_adj = adjs[0]
    negative_adj = adjs[1]
    dimension = get_hfw()
    pair_counts = get_pairs(brandname)
    items = list(pair_counts.items())
    items.sort(key=lambda x:x[1], reverse=True)
    vector = []
    # each dimension
    for d in dimension:
        val = 0
        adj_count = 0
        dimension_score = 0
        # iteration in pairs to
        for pairs_ct in items:
            pairs, count = pairs_ct
            count = int(count)
            if pairs[0] == d:
                if pairs[1] in positive_adj:
                    val += 1 * count
                elif pairs[1] in negative_adj:
                    val -= 1 * count
                adj_count += count
        if adj_count != 0:
            dimension_score = val / adj_count
        dimension_res = (d, dimension_score)
        vector.append(dimension_res)

    return vector

def compute_value(brandname):
    vector = compute_vector(brandname)
    value = 0.0
    weights = get_hfw_weight()
    total = 0.0
    for w in weights:
        total += w
    st_weight = list()
    for w in weights:
        st_weight.append(w/total)
    for i in range(len(vector)):
        value += vector[i][1] * st_weight[i]
    return value

def main():
    items = get_brands()
    score_line = ""
    star_line = ""
    for i in items:
        score = compute_value(i)
        star = get_staratings(i)
        if True: #star < (20*score+2.5) and star > (1.25*16 / 3)*(score+0.125):
            score_line += str(score) + ", "
            star_line += str(star) + ", "
    print(score_line)
    print(star_line)

# main()
# compute_vector()
# get_sentence("samsung smh1816s 1.8 cu. ft. stainless steel over-the-range pacifier")
main()


from datetime import datetime

from django.contrib.auth.models import User
from django.db import models

class Organisation(models.Model):
    """
    An organisation that the user belongs to.
    Eg: user_1 belongs to xyz organisation

    # Create an organisation
    >>> organisation_1 = Organisation.objects.create(organisation_name="xyz", organisation_logo="image1.png")
    >>> organisation_2 = Organisation.objects.create(organisation_name="abc", additional_details="Mumbai based company")
    """
    organisation_name = models.CharField(
        max_length=200,
        primary_key=True,
    )
    organisation_logo = models.ImageField(
        upload_to='Organisation/Organisation Logo/%Y/%m/%d/',
        null=True,
        blank=True,
        verbose_name="Organisation Logo"
    )
    additional_details = models.TextField(
        blank=True
    )

    def __str__(self):
        return self.organisation_name

class Profile(models.Model):
    """
    A Profile associated with an existing user.
    Eg: organisation name and phone number are some profile details associated with user_1

    # Create a user profile
    >>> prof_1 = Profile.objects.create(user=user_1, organisation_name="abc", profile_picture="image2.png")
    >>> prof_2 = Profile.objects.create(user=user_2, organisation_name="abc", phone_number="9999999999")
    """
    user = models.OneToOneField(
        User,
        on_delete=models.CASCADE,
        primary_key=True
    )
    organisation_name = models.ForeignKey(
        Organisation,
        on_delete=models.CASCADE,
        null=True,
    )
    profile_picture = models.ImageField(
        upload_to='Organisation/Employee Profile Picture/%Y/%m/%d/',
        null=True,
        blank=True,
        verbose_name="Employee Profile picture"
    )
    phone_number = models.CharField(
        blank=True,
        max_length=10
    )

    def __str__(self):
        return self.user.username  # pylint: disable = E1101

class Header(models.Model):
    """
    A Header list associated with each organisation.
    Eg: Organisation xyz may contain headers in the csv file such as- user_id, title etc

    # Create a header instance
    >>> Header.objects.create(organisation_name="xyz", header="user_id")
    """
    organisation_name = models.ForeignKey(
        Organisation,
        on_delete=models.CASCADE,
    )
    header = models.CharField(
        max_length=200
    )

    class Meta:
        verbose_name_plural = 'Headers'

class Category(models.Model):
    """
    A Category list associated with each organisation.
    Eg: Organisation xyz may contain categories in the csv file such as- hawkers, garbage etc

    # Create a category instance
    >>> Category.objects.create(organisation_name="xyz", category="hawkers")
    """
    organisation_name = models.ForeignKey(
        Organisation,
        on_delete=models.CASCADE,
    )
    category = models.CharField(
        max_length=200
    )

    class Meta:
        verbose_name_plural = 'Category'

class File(models.Model):
    """
    A File uploaded by the logged-in user.
    Eg: user_1 may upload a .csv file on 12/12/12

    # Create a file instance
    >>> File.objects.create(uploaded_by=user_1, csv_file="file1.csv", uploaded_date = "Jan. 29, 2019, 7:59 p.m.")
    """
    uploaded_by = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
    )
    csv_file = models.FileField(
        upload_to='Organisation/CSV File/%Y/%m/%d/',
    )
    uploaded_date = models.DateTimeField(
        default=datetime.now,
    )

    class Meta:
        verbose_name_plural = 'CSV File Meta'


import os
import sys
import numpy as np
import pandas as pd
import argparse
import pybedtools
import re
import pyBigWig as pbw
import time
import urllib.request

def main():

    start = time.time()
    print("Generating consensus peak file...")
    args = parse_args()
    #for
    Input_peaks=pd.read_csv(args.Peaks, sep="\t")
    # Current peaks =
    for i in range(0, len(Input_peaks.index)):

        urllib.request.urlretrieve(Input_peaks.iloc[i]["URL"], "../tmp/tmp_peak.bed.gz")
        Current_peaks=pybedtools.BedTool("../tmp/tmp_peak.bed.gz")
        Current_peaks_pd=pd.read_table(Current_peaks.fn)
        Current_peaks_pd.to_csv("../tmp/Concat_peaks.bed", mode='a', header=False, sep="\t", index=False)

    # Formatted peaks =

    print(str(len(Input_peaks.index))+" peak files read in "+str(time.time()-start)+" seconds...")

    Concat_peaks=pybedtools.BedTool("../tmp/Concat_peaks.bed")
    Concat_peaks_sorted=Concat_peaks.sort()
    Concat_peaks_merged=Concat_peaks_sorted.merge(d=0)

    Concat_peaks_merged.saveas(args.outdir+args.prefix+"_Consensus_peaks.bed")

def parse_args():
    """ Load command line args """
    parser = argparse.ArgumentParser()
    parser.add_argument('--prefix', metavar="<str>", help=("Output prefix"), type=str, required=True)
    parser.add_argument('--Peaks', metavar="<str>", help=("Input peak URLs"), type=str, required=True)
    parser.add_argument('--outdir', metavar="<str>", help=("Output directory"), type=str, required=True)

    args = parser.parse_args()
    return args

if __name__ == '__main__':

    main()

# python Generate_consensus_peaks.py --prefix BLUEPRINT --Peaks ~/BLUEPRINT_peak_URLs.tsv --outdir ~/BLUEPRINT_peaks/
#


import json
from pydantic import parse_obj_as
from abc import ABC, abstractmethod
from typing import Any

from aiober.methods.base import Response
from .viber import ViberAPIServer, PRODUCTION

DEFAULT_TIMEOUT: float = 60.0

class BaseSession(ABC):

    def __init__(self):
        self.api: ViberAPIServer = PRODUCTION
        self.timeout = DEFAULT_TIMEOUT

    @abstractmethod
    async def make_request(self, bot, timeout: int = None):

        pass

    def check_response(self, bot, status_code: int, content: str) -> Response:
        try:
            json_data = json.loads(content)
        except Exception as E:
            raise UnicodeDecodeError("failed to decode object")

        print(json_data)

        response = parse_obj_as(Response, json_data)

        if 200 <= status_code <= 220:
            return response

        raise RuntimeError(f'status code {status_code}')


# 내 풀이 & 교수님 풀이
from cs1robots import *

load_world('worlds/rain1.wld')
# load_world('worlds/rain2.wld')
hubo = Robot(beepers=10, street=6, avenue=2)
hubo.set_trace('blue')

def turn_right():
    for _ in range(3):
        hubo.turn_left()

def turn_around():
    for _ in range(2):
        hubo.turn_left()

def mark_starting_point_and_move():
    hubo.move()
    hubo.drop_beeper()
    turn_right()
    hubo.move()

def judgment():
    if hubo.right_is_clear():
        turn_right()
        hubo.move()
        if hubo.right_is_clear():
            turn_around()
            hubo.move()
            turn_right()
            hubo.drop_beeper()
            hubo.move()
    elif hubo.front_is_clear():
        hubo.move()
    else:
        hubo.turn_left()

mark_starting_point_and_move()
while not hubo.on_beeper():
    judgment()
hubo.pick_beeper()
turn_right()


#!/usr/bin/env python3

import sys, re, argparse

R = re.compile("(?P<number>\d+)\s+(?P<repeat>\d+)R")

def main():
    """
    mcnp2phits - converts *some* parts of MCNP deck into PHITS format
    """
    parser = argparse.ArgumentParser(description=main.__doc__,
                                     epilog="Homepage: https://github.com/kbat/mc-tools")
    parser.add_argument("mcnp", type=str,
                        help="MCNP file name")
    parser.add_argument("phits", type=str,
                        help="PHITS file name")

    args = parser.parse_args()

    phits = open(args.phits, 'w')
    mcnp = open(args.mcnp, 'r')

    for line in mcnp.readlines():
        s = R.search(line)
        if s:
            print(re.sub(R,
                lambda m: (m.group('number')+" ")*(int(m.group('repeat'))+1),
                         line.rstrip()))
        else:
            print(line.rstrip())

if __name__ == "__main__":
    sys.exit(main())


import locale
import sys

class Base_Model():
	trim ='normal'
	en_lit = 1.5
	mile_range=450
	tank_cap = 45
	color='white'
	trans='auto'

	@classmethod
	def miles_per_liter(cls):
		return cls.mile_range / cls.tank_cap

	@classmethod
	def miles_per_gallon(cls):
		return cls.miles_per_liter() * 3.78541

	def __init__(self, price, trans='auto', color='white'):
		self.price = price
		self.trans = trans
		self.color = color

	def info(self):
		if sys.platform.startswith('win'):
			locale.setlocale(locale.LC_ALL, 'us')
		else:
			locale.setlocale(locale.LC_ALL, 'en_US.utf8')
		print('the price of %s was %s ' % (self, locale.currency(self.price)))

	def __str__(self):
		return 'a %s  base model with %s trans' % (self.color, self.trans)

coop = Base_Model(color='green',trans='auto', price=21313)
coop.info()
print('The %s gets %4.1f miles per gallon' % (coop, coop.miles_per_gallon()))
print('The %s gets %4.1f miles per liter' % (coop, coop.miles_per_liter()))
print('The %s gets %4.1f miles per gallon' % (Base_Model, Base_Model.miles_per_gallon()))


from django.db.models import fields
from django.forms.forms import Form
from django.shortcuts import render, redirect, HttpResponseRedirect
from django.urls import reverse
from django.views.generic import ListView, DetailView, UpdateView, CreateView, FormView
from django.http import Http404
from django.core.paginator import Paginator
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from . import models as noti_model
from reviews import forms
from notifications import forms as noti_forms
from users import mixins
from users import models as user_model

# Create your views here.

def BoardView(request):
    page = request.GET.get("page", 1)
    notifications = noti_model.Posting.objects.filter(notification=True)
    normals = noti_model.Posting.objects.filter(notification=False)
    paginator = Paginator(normals, 14, orphans=5)
    normals = paginator.page(int(page))
    return render(
        request,
        "notifications/posting_list.html",
        {"normals": normals, "notifications": notifications},
    )

@login_required
def post_detail(request, pk):
    try:
        post = noti_model.Posting.objects.get(pk=pk)
        print(post.notification)
        form = forms.CreateCommentForm()
        user_comment = None
        if request.method == "POST":
            print(request.POST)
            comment_form = forms.CreateCommentForm(request.POST)
            if comment_form.is_valid():
                user_comment = comment_form.save(commit=False)
                user_comment.post = post
                user_comment.user = request.user
                user_comment.save()
                return HttpResponseRedirect(
                    reverse("notifications:detail", kwargs={"pk": pk}),
                    {"post": post, "form": form},
                )  ## to prevent us from double submit
        return render(
            request, "notifications/detail.html", {"post": post, "form": form}
        )
    except noti_model.Posting.DoesNotExist:
        return Http404

def search(request):
    filter_args = {}
    keyword = request.GET.get("keyword")
    print(keyword)
    if keyword != None:
        filter_args["title__contains"] = keyword
        results = noti_model.Posting.objects.filter(**filter_args)

    else:
        results = noti_model.Posting.objects.all()
    page = request.GET.get("page", 1)
    paginator = Paginator(results, 12, orphans=5)
    posts = paginator.page(int(page))
    return render(
        request,
        "notifications/search.html",
        {"page": posts, "keyword": keyword},
    )

class PostPhotosView(DetailView):
    model = noti_model.Posting
    template_name = "notifications/post_photos.html"

    def get_object(self, queryset=None):
        post = super().get_object(queryset=queryset)
        if post.user.pk != self.request.user.pk:
            raise Http404()
        return post

class EditPhotoView(UpdateView):

    model = noti_model.Photo
    template_name = "notifications/photo_edit.html"
    fields = ("caption",)
    pk_url_kwarg = "photo_pk"

    def get_success_url(self):
        post_pk = self.kwargs.get("post_pk")
        return reverse("notifications:photos", kwargs={"pk": post_pk})

class AddPhotoView(FormView):
    template_name = "notifications/photo_create.html"
    form_class = noti_forms.CreatePhotoForm

    def form_valid(self, form):
        pk = self.kwargs.get("pk")
        form.save(pk)
        return redirect(reverse("notifications:photos", kwargs={"pk": pk}))

class UploadPostView(mixins.LoggedInOnlyView, FormView):
    template_name = "notifications/post_create.html"
    form_class = noti_forms.CreatePostForm

    def form_valid(self, form):
        noti = self.request.POST.get("notificataion")
        if noti == "on":
            bool = True
        else:
            bool = False
        pk = self.request.user.pk
        form.save(pk, bool)
        return redirect("/notifications/")

@login_required
def delete_photo(request, post_pk, photo_pk):
    user = request.user
    try:
        post = noti_model.Posting.objects.get(pk=post_pk)
        if post.user != user:
            messages.error(request, "You are not athorized")
        else:
            noti_model.Photo.objects.filter(pk=photo_pk).delete()
            messages.success(request, "Photo Deleted")
        return redirect(reverse("notifications:photos", kwargs={"pk": post_pk}))
    except noti_model.Posting.DoesNotExist:
        return redirect(reverse("core:home"))


from fastapi import FastAPI, Request, Response
import http, csv, json, yaml
import xml.etree.ElementTree as ET

app = FastAPI()

@app.get("/read-txt")
def _readTxtEndpoint():
    with open('./text_file.txt') as f:
        lines = f.read()
    return {"resultSet": lines}

@app.get("/read-csv")
def _readCsvEndpoint():
    with open('./csv_file.csv') as csv_file:
        list = []
        reader = csv.reader(csv_file)
        for row in reader:
            list.append(row)
        csv_file.close()
    keys = list[0]
    values = list[1]
    myDict = {k: v for k, v in zip(keys, values)}
    return {"resultSet": myDict}

@app.get("/read-yaml")
def _readYamlEndpoint():
    with open('./yaml_file.yaml') as yaml_file:
        output = yaml.safe_load(yaml_file)
    return {"resultSet": output}

@app.get("/read-xml")
def _readXmlEndpoint():
    root = ET.parse('./xml_file.xml').getroot()
    output = root[0].text
    return {"resultSet": output}


"""
What are all of the letters that never appear consecutively in an English word?
For example, we know that “U” is not an answer, because of the word VACUUM, and we know that “A” is not an answer, because of “AARDVARK”, but which letters never appear consecutively?

"""
input_filename = "sowpods.txt"

#
def find_letters_never_consecutive(filename):
    # We'll use sets to make finding the difference easier later
    consecutive_letters = set()
    # Sets do not allow duplicates
    alphabet = set('ABCDEFGHIJKLMNOPQRSTUVWXYZ')

    with open(filename, 'r') as file:
        words = file.readlines()

    for word in words:
        word = word.strip()
        preceding_letter = None
        for letter in word:
            if letter == preceding_letter:
                consecutive_letters.add(letter)
            preceding_letter = letter

    # Find the letters that do not appear consecutively
    never_consecutive_letters = alphabet - consecutive_letters
    return never_consecutive_letters

non_consecutive_letters = find_letters_never_consecutive(input_filename)
print("Letters that never appear consecutively in the input file: ", non_consecutive_letters)


import os
from torch.utils.data import Dataset
from torchvision.transforms import RandomCrop, Resize, InterpolationMode, RandomHorizontalFlip
from torchvision.transforms.functional import rotate
from torchvision.io import read_image
import numpy as np

class ImageData(Dataset):
    def __init__(self, data_path, HR_shape=None, training=True):
        super(ImageData, self).__init__()
        self.data_path = data_path
        self.data = os.listdir(data_path)
        self.training = training
        if training:
            LR_shape = HR_shape // 4
            self.crop = RandomCrop((HR_shape, HR_shape), pad_if_needed=True)
            self.resize = Resize((LR_shape, LR_shape), InterpolationMode.BICUBIC)
            self.rand_flip = RandomHorizontalFlip()
        else:
            self.crop = RandomCrop((400, 400), pad_if_needed=True)

    def __len__(self):
        return len(self.data)

    def __getitem__(self, index):
        file_name = self.data[index]
        image = read_image(os.path.join(self.data_path, file_name))
        image = self.crop(image)
        if self.training:
            image = self.rand_flip(image)
            if np.random.rand() < 0.5:
                image = rotate(image, 90)
            LR_image = self.resize(image) / 255.0
        else:
            LR_image = Resize((image.shape[1] // 4, image.shape[2] // 4), InterpolationMode.BICUBIC)(image) / 255.0
        HR_image = 2 * (image / 255.0) - 1
        return LR_image, HR_image


from __future__ import annotations

from typing import TYPE_CHECKING

from randovania.game_description.pickup import pickup_category
from randovania.game_description.pickup.pickup_entry import PickupEntry, PickupGeneratorParams, PickupModel
from randovania.game_description.resources.location_category import LocationCategory
from randovania.games.prime3.patcher import corruption_items
from randovania.generator.pickup_pool import PoolResults

if TYPE_CHECKING:
    from randovania.game_description.resources.resource_database import ResourceDatabase

ENERGY_CELL_CATEGORY = pickup_category.PickupCategory(
    name="energy_cell", long_name="Energy Cell", hint_details=("an ", "energy cell"), hinted_as_major=True, is_key=True
)

def add_energy_cells(
    resource_database: ResourceDatabase,
) -> PoolResults:
    """
    :param resource_database:
    :return:
    """
    item_pool: list[PickupEntry] = []

    for i in range(9):
        item_pool.append(create_energy_cell(i, resource_database))

    return PoolResults(item_pool, {}, [])

def create_energy_cell(
    cell_index: int,
    resource_database: ResourceDatabase,
) -> PickupEntry:
    return PickupEntry(
        name=f"Energy Cell {cell_index + 1}",
        progression=((resource_database.get_item(corruption_items.ENERGY_CELL_ITEMS[cell_index]), 1),),
        extra_resources=(
            (resource_database.get_item(corruption_items.ENERGY_CELL_TOTAL_ITEM), 1),
            (resource_database.get_item(corruption_items.PERCENTAGE), 1),
        ),
        model=PickupModel(
            game=resource_database.game_enum,
            name=corruption_items.ENERGY_CELL_MODEL,
        ),
        pickup_category=ENERGY_CELL_CATEGORY,
        broad_category=pickup_category.GENERIC_KEY_CATEGORY,
        generator_params=PickupGeneratorParams(
            preferred_location_category=LocationCategory.MAJOR,
            probability_offset=0.25,
        ),
    )


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
from collections import deque
class Solution:
    def reorderList(self, head: Optional[ListNode]) -> None:
        if not head:
            return
        q=deque()
        node=head
        while node:             #insert elements of linked list into a queue
            node=node.next
            if not node:
                break
            q.append(node)

        while q:
            if head:            #pop last element from queue and insert in head.next
                temp=q.pop()
                head.next=temp
                head=head.next
            if head and q:      #pop first element from queue and insert in head.next
                temp=q.popleft()
                head.next=temp
                head=head.next
        head.next=None


"""
/*** 本模块实现了自定义音乐查询获取并返回音乐CQ码kuq接口进行反向传输  ****/
/*** 音乐可以来自任何平台，并且支持查询操作****/
/*** write by @fengx1a0
"""
class FindMusic():
	def __init__(self,key):
		self.__key = key
		import requests
		self.__request = requests.get
		handle = self.__request(url="http://musicapi.leanapp.cn/search?keywords="+self.__key)
		_json = handle.json()
		self.__id = str(_json['result']['songs'][0]['id'])
		self.__songname = _json['result']['songs'][0]['name']
		self.__albumid = str(_json['result']['songs'][0]['album']['id'])

		tmp = _json['result']['songs'][0]['artists']
		self.__auth = ''
		for i in tmp:
			self.__auth+=i["name"]
			self.__auth+="/"
		self.__auth = self.__auth[:-1]
		handle.close()

	def get_url(self):
		return "https://music.163.com/#/song?id="+self.__id
	def get_image(self):
		handle = self.__request(url="http://musicapi.leanapp.cn/album?id="+self.__albumid)
		_json = handle.json()
		imageurl = _json['songs'][0]['al']['picUrl']
		handle.close()
		return imageurl
	def getaudio(self):
		return "https://music.163.com/song/media/outer/url?id="+self.__id+".mp3"
	def gettitle(self):
		return self.__songname
	def getcontent(self):
		return self.__auth

def get_music(msg): # 音乐名+fuzz搜索

	music=msg[6:-1]
	musicInfo = FindMusic(msg)
	try:
		musicInfo = FindMusic(msg)
	except:
		return "呜呜呜~该音乐未找到..."
	msg = "[CQ:music,type=custom,url={},audio={},title={},content={},image={}]".format(musicInfo.get_url() ,musicInfo.getaudio(),musicInfo.gettitle(),musicInfo.getcontent(),musicInfo.get_image())
	print(msg)
	return msg


from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import csv
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import re
import json

chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--start-maximized')
chrome_options.add_argument('--single-process')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument("--incognito")
chrome_options.add_argument('--disable-blink-features=AutomationControlled')
chrome_options.add_experimental_option('useAutomationExtension', False)
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_argument("disable-infobars")
ser = Service("./chromedriver.exe")
driver = webdriver.Chrome(options=chrome_options ,service=ser)

mainUrl = "https://batdongsan.com.vn/nha-dat-ban"

houseList = []
c = 1
for i in range(3, 123):
    url = "https://batdongsan.com.vn/nha-dat-ban/p" + str(i+1)
    driver.get(url)
    houseTags = driver.find_elements(by=By.CSS_SELECTOR, value=".js__product-link-for-product-id")
    houseUrls = [el.get_attribute("href") for el in houseTags]
    # for subUrl in houseUrls:
    for subUrl in houseUrls:
        print(c)
        print(subUrl)
        driver.get(subUrl)
        delay = 5  # seconds
        try:
            myElem = WebDriverWait(driver, delay).until(EC.presence_of_element_located(
                (By.CSS_SELECTOR, '.re__media-thumb-item.js__media-thumbs-item.slick-slide.slick-active')))
        except Exception as e:
            print(e)
            continue
        html = driver.page_source
        soup = BeautifulSoup(html, "html.parser")
        house = {}
        try:
            house['Original Link'] = subUrl
            house['Tên người bán'] = soup.select_one('body > div.re__main > div > div.re__main-sidebar > div.re__sidebar-box.re__contact-box.js__contact-box > div.re__contact-name.js_contact-name')['title']
            house['Số điện thoại'] = soup.select_one('body > div.re__main > div > div.re__main-sidebar > div.re__sidebar-box.re__contact-box.js__contact-box > div.re__btn.re__btn-cyan-solid--md.phone > span')['mobile']
            house['Tiêu đề'] = soup.select_one('#product-detail-web > h1').getText()
            house['Địa chỉ'] = soup.find(class_ = "re__pr-short-description").getText()
            moTa = soup.find(class_="re__section-body re__detail-content js__section-body js__pr-description js__tracking").getText()
            s = re.sub('<br\s*?>', ' ', moTa)
            house['Mô tả'] = s
            elements = soup.find_all(class_ = "re__pr-specs-content-item-title")
            elements2 = soup.find_all(class_ = "re__pr-specs-content-item-value")
            for i in range(len(elements)):
                a = elements2[i].getText()
                b = str(a)
                house[str(elements[i].getText())] = b
            house['Ngày đăng'] = soup.select_one('#product-detail-web > div.re__pr-short-info.re__pr-config.js__pr-config > div:nth-child(1) > span.value').getText()
            house['Ngày hết hạn'] = soup.select_one('#product-detail-web > div.re__pr-short-info.re__pr-config.js__pr-config > div:nth-child(1) > span.value').getText()
            house['Mã tin'] = soup.select_one('#product-detail-web > div.re__pr-short-info.re__pr-config.js__pr-config > div:nth-child(4) > span.value').getText()
            elements = soup.find_all("div", {"class":"re__media-thumb-item js__media-thumbs-item slick-slide slick-active"})
            ImgArr = []
            for el in elements:
                ImgArr.append(el.findChild("img", recursive=False)['data-src'])
            rs_s = ''
            for i in range(len(ImgArr)- 1):
                rs_s = rs_s + ImgArr[i] + ', '
            house['Ảnh'] = rs_s
            houseList.append(house)
            c += 1
            print(house)
        except Exception as e:
            print(e)
            continue
print('so luong data: ' + str(len(houseList)))
s = set()
for i in houseList:
    s.update(i)
header = list(s)

with open("Bat-dong-san2.csv", 'w', encoding="utf-8") as f:
    writer = csv.writer(f)
    writer.writerow(header)
    for d in houseList:
        writer.writerow([d.get(i, "NULL") for i in header])

# with open('outputfile.json', 'w') as fout:
#     json.dump(houseList, fout)


from director.consoleapp import ConsoleApp
from director import robotsystem
from director import visualization as vis
from director import objectmodel as om
from director import ikplanner
from director import ikconstraintencoder as ce
from director import ikconstraints
from director import transformUtils

import numpy as np
import pprint
import json

def getRobotState():
    return robotStateJointController.q.copy()

def buildConstraints():
    '''
    For testing, build some constraints and return them in a list.
    '''

    startPose = getRobotState()

    startPoseName = 'plan_start'
    endPoseName = 'plan_end'

    ikPlanner.addPose(startPose, startPoseName)
    ikPlanner.addPose(startPose, endPoseName)

    constraints = []
    constraints.extend(ikPlanner.createFixedFootConstraints(startPoseName))
    constraints.append(ikPlanner.createMovingBaseSafeLimitsConstraint())
    constraints.append(ikPlanner.createLockedLeftArmPostureConstraint(startPoseName))
    constraints.append(ikPlanner.createLockedRightArmPostureConstraint(startPoseName))
    constraints.append(ikPlanner.createLockedRightArmPostureConstraint(startPoseName))

    targetFrame = ikPlanner.getLinkFrameAtPose(ikPlanner.getHandLink('left'), startPose)

    p, o = ikPlanner.createPositionOrientationGraspConstraints('left', targetFrame)
    p.tspan = [1.0, 1.0]
    o.tspan = [1.0, 1.0]
    constraints.extend([p, o])

    return constraints

def reconstructConstraints(constraints):
    '''
    Convert dicts (decoded from json) back to the original
    constraint classes using the 'class' information in the dict
    '''
    objs = []

    for c in constraints:
        objClass = getattr(ikconstraints, c['class'])
        del c['class']
        obj = objClass()
        objs.append(obj)

        for attr, value in c.items():
            if isinstance(value, dict) and 'position' in value and 'quaternion' in value:
                value = transformUtils.transformFromPose(value['position'], value['quaternion'])
            setattr(obj, attr, value)

    return objs

def testPlanConstraints():

    ikPlanner.planningMode = 'dummy'

    constraints = buildConstraints()
    poses = ce.getPlanPoses(constraints, ikPlanner)

    poseJsonStr = json.dumps(poses, indent=4)
    constraintsJsonStr = ce.encodeConstraints(constraints, indent=4)

    print(poseJsonStr)
    print(constraintsJsonStr)

    print('--------------decoding--------------------')
    constraints = ce.decodeConstraints(constraintsJsonStr)
    pprint.pprint(constraints)

    print('--------------reconstructing--------------')
    constraints = reconstructConstraints(constraints)

    print('--------------matlab commands---------------')
    for c in constraints:
        c.printCommands()

app = ConsoleApp()
view = app.createView()
robotSystem = robotsystem.create(view=view, planningOnly=True)

app.setupGlobals(globals())
globals().update(dict(robotSystem))

testPlanConstraints()


def _impl(ctx):
    out = ctx.actions.declare_file(ctx.label.name)
    ctx.actions.write(out, "Fake executable")
    return [
        cc_common.create_cc_toolchain_config_info(
            ctx = ctx,
            toolchain_identifier = "local_linux",
            host_system_name = "local",
            target_system_name = "local",
            target_cpu = "local",
            target_libc = "local",
            compiler = "compiler",
            abi_version = "local",
            abi_libc_version = "local",
        ),
        DefaultInfo(
            executable = out,
        ),
    ]

cc_toolchain_config = rule(
    implementation = _impl,
    attrs = {},
    provides = [CcToolchainConfigInfo],
    executable = True,
)


from django.http import JsonResponse
from django.shortcuts import render
from redis_ import rd
# Create your views here.
from django.views.decorators.cache import cache_page

from art.models import Art
from user import helper
import redis_
from art import tasks

@cache_page(30)
def show(request,id):
    login_user = helper.getLoginInfo(request)  #读取session登陆信息
    #阅读art_id的文章
    art = Art.objects.get(pk=id)

    #写入到阅读排行中（Redis->ReadTopRank）
    redis_.incrTopRank(id)

    readTopRank = redis_.getReadTopRank(5)  #[(,score)]

    return render(request,'art/show.html',locals())

def qdArt(request,id):
    #获取当前登录的用户信息
    login_user = helper.getLoginInfo(request)
    if not login_user:
        return JsonResponse({'msg':'请先登录','code':101})

    tasks.qdTask.delay(login_user.get(id),id)  #延迟异步执行
    return JsonResponse({'msg':'正在抢读','code':201})

def queryQDState(request,id):
    login_user = helper.getLoginInfo(request)
    if not login_user:
        return JsonResponse({'msg':'请先登录','code':101})

    uid = login_user.get('id')
    # if rd.hexists('qdArt',uid):
    #     # 一个用户抢两本书，查询最新的id的抢读状态，而不是之前抢读的状态
    #     qdId = rd.hget('qdArt', uid)  # 已抢的书id,  qdId和id可能不一样

    if login_user.get('id'):
        art = Art.objects.get(pk=id)
        return JsonResponse({'msg':'抢读成功','code':200,
                             'art':{'title':art.title,
                                    'author':art.author}
                             })
    if rd.hlen('qdArt') < 5:
        return JsonResponse({'msg': '抢读中', 'code': 201})
    else:
        return JsonResponse({'msg': '抢读失败', 'code': 300})


"""
Given a list of dictionary items, find the most optimal host with the least distance.
"""
import math

friends = [{"name": 'Bob', 'location': (5,2,10)}, {"name": 'Radha', 'location': (2,3,5)},
    {"name": 'Mary', 'location': (19,3,4)}, {"name": 'Skyler', 'location': (1,5,3)}]

def find_host(friends):
    initial_value = friends[0]['location']
    distances = []
    d = {}
    for i in range(0, len(friends)):
        for j in range(0, len(friends)):
            if i != j:
                x1 = friends[i]['location'][0]
                y1 = friends[i]['location'][1]
                z1 = friends[i]['location'][2]
                x2 = friends[j]['location'][0]
                y2 = friends[j]['location'][1]
                z2 = friends[j]['location'][2]
                dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2 +(z2-z1)**2)
                d[friends[i]['name']] = dist

    initial_value = list(d.values())[0]

    for key, value in d.items():
        initial_value = min(value, initial_value)
        min_item = initial_value
    for key, value in d.items():
        if value == min_item:
            return key

print(find_host(friends))


import sys
import json
from mycroft.messagebus.client.ws import WebsocketClient
from mycroft.messagebus.message import Message
from mycroft.configuration import ConfigurationManager
from websocket import create_connection

def main():
    """
        Main function, will run if executed from command line.

        Sends parameters from commandline.

        Param 1:    message string
        Param 2:    data (json string)
    """
    # Parse the command line
    if len(sys.argv) == 2:
        messageToSend = sys.argv[1]
        dataToSend = {}
    elif len(sys.argv) == 3:
        messageToSend = sys.argv[1]
        try:
            dataToSend = json.loads(sys.argv[2])
        except BaseException:
            print("Second argument must be a JSON string")
            print("Ex: python -m mycroft.messagebus.send speak "
                  "'{\"utterance\" : \"hello\"}'")
            exit()
    else:
        print("Command line interface to the mycroft-core messagebus.")
        print("Usage: python -m mycroft.messagebus.send message")
        print("       python -m mycroft.messagebus.send message JSON-string\n")
        print("Examples: python -m mycroft.messagebus.send system.wifi.setup")
        print("Ex: python -m mycroft.messagebus.send speak "
              "'{\"utterance\" : \"hello\"}'")
        exit()

    send(messageToSend, dataToSend)

def send(messageToSend, dataToSend=None):
    """
        Send a single message over the websocket.

        Args:
            messageToSend (str):    Message to send
            dataToSend (dict):      data structure to go along with the
                                    message, defaults to empty dict.
    """
    dataToSend = dataToSend or {}

    # Calculate the standard Mycroft messagebus websocket address
    config = ConfigurationManager.get().get("websocket")
    url = WebsocketClient.build_url(config.get("host"),
                                    config.get("port"),
                                    config.get("route"),
                                    config.get("ssl"))

    # Send the provided message/data
    ws = create_connection(url)
    packet = Message(messageToSend, dataToSend).serialize()
    ws.send(packet)
    ws.close()

if __name__ == '__main__':
    try:
        main()
    except IOError:
        print('Could not connect to websocket, no message sent')


import sys
import tester
import signal
from utility import interpret, execute

def handler(x, y):
    print('Too slow!')
    sys.exit(-1)

signal.signal(signal.SIGALRM, handler)
signal.alarm(60 * 10)

def test_compiler(compiler_code):
    testcase = tester.generate_testcases()
    for code, inputstr in testcase:
        output1 = interpret(code, inputstr)
        assembly = execute(compiler_code, code)
        output2 = execute(assembly, inputstr)

        if output1 != output2:
            print(b'Different output generated.\ncode:\n%s\noutput1:\n%s\noutput2:\n%s\n' % (
                code, output1, output2))
            return False

    return True

EOF = b'TSGCTF'

if __name__ == '__main__':
    sys.stdout.buffer.write(b'Input your compiler. Append "%s" at the end of your compiler.\n' % EOF)

    code = b''
    while True:
        s = sys.stdin.buffer.readline()
        idx = s.find(EOF)
        if idx < 0:
            code += s
        else:
            code += s[:idx]
            break
        if len(code) >= 20000000:
            print('Too long!')
            exit(-1)

    print('Thanks! I\'ll run some tests.')

    if test_compiler(code):
        print('Your compiler works!')
    else:
        print('Your compiler is broken.')
        exit(-1)

    compiler_code = open('compiler.x', 'rb',).read()
    code2 = execute(code, compiler_code)
    if test_compiler(code2):
        print('Compiled compiler works!')
    else:
        print('Compiled compiler is broken.')
        exit(-1)

    code3 = execute(code2, compiler_code)
    if code2 != code3:
        print('Compiled compiler fails to compile compiler.')
        exit(-1)

    print('Thanks! I will give you the first byte of the flag!')
    flag = open('flag', 'rb').read()
    flag_code = b'flag; main(){ flag = "%s"; write([flag[0]]); }' % flag
    flag_program = execute(code3, flag_code)
    print(execute(flag_program, b''))


from statistics import mode
import pytorch_lightning as pl
from torch.nn import functional as F
from torch import optim
from transformers import AutoModelForSequenceClassification
import torch
import pandas as pd
import numpy as np
from prediction_stats import print_stats

trans_cache_dir =  "/cluster/scratch/gboeshertz/huggingface_cache"

class BaseModule(pl.LightningModule):

    def __init__(self, config,class_weights=None):
        super().__init__()
        self.save_hyperparameters()
        self.config = config
        self.model = AutoModelForSequenceClassification.from_pretrained(config['model_name'], num_labels=2,
                                                                        ignore_mismatched_sizes=True,cache_dir =trans_cache_dir)

        self.test_list_logits = []
        self.test_list_labels = []
        if class_weights is not None:
            self.class_weights = torch.from_numpy(class_weights).float()

    def load_ckpt(self,path):
        model_dict = torch.load(path,map_location= "cuda" if torch.cuda.is_available() else "cpu" )['state_dict']
        model_dict = {k.replace('model.',''):v for k,v in model_dict.items() if 'model' in k}
        #if not torch.cuda.is_available():
        #    for k, v in model_dict.items():
        #        model_dict[k] = v.cpu()

        self.model.load_state_dict(model_dict)

    def forward(self, x):
        # x should be a dictionnary with at least a key input_ids
        return self.model(x).logits

    def get_loss(self,preds,y,weights=None):
        return F.cross_entropy(preds.logits,y,weight=weights)

    def predict_sentence(self, tokens,masks=None,segs = None):

        with torch.no_grad():
            tokens = tokens.to(self.model.device)
            if segs is not None:
                segs = segs.to(self.model.device)

            if masks is not None:
                masks = masks.to(self.model.device)

            output = self.model(tokens, token_type_ids=segs, attention_mask=masks)

            pred = output.logits.argmax(axis=1).item()
        return pred

    def batch_step(self,batch):
        if self.config["use_hypothesis"]:
            pair_token_ids, mask_ids, seg_ids, y = batch

            pair_token_ids = pair_token_ids.to(self.model.device)
            mask_ids = mask_ids.to(self.model.device)
            seg_ids = seg_ids.to(self.model.device)
            labels = y.to(self.model.device)
            output = self.model(pair_token_ids,
                                    token_type_ids=seg_ids,
                                    attention_mask=mask_ids,
                                    labels=labels)

        else:
            premise_ids, mask_ids, y = batch
            premise_ids = premise_ids.to(self.model.device)
            mask_ids = mask_ids.to(self.model.device)
            labels = y.to(self.model.device)

            output = self.model(premise_ids,
                            attention_mask=mask_ids,
                            labels=labels)

        return output

    def training_step(self, batch, batch_idx):

        if self.class_weights is not None:
            self.class_weights = self.class_weights.to(self.model.device)

        output = self.batch_step(batch)
        #accuracy = (output.logits.argmax(axis=0) == y).mean()
        y = batch[-1].to(self.model.device)
        loss = self.get_loss(output,y,self.class_weights)
        accuracy = (output.logits.argmax(axis=1) == y).float().mean().item()
        self.log("train_acc", accuracy,on_step=True, on_epoch=True, prog_bar=True, logger=True)
        self.log("train_loss", loss,on_step=True, on_epoch=True, prog_bar=True, logger=True)
        return loss

    def validation_step(self, batch, batch_idx):
        if self.class_weights is not None:
            self.class_weights = self.class_weights.to(self.model.device)

        output = self.batch_step(batch)
        y = batch[-1].to(self.model.device)
        loss = self.get_loss(output,y,self.class_weights)
        accuracy = (output.logits.argmax(axis=1) == y).float().mean().item()

        self.log("val_acc", accuracy,on_step=True, on_epoch=True, prog_bar=True, logger=True)
        self.log("val_loss", loss,on_step=True, on_epoch=True, prog_bar=True, logger=True)

    def test_step(self, batch, batch_idx):
        logits = self.batch_step(batch).logits
        self.test_list_logits.append(logits)
        self.test_list_labels.append(batch[-1].to(self.model.device))

    def test_epoch_end(self, outputs):
        test_outputs = torch.vstack(self.test_list_logits).cpu().numpy()
        test_labels = torch.vstack(self.test_list_labels).cpu().numpy()[:,0]

        test_outputs = test_outputs.argmax(axis=1)

        preds_labels = np.vstack((test_outputs,test_labels))
        with open('test_outputs/'+ self.config["test_out_path"] + '.npy', 'wb') as f:
            np.save(f, preds_labels)

        print_stats(preds_labels,self.config["test_out_path"])

    def configure_optimizers(self):
        optimizer = optim.AdamW(self.parameters(), lr=self.config['lr'])
        lr_scheduler = optim.lr_scheduler.MultiStepLR(optimizer, milestones=[1, 2], gamma=0.1)
        return [optimizer], [lr_scheduler]