from flask import render_template, url_for, request, redirect
from flask.helpers import send_from_directory
from init import app

from controller.userManagement import userLogin, isTeacher , redirectTeacher
from controller.challengeControl import makeChallenge, stopChallenge, getChallenge
from controller.mapControl import createMap, deleteMap, getGrid, isValidMap, makeChallenge
from controller.instructionControl import setInstruction, getInstruction
from controller.scoreControl import setHighscore, getHighscore, checkHighscore
from currentMap import getCurrentMap
from controller.apiManagement import getCarInstruction, getCarData, setCarData

import sqlite3

@app.route('/')
def index():
    return render_template("landing.html")

@app.route('/createMap', methods=['GET', 'POST'])
def createNewMap():

     if request.method == 'GET':
        return createMap(0)
     elif request.method == 'POST':
        form = request.form

        if(isValidMap(form)):
            return createMap(form)
        else:
            redirect(url_for('createNewMap'))

# Routing Background Sounds
@app.route('/media/<path:filename>')
def download_file(filename):
    return send_from_directory("media/",filename)

@app.route('/teacher', methods=['GET', 'POST'])
def teacher():
    if isTeacher():
        con = sqlite3.connect("database.db")
        con.row_factory = sqlite3.Row

        cur = con.cursor()
        cur.execute("select * from map")

        maps = cur.fetchall()
        return render_template("teacherdashboard.html", maps=maps)
    else:
        return render_template("landing.html")

@app.route('/teacherdashboard')
def teacherdashboard():
    return redirectTeacher()

@app.route('/getCarInstructions', methods = ['POST', 'GET'])
def getCarNewInstructions():
    if 'map_id' in request.args:
        map_id = int(request.args['map_id'])
    else:
        return "Error: No id field provided. Please specify an id."
    return getCarInstruction(map_id)

@app.route('/getCarDatas', methods = ['POST', 'GET'])
def getCarDataRouting():
    return getCarData()

@app.route('/setCarStatus', methods = ['GET', 'POST'])
def setCarStatus():
    if 'car_id' in request.args:
        car_id = int(request.args['car_id'])
    else:
        return "Error: No id field provided. Please specify an id."
    if 'distance' in request.args:
            distance = int(request.args['distance'])
    else:
        return "Error: No id field provided. Please specify an id."
    print(type(distance))
    return setCarData(distance)

@app.route('/deleteMap/<id>')
def delMap(id):
    return deleteMap(id)

@app.route('/createChallenge', methods = ['POST', 'GET'])
def createChallenge():
    try:
        map_id = request.form['map_id']
        pin = request.form['pin']
    except:
        return redirect(url_for('teacherdashboard'))
    return makeChallenge(map_id, pin)

@app.route('/student', methods=['GET', 'POST'])
def student():
    if request.method == 'GET':
        return render_template("studentdashboard.html")
    else:
        pin = request.form['pin']
        if pin != '':
            return redirect(url_for('gameLobby'))
        return render_template("studentdashboard.html")

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'GET':
        return render_template("login.html")
    else:
        if userLogin(request.form['username'], request.form['password']):
            return redirect(url_for('teacher'))
        return render_template("login.html")

@app.route('/game/<game_id>')
def game(game_id):
    return render_template("game_map.html", game_map=getGrid(game_id))

@app.route('/api/currentmap/<id>')
def currMap(id):
    return getCurrentMap(id)

@app.route('/createInstructions', methods = ['POST'])
def createNewInstruction():
    if request.method == 'POST':
        try:
                print('createInstructions', request.json['map_id'])
                map_id = request.json['map_id']
                executed = request.json['executed']
                command = request.json['command']
                session_id = request.json['session_id']

        except:
                return redirect(url_for('dashboard'))
        return setInstruction(map_id, executed, command, session_id)

@app.route('/getInstructions', methods = ['POST'])
def getNewInstructions():
    if request.method == 'POST':
        try:
                print('getInstructions', request.json['map_id'])
                print('getInstructions', request.json['session_id'])
                map_id = request.json['map_id']
                session_id = request.json['session_id']
                print("Type of map_id", type(map_id))
                print("Type of map_id", type(session_id))

        except:
                return redirect(url_for('dashboard'))
        return getInstruction(map_id, session_id )

@app.route('/game_start')
def gameStart():
    return render_template("game/game_start.html")

@app.route('/game_lobby')
def gameLobby():
    return render_template("game/game_lobby.html")

@app.route('/start_game_lobby')
def start_gameLobby():
    return render_template("game/start_game_lobby.html")

def currMap(id):
    return getCurrentMap(id)

@app.route('/endgame', methods = ['GET', 'POST'])
def endgame():
    if request.method == 'POST':
        score = 3000
        mapId = 4
        detailsTable = getHighscore(mapId)
        if checkHighscore(score,detailsTable):
            redirect(url_for('challengeCompleted'))
        else:
            redirect(url_for('challengeCompleted'))

@app.route('/addHighscore', methods = ['GET'])
def addHighscore():
    detailsTable = getHighscore(4)
    return render_template('addhighscore.html', detailsTable = detailsTable)

@app.route('/sethighscore', methods = ['POST'])
def sethighscore():
    mapId = 4
    score = 3000
    if request.method == 'POST':
        name = request.form['username']
        setHighscore(mapId, name, score)
        # detailsTable = getHighscore(mapId)
        return redirect(f'viewhighscore/{mapId}')

@app.route('/viewhighscore/<mapId>', methods = ['GET'])
def viewhighscore(mapId):
    mapId = 4
    detailsTable = getHighscore(mapId)
    return render_template("viewhighscore.html", detailsTable = detailsTable)

@app.route('/challengeCompleted')
def challengeCompleted():
    return render_template('challengeCompleted.html') # render_template("challengeCompleted.html")

if __name__ == "__main__":
    context = ('cert.pem', 'key.pem')
    app.run(ssl_context=context, debug=True)
    app.run(debug=True, host="0.0.0.0", port="5000")


## this is only slightly less naive
# than the bare 'string'.split()
# but breaking it out as a separate function
# lets us change it without affecting the
# rest of the code
import re
tokenize = re.compile(r"[.,?! \t\n]").split

class WordCounter():
    def __init__(self, filename):
        self.fname = filename
        with open(self.fname,'rUt') as fi:
            self.content = fi.read()
            # the file automatically closes at the end of the with block

        my_dict = {}

        for word in tokenize(self.content):
            if word not in my_dict:
                my_dict[word] = 1
            else:
                my_dict[word] += 1

        my_list = []

        for k, v in my_dict.items():
            a_list = my_list.append((v, k))
            # you cannot return anything from a constructor (__init__ method)
            #return a_list

        b_list = sorted(a_list, reverse=True)
        tops = b_list[:20]

    def word_count_method(self):
        len(content.split())
        self.word_count = len(content.split())

    def punct_count_method(self):
        punct = ".',"

        d = {}

        for item in content:
            if item in punct:
                if item not in d:
                    d[item] = 0
                d[item] += 1
            # self.punct_count =

    def pretty_print_method(self):
        # i need this to take the value from "tops" line 81
        for element in tops:
            print(element[1], element[0])

def main():

    # to work on file in the class must input "string-indicating-file-name"
    test_file = WordCounter("test.txt")

    test_file.pretty_print_method()

if __name__ == '__main__':

    main()


import os, subprocess, json, re
import shutil
import configparser

skip_connections = ['kylin_default', 'local_mysql', 'postgres_default', 'tableau_default']

secretsFolder = "secrets"
connections = []
configs = []
bashScript = []
TFScript = []

def loadConfig():
    config = configparser.ConfigParser()
    config.read(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'settings.cfg'))
    return config

def deleteSecrets(secretsPath):
    try:
        shutil.rmtree(secretsPath)
    except OSError as e:
        print("Error: %s - %s." % (e.filename, e.strerror))

def importConnections(secretsPath):
    global connections
    getConnections = "airflow connections list -o json"

    proc = subprocess.Popen([getConnections], stdout=subprocess.PIPE, shell=True)
    (out, err) = proc.communicate()
    if proc.returncode != 0:
        exit(1)         # Stop execution if connections listing didn't succeed
    responseColored = out.decode("utf-8")               #
    formatter = re.compile(r'\x1b[^m]*m')               # Compile formatting rule as
    connectionsClean = formatter.sub('', responseColored)    #

    try:
        connArray = json.loads(connectionsClean)
    except:
        print("Error loading connections")
        exit(1)

    for conn in connArray:
        if conn['password']:
            if conn['conn_id'] not in skip_connections:
                connections.append({"conn_id": conn['conn_id'], "uri": conn['get_uri']})
        else:
            pass

    try:
        os.mkdir(secretsPath)
    except OSError as error:
        print(error)

    if len(connections)>0:
        bashScript.append("gcloud services enable secretmanager.googleapis.com")
        configs.append({"section": "secrets", "key": "backend", "value": "airflow.providers.google.cloud.secrets.secret_manager.CloudSecretManagerBackend"})
        for connection in connections:
            if 'uri' in connection:
                bashScript.append("gcloud secrets create airflow-connections-" + connection['conn_id'] + " --replication-policy=\"automatic\"")
                with open(os.path.join(secretsPath,connection['conn_id']), "w") as myfile:
                    try:
                        myfile.write(connection['uri'])
                    except:
                        print("Error writing secret to a file")
                bashScript.append("gcloud secrets versions add airflow-connections-" + connection['conn_id'] + " --data-file="+os.path.join(secretsPath,connection['conn_id']))

    for line in bashScript:
        print(line)
        proc = subprocess.Popen([line], stdout=subprocess.PIPE, shell=True)
        (out, err) = proc.communicate()
        if proc.returncode != 0:
            deleteSecrets(secretsPath)
            exit(1)                                 # Stop execution if connections listing didn't succeed

    deleteSecrets(secretsPath)

def main():
    secretsPath = os.path.join(os.path.dirname(os.path.realpath(__file__)), secretsFolder)
    config = loadConfig()

    bashScript.append("gcloud config set project " + config['google_cloud']['project'])

    if config['features'].getboolean('connections'):
        importConnections(secretsPath)

if __name__ == "__main__":
    main()


import requests
from urllib.parse import urlencode
from requests.adapters import HTTPAdapter
from urllib3.util import Retry
from datetime import datetime
import json
from ratelimiter import RateLimiter

class PushshiftAPI:
    def __init__(self, cloudwatch_logger) -> None:
        self.start_time = datetime.utcnow()
        self.request_count = 0
        self.last_request = None
        self.last_response = None
        self.cloudwatch = cloudwatch_logger

        """Confgure requests module"""
        # Define retry strategy for requests
        retry_strategy = Retry(
            total=5,
            status_forcelist=[408, 429, 500, 502, 503, 504, 522],
            method_whitelist=["GET"],
            backoff_factor=4,
            respect_retry_after_header=False
        )
        adapter = HTTPAdapter(max_retries=retry_strategy)
        self.request = requests.Session()
        self.request.mount("https://", adapter)
        self.request.mount("http://", adapter)

    def _create_params(self, subreddits:list, before:int, after:int, size:int):

            params = {
                "subreddit": ','.join(subreddits),
                "size": size,
                "sort": "desc",
                "sort_type": "created_utc",
                "metadata": "true"
            }

            if before is not None:
                params['before'] = before

            if after is not None:
                params['after'] = after

            return params

    def _set_endpoint(self, post_type:str) -> str:
        # Specify endpoint to search based on post type
        if post_type == "comments":
            return "comment"
        elif post_type == "submissions":
            return "submission"
        else:
            raise Exception("Could not specify Pushshift endpoint")

    def _create_url(self, endpoint:str, params:dict) -> str:
        url = f"https://api.pushshift.io/reddit/{endpoint}/search"
        encoded_url = url + '?' + urlencode(params)

        return encoded_url

    @RateLimiter(max_calls=100, period=60)
    def _get(self, endpoint:str, params:dict):
        url = self._create_url(endpoint, params)
        response = self.request.get(url)

        self.request_count += 1

        return PushshiftResponse(response, endpoint, params, cloudwatch_logger=self.cloudwatch)

    def get(self, post_type:str, subreddits:list, before:int=None, after:int=None, size:int=100):
        """Retrieve comments from pushshift.

        Args:
            post_type (str): Submissions or comments.
            subreddits (list): List of subreddits to search.
            before (int, optional): Max created_utc of posts to get (epoch timestamp). Defaults to None.
            after (int, optional): Max created_utc of posts to get (epoch timestamp). Defaults to None.
            size (int, optional): Number of results to return. Max is 100. Defaults to 100.

        Returns:
            PushshiftResponse: Returns a PushshiftResponse object.
        """
        endpoint = self._set_endpoint(post_type)
        params = self._create_params(subreddits=subreddits, before=before, after=after, size=size)
        response = self._get(endpoint, params)

        return response

    def get_next(self, response):
        # Check if response is empty or no more results

        endpoint = response.endpoint
        params = response.request_params

        # Set max_created_utc to earliest result
        params['before'] = response.min_created_at
        response = self._get(endpoint=endpoint, params=params)

        return response

    def progress(self):
        # Return crawler status
        current_time = datetime.utcnow()
        elapsed_time = current_time - self.start_time
        return f"Crawled {self.request_count} pages in {round(elapsed_time.seconds / 60.0, 2)} mins."

    def log_progress(self, interval:int=100):
        # Record progress every N pages
        if self.request_count % interval == 0:
            self.cloudwatch.log(self.progress())

class PushshiftResponse:
    def __init__(self, response, endpoint, request_params, cloudwatch_logger) -> None:
        self.cloudwatch = cloudwatch_logger

        self._validate_response(response)

        self.response = response
        self.endpoint = endpoint
        self.request_params = request_params

        self.json = response.json()
        self.data = self.json.get('data')
        self.metadata = self.json.get('metadata')

        self._validate_metadata(self.metadata)
        self._validate_data(self.data)

        self.min_created_at = self._min_created_at(self.data)
        self.max_created_at = self._max_created_at(self.data)

    def _validate_response(self, response):
        # This should only trigger if all retries failed
        if not response.ok:
            message = f'ERROR: Response not ok. Server returned status code {response.status_code}. Query params: {json.dumps(self.request_params)}.'
            self.cloudwatch.log(message)
            raise Exception(message)
        else:
            return True

    def _validate_metadata(self, metadata):
        # Check if metadata exists
        if metadata is None:
            message = f"No metadata returned for query {json.dumps(self.request_params)}"
            self.cloudwatch.log(message)
            raise Exception(message)

        # Check if request timed out
        if metadata["timed_out"]:
            self.cloudwatch.log(f"WARNING: Search timed out. Metadata:\n{json.dumps(metadata, indent=4)}")

        # Check number of results returned
        if metadata['total_results']==0:
            message = f"No data returned for query {json.dumps(self.request_params)}. Metadata:\n{json.dumps(metadata, indent=4)}"
            self.cloudwatch.log(message)
            raise Exception(message)

        # Check shards
        shards = metadata["shards"]
        if shards["failed"] > 0:
            self.cloudwatch.log(f'WARNING: Failed shards: {shards["failed"]}. Metadata:\n{json.dumps(metadata, indent=4)}')
        if shards["skipped"] > 0:
            self.cloudwatch.log(f'WARNING: Skipped shards: {shards["skipped"]}. Metadata:\n{json.dumps(metadata, indent=4)}')

        return True

    def _validate_data(self, data):
        if data is None:
            message = f"No data returned for query {json.dumps(self.request_params)}. Metadata:\n{json.dumps(self.metadata, indent=4)}"
            self.cloudwatch.log(message)
            raise Exception(message)

        return True

    def _max_created_at(self, data):
        return data[0].get('created_utc')

    def _min_created_at(self, data):
        return data[-1].get('created_utc')


"""
Weighted Ordered FP-Tree Algorithm implementation.

WOFP:
Yuanyuan Li, Shaohong Yin (2020).
Mining algorithm for weighted FP-tree frequent item sets based on two-dimensional table.
Journal of Physics: Conference Series.
DOI:10.1088/1742-6596/1453/1/012002

@ June 2021
@ Asloudj Yanis
"""

import pandas as pd
import time as tm
from treelib import Node, Tree
from collections import Counter, OrderedDict

def weigh_transaction(trans, weight_items):
    """
    # Description
    Calculates the average weight of a transaction according to the individual weights of the items.

    # Arguments
    ``trans`` (list): a transaction. Each element is an item. \n
    ``weight_items`` (dict): the items names as keys and their weights as values.

    # Usage
    >>> t = ["A", "B", "D", "C"]
    >>> weight = {'A': 2, 'B': 4, 'D': 8, 'C': 1}
    >>> print(weigh_transaction(t, weight))
    ... 3.75
    """
    total = 0
    for item in trans:
        total += weight_items[item]
    return total / len(trans)

def get_frequency_and_weight(trans_db, weight_items, min_sup = 0.1):
    """
    # Description
    Returns a data frame containing the frequencies of the frequent items only, in a descending order.

    # Arguments
    ``trans_db`` (list of sublists): your transaction database. A sublist is a transaction with its items. \n
    ``min_sup`` (float): the minimum support necessary to keep an item. 0 < min_sup <= 1.

    # Usage
    >>> trans = [
            ["A", "C", "B", "D"],
            ["B", "E", "D"],
            ["E", "B", "A"],
            ["Y", "C", "D"],
            ["D", "A"]
        ]
    >>> w_items = {'A': 2, 'B': 4, 'D': 8, 'C': 1, 'Y': 0.5, 'E': 23}
    >>> freq_items, trans_weights = get_frequency_and_weight(trans_db = trans, weight_items = w_items, min_sup = 0.25)
    >>> print(freq_items)
    ... item  freq
    3    D   0.8
    0    A   0.6
    1    B   0.6
    2    C   0.4
    4    E   0.4
    >>> print(trans_weights)
    ... [3.75, 11.666666666666666, 9.666666666666666, 3.1666666666666665, 5.0]
    """
    item_freq = {}
    total_trans = len(trans_db)
    weight_trans = []
    for t in trans_db:
        weight_trans.append(weigh_transaction(t, weight_items))
        for item in t:
            try:
                item_freq[item] += 1/total_trans
            except KeyError:
                item_freq[item] = 1/total_trans
    freq_df = pd.DataFrame({'item': list(item_freq.keys()), 'freq': list(item_freq.values())})
    freq_df = freq_df[freq_df['freq'] > min_sup]
    freq_df = freq_df.sort_values(by=['freq', 'item'], ascending=[False, True])
    return freq_df, weight_trans

def order_transaction(trans, freq_items):
    """
    # Description
    Orders a transaction according to the frequency values of the items.

    # Arguments
    ``trans`` (list): a transaction. Each element is an item. \n
    ``freq_items`` (df): the items names and their frequencies, ordered by descending frequency and ascending name.

    # Usage
    >>> trans = [
            ["A", "C", "B", "D"],
            ["B", "E", "D"],
            ["E", "B", "A"],
            ["Y", "C", "D"],
            ["D", "A"]
        ]
    >>> w_items = {'A': 2, 'B': 4, 'D': 8, 'C': 1, 'Y': 0.5, 'E': 23}
    >>> freq_items, trans_weights = get_frequency_and_weight(trans_db = trans, weight_items = w_items, min_sup = 0.25)
    >>> print(order_transaction(trans[3], freq_items))
    ... ['D', 'C']
    """
    order = list(freq_items['item'])
    frequent_trans = [x for x in trans if x in order]
    return sorted(frequent_trans, key = order.index)

def construct_fptree(trans_db, freq_items, weight_trans):
    """
    # Description
    Returns an FP tree and a dict of items and their nids from the transactions stored in a database.

    # Arguments
    ``trans_db`` (list of sublists): your transaction database. A sublist is a transaction with its items. \n
    ``freq_items`` (df): the items names and their frequencies, ordered by descending frequency and ascending name. \n
    ``weight_items`` (list): a list with the respective weights of each transactions.

    # Usage
    >>> trans = [
            ["A", "C", "B", "D"],
            ["B", "E", "D"],
            ["E", "B", "A"],
            ["Y", "C", "D"],
            ["D", "A"]
        ]
    >>> w_items = {'A': 2, 'B': 4, 'D': 8, 'C': 1, 'Y': 0.5, 'E': 23}
    >>> freq_items, trans_weights = get_frequency_and_weight(trans_db = trans, weight_items = w_items, min_sup = 0.25)
    >>> tree, item_nodes = construct_fptree(trans, freq_items, trans_weights)
    >>> tree.show(idhidden=True)
    ...
    [0]
    ├── A[7]
    │   └── B[8]
    │       └── E[9]
    └── D[1]
        ├── A[2]
        │   └── B[3]
        │       └── C[4]
        ├── B[5]
        │   └── E[6]
        └── C[10]
    >>> print(item_nodes)
    ... OrderedDict([('D', [1]), ('A', [2, 7]), ('B', [3, 5, 8]), ('C', [4, 10]), ('E', [6, 9])])
    """
    # ordered dict indicating the nodes ids corresponding to each item (one item can have multiple nids)
    item_nodes = OrderedDict()

    # init the tree
    tree = Tree()
    tree.create_node(tag="", identifier=0)
    nid = 1 # node id

    # weights counter
    k = 0
    total_weight = sum(trans_weights)

    for t in trans_db:
        parent_nid = 0
        t = order_transaction(t, freq_items)

        for item in t:

            # get the children nids of the current parent
            children_nids = tree.is_branch(parent_nid)

            # if there is no node child corresponding to the item, add it on the tree and save the new nid in the dict
            if not any(nid in item_nodes[item] for nid in children_nids):
                try:
                    item_nodes[item].append(nid)
                except KeyError:
                    item_nodes[item] = [nid]
                tree.create_node(tag=item, identifier=nid, parent=parent_nid, data = trans_weights[k] / total_weight)

                # generate a new unique nid and navigate to the new node
                parent_nid = nid
                nid += 1

            # if there is, update the weight of the node and navigate to it
            else:
                parent_nid = min(set(item_nodes[item]).intersection(set(children_nids)))
                tree[parent_nid].data += trans_weights[k] / total_weight

    return tree, item_nodes

def get_branch(fptree, nid):
    """
    # Description
    Returns a dict corresponding to the parents of a node and the data value of the child node

    # Arguments
    ``fptree`` (Tree Object): items nodes and their weights stored in a treelib Tree Object.
    ``nid`` (int): unique id corresponding to an existing node of the Tree Object.

    # Usage
    >>> trans = [
            ["A", "C", "B", "D"],
            ["B", "E", "D"],
            ["E", "B", "A"],
            ["Y", "C", "D"],
            ["D", "A"]
        ]
    >>> w_items = {'A': 2, 'B': 4, 'D': 8, 'C': 1, 'Y': 0.5, 'E': 23}
    >>> freq_items, trans_weights = get_frequency_and_weight(trans_db = trans, weight_items = w_items, min_sup = 0.25)
    >>> tree, item_nodes = construct_fptree(trans, freq_items, trans_weights)
    >>> print(get_branch(tree, 4))
    ... {'B': 0.11278195488721804, 'A': 0.11278195488721804, 'D': 0.11278195488721804}
    """
    branch = {}
    leaf_node = fptree.get_node(nid)
    value = leaf_node.data
    parent_node = fptree.get_node(leaf_node.predecessor(fptree._identifier))
    while parent_node.predecessor(fptree._identifier) != None:
        branch[parent_node.tag] = value
        parent_node = fptree.get_node(parent_node.predecessor(fptree._identifier))
    return branch

def get_condition_tree(item, fptree, item_nodes):
    """
    # Description
    Returns the conditional FP tree of a given item according to its nodes values on a tree.

    # Arguments
    ``item`` (string): the item you want the conditional FPtree of.
    ``fptree`` (Tree Object): items nodes and their weights stored in a treelib Tree Object.
    ``item_nodes`` (dict): items tags and their respective nodes ids.

    # Usage
    >>> trans = [
            ["A", "C", "B", "D"],
            ["B", "E", "D"],
            ["E", "B", "A"],
            ["Y", "C", "D"],
            ["D", "A"]
        ]
    >>> w_items = {'A': 2, 'B': 4, 'D': 8, 'C': 1, 'Y': 0.5, 'E': 23}
    >>> freq_items, trans_weights = get_frequency_and_weight(trans_db = trans, weight_items = w_items, min_sup = 0.25)
    >>> tree, item_nodes = construct_fptree(trans, freq_items, trans_weights)
    >>> print(get_condition_tree('A', tree, item_nodes))
    ... Counter({'D': 0.22556390977443608, 'B': 0.11278195488721804, 'A': 0.11278195488721804})
    """
    cond_tree = Counter()
    for nid in item_nodes[item]:
        cond_tree += Counter(get_branch(fptree, nid))
    return cond_tree

def get_association_rules(fptree, item_nodes, min_weight = 0.20):
    """
    # Description
    Returns the items association rules by reading the FPtree.

    # Arguments
    ``fptree`` (Tree Object): items nodes and their weights stored in a treelib Tree Object.
    ``item_nodes`` (dict): items tags and their respective nodes ids.
    ``min_weight`` (float): itemsets with a weight equal or higher to this value (0<v<1) are considered patterns.

    # Usage
    >>> trans = [
            ["A", "C", "B", "D"],
            ["B", "E", "D"],
            ["E", "B", "A"],
            ["Y", "C", "D"],
            ["D", "A"]
        ]
    >>> w_items = {'A': 2, 'B': 4, 'D': 8, 'C': 1, 'Y': 0.5, 'E': 23}
    >>> freq_items, trans_weights = get_frequency_and_weight(trans_db = trans, weight_items = w_items, min_sup = 0.25)
    >>> tree, item_nodes = construct_fptree(trans, freq_items, trans_weights)
    >>> get_association_rules(tree, item_nodes, min_weight = 0.5)
    """
    item_nodes.popitem(last=False)
    frequent_pattern = {}
    for item in item_nodes.keys():
        cond_tree = get_condition_tree(item, fptree, item_nodes)
        for parent_item in cond_tree.keys():
            if cond_tree[parent_item] > min_weight:
                pattern = "%s:%s" % (item, parent_item)
                frequent_pattern[pattern] = round(cond_tree[parent_item], 2)
    return frequent_pattern

def filter_patterns(freq_pat, fix_length = 2):
    """
    # Description
    Returns a dict of frequent patterns after filtering according to items prefix or suffix.

    # Arguments
    ``freq_pat`` (dict): frequent patterns and their associated weight value.
    ``fix_length`` (int): the length of the suffix or prefix.

    # Usage
    >>> trans = [
            ["GO1", "GO3", "GO2", "R-1"],
            ["GO2", "HP1", "R-1"],
            ["HP1", "GO2", "GO1"],
            ["R-2", "GO3", "R-1"],
            ["R-1", "GO1"]
        ]
    >>> w_items = {'GO1': 1, 'GO2': 1, 'R-1': 1, 'GO3': 1, 'R-2': 1, 'HP1': 1}
    >>> freq_items, trans_weights = get_frequency_and_weight(trans_db = trans, weight_items = w_items, min_sup = 0.25)
    >>> tree, item_nodes = construct_fptree(trans, freq_items, trans_weights)
    >>> asso_rules = get_association_rules(tree, item_nodes)
    >>> print(asso_rules)
    ... {'GO1:R-1': 0.4, 'GO2:GO1': 0.4, 'GO2:R-1': 0.4, 'GO3:R-1': 0.4, 'HP1:GO2': 0.4}
    >>> print(filter_patterns(asso_rules))
    ... {'GO1:R-1': 0.4, 'GO2:R-1': 0.4, 'GO3:R-1': 0.4, 'HP1:GO2': 0.4}
    """
    patterns_to_del = []
    for pat in freq_pat.keys():
        terms = pat.split(":")
        if terms[0][:fix_length] == terms[1][:fix_length]:
            patterns_to_del.append(pat)
    for k in patterns_to_del:
        freq_pat.pop(k)
    return freq_pat

import time as tm
begin = tm.time()

trans = [
        ["GO1", "GO3", "GO2", "R-1"],
        ["GO2", "HP1", "R-1"],
        ["HP1", "GO2", "GO1"],
        ["R-2", "GO3", "R-1"],
        ["R-1", "GO1"]
    ]
w_items = {'GO1': 1, 'GO2': 1, 'R-1': 1, 'GO3': 1, 'R-2': 1, 'HP1': 1}
freq_items, trans_weights = get_frequency_and_weight(trans_db = trans, weight_items = w_items, min_sup = 0.25)
tree, item_nodes = construct_fptree(trans, freq_items, trans_weights)
asso_rules = get_association_rules(tree, item_nodes)
print(asso_rules)
print(filter_patterns(asso_rules))

end = tm.time()
print(end - begin)


"""Evaluates (back-tests) the performance of a machine learning model created with svm_trainer in the stock market"""
from itertools import combinations
import numpy as np
import pandas as pd

from sklearn.metrics import accuracy_score

from svm_trainer import Trainer

class Tester:
    def __init__(self):
        trainer = Trainer(20)
        data = trainer.get_data()

        # Split data into training and testing sets
        self.train, self.test = np.array_split(data, 2)

    def test_model(self, features):
        # Select features to be tested
        x_train = self.train[features]
        x_test = self.test[features]

        # Parse out targets from data
        y_train = self.train['class']
        y_test = self.test['class']

        # Train the machine learning model
        model = Trainer.train_model(x_train, y_train)

        # Evaluate the model's performance
        y_test_prediction = model.predict(x_test)
        print('\nFeatures:', features, 'Accuracy:', round(accuracy_score(y_test, y_test_prediction), 3))

        # Create, normalize, and display a confusion matrix
        y_test_prediction = pd.Series(list(y_test_prediction), name='Predicted')
        y_test = pd.Series(list(y_test), name='Actual')
        cm = pd.crosstab(y_test, y_test_prediction)
        print(cm/cm.sum(axis=0))

def main():
    tester = Tester()
    features = ['ADX', 'MACD', 'OBV', 'RSI']

    # Check feature combos of all lengths
    for num in range(1, len(features) + 1):
        for combo in combinations(features, num):
            tester.test_model(list(combo))

if __name__ == '__main__':
    main()


import numpy as np
import math
from scipy.stats import binom

array = np.random.randint(2, size =(10000, 10))

ones = 0
zero_one = 0
one_one = 0
two_one = 0
three_one = 0
four_one = 0
five_one = 0
six_one = 0
seven_one = 0
eight_one = 0
nine_one = 0
ten_one = 0

for i in range(10000):
    for j in range(10):
        if array[i][j] == 1:
            ones = ones + 1
        else:
            continue
    if ones == 0:
        zero_one = zero_one + 1
        ones = 0
    elif ones == 1:
        one_one = one_one + 1
        ones = 0
    elif ones == 2:
        two_one = two_one + 1
        ones = 0
    elif ones == 3:
        three_one = three_one + 1
        ones = 0
    elif ones == 4:
        four_one = four_one + 1
        ones = 0
    elif ones == 5:
        five_one = five_one + 1
        ones = 0
    elif ones == 6:
        six_one = six_one + 1
        ones = 0
    elif ones == 7:
        seven_one = seven_one + 1
        ones = 0
    elif ones == 8:
        eight_one = eight_one + 1
        ones = 0
    elif ones == 9:
        nine_one = nine_one + 1
        ones = 0
    elif ones == 10:
        ten_one = ten_one + 1
        ones = 0
    else:
        continue

p_sim = float((zero_one + one_one + two_one + three_one + four_one)) / float(10000)

"""
print(zero_one)
print(one_one)
print(two_one)
print(three_one)
print(four_one)
print(five_one)
print(six_one)
print(seven_one)
print(eight_one)
print(nine_one)
print(ten_one)
"""

print(p_sim)

pmf_4 = binom.pmf(4, 10, 0.5, loc=0)
pmf_3 = binom.pmf(3, 10, 0.5, loc=0)
pmf_2 = binom.pmf(2, 10, 0.5, loc=0)
pmf_1 = binom.pmf(1, 10, 0.5, loc=0)
pmf_0 = binom.pmf(0, 10, 0.5, loc=0)
p_pmf = pmf_0 + pmf_1 + pmf_2 + pmf_3 + pmf_4

print(p_pmf)


#   DAY99.py
#   Written by Selene R. Schmittling
#   [DATE]

#   Import Statements
import pandas as pd

#   Global vars
LOGGING = True

#   Utility Functions
def getdata(ifile):
    indata = pd.read_csv(ifile, sep=" ", header=False)
    # Add code to process data
    with open(ifile) as f:
        for line in f:
            print(line)

def part1(data, logf):
    if LOGGING:
        logf.writelines("PART 1\n ********************\n\n")
    print("In part 1")

def part2(data, logf):
    if LOGGING:
        logf.writelines("\n\nPART 2\n ********************\n\n")
    print("In part 2")

#   Main Function
def main():
    if LOGGING:
        logf = open('logfile.txt','w')
    else:
        logf = ""
    data = getdata('data/DAY99.txt',sep=" ")
    print("In main")

if __name__ == "__main__":
    main()


# Danny Garcia
# InputBox.py

# Modules
from .Text import Text as Text
from .Line import Line as Line
from .Rectangle import Rectangle as Rectangle

# Input InputBox Class
class InputBox:

    # Constructor
    def __init__(self, window_name, point, length=200, height=30, text="SEARCH",
    bar_color=(200, 200, 200), outline_color=(0, 0, 0), outline_width=0,
    text_y_margin=-1, text_x_margin=6, font="Consolas", font_size=20,
    font_color=(20, 20, 20), bold=False, italic=False, pointer_width=2,
    pointer_length=10, pointer_x_margin=4, pointer_y_margin=4,
    pointer_blink_frames=400, pointer_color=(0, 0, 0), force_case="NONE",
    inplace_draw=True):

        # Arguments
        self._window = window_name
        self._point = point
        self.length = length
        self.height = height
        self.text = text
        self.bar_color = bar_color
        self.outline_color = outline_color
        self.outline_width = outline_width
        self.text_y_margin = text_y_margin
        self.text_x_margin = text_x_margin
        self.font = font
        self.font_size = font_size
        self.font_color = font_color
        self.bold = bold
        self.italic = italic
        self.pointer_width = pointer_width
        self.pointer_length = pointer_length
        self.pointer_x_margin = pointer_x_margin
        self.pointer_y_margin = pointer_y_margin
        self.pointer_blink_frames = pointer_blink_frames
        self.pointer_color = pointer_color
        self.force_case = force_case

        # Components
        self._bar, self._text, self._pointer = "N/A", "N/A", "N/A"

        # Variables
        self._current_status = "INACTIVE"
        self._status = "NONE"
        self._action = "NONE"
        self._initializing = True

        self._set_boundaries()
        self.draw() if inplace_draw else None

    # Class representation
    def __repr__(self):
        text = "<InputBox Object>\n"
        text += f"Point {self._point}\n"
        text += f"Text: {self._text}\n"
        return text

    # Class print behaviour
    def __str__(self): return self.__repr__()

    # Gets draw status of the inputBox
    def is_drawn(self): return (self,) in self._window.object_list

    # Sets the inputBox's boundaries
    def _set_boundaries(self):
        self._left_boundary = self._point[0]
        self._right_boundary = self._point[0] + self.length
        self._up_boundary = self._point[1]
        self._down_boundary = self._point[1] + self.height

    # Draws the inputBox
    def draw(self):
        if not self.is_drawn(): self._window.object_list.append((self,))
        else: print("Object already drawn.")

    # Undraw the inputBox
    def undraw(self):
        if self.is_drawn():
            self._window.window.delete(self._bar, self._text, self._pointer)
            del self._window.object_list[self._window.object_list.index((self,))]
        else: print("Object isn't drawn.")

    # Updates the inputBox
    def update(self):
        # Update status
        if self._window.last_mouse[0]:
            if self._left_boundary < self._window.last_motion[0] < \
            self._right_boundary and self._up_boundary < \
            self._window._last_motion[1] < self._down_boundary:

                if self._current_status != "CLICKED":
                    self._current_status = "CLICKED"
            else:
                if self._current_status != "INACTIVE":
                    self._current_status = "INACTIVE"

        # Undraws components
        if not self._initializing and self._current_status != self._status:
            self._bar.undraw()
            self._text.undraw()
            self._pointer.undraw()

            # Reset components
            self._bar, self._text, self._pointer = "N/A", "N/A", "N/A"

        # Update Text
        if self._window.last_key and self._current_status == "CLICKED":
            if self._window.last_key == "\n":
                if self._action != "SUBMITTED":
                    self._action = "SUBMITTED"
            else:
                if self._action != "NONE":
                    self._action = "NONE"
                if self._window.last_key == "\b":
                    if len(self.text) > 0:
                        self.text = self.text[:-1]
                else:
                    if len(self._window.last_key) == 1:
                        temp_text = Text(self._window, (self._point[0] + \
                        self.text_x_margin, self._point[1] + \
                        self.text_y_margin), self.text + self._window.last_key,
                        self.font, self.font_size, color=self.font_color,
                        bold=self.bold, italic=self.italic, inplace=False)
                        if temp_text.width() <= self.length:
                            if self.force_case == "UPPER":
                                self.text += self._window.last_key.upper()
                            elif self.force_case == "LOWER":
                                self.text += self._window.last_key.lower()
                            else: self.text += self._window.last_key

        # Update components
        # Inactive
        if self._current_status == "INACTIVE" and \
        self._current_status != self._status:

            # Bar
            self._bar = Rectangle(self._window, self._point, (self._point[0] + \
            self.length, self._point[1] + self.height), color=self.bar_color,
            outline_color=self.outline_color, outline_width=self.outline_width)

            # Text
            self._text = Text(self._window, (self._point[0] + \
            self.text_x_margin, self._point[1] + self.text_y_margin),
            self.text, self.font, self.font_size, color=self.font_color,
            bold=self.bold, italic=self.italic)
            assert self._text.width() < self.length, \
            "Text must be shorter than inputBox bar."

            # Pointer
            self._pointer = Line(self._window, (0, 0), (0, 0))
            self._status = self._current_status

        # Performance is affected when typing
        elif self._current_status == "CLICKED":
            if self._status == self._current_status:
                self._bar.undraw()
                self._text.undraw()
                self._pointer.undraw()

            # Bar
            self._bar = Rectangle(self._window, self._point, (self._point[0] + \
            self.length, self._point[1] + self.height), color=self.bar_color,
            outline_color=self.outline_color, outline_width=self.outline_width)

            # Text
            self._text = Text(self._window, (self._point[0] + \
            self.text_x_margin, self._point[1] + self.text_y_margin), self.text,
            self.font, self.font_size, color=self.font_color, bold=self.bold,
            italic=self.italic)
            assert self._text.width() < self.length, \
            "Text must be shorter than inputBox bar."

            # Pointer
            if self._window.frame % self.pointer_blink_frames \
            <= self.pointer_blink_frames // 2:

                self._pointer = Line(self._window, (self._point[0] + \
                self._text.width() + self.pointer_x_margin + self.text_x_margin,
                self._point[1] + self.text_y_margin + self.pointer_y_margin),
                (self._point[0] + self._text.width() + self.pointer_x_margin + \
                self.text_x_margin, self._point[1] + self.height - \
                self.text_y_margin - self.pointer_y_margin),
                color=self.pointer_color, width=self.pointer_width)
            else: self._pointer = Line(self._window, (0, 0), (0, 0))

            self._status = self._current_status

        if self._initializing:
            self._initializing = False

    # Moves the inputBox
    def move(self, x=0, y=0):
        self._point = (self._point[0] + x, self._point[1] + y)

        self._set_boundaries()

    # Copies the inputBox
    def copy(self):
        return InputBox(self._window, self.point, length=self.length,
        height=self.height, text=self.text, bar_color=self.bar_color,
        outline_color=self.outline_color, outline_width=self.outline_width,
        text_y_margin=self.text_y_margin, text_x_margin=self.text_x_margin,
        font=self.font, font_size=self.font_size, font_color=self.font_color,
        bold=self.bold, italic=self.italic, pointer_width=self.pointer_width,
        pointer_length=self.pointer_length, pointer_x_margin = \
        self.pointer_x_margin, pointer_y_margin=self.pointer_y_margin,
        pointer_blink_frames=self.pointer_blink_frames, pointer_color = \
        self.pointer_color)

    # Properties
    @property
    def point(self):
        return self._point

    # Setters
    @point.setter
    def point(self, point):
        self._point = point

        self._set_boundaries()


import torch
import numpy as np
import torch.nn as nn

from lib.estimator.resnet import factory as resnet_factory

class Tester(nn.Module):
    def __init__(
        self,
        num_devices: int,
        device: torch.device,
        verbose: bool = False,
        load_checkpoint: str = None,
        overwrite_num_classes: int = None
    ):
        super(Tester, self).__init__()

        self.dnn = self._model_factory(
            num_devices=num_devices,
            overwrite_num_classes=overwrite_num_classes)

        if verbose:
            print(self.dnn)

        self.device = device

        self.load(checkpoint=load_checkpoint)
        self.dnn = self.dnn.to(self.device)

        # Log number of parameters
        print(f"Number of parameters found in module: {self._count_vars()}")

    def _model_factory(self, num_devices: int = 3, overwrite_num_classes: int = None) -> nn.Module:
        """Builds a DNN model to be used as a __Estimator__. The instance
        is compiled using the local `resnet` modules.

        Parameters
        ----------
        num_devices : int, optional (default=3)
            Number of devices found in the heterogeneous platform.
        overwrite_num_classes : int, optional (default=None)
            Overwrites the number of classes in the model. If None, the number of classes is set to `num_devices`.

        Returns
        -------
        nn.Module
            The __Estimator__ model.
        """
        return resnet_factory(in_channels=num_devices, n_classes=overwrite_num_classes if overwrite_num_classes else num_devices)

    def _count_vars(self) -> int:
        """Counts trainable parameters of nn.Module.

        Returns
        -------
        int
            Number of total trainable parameters found in the model.
        """
        return sum([np.prod(p.shape) for p in self.parameters()])

    def test(self, mapping: np.ndarray):
        """Invoke the model on a single test sample.

        Parameters
        ----------
        mapping : np.ndarray
            The single input vector.

        Returns
        -------
        torch.Tensor
            Prediction (workload) on the custom input.
        """
        # Switch to evaluation mode
        self.dnn.eval()

        with torch.no_grad():

            # map data onto device
            mapping = torch.from_numpy(mapping).type(
                torch.FloatTensor).to(self.device)

            if len(mapping.shape) == 3:
                mapping = mapping.unsqueeze(0)

            # compute output from model
            output = self.dnn(mapping)

            # detach model prediction
            output = output.detach().cpu().numpy()

        return output

    def load(self, checkpoint: str = None):
        """Load a checkpoint.

        Parameters
        ----------
        checkpoint : str
            Path to the checkpoint to load.
        """

        checkpoint = torch.load(checkpoint)
        self.epoch = checkpoint['epoch']
        self.dnn.load_state_dict(checkpoint['model_state_dict'])
        self.best_acc = checkpoint['acc']
        self.best_loss = checkpoint['loss']


import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import re
import pandas as pd
import numpy as np

import pprint

def weight(reports):

    stop_words = set(stopwords.words('english'))

    chars_to_remove = ['?', '!', '[', ']', '`', '\'\'', '<', '>', '(', ')', ',', ':']
    rx = '[' + re.escape(''.join(chars_to_remove)) + ']'

    words = {}
    i = 1
    for report in reports:
        words[i] = []
        for k1 in report.keys():
            if (k1 != 'title'):
                for k2 in report[k1]['text'].keys():
                    sentence = report[k1]['text'][k2]
                    sentence = re.sub(r'(?<!\d)\.(?!\d)', '', sentence)
                    sentence = re.sub(rx, '', sentence)
                    sentence = sentence.lower()

                    word_tokens = word_tokenize(sentence)
                    for w in word_tokens:
                        if w not in stop_words:
                            words[i].append(w)

        # print(words[i])
        words[i] = list(filter(None, words[i]))
        words[i] = list(set(words[i]))
        # print(words[i])
        i += 1
        # break

    s_words = {}
    t_words = {}
    r_words = {}
    i = 1
    for report in reports:
        s_words[i] = {}
        t_words[i] = {}
        r_words[i] = {}
        for k1 in report.keys():
            if (k1 != 'title'):
                user = report[k1]['user']
                t_words[i][k1] = []
                if user not in s_words[i]:
                    s_words[i][user]=[]

                for k2, v in report[k1]['text'].items():
                    sentence = v
                    sentence = re.sub(r'(?<!\d)\.(?!\d)', '', sentence)
                    sentence = re.sub(rx, '', sentence)
                    sentence = sentence.lower()

                    word_tokens = word_tokenize(sentence)

                    r_words[i][k2] = []
                    temp = []
                    for w in word_tokens:
                        if w not in stop_words:
                            r_words[i][k2].append(w)
                            temp.append(w)

                    s_words[i][user].extend(temp)
                    t_words[i][k1].extend(temp)

        i += 1
        # break

    for k1 in s_words.keys():
        for k2 in s_words[k1].keys():
            s_words[k1][k2] = nltk.FreqDist(s_words[k1][k2])

    for k1 in t_words.keys():
        for k2 in t_words[k1].keys():
            t_words[k1][k2] = nltk.FreqDist(t_words[k1][k2])

    pp = pprint.PrettyPrinter(indent=4)
    # pp.pprint(t_words[1])
    # pp.pprint(s_words[1])
    # pp.pprint(r_words[1])

    sprob = {}
    tprob = {}
    for r_key in words.keys():
        sprob[r_key] = {}
        for i in range(len(words[r_key])-1):
            max = 0
            sum = 0
            for u_key in s_words[r_key].keys():
                if words[r_key][i] in s_words[r_key][u_key]:
                    if s_words[r_key][u_key][words[r_key][i]] > max:
                        max = s_words[r_key][u_key][words[r_key][i]]
                    sum += s_words[r_key][u_key][words[r_key][i]]
            if sum != 0:
                sprob[r_key][words[r_key][i]] = max / sum
            else:
                sprob[r_key][words[r_key][i]] = sum

        tprob[r_key] = {}
        for i in range(len(words[r_key])-1):
            max = 0
            sum = 0
            for t_key in t_words[r_key].keys():
                if words[r_key][i] in t_words[r_key][t_key]:
                    if t_words[r_key][t_key][words[r_key][i]] > max:
                        max = t_words[r_key][t_key][words[r_key][i]]
                    sum += t_words[r_key][t_key][words[r_key][i]]
            if sum != 0:
                tprob[r_key][words[r_key][i]] = max / sum
            else:
                tprob[r_key][words[r_key][i]] = sum

    # pp = pprint.PrettyPrinter(indent=4)
    # pp.pprint(tprob[35])
    # pp.pprint(sprob[35])

    return sprob, tprob, r_words

def lex_features(sprob, tprob, r_words):

    df_lex = []

    for k1 in r_words.keys():
        dict_lex = {}
        for k2 in r_words[k1].keys():
            # print(r_words[k1][k2])
            s_temp_sum = 0.0
            s_temp_max = 0.0
            s_temp_avg = 0.0
            t_temp_sum = 0.0
            t_temp_max = 0.0
            t_temp_avg = 0.0
            for word in r_words[k1][k2]:
                if word in sprob[k1]:
                    s_temp_sum += sprob[k1][word]
                    if sprob[k1][word] > s_temp_max:
                        s_temp_max = sprob[k1][word]
            if (len(r_words[k1][k2]) != 0):
                s_temp_avg = s_temp_sum / len(r_words[k1][k2])
            else:
                s_temp_avg = 0.0

            for word in r_words[k1][k2]:
                if word in tprob[k1]:
                    t_temp_sum += tprob[k1][word]
                    if tprob[k1][word] > t_temp_max:
                        t_temp_max = tprob[k1][word]
            if (len(r_words[k1][k2]) != 0):
                t_temp_avg = t_temp_sum / len(r_words[k1][k2])
            else:
                t_temp_avg = 0.0

            dict_lex[k2] = {
                            'SSM': s_temp_sum,
                            'SMX': s_temp_max,
                            'SMN': s_temp_avg,
                            'TSM': t_temp_sum,
                            'TMX': t_temp_max,
                            'TMN': t_temp_avg
            }

        df_lex.append(pd.DataFrame.from_dict(dict_lex, orient = 'index', dtype = float, columns = ['SSM','SMX','SMN','TSM','TMX','TMN']))

        # print(df_lex)
        # break

    return df_lex


import os
import json
from enum import Enum, unique
from debug import DebugMessage, DebugExit

class JTEData():
    work_book = None
    work_sheet = None
    dlg_json = {}
    flow_order = []

@unique
class XlHead(Enum):
    '''Excel表格的表头枚举'''
    GUID = 1
    TechIndex = 2
    TechJump = 3
    WorkIndex = 4
    Owner = 5
    Content = 6
    WorkJump = 7

@unique
class DlgNodeType(Enum):
    '''DlgSystem中的节点类型
    Start: -1, Sequence: 0, Speech: 1, Selector: 2, End: 3.
    '''
    Start = -1
    Sequence = 0
    Speech = 1
    Selector = 2
    End = 3

class JsonToExcel(object):
    def __init__(self):
        super(JsonToExcel, self).__init__()
        print("JsonToExcel Init.")
        # get json file path
        json_dir = os.path.abspath(os.path.join(os.getcwd(), "../.."))
        json_path = '%s/Dialogue/Dlg_TestFile.dlg.json' % json_dir
        # factory
        self.dlg_json_loader(json_path)

    @staticmethod
    def dlg_json_loader(json_file):
        """加载DlgSystem资产的json文件
        """
        if not os.path.exists(json_file):
            DebugExit("Dlg json's path")
            return

        with open(json_file, encoding="UTF-8") as f:
            dj_file = json.load(f)
        if not dj_file:
            return

        # 获取开始节点
        JTEData.dlg_json = dj_file
        print("Dlg json has been loaded.")
        f.close()

    @staticmethod
    def get_node_children():
        '''获取所有node中index与children的关系
        （StartNode的索引记作-1）
        '''
        dlgfile = JTEData.dlg_json
        if not dlgfile:
            DebugExit("Dlg json")
            return

        node_relations = {}
        start_children = dlgfile["StartNode"]["Children"]
        start_targets = []
        # StartNode
        for child in start_children:
            start_targets.append(child["TargetIndex"])
        node_relations[-1] = start_targets
        # Other nodes
        all_nodes = dlgfile["Nodes"]
        for node in all_nodes:
            node_index = node["__index__"]
            node_targets = []
            node_children = node["Children"]
            for child in node_children:
                node_targets.append(child["TargetIndex"])
            node_relations[node_index] = node_targets

        # DebugMessage("Node Realations", node_relations)

        return node_relations

    @staticmethod
    def flow_order(node_and_children):
        """将node按flow的先后顺序排序。
        依赖于node_relations。
        """
        if not node_and_children:
            DebugExit("Node and children")
            return

        flow_dict = {}
        flow_order = [-1]

        # 先处理StartNode
        start_node = node_and_children.pop(-1)
        flow_dict[-1] = start_node

        rec = -1
        while node_and_children != {}:
            # 拿到flow中最后一个有效的chidren的第一个child
            flow_revers = list(flow_dict.values())
            flow_revers.reverse()
            for target_list in flow_revers:
                if target_list == []:
                    continue
                last_node_first_child = target_list.pop(0)
                break
            DebugMessage("当前处理节点", last_node_first_child)

            # 一些安全措施
            if last_node_first_child == rec:
                DebugMessage("发现重复处理", rec)
                break
            rec = last_node_first_child

            # 检查child是否也是其他节点的child
            same_elm_found = False
            for node_children in node_and_children.values():
                if last_node_first_child in node_children:
                    same_elm_found = True
                    DebugMessage("发现该节点出现在后续流程中", last_node_first_child)
                    break

            # TODO 如果节点有多个入链
            # 需要判断其是否处于一个循环链中，
            # 如果是的话，先加入flow当中
            DebugMessage("Current Flow", flow_dict)
            if same_elm_found:
                JsonToExcel.is_loop_flow(
                    last_node_first_child, node_and_children)

            # 如果其他节点没有该child，则将其置入flow
            if not same_elm_found:
                popitem = node_and_children.pop(last_node_first_child)
                flow_dict[last_node_first_child] = popitem
                flow_order.append(last_node_first_child)

        DebugMessage("Flow Order", flow_order)

        return flow_order

    @staticmethod
    def is_loop_flow(index, node_and_children):
        """判断一个node是否处于循环当中。
        好几把烦啊我要哭了"""

        parse_record = {}

        parse_record[index] = node_and_children[index]

        # print(parse_record)

        r_prec = list(parse_record.values())
        r_prec.reverse()

        for targets in r_prec:
            if targets == []:
                continue
            last_node = targets[0]

    @staticmethod
    def indent_level(node_and_children: dict, flow_order: list):
        """计算每个节点的缩进等级。
        依赖于node_relations，以及flow_order。
        """
        if not node_and_children:
            DebugExit("Node Relations")
            return

        if not flow_order:
            DebugExit("Flow Order")
            return

        indent_level = {}

        for index in flow_order:

            branch_flag = False  # 分支标记
            in_links = []  # 所有入链的来源节点

            # 检查index在children中出现的次数
            t = 0
            for n_key, n_value in node_and_children.items():
                if index in n_value:
                    t += 1  # 出现次数
                    in_links.append(n_key)  # 记录入链
                    if n_value.__len__() >= 2:
                        branch_flag = True  # 判断是分支
            # 检查完毕

            # 如果一个index在所有出链中出现次数为0，
            # 该index有可能对应着一个开始节点或者悬浮节点，
            # 将其等级设置为0即可。
            if t == 0:
                indent_level[index] = 0
            # 如果一个index在所有出链中次数为1，
            # 检查其入链的来源节点是否为分支，
            # 如果是分支，则设置其缩进等级+1，否则继承缩进等级。
            elif t == 1:
                in_link = in_links[0]
                level = indent_level[in_link]
                if branch_flag:
                    level += 1
                indent_level[index] = level
            # 如果一个index在所有出链中次数大于等于2，
            # 获取所有入链的来源节点的缩进等级，进而找到最小值，
            # 使缩进等级等于最小值-1（需保证大于0）
            elif t >= 2:
                link_levels = []
                for link in in_links:
                    link_level = indent_level[link]
                    link_levels.append(link_level)
                level = min(link_levels)
                if level >= 1:
                    level -= 1

                indent_level[index] = level

        DebugMessage("Indent Level", indent_level)

        return indent_level

if __name__ == "__main__":
    JsonToExcel()
    # TempTest
    node_and_children = JsonToExcel.get_node_children
    flow_order = JsonToExcel.flow_order(node_and_children())
    # JsonToExcel.indent_level(node_and_children(), flow_order)


import os

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

from keras import backend as K
sess = tf.Session()
K.set_session(sess)

from keras.layers import *
from keras.metrics import *
from keras.objectives import categorical_crossentropy

PATH = os.path.dirname(os.path.abspath(__file__))

# save downloaded data to a directory named MNIST_data
mnist_data = input_data.read_data_sets('MNIST_data', one_hot=True)

# mnist images are 28**2 = 784 pixels, and 10 classes
img = tf.placeholder(tf.float32, shape=(None, 784))
labels = tf.placeholder(tf.float32, shape=(None, 10))

# make model
x = Dense(32, activation='relu')(img)
preds = Dense(10, activation='softmax')(x)

# define loss
loss = tf.reduce_mean(categorical_crossentropy(labels, preds))

# define performance metrics
acc_value = tf.reduce_mean(categorical_accuracy(labels, preds))

# define which variables to capture in summary
loss_summary = tf.summary.scalar('loss', loss)
acc_value_summary = tf.summary.scalar('acc', acc_value)
summary_op = tf.summary.merge_all()

# choose optimizer
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(loss)

# initialize all variables
log_path = os.path.join(PATH, 'logs')
batch_size = 50
init_op = tf.global_variables_initializer()
sess.run(init_op)
train_writer = tf.summary.FileWriter(log_path + '/train', graph=sess.graph)
valid_writer = tf.summary.FileWriter(log_path + '/valid', graph=sess.graph)
test_writer = tf.summary.FileWriter(log_path + '/test', graph=sess.graph)

samples_per_epoch = mnist_data.train.labels.shape[0]  # 1 pass over dataset
epochs = 10
assert samples_per_epoch % batch_size == 0, \
    'batch size {} does not divide epoch size {}'.format(batch_size, samples_per_epoch)

# run training loop
with sess.as_default():
    epoch = 0
    while epoch < epochs:
        print('Starting epoch {}'.format(epoch))
        sampled = 0
        while sampled < samples_per_epoch:
            # train
            batch = mnist_data.train.next_batch(batch_size)
            summary, _ = sess.run([summary_op, train_step], feed_dict={
                img: batch[0],
                labels: batch[1]
            })
            train_writer.add_summary(summary, epoch*samples_per_epoch + sampled)

            # validation
            valid_batch = mnist_data.validation.next_batch(batch_size)
            summary, _, _ = sess.run([summary_op, acc_value, loss], feed_dict={
                img: valid_batch[0],
                labels: valid_batch[1]
            })
            valid_writer.add_summary(summary, epoch*samples_per_epoch + sampled)

            sampled += batch_size
        epoch += 1

        # test
        summary, _, _ = sess.run([summary_op, acc_value, loss], feed_dict={
            img: mnist_data.test.images,
            labels: mnist_data.test.labels
        })
        test_writer.add_summary(summary, epoch*samples_per_epoch)

# tensorboard --logdir=`pwd`/mnist/logs
# localhost:6006


import tweepy
import time
import webbrowser
import json

secrets = json.load(open('C:/Users/lenovo/Documents/GitHub/Hello-World/Twitter_API/secrets.json', 'r'))

consumer_key = secrets['CONSUMER_KEY']
consumer_secret = secrets['CONSUMER_SECRET']
callback_uri = 'oob' #

auth = tweepy.OAuthHandler(consumer_key, consumer_secret, callback_uri)
redirect_url = auth.get_authorization_url()

webbrowser.open(redirect_url)

user_pin_input = input('What\'s The Pin Code ? ')
print(user_pin_input)

auth.get_access_token(user_pin_input)
print(auth.access_token, auth.access_token_secret)

api = tweepy.API(auth)
me = api.me()

print(me.screen_name) # username :> codewithlenny

## Tweet Using Python (status_update)
# new_status = api.update_status('Hello, World! - Did you know you could tweet using your Python Script? This tweet is an example of one of Python\'s Amazing Features. - @joincfe \n\n#python3')
# new_status.destroy() -> Delete


import argparse
import random
import utils

from difflib import SequenceMatcher

ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

class SubstitutionCipher:
    """ Solve substitution cipher using hill climbing approach """
    cipher_text = ""   # Stores the original cipher-text
    key = ""           # Represents the mapping between cipher-text and plain-text letters
    found_the = False  # Did we successfully crack the work 'THE'

    def __init__(self, cipher_text, fitness):
        self.cipher_text = cipher_text  # Prepare input text for decryption

        # Start with a random key
        key = [letter for letter in ALPHABET]
        random.shuffle(key)

        # We can guess 'THE' extremely accurately
        self.found_the = False
        tri = [k for k in utils.get_occurence_dict(utils.get_trigraphs(cipher_text))][0]
        di = [k for k in utils.get_occurence_dict(utils.get_digraphs(cipher_text))][0]
        if di in tri:
            # Successfully found 'THE'
            t_index = key.index(tri[0])  # find the index for letter corresponding to 'T'
            key[t_index], key[19] = key[19], key[t_index]  # T is 19th in alphabet (starting at 0)

            h_index = key.index(tri[1])  # find the index for letter corresponding to 'H'
            key[h_index], key[7] = key[7], key[h_index]  # H is 7th in alphabet (starting at 0)

            e_index = key.index(tri[2])   # find the index for letter corresponding to 'E'
            key[e_index], key[4] = key[4], key[e_index]  # E is 4th in alphabet (starting at 0)
            self.found_the = True

        self.key = "".join(key)  # Convert back to string

        # Calculate log probabilities for mono, di, tri, quadgraphs
        self.mono_prob = utils.get_log_probability(fitness + "monographs.txt")
        self.di_prob = utils.get_log_probability(fitness + "digraphs.txt")
        self.tri_prob = utils.get_log_probability(fitness + "trigraphs.txt")
        self.quad_prob = utils.get_log_probability(fitness + "quadgraphs.txt")

    def solve(self):
        LOOP_LIMIT = 250  # This many failed swaps in a row will stop the algorithm

        iterations = 0
        bad_swaps = 0
        while True:
            iterations += 1
            success = self.key_swap()

            if not success:
                bad_swaps += 1
                if bad_swaps == LOOP_LIMIT:
                    break
            else:
                bad_swaps = 0  # Reset the counter

        print("Iterations: " + str(iterations))
        print("Key:        " + str(self.key))
        print("Ciphertext: " + str(self.cipher_text))
        print("Plaintext:  " + str(self.apply_key()))

        # For testing
        return round(SequenceMatcher(None, self.key, "JDNVPWOZAMXKGIESUTBFQRYHLC").ratio(), 5)

    def apply_key(self):
        """Decrypt the cipher-text using the current key"""
        text = self.cipher_text

        for i in range(len(self.key)):
            text = text.replace(self.key[i], ALPHABET[i].lower())  # We write lowercase so we dont swap characters twice

        return text.upper()  # Return everything to uppercase

    def calculate_fitness(self):
        """
        Calculate the language 'fitness' of the current key(ciphertext)

        Each score is calculated by adding the log probabilities. Ex:
        log(prob(CRYPTO)) = log(prob(CRY)) + log(prob(RYP)) + log(prob(YPT)) + log(prob(PTO))

        Probabilities are negative. Closer to 0 is more likely.
        """
        text = self.apply_key()

        # mono_score = 0
        # monographs = utils.get_monographs(text)
        # for mono in monographs:
        #     # If the substring is not present, the probability is ~0
        #     if mono in self.mono_prob:
        #         mono_score += self.mono_prob[mono]

        di_score = 0
        digraphs = utils.get_digraphs(text)
        for di in digraphs:
            # If the substring is not present, the probability is ~0
            if di in self.di_prob:
                di_score += self.di_prob[di]

        tri_score = 0
        trigraphs = utils.get_trigraphs(text)
        for tri in trigraphs:
            # If the substring is not present, the probability is ~0
            if tri in self.tri_prob:
                tri_score += self.tri_prob[tri]

        quad_score = 0
        quadgraphs = utils.get_quadgraphs(text)
        for quad in quadgraphs:
            # If the substring is not present, the probability is ~0
            if quad in self.quad_prob:
                quad_score += self.quad_prob[quad]

        return di_score + tri_score + (quad_score / 2)
        # return (mono_score / 8) + (di_score / 4) + (tri_score / 2) + quad_score

    def key_swap(self):
        """ Randomly swap key values anc check if that improved the score. """
        old_key = self.key  # In case we need to revert
        old_score = self.calculate_fitness()

        # Do not swap 'THE'
        if not self.found_the:
            options = range(0, 26)
        else:
            options = list(range(0, 4)) + list(range(5, 7)) + list(range(8, 19)) + list(range(20, 26))

        index_1 = random.choice(options)
        index_2 = random.choice(options)
        # Do not allow the same index
        while index_1 == index_2:
            index_2 = random.choice(options)
        list_key = list(self.key)  # Convert to list for swapping
        list_key[index_1], list_key[index_2] = list_key[index_2], list_key[index_1]
        self.key = "".join(list_key)  # Convert back to string

        # Revert if old key had better score (closer to 0, scores are negative)
        if not float(old_score) >= float(self.calculate_fitness()):
            return 1  # Swap was successful

        self.key = old_key
        return 0  # Swap was unsuccessful

if __name__ == "__main__":
    # Parse Args
    parser = argparse.ArgumentParser()
    parser.add_argument('cipher_file', metavar='C', type=str, help="Text file containing the cipher-text.")
    parser.add_argument('--training', dest='training', type=str, default=None,
                        help="Optional: training text to determine language fitness.")
    args = parser.parse_args()

    # Determine what data to calculate language "fitness" with
    fitness_path = "languages/en_us/"  # Use English letter frequencies by default
    # Calculate letter frequencies if they provide training text
    if not args.training is None:
        fitness_path = utils.CUSTOM_FITNESS  # point to custom letter frequencies

        # Read training data
        with open(args.training, 'r') as t_handle:
            training_text = utils.text_preprocessing(t_handle.read())

            # Count frequencies and write to file
            utils.write_custom_fitness_data(training_text)

    # Prepare user inputs for decryption
    with open(args.cipher_file, 'r') as c_handle:
        ct = utils.text_preprocessing(c_handle.read())

    # Solve
    cipher = SubstitutionCipher(cipher_text=ct, fitness=fitness_path)
    cipher.solve()


from c import main
import unittest
from parameterized import parameterized

class CTest(unittest.TestCase):

    @parameterized.expand([
        ("Case1", 2, "b",),
        ("Case2", 27, "aa",),
        ("Case3", 123456789, "jjddja",),
        ("Edge1", 1, "a",),
        ("Edge2", 26, "z",),
        ("Edge3", 1000000000000001, "gbdpxgrzxjm",),
    ])
    def test_main(self, _, _n, expected):
        self.assertEqual(main(_n), expected)


from kivy.app import App

from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.image import Image
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.checkbox import CheckBox
from kivy.uix.scatter import Scatter

from kivy.logger import Logger

from kivy.lang import Builder

import logging

import random, os

from ImageLib import ImageLibrary

Builder.load_string('''
<CategoryWidget>:
	orientation: 'horizontal'

    CheckBox:
    	id: chb
    	active: True
    Label:
    	id: label
    	text: '<Make name>'

<CategoryPanel>
	orientation: 'vertical'

''')

class CategoryWidget(BoxLayout):
	pass

class CategoryPanel(BoxLayout):
	pass

class ImageBox(GridLayout):

	def __init__(self,  imageLibrary=None, **kwargs):

		super(ImageBox, self).__init__(**kwargs)
		#self.RandomImages(app=kwargs['app'])

		self.rows = 2
		self.add_widget(Label(text="There is no images yet, please set \"settings\" -> \"Image root\""))

	def AddImageAndCategories(self, imageDict, clb=None):

		self.clear_widgets()

		l = len(imageDict.keys())
		if l % 2:
			self.rows = l / 2 + 1
		else:
			self.rows = l / 2

		categories = imageDict.keys()

		if len(categories) > 1:
			random.shuffle(categories)

		for category in categories:

			img = Image(source=imageDict[category]['image'])
			img.name = category

			if clb:
				img.bind(on_touch_down=clb)

			img = self.add_widget(img)

class MyLayout(FloatLayout):

	def __init__(self, **kwargs):

		super(MyLayout, self).__init__(**kwargs)

		imagebox = ImageBox(size_hint=(.8, 1), pos_hint={'x':.2}, id='ImageBox')

		self.add_widget(imagebox)

		button = Button(text='randomize', size_hint=(.18, .18), pos_hint={'y':.81, 'x':.01}, background_color=(0,2,0,1), id='RandomBtn')
		self.add_widget(button)

		button = Button(text='options', size_hint=(.18, .18), pos_hint={'y':.01, 'x':.01}, id='SettingsBtn')
		self.add_widget(button)

		categoryPanel = CategoryPanel(size_hint=(.15, .6), pos_hint={'y':.2}, id='CategoryPanel')

		self.add_widget(categoryPanel)

	def AddCategoryCheckbox(self, categories, clb=None):

		categoryPanel = self.GetWidgetById('CategoryPanel')
		categoryPanel.clear_widgets()

		idx = 1
		for cat in categories:

			box = CategoryWidget()
			box.ids['label'].text = cat
			#box.checkbox.active = False

			if clb:
				box.ids['chb'].bind(active=clb)
				box.ids['chb'].name = cat

			categoryPanel.add_widget(box)

	def GetWidgetById(self, widgetId):

		for widg in self.walk():

			if widg.id == widgetId:
				Logger.debug('found widget {0}, id = {1}'.format(widg, widgetId))
				return widg

class RndArtApp(App):

	def on_randomize_button(self, instance):

		Logger.debug('The button <%s> is being pressed' % instance.text)

		imageDict = self._imglib.GetImageDict()
		imgbox = self._layout.GetWidgetById('ImageBox')
		imgbox.AddImageAndCategories(imageDict, clb=self.on_my_touch_down)

	def on_options_button(self, instance):

		Logger.debug('The button <%s> is being pressed' % instance.text)

		self.open_settings()

	def on_my_touch_down(self, instance, touch):

		isX = 0
		isY = 0

		if touch.x > instance.x and touch.x < instance.x + instance.width:
			isX = 1
		if touch.y > instance.y and touch.y < instance.y + instance.height:
			isY = 1

		if isX and isY:
			Logger.info('on_my_touch_down: %s' % instance.name)
			instance.source = self._imglib.GetRandomImageFromCategory(instance.name)

	def on_checkbox_active(self, checkbox, value):
		if value:
			Logger.debug('The checkbox {0} is active'.format(checkbox.name))

			self._imglib.UnbanCategory(checkbox.name)
		else:
			Logger.debug('The checkbox {0} is inactive'.format(checkbox.name))
			self._imglib.BanCategory(checkbox.name)

		imageDict = self._imglib.GetImageDict()
		imgbox = self._layout.GetWidgetById('ImageBox')
		imgbox.AddImageAndCategories(imageDict, clb=self.on_my_touch_down)

	def __init__(self, **arg):
		super(RndArtApp, self).__init__(**arg)

		# ... or
		# App.__init__(self, **arg)

	def build_config(self, config):

		config.setdefaults('ImageLib',
			{'root': 'images/lib'})

	def build_settings(self, settings):

		json = '''
			[

			    {
			        "type": "string",
			        "title": "Image root",
			        "desc": "Choose the root image directory",
			        "section": "ImageLib",
			        "key": "root"
			    }

			]'''

		settings.add_json_panel('Options', self.config, data=json)

	def on_config_change(self, config, section, key, value):

		# Logger.info("main.py: App.on_config_change: {0}, {1}, {2}, {3}".format(
		# 	config, section, key, value))

		if section in 'ImageLib':

			if key in 'root':

				imglib = ImageLibrary(value)

				imageDict = imglib.GetImageDict()

				self._layout.GetWidgetById('ImageBox').AddImageAndCategories(imageDict, clb=self.on_my_touch_down)

				self._layout.AddCategoryCheckbox(imglib.GetCategories(), self.on_checkbox_active)

				self._imglib = imglib

	def GetBannedCategoriesFromConfig(self):

		logging.debug("GetBannedCategoriesFromConfig()")

		result = []

		res = self.config.get('ImageLib', 'unchecked_categoreis')

		if res:

			[configRoot, unchecked] = res.split("*")

			if configRoot == self._imglib._root:
				unchecked = unchecked.split(',')

				logging.debug("found unchecked categories %s" % ' '.join(unchecked))
				return unchecked

		return result

	def UncheckCheckBoxes(self):

		categories = self.GetBannedCategoriesFromConfig()

		if categories:

			for widget in self._layout.GetWidgetById('CategoryPanel').children:

				if widget.ids['chb'].name in categories:

					logging.info("unchecking category")

					widget.ids['chb'].active = False

	def build(self):

		#````````````#
		# Initialize #
		#____________#

		root = self.config.get('ImageLib', 'root')
		imglib = ImageLibrary(root)

		self._imglib = imglib

		layout = MyLayout()

		self._layout = layout

		#````````````````#
		# fill with data #
		#________________#

		for category in self.GetBannedCategoriesFromConfig():
			self._imglib.BanCategory(category)

		imageDict = imglib.GetImageDict()

		layout.GetWidgetById('ImageBox').AddImageAndCategories(imageDict, clb=self.on_my_touch_down)

		layout.AddCategoryCheckbox(self._imglib.GetCategories(), self.on_checkbox_active)
		self.UncheckCheckBoxes()

		layout.GetWidgetById('RandomBtn').bind(on_press=self.on_randomize_button)
		layout.GetWidgetById('SettingsBtn').bind(on_press=self.on_options_button)

		return layout

	def on_stop(self):

		logging.debug("saving checked directory state...")

		unchecked = []

		res = ''

		for widget in self._layout.GetWidgetById('CategoryPanel').children:
			if not widget.ids['chb'].active:
				logging.debug("found unchecked category: %s" % widget.ids['chb'].name)
				unchecked.append(widget.ids['chb'].name)

		if unchecked:

			res = self._imglib._root + '*' + ','.join(unchecked)

		else:
			logging.debug("there is no unchecked boxes")

		self.config.set("ImageLib", "unchecked_categoreis", res)
		self.config.write()

if __name__ == "__main__":

	Logger.setLevel(logging.DEBUG)

	RndArtApp().run()


a=int(input())
b=str(input())

# step 1: move across the string from left to right
# step 2: count each and every character
# step 3: get the total for each character and compare

# of a in the string
# of b in the string
aCount=0
bCount=0

for i in range (a):
    if b[i] == 'A':
      aCount = aCount + 1

    elif b[i] == 'B':
        bCount = bCount + 1

if aCount>bCount:
    print("A")

elif bCount>aCount:
    print("B")

else:
    print("Tie")


import pandas as pd
import numpy as np
import gensim
import sklearn
import seaborn as sns
from preprocess_ import *
'''
#### INPUT ::
df==input dataframe
text==text column
label_column==label column
#### OUTPUT ::
sorted dictionary with words and their frequency count in corpus
'''
def preprocess_for_filter(df,text_column='concept',label_column='label'):
    from collections import Counter
    cop_df=df.copy()
    cop_df[text_column]=cop_df[text_column].apply(lambda x: x.split(' '))
    corpus=[]
    for i in cop_df[text_column]:
        for j in i:
            corpus.append(j)
    cnt=Counter(corpus)
    sorted_dict=sorted(cnt.items(),key=lambda x: x[1],reverse=True)
    return sorted_dict
'''
#### INPUT ::
sorted_dict==sorted dictionary of all words and their frequency in corpus
alpha = rate of order (usually a power of 2) increase value in multiples of  2^n (n=subset of positive Integers) for more aggresive filtering
#### OUTPUT ::
list of words deemed to be noisy after zipf filtering for the corpus.
'''
def zipf_filter(df,text_column='concept',label_column='label',alpha=2):
    cop_df=df.copy()
    sorted_dict=preprocess_for_filter(cop_df,text_column='concept',label_column='label')
    zipf_list=sorted_dict
    rank_list=[(i,x) for i,x in enumerate(zipf_list,1)]
    k=int(rank_list[0][1][1])/2
    alpha=2
    zipff=[(float(k)/(x[0])**alpha,np.log(x[1][1])) for x in rank_list]

    cnt=0
    for i in zipff:
        if i[1]!=0:
            if i[0]>i[1]:
                cnt=cnt+1

    noise_words=[x[0] for x in zipf_list[0:cnt]]
    return noise_words


n = int(input())
student = list()
for i in range(n):
    student.append([input(), float(input())])

score = list(set([student[i][1] for i in range(len(student))]))
score.sort()
output = [x[0] for x in student if x[1] == score[1]]
output.sort()
for s in output:
    print(s)


from PyQt5.QtWidgets import QStyle
from PyQt5.QtCore import Qt

from app.utilities import str_utils
from app.utilities.data import Data
from app.data.database import DB

from app.extensions.custom_gui import DeletionDialog, PropertyBox, ComboBox
from app.extensions.list_dialogs import MultiAttrListDialog
from app.extensions.list_models import MultiAttrListModel

from app.data import equations, level_units, item_components, components

import logging

class EquationMultiModel(MultiAttrListModel):
    def data(self, index, role):
        if not index.isValid():
            return None
        if index.column() == 1 and role == Qt.DecorationRole:
            equation = self._data[index.row()]
            good = self.test_equation(equation)
            if good:
                icon = self.window.style().standardIcon(QStyle.SP_DialogApplyButton)
            else:
                icon = self.window.style().standardIcon(QStyle.SP_DialogCancelButton)
            return icon
        elif role == Qt.DisplayRole or role == Qt.EditRole:
            data = self._data[index.row()]
            attr = self._headers[index.column()]
            return getattr(data, attr)
        return None

    def test_equation(self, equation) -> bool:
        try:
            from app.engine import equations as parse
            parser = parse.Parser()
            test_unit = level_units.UniqueUnit(DB.units[0].nid, 'player', None, (0, 0))
            test_unit.stats = {k: v for (k, v) in test_unit.bases.items()}
            test_unit.stat_bonus = lambda x: 0
            result = parser.get(equation.nid, test_unit)
            result = parser.get_expression(equation.expression, test_unit)
            return True
        except Exception as e:
            logging.error("TestEquation Error: %s" % e)
            return False

    def delete(self, idx):
        element = self._data[idx]
        affected_items = item_components.get_items_using(components.Type.Equation, element.nid, DB)
        if affected_items:
            affected = Data(affected_items)
            from app.editor.item_editor.item_model import ItemModel
            model = ItemModel
            msg = "Deleting Equation <b>%s</b> would affect these items" % element.nid
            combo_box = PropertyBox("Equation", ComboBox, self.window)
            objs = [eq for eq in DB.equations if eq.nid != element.nid]
            combo_box.edit.addItems([eq.nid for eq in objs])
            obj_idx, ok = DeletionDialog.get_simple_swap(affected, model, msg, combo_box)
            if ok:
                swap = objs[obj_idx]
                item_components.swap_values(affected_items, components.Type.Equation, element.nid, swap.nid)
            else:
                return
        super().delete(idx)

    def create_new(self):
        nids = [d.nid for d in self._data]
        nid = str_utils.get_next_name("EQUATION", nids)
        new_equation = equations.Equation(nid)
        DB.equations.append(new_equation)
        return new_equation

    def on_attr_changed(self, data, attr, old_value, new_value):
        if attr == 'nid':
            self._data.update_nid(data, new_value)
            affected_items = item_components.get_items_using(components.Type.Equation, old_value, DB)
            item_components.swap_values(affected_items, components.Type.Equation, old_value, new_value)

class EquationDialog(MultiAttrListDialog):
    locked_vars = {"HIT", "AVOID", "CRIT_HIT", "CRIT_AVOID",
                   "DAMAGE", "DEFENSE", "MAGIC_DAMAGE", "MAGIC_DEFENSE",
                   "HITPOINTS", "MOVEMENT", "CRIT_ADD", "CRIT_MULT",
                   "SPEED_TO_DOUBLE", "STEAL_ATK", "STEAL_DEF",
                   "HEAL", "RESCUE_AID", "RESCUE_WEIGHT", "RATING"}

    @classmethod
    def create(cls):
        def deletion_func(model, index):
            return model._data[index.row()].nid not in cls.locked_vars

        dlg = cls(DB.equations, "Equation", ("nid", "expression"),
                  EquationMultiModel, (deletion_func, None, deletion_func), cls.locked_vars)
        return dlg

    def accept(self):
        super().accept()
        from app.engine import equations
        equations.clear()

# Testing
# Run "python -m app.editor.equation_widget"
if __name__ == '__main__':
    import sys
    from PyQt5.QtWidgets import QApplication
    app = QApplication(sys.argv)
    DB.load('default.ltproj')
    window = EquationDialog.create()
    window.show()
    app.exec_()


import logging
from string import Formatter

default_suffixes = ('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB')
default_factor = 1024

class ByteFormatter(Formatter):
    def __init__(self, factor=default_factor, suffixes=default_suffixes):
        self.factor = factor
        self.suffixes = suffixes
        self.suffix = None

    def format_field(self, value, format_spec):
        if self.suffix == 'B':
            format_spec = '.0f'
        elif self.suffix:
            format_spec = '.2f'

        result = super().format_field(value, format_spec)

        if self.suffix:
            result += ' ' + self.suffix
            self.suffix = None

        return result

    def convert_field(self, value, conversion):
        if conversion == 'b':
            for suffix in self.suffixes:
                if abs(value) < self.factor:
                    self.suffix = suffix
                    return value
                value /= self.factor
        else:
            return super().convert_field(value, conversion)

class Message:
    def __init__(self, fmt, args):
        self.fmt = fmt
        self.args = args
        self.formatter = ByteFormatter()

    def __str__(self):
        return self.formatter.format(self.fmt, *self.args)

class ByteLoggerAdapter(logging.LoggerAdapter):
    def log(self, level, msg, *args, **kwargs):
        if self.isEnabledFor(level):
            msg, kwargs = self.process(msg, kwargs)
            self.logger._log(level, Message(msg, args), (), **kwargs)

def get_logger(name):
    return ByteLoggerAdapter(logging.getLogger(name), {})


from operator import mod
import sys
import math
import struct
import os
#from io import BytesIO
from PIL import Image
import matplotlib.pyplot as plt

doscolors = [
    (0x00, 0x00, 0x00),  # 0
    (0x00, 0x00, 0xa8),  # 1
    (0x00, 0xa8, 0x00),  # 2
    (0x00, 0xa8, 0xa8),  # 3
    (0xa8, 0x00, 0x00),  # 4
    (0xa8, 0x00, 0xa8),  # 5
    (0xa8, 0xa8, 0x00),  # 6
    (0xa8, 0xa8, 0xa8),  # 7

    (0x54, 0x54, 0x54),  # 8
    (0x54, 0x54, 0xff),  # 9
    (0x54, 0xff, 0x54),  # 10
    (0x54, 0xff, 0xff),  # 11
    (0xff, 0x54, 0x54),  # 12
    (0xff, 0x54, 0xff),  # 13
    (0xff, 0xff, 0x54),  # 14
    (0xff, 0xff, 0xff),  # 15
]

def color_distance(a, b):
    return math.sqrt((a[0]-b[0])**2 + (a[1]-b[1])**2 + (a[2]-b[2])**2)

def nearest_color(color):
    nearest = 0

    for i in range(len(doscolors)):
        if color_distance(color, doscolors[i]) < color_distance(color, doscolors[nearest]):
            nearest = i

    return nearest

buf = []
src_dir = r'src/'
dst_file = r'imgout.txt'

for imgf in os.listdir(src_dir):
    img = Image.open(os.path.join(src_dir, imgf)).convert("RGB")
    img = img.resize((80, 50))
    w, h = img.size

    for y in range(0, h, 2):
        for x in range(w):
            b = (nearest_color(img.getpixel((x, y))) <<
                 4) | nearest_color(img.getpixel((x, y+1)))
            buf.append(b)
    img.close()

with open(dst_file, "w") as out:
    c = 0
    out.write("DB\t")
    for i in buf:
        si = hex(i).upper().removeprefix('0X')
        if si[0].isalpha():
            si = '0' + si
        si = si + 'H'
        out.write(si)
        c = c + 1
        if mod(c+1, 25) == 0:
            out.write('\nDB\t')
        else:
            out.write(',')
    print(c)


import pathlib
import setuptools

def readVersion(relativePath):
	path = pathlib.Path(__file__).parent / relativePath
	with open(path) as stream:
		for line in stream:
			if line.startswith('__version__'):
				return line.replace('"', "'").split("'")[1]
		raise RuntimeError('Could not find version string')

setuptools.setup(
	name='ktxjuggle',
	version=readVersion('ktxjuggle/__init__.py'),
	description='Converts KTX texture files to JSON and back',
	url='https://github.com/sevmeyer/ktxjuggle',
	author='Severin Meyer',
	author_email='hello@sev.dev',
	license='Boost Software License 1.0',

	packages=['ktxjuggle'],
	entry_points={'console_scripts': ['ktxjuggle=ktxjuggle.__main__:main']},
	python_requires='>=3.6',
)


import sys
import pygame
from settings import WINDOW_HEIGHT, WINDOW_WIDTH, FPS
from level import MainMenu

class Game:
    def __init__(self) -> None:
        pygame.init()
        self.screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
        pygame.mouse.set_cursor(
            (8, 8), (0, 0), (0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0))
        pygame.display.set_caption("Ghost Runner")
        pygame.display.set_icon(pygame.image.load(
            "assets/Obstacle.png"))
        self.clock = pygame.time.Clock()
        self.level = MainMenu(self)

    def run(self):
        while True:
            events = pygame.event.get()
            for event in events:
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                self.level.events(event)
            self.level.update(1/FPS)

            self.level.draw()
            pygame.display.update()

            self.clock.tick(FPS)

if __name__ == "__main__":
    g = Game()
    g.run()


import sys
import os
import argparse
import pickle
import json

from tqdm import tqdm
import pandas as pd

sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', '..'))

import configs
from utils import logging_utils as logu
from utils.logging_utils import print_verbose
from utils import laion_utils as laionu

if __name__ == '__main__':
    # ----- Get arguments from input -----
    parser = argparse.ArgumentParser()

    # Path
    parser.add_argument('--clip_retrieval_json_path', type=str,
                        default=os.path.join('laion400m', 'processed', 'from_clip_retrieval',
                                             'top20_subset_most_similars_from_laion_400m.json'))

    parser.add_argument('--labels_path', type=str, default=os.path.join('laion400m', 'processed', 'ilsvrc_labels'))
    parser.add_argument('--labels_file_name', type=str, default='wnid2laionindices(substring_matched).pkl')

    parser.add_argument('--laion_path', type=str, default=os.path.join('laion400m'))

    # Logging
    parser.add_argument('--no_verbose', dest='verbose', action='store_false')

    # Overwrite?
    parser.add_argument('--no_safe', dest='safe', action='store_false')

    # Convert to dictionary
    params = vars(parser.parse_args())

    # ----- Init. -----
    logu.verbose = params['verbose']

    open_type = 'xb' if params['safe'] else 'wb'

    # ----- Loading -----
    print_verbose('loading ...')

    print_verbose('\tloading clip retrieval results ...')
    with open(params['clip_retrieval_json_path'], 'r') as f:
        cr_results = json.load(f)
    cr_results = {int(k): v for k, v in cr_results.items()}

    print_verbose('\tloading labels ...')
    with open(os.path.join(params['labels_path'], params['labels_file_name']), 'rb') as f:
        wnid2laionindices = pickle.load(f)

    print_verbose('done!\n')

    # ----- Find the inverse map -----
    print_verbose('finding inverse map ...')

    laionindex2wnids = {}
    for wnid, laion_indices in wnid2laionindices.items():
        for laion_idx in laion_indices:
            if laion_idx not in laionindex2wnids:
                laionindex2wnids[laion_idx] = []
            laionindex2wnids[laion_idx].append(wnid)

    print_verbose('done!\n')

    # ----- Reformat -----
    wnid2crindex2sims = {}
    wnid2crindex2text = {}
    wnid2crindex2url = {}

    for laion_idx, results in tqdm(cr_results.items(), desc='collecting results into dicts'):
        wnids = laionindex2wnids[laion_idx]

        for wnid in wnids:
            if wnid not in wnid2crindex2sims:
                wnid2crindex2sims[wnid] = {}
                wnid2crindex2text[wnid] = {}
                wnid2crindex2url[wnid] = {}

            if isinstance(results, dict):
                continue

            for res in results:
                cr_idx = res[configs.CLIPRetrievalConfig.ID_COL]

                similarity = res[configs.CLIPRetrievalConfig.SIMILARITY_COL]
                text = res[configs.CLIPRetrievalConfig.TEXT_COL]
                url = res[configs.CLIPRetrievalConfig.URL_COL]

                if cr_idx not in wnid2crindex2sims[wnid]:
                    wnid2crindex2sims[wnid][cr_idx] = [similarity]
                    wnid2crindex2text[wnid][cr_idx] = text
                    wnid2crindex2url[wnid][cr_idx] = url
                else:
                    wnid2crindex2sims[wnid][cr_idx].append(similarity)

    # Reduction
    wnid2crindices = {}
    wnid2crsims = {}
    wnid2texts = {}
    wnid2urls = {}

    for wnid in tqdm(wnid2crindex2sims, desc='reducing the results'):
        crindex2sims = wnid2crindex2sims[wnid]
        crindex2text = wnid2crindex2text[wnid]
        crindex2url = wnid2crindex2url[wnid]

        wnid2crindices[wnid] = []
        wnid2crsims[wnid] = []
        wnid2texts[wnid] = []
        wnid2urls[wnid] = []

        for cr_idx in crindex2sims:
            rept = len(crindex2sims[cr_idx])

            wnid2crindices[wnid].extend([cr_idx]*rept)
            wnid2crsims[wnid].extend(crindex2sims[cr_idx])
            wnid2texts[wnid].extend([crindex2text[cr_idx]]*rept)
            wnid2urls[wnid].extend([crindex2url[cr_idx]]*rept)

    # ----- Parse -----
    text_col = configs.LAIONConfig.TEXT_COL
    url_col = configs.LAIONConfig.URL_COL

    df_index = []
    df_dict = {text_col: [], url_col: []}

    for wnid, crindices in tqdm(wnid2crindices.items(), desc='getting a dataframe out of results'):
        df_index.extend(crindices)
        df_dict[text_col].extend(wnid2texts[wnid])
        df_dict[url_col].extend(wnid2urls[wnid])

    # Create a dataframe
    df = pd.DataFrame(df_dict, index=df_index)

    # Drop duplicates
    df.index.name = 'cr_index'
    df = df.groupby('cr_index').first()

    # ----- Save -----
    print_verbose('saving...')

    # Save labels
    print_verbose(f'\tsaving distinct {len(wnid2crindices)} labels.')

    with open(os.path.join(params['labels_path'], 'wnid2smcrindices.pkl'), open_type) as f:
        pickle.dump(wnid2crindices, f)

    # Save similarities
    print_verbose(f'\tsaving image to image similarities.')

    with open(os.path.join(params['labels_path'], 'wnid2smcrimgimgsims.pkl'), open_type) as f:
        pickle.dump(wnid2crsims, f)

    # Save df
    print_verbose(f'\tsaving df with {len(df)} rows.')

    prefix = configs.LAIONConfig.SUBSET_SM_FILTERED_MOST_SIMILAR_IMG_IMG_PREFIX
    # For compatibility only
    subset_file_name = prefix + laionu.get_laion_subset_file_name(0, configs.LAIONConfig.NUM_PARTS - 1)
    subset_file_path = os.path.join(params['laion_path'], subset_file_name)

    if os.path.exists(subset_file_path) and params['safe']:
        raise Exception('Subset already exists!')

    df.to_parquet(subset_file_path, index=True)

    print_verbose('done!\n')