import streamlit as st
import pickle
import os
import pandas as pd
import numpy as np

ROOT_DIR = os.getcwd()
SAVED_DIR_PATH = "saved_models"
SAVED_ZERO_FILE="0"
MODEL_FILE_DIR ="model"
MODEL_FILE_NAME = "model.pkl"
TRANSFORMER_FILE_DIR="transformer"
TRANSFORMER_FILE_NAME="transformer.pkl"
# TARGET_ENCODER_FILE_DIR="target_encoder"
# TARGET_ENCODER_FILE_NAME="target_encoder.pkl"

MODEL_DIR = os.path.join(ROOT_DIR, SAVED_DIR_PATH,SAVED_ZERO_FILE,MODEL_FILE_DIR,MODEL_FILE_NAME)
# print("MODEL_PATH:-",MODEL_DIR)

TRANSFORMER_DIR= os.path.join(ROOT_DIR, SAVED_DIR_PATH,SAVED_ZERO_FILE,TRANSFORMER_FILE_DIR,TRANSFORMER_FILE_NAME)
# print("TRANSFORMER_PATH:-",TRANSFORMER_DIR)

# TARGET_ENCODER_DIR= os.path.join(ROOT_DIR, SAVED_DIR_PATH,SAVED_ZERO_FILE,TARGET_ENCODER_FILE_DIR,TARGET_ENCODER_FILE_NAME)
# print("TARGET_ENCODER_PATH:-",TARGET_ENCODER_DIR)

# Load the Model.pkl, Transformer.pkl and Target.pkl
model=pickle.load(open(MODEL_DIR,"rb"))
# print(model)
transfomer=pickle.load(open(TRANSFORMER_DIR,"rb"))
# print(transfomer)

# About page
def about_page():
    st.title('Predicting the Financial Burden of Lung Cancer')
    st.write('The project aims to develop a predictive model that estimates the annual out-of-pocket costs for patients diagnosed with Stage 3&4 lung cancer. By considering factors such as age, comorbidities, and primary insurance, the model will enable patients to proactively plan for future financial burdens associated with their diagnosis. The ultimate goal is to alleviate the financial stress and reduce the likelihood of personal bankruptcy that over 40% of cancer patients experience within four years of diagnosis.')

def visualization_page():...

# Main prediction page
def prediction_page():
    # Title and input fields
    st.title('Predicting the Financial Burden of Lung Cancer')
    st.subheader('Patient Information')
    AGE = st.number_input('Age', min_value=0, max_value=120, value=30)
    SEX = st.selectbox('Gender', ('Male', 'Female', 'Other'))
    RACE = st.selectbox('Race', ('Black', 'Other', 'White', 'Hispanic', 'Native American', 'Asian or Pacific Islander'))
    HOSPID = st.text_input('Hospital ID')
    NCHRONIC = st.selectbox('Numbder of Chronic Diseases', ('1', '2'))
    ZIPINC_QRTL = st.selectbox('Income Level to ZIP code', ('1', '2', '3'))
    NPR = st.number_input('Net Patient Revenue')
    # Add input fields for other features as needed

    # Hospital and Insurance Information
    st.subheader('Hospital and Insurance Information')
    DRG= st.selectbox('Diagnosis Related Group', ('ICD-10-CM', 'ICD-10-CM/PCS', 'ICD-9-CM', 'ICD-10-PCS'))
    DXn = st.selectbox('Level of disease diagnosis', ('3', '4'))
    CM_DRUG = st.selectbox('Drug Intake', ('current', 'never', 'former'))
    PAY1 = st.selectbox('Payment Method', ('Medicare', 'Medicaid', 'Private including HMO', 'Self-Pay', 'No charge', 'Other'))
    PAY2 = st.selectbox('Insurance Company', ('COBRA Coverage', 'Secondary Health Insurance', 'Employer-Sponsored Plans', 'Government Programs', 'NONE'))

    # Checkbox for presence of medical conditions
    st.subheader('Medical Conditions')
    CM_AIDS = st.checkbox('AIDS')
    CM_ALCOHOL = st.checkbox('Alcohol Consumption')
    CM_ANEMDEF = st.checkbox('Congenital Monosomy with Anemia and Defects')
    CM_ARTH = st.checkbox('Arthritis')
    CM_BLDLOSS = st.checkbox('Blood Loss')
    CM_CHF = st.checkbox('Congestive Heart Failure')
    TRAN_IN= st.selectbox('Transfer patient In', ('Transferred from acute care hospital', 'Not a transfer', 'Transferred from another health facility', 'Transferred from '))
    TRAN_OUT = st.selectbox('Transfer patient Out', ('Not a transfer', 'Transferred out to acute care hospital', 'Transferred out to another health facility'))

    # Prediction button
    if st.button('Predict'):
        # Preprocess the input features
        input_data = {
            'AGE': [AGE],
            'SEX': [SEX],
            'RACE': [RACE],
            'HOSPID':[HOSPID],
            'NCHRONIC':[NCHRONIC],
            'ZIPINC_QRTL':[ZIPINC_QRTL],
            'NPR':[NPR],
            'DRG':[DRG],
            'DXn':[DXn],
            'CM_DRUG':[CM_DRUG],
            'PAY1':[PAY1],
            'PAY2':[PAY2],
            'CM_AIDS': ['yes' if CM_AIDS else 'no'],
            'CM_ALCOHOL': ['yes' if CM_ALCOHOL else 'no'],
            'CM_ANEMDEF': ['yes' if CM_ANEMDEF else 'no'],
            'CM_ARTH': ['yes' if CM_ARTH else 'no'],
            'CM_BLDLOSS': ['yes' if CM_BLDLOSS else 'no'],
            'CM_CHF': ['yes' if CM_CHF else 'no'],
            'TRAN_IN':[TRAN_IN],
            'TRAN_OUT':[TRAN_OUT]
        }
        # Convert input data to a Pandas DataFrame
        input_df = pd.DataFrame(input_data)
        # Perform the transformation using the loaded transformer
        transformed_data = transfomer.transform(input_df)
        # Reshape the transformed data as a NumPy array
        input_arr = np.array(transformed_data)

        # Make the prediction using the loaded model
        prediction = model.predict(input_arr)
        st.subheader('Prediction')
        st.write(f'The predicted total charge is: {prediction[0]:.2f}')

# Teams page
def collaborators_page():
    st.title('Predicting the Financial Burden of Lung Cancer')
    st.write('Meet our awesome team members:')
    st.write('- Team Member 1')
    st.write('- Team Member 2')
    st.write('- Team Member 3')
    # Add more team members as needed

# Create a dictionary with page names and their corresponding functions
pages = {
    'About': about_page,
    'Visualization ':visualization_page,
    'Prediction': prediction_page,
    'Collaborators': collaborators_page,
}

# Streamlit application
def main():
    # Sidebar navigation
    st.sidebar.title('Navigation')
    selected_page = st.sidebar.radio('Go to', list(pages.keys()))

    # Display the selected page content
    pages[selected_page]()

# Run the Streamlit application
if __name__ == '__main__':
    main()


from asyncio.events import AbstractEventLoop
from typing import Union
from wmp.asyncio import Transport

class FakeTransport():
    def __init__(self, asyncio: Transport, loop: AbstractEventLoop):
        self._asyncio = asyncio
        self._loop = loop
        self._last_data: str
        self._next_response: Union[str, None] = None
        self._ready = False

    async def wait_for_response(self):
        while True:
            if self._ready:
                if self._next_response:
                    self._asyncio.data_received(
                        bytes(self._next_response, "UTF-8"))
                self._ready = False
                return

    def write(self, data: bytes) -> None:
        self._last_data = data.decode("utf-8")
        self._ready = True

    def set_next_response(self, response: str):
        self._next_response = response

    def get_last_data(self):
        return self._last_data


# -*- coding: utf-8 -*-
"""
Created on Tue Jun 27 14:06:33 2023

@author: 서다현
"""

'''
이용 데이터: 고객 서비스 이탈 예측 데이터
작업 유형: 서비스 이탈 예측(분류)
사용 모델: 텐서플로우 기반 인공신경망
'''

# 패키지 임포트
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from sklearn.metrics import accuracy_score

# 데이터 로드
x_train = pd.read_csv("https://raw.githubusercontent.com/Datamanim/datarepo/main/churnk/X_train.csv")
y_train = pd.read_csv("https://raw.githubusercontent.com/Datamanim/datarepo/main/churnk/y_train.csv")
x_test = pd.read_csv("https://raw.githubusercontent.com/Datamanim/datarepo/main/churnk/X_test.csv")

# 데이터 전처리
X = x_train.drop("CustomerId", axis = 1)
y = y_train.drop("CustomerId", axis = 1)
x_test_id = x_test.pop("CustomerId")

label = LabelEncoder()

X["Surname"] = label.fit_transform(X["Surname"])
X["Geography"] = label.fit_transform(X["Geography"])
X["Gender"] = label.fit_transform(X["Gender"])
x_test["Surname"] = label.fit_transform(x_test["Surname"])
x_test["Geography"] = label.fit_transform(x_test["Geography"])
x_test["Gender"] = label.fit_transform(x_test["Gender"])

scale = MinMaxScaler()

target = ["CreditScore", "Balance", "EstimatedSalary"]
X[target] = scale.fit_transform(X[target])

# 데이터셋 분리
X_tr, X_val, y_tr, y_val = train_test_split(X, y, test_size = 0.3, random_state = 111)

# 모델링
model = Sequential()
model.add(Dense(20, input_dim = 11, activation = "relu"))
model.add(Dense(1, activation = "sigmoid"))

model.summary()
model.compile(loss = "binary_crossentropy", optimizer = "adam", metrics = "accuracy")

model.fit(X_tr, y_tr, epochs = 20, batch_size = 10)

# 모델 평가
y_pred = model.predict(X_val)
y_pred = tf.cast(y_pred >= 0.5, dtype = tf.int32)
print("accuracy_score =", accuracy_score(y_val, y_pred))

# 결과
model.fit(X, y, epochs = 20, batch_size = 10)
pred = tf.cast(model.predict(x_test) >= 0.5, dtype=tf.int32)
predict = pd.DataFrame(pred)[0]

result = pd.DataFrame({"CustomerId":x_test_id, "Exited":predict})

y_test = pd.read_csv("https://raw.githubusercontent.com/Datamanim/datarepo/main/churnk/y_test.csv")
print(accuracy_score(y_test.iloc[:,1], pred))

result.to_csv("20230627 01.csv", index = False)
pd.read_csv("20230627 01.csv")


def main():
    """ We're going to try to make a tic tac toe game.
        We will draw a board, take numbers inputs.
    """
    #Initialize variables
    playArray = list(range(1, 10))
    while True:
        turn = 0
        for vals in playArray:
            if vals == "X" or vals == "O":
                turn = turn + 1
        playerChar = checkPlayer(turn)
        printBoard(playArray)
        print("Player {}, where you would like to play?".format(playerChar))
        userInput = input()
        if userInput == "q" or userInput == "Q":
            return
        action = validateInput(userInput, playArray)
        if action != 0:
            playArray[action - 1] = playerChar
            turn = turn + 1
        print(turn)
        if checkForEnd(playArray, playerChar):
            print("Congratulations!!!!! YOU WIN!!!! \n PLAYER {}".format(
                playerChar))
            break
        if turn == 9:
            print("Cats game, tie")
            break

def checkForEnd(playArray, player):
    win = False
    if playArray[6] == playArray[7] == playArray[8] == player:
        win = True
    elif playArray[6] == playArray[4] == playArray[2] == player:
        win = True
    elif playArray[6] == playArray[3] == playArray[0] == player:
        win = True
    elif playArray[7] == playArray[4] == playArray[1] == player:
        win = True
    elif playArray[8] == playArray[4] == playArray[0] == player:
        win = True
    elif playArray[8] == playArray[5] == playArray[2] == player:
        win = True
    elif playArray[3] == playArray[4] == playArray[5] == player:
        win = True
    elif playArray[0] == playArray[1] == playArray[2] == player:
        win = True
    return win

def checkPlayer(turnCount):
    if turnCount % 2 == 0:
        return "X"
    else:
        return "O"

def printBoard(playArray):
    print("{}|{}|{}".format(playArray[6], playArray[7], playArray[8]))
    print("-----")
    print("{}|{}|{}".format(playArray[3], playArray[4], playArray[5]))
    print("-----")
    print("{}|{}|{}".format(playArray[0], playArray[1], playArray[2]))

def validateInput(inputVal, playArray):
    try:
        int(inputVal)
    except:
        print("Not a valid input.  Try again")
        return 0
    intVal = int(inputVal)
    if intVal > 9 or intVal < 1:
        print("Not a valid input.  Please enter a value between 1 and 9")
        return 0
    elif playArray[intVal - 1] == "X" or playArray[intVal - 1] == "O":
        print(
            "Not a valid input.  Please enter an available spot on the play board"
        )
        return 0
    else:
        return intVal

if __name__ == "__main__":
    main()


import openpyxl
path= "C:\\python\\data\\data.xlsx"
workbook= openpyxl.load_workbook(path)
sheet=workbook.active   # single sheet
 # sheet=workbook.get_sheet_by_name("Sheet1")  multiple sheet
rows=sheet.max_row
colm=sheet.max_column
print(rows,colm)
for r in range(1,rows+1):
    for c in range(1,colm+1):
        print(sheet.cell(row=r,column=c).value, end="    ")
    print()


class Colors():
    COLOR_WHITE = "COLOR_WHITE"
    COLOR_RED = "COLOR_RED"
    COLOR_GREEN = "COLOR_GREEN"
    COLOR_YELLOW = "COLOR_YELLOW"
    COLOR_BLUE = "COLOR_BLUE"
    COLOR_BROWN = "COLOR_BROWN"
    COLOR_GOLD = "COLOR_GOLD"
    COLOR_VIOLET = "COLOR_VIOLET"
    COLOR_STEEL = "COLOR_STEEL"
    COLOR_AZURE = "COLOR_AZURE"
    COLOR_ORANGE = "COLOR_ORANGE"
    COLOR_WOOD = "COLOR_WOOD"
    COLOR_POPPY = "COLOR_POPPY"
    COLOR_GRANAT = "COLOR_GRANAT"
    COLOR_BLOOD = "COLOR_BLOOD"


from uuid import uuid4

from sanic import Blueprint
from sanic.response import json

from rbac.common.task import Task
from rbac.server.api.auth import authorized
from rbac.server.api import utils
from rbac.server.db import tasks_query
from rbac.server.db.relationships_query import fetch_relationships

TASKS_BP = Blueprint("tasks")

@TASKS_BP.get("api/tasks")
@authorized()
async def get_all_tasks(request):
    """Get all tasks."""
    head_block = await utils.get_request_block(request)
    start, limit = utils.get_request_paging_info(request)
    task_resources = await tasks_query.fetch_all_task_resources(
        request.app.config.DB_CONN, start, limit
    )
    return await utils.create_response(
        request.app.config.DB_CONN,
        request.url,
        task_resources,
        head_block,
        start=start,
        limit=limit,
    )

@TASKS_BP.post("api/tasks")
@authorized()
async def create_new_task(request):
    """Create a new task."""
    required_fields = ["name", "administrators", "owners"]
    utils.validate_fields(required_fields, request.json)

    txn_key, txn_user_id = await utils.get_transactor_key(request)
    task_id = str(uuid4())
    batch_list = Task().batch_list(
        signer_keypair=txn_key,
        signer_user_id=txn_user_id,
        task_id=task_id,
        name=request.json.get("name"),
        admins=request.json.get("administrators"),
        owners=request.json.get("owners"),
        metdata=request.json.get("metadata"),
    )
    await utils.send(
        request.app.config.VAL_CONN, batch_list, request.app.config.TIMEOUT
    )
    return create_task_response(request, task_id)

@TASKS_BP.get("api/tasks/<task_id>")
@authorized()
async def get_task(request, task_id):
    """Get a specific task by task_id."""
    head_block = await utils.get_request_block(request)
    task_resource = await tasks_query.fetch_task_resource(
        request.app.config.DB_CONN, task_id
    )
    return await utils.create_response(
        request.app.config.DB_CONN, request.url, task_resource, head_block
    )

@TASKS_BP.post("api/tasks/<task_id>/admins")
@authorized()
async def add_task_admin(request, task_id):
    """Propose add a task admin."""
    required_fields = ["id"]
    utils.validate_fields(required_fields, request.json)

    txn_key, txn_user_id = await utils.get_transactor_key(request)
    proposal_id = str(uuid4())
    approver = await fetch_relationships("task_admins", "task_id", task_id).run(
        request.app.config.DB_CONN
    )
    batch_list = Task().admin.propose.batch_list(
        signer_keypair=txn_key,
        signer_user_id=txn_user_id,
        proposal_id=proposal_id,
        task_id=task_id,
        next_id=request.json.get("id"),
        reason=request.json.get("reason"),
        metadata=request.json.get("metadata"),
        assigned_approver=approver,
    )
    await utils.send(
        request.app.config.VAL_CONN, batch_list, request.app.config.TIMEOUT
    )
    return json({"proposal_id": proposal_id})

@TASKS_BP.post("api/tasks/<task_id>/owners")
@authorized()
async def add_task_owner(request, task_id):
    """Propose add a task owner."""
    required_fields = ["id"]
    utils.validate_fields(required_fields, request.json)

    txn_key, txn_user_id = await utils.get_transactor_key(request)
    proposal_id = str(uuid4())
    approver = await fetch_relationships("task_admins", "task_id", task_id).run(
        request.app.config.DB_CONN
    )
    batch_list = Task().owner.propose.batch_list(
        signer_keypair=txn_key,
        signer_user_id=txn_user_id,
        proposal_id=proposal_id,
        task_id=task_id,
        next_id=request.json.get("id"),
        reason=request.json.get("reason"),
        metadata=request.json.get("metadata"),
        assigned_approver=approver,
    )
    await utils.send(
        request.app.config.VAL_CONN, batch_list, request.app.config.TIMEOUT
    )
    return json({"proposal_id": proposal_id})

def create_task_response(request, task_id):
    """Prepare the json response for create new task."""
    task_resource = {
        "id": task_id,
        "name": request.json.get("name"),
        "owners": request.json.get("owners"),
        "administrators": request.json.get("administrators"),
        "roles": [],
        "proposals": [],
    }

    if request.json.get("metadata"):
        task_resource["metadata"] = request.json.get("metadata")

    return json({"data": task_resource})


import cv2
import numpy as np

def sketch_transform(frame):
    img_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    img_gray_blurred = cv2.GaussianBlur(img_gray, (7, 7), 0)
    img_canny = cv2.Canny(img_gray_blurred, 10, 80)
    _, mask = img_canny_inverted = cv2.threshold(img_canny, 30, 255, cv2.THRESH_BINARY_INV)
    return mask

cap = cv2.VideoCapture(0)
while cap.isOpened():
    ret, frame = cap.read()
    showCrosshair = False
    fromCenter = False
    r = cv2.selectROI("Image", frame, fromCenter, showCrosshair)
    break

while True:
    _, img_frame = cap.read()
    rect_img = img_frame[int(r[1]):int(r[1]+r[3]), int(r[0]):int(r[0]+r[2])]

    sketch_rect = rect_img
    sketch_rect = sketch_transform(sketch_rect)

    sketch_rect_rgb = cv2.cvtColor(sketch_rect, cv2.COLOR_GRAY2RGB)
    img_frame[int(r[1]):int(r[1]+r[3]), int(r[0]):int(r[0]+r[2])] = sketch_rect_rgb

    cv2.imshow("sketch", img_frame)

    if cv2.waitKey(1) == ord("q"):
        break

cap.release()
cv2.destroyAllWindows()


# -*- coding: utf-8 -*-
# Definition for singly-linked list.
#这个题目利用了list的反转函数。
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    def reversePrint(self, head):
        vaild_num =[]
        aa= head
        while aa:
            vaild_num.append(aa.val)
            aa=aa.next
        vaild_num.reverse()
        return vaild_num

a1 = ListNode(1)
b1 = ListNode(3)
c1 = ListNode(2)

a1.next=b1
b1.next=c1
ss = Solution()
print(ss.reversePrint(a1))


import numpy as np
import qoplots.qoplots as qoplots
qoplots.init("rose_pine", doc_type = "presentation")
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import time
from fractions import Fraction as Frac

def wave(idx, w, x_max, x):
    x_local = x + x_max * idx
    if idx % 2 == 1:
        x_local = x_local[::-1]
    y = np.sin(2 * np.pi * x_local / w) * (-1) ** (idx)
    return y

color = qoplots.Scheme.accents[0].base.css
fig = plt.figure()
fig.set_size_inches(8, 4.5)
ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
fig.add_axes(ax)
ax.set_xlim(0, 1)
ax.set_ylim(-4, 4)

N_data = 2
N_show = 2
max_frames = 2000

plt.axhline(1, color = qoplots.Scheme.foreground[3].css, ls = "--")
plt.axhline(-1, color = qoplots.Scheme.foreground[3].css, ls = "--")

lines = [None] * N_show
total = None
x_max = 1
x = np.linspace(0, x_max, 300)
a = 1 / (8 * np.pi)
f = 20 * np.pi
t = np.linspace(0, 1, max_frames // 2)
ws = 1/(a * (t * f - np.sin(t * f)) + 0.5)
ws = np.concatenate((ws, ws[::-1]))
y = np.zeros((N_data, len(x)))
for i in range(N_data):
    y[i] = wave(i, ws[0], x_max, x)
    if i < N_show:
        lines[i], = plt.plot(x, y[i] + 1, color = color, lw = 1, alpha = (1-i/N_show), ls = "solid" if i == 0 else ":")

y_total = np.sum(y, axis = 0)
# normalise
y_total = y_total / N_data
total, = plt.plot(x, y_total - 1, lw = 2, alpha = 1, ls = "solid", color = qoplots.Scheme.accents[1].base.css)

textlambda = plt.text(0.825, 2.5, "$\lambda =$", color = qoplots.Scheme.accents[4].base.css, ha = "right", va = "center", fontsize = 20)
text = plt.text(0.9, 2.5, "$2L$", color = qoplots.Scheme.accents[4].base.css, ha = "right", va = "center", fontsize = 20)

def update(frame):

    # return to the start of the line after printing
    def bar(f, max_f, width = 50):
        completed = int(f / max_f * width)
        return f"[{'=' * completed}{' ' * (width - completed)}]"
    def format_time(remaining):
        if remaining < 60:
            return f"{remaining:.2f}s"
        elif remaining < 3600:
            return f"{remaining / 60:.0f}:{remaining % 60:0>2.0f}"
        else:
            return f"{remaining / 3600:.2f}h"

    current_time = time.time()
    elapsed_time = current_time - start_time
    remaining_time = elapsed_time / (frame + 1) * (max_frames - frame - 1)
    print(f"Frame: {frame: >3d} / {max_frames} {bar(frame, max_frames)} ({frame / max_frames * 100:.2f}%, {format_time(remaining_time)})", end = "\x1b[0G", flush = True)

    w = ws[frame]
    frac = Frac(w).limit_denominator(300)
    if frac.denominator == 1:
        text.set_text(f"${frac.numerator if frac.numerator != 1 else ''}L$")
    else:
        text.set_text(f"$\\frac{{{frac.numerator}}}{{{frac.denominator}}}L$")

    y = np.zeros((N_data, len(x)))
    for i in range(N_data):
        y[i] = wave(i, w, x_max, x)
        if i < N_show:
            lines[i].set_data(x, y[i] + 1)

    y_total = np.sum(y, axis = 0)
    # normalise
    y_total = y_total / N_data
    total.set_data(x, y_total - 1)

start_time = time.time()
anim = FuncAnimation(fig, update, frames=np.arange(0, max_frames), interval=20)
anim.save('animation.mp4', fps=30, dpi = 400)


import gettext
import multiprocessing as MPR
import os
import os.path as osp
import queue
import re
import signal
import tempfile
import time
import traceback
from functools import partial
from glob import glob
from subprocess import PIPE, Popen

import code_aster.Messages
from code_aster.Messages import UTMESS, MessageLog
from code_aster.Utilities import convert, is_int
from code_aster.Utilities import localization as LO

ENCODING = "utf-8"
VALUES = MessageLog.default_args.copy()
VALUES['ktout'] = 'xxxxxx'

REI = re.compile(r'%\(i[0-9]+\)[\.0-9\-\+ ]*([a-zA-Z])', re.M)
RER = re.compile(r'%\(r[0-9]+\)[\.0-9\-\+ ]*([a-zA-Z])', re.M)
REK = re.compile(r'%\(k[0-9]+\)[\.0-9\-\+]*([a-zA-Z])', re.M)
RE1 = re.compile(r'%\((.[^0-9].*?)\)[\.0-9\-\+ ]*[a-zA-Z]', re.M)
RE2 = re.compile(r'%\(([^irk].*?)\)[\.0-9\-\+ ]*[a-zA-Z]', re.M)

RE_UNAUTH = [
    re.compile(r'([*#=\+\-!/\?<>&@]{4})', re.M),
]

try:
    import aster
    from code_aster.Cata.Syntax import _F
    from code_aster.Commands import CREA_TABLE, TEST_TABLE
    loginfo = partial(aster.affiche, 'MESSAGE')
except ImportError:
    def loginfo(msg):
        print(msg)
logdbg = None

class Checker(object):
    """A simple checker object."""
    def __init__(self, aspell):
        self.err = []
        self.wrn = []
        self.allwarns = []
        self.lang = None
        self.mod = None
        self.words = set()
        self.allwords = set()
        self.ignored = set()
        self.aspell = aspell

    def set_ignored(self, errors):
        self.ignored = set(errors)

    def set_current_lang(self, lang):
        self.lang = lang

    def set_current_mod(self, mod):
        self.mod = mod

    def info(self, msg):
        loginfo("<%s> %s: %s" % (self.lang, self.mod, msg))

    def error(self, error):
        self.err.append((self.lang, self.mod, error))

    def warning(self, error):
        self.wrn.append((self.lang, self.mod, error))

    def warning_spell(self, idmess, words):
        msg = "%s: unknown words %s" % (idmess, tuple(words))
        if set(words).difference(self.ignored):
            self.warning(msg)
            self.words.update(words)
        self.allwarns.append((self.lang, self.mod, msg))
        self.allwords.update(words)

    def count_errors(self):
        return len(self.err)

    def count_unknown_words(self):
        return len(self.get_unknown_words())

    def get_errors(self):
        txt = ["<%s> %s: %s" % err for err in self.err]
        return os.linesep.join(txt)

    def get_warnings(self):
        txt = ["<%s> %s: %s" % wrn for wrn in self.wrn]
        return os.linesep.join(txt)

    def get_all_warnings(self):
        txt = ["<%s> %s: %s" % wrn for wrn in self.allwarns]
        return os.linesep.join(txt)

    def get_unknown_words(self):
        lw = list(self.words)
        lw.sort()
        return lw

    def get_all_words(self):
        lw = list(self.allwords)
        lw.sort()
        return lw

    def get_modules(self):
        smod = set([mod for lang, mod, err in self.err])
        lmod = list(smod)
        lmod.sort()
        return lmod

def read_file(stream, queue):
    """Read on stream and put lines into queue"""
    while True:
        line = stream.readline().strip()
        logdbg and logdbg("aspell outputs: %r" % line)
        queue.put(line)

class AspellCall(object):
    """A pipe to call aspell"""
    def __init__(self, personal_dict, lang, encoding):
        """Open the pipe"""
        cmd = ['aspell', 'pipe',
               '--encoding=%s' % encoding,'--lang=%s' % lang]
        if personal_dict and osp.exists(personal_dict):
            cmd.append('--personal=%s' % personal_dict)
        logdbg and logdbg('command: ' + ' '.join(cmd))
        self.lang = lang
        self.pipe = Popen(cmd, stdin=PIPE, stdout=PIPE, universal_newlines=True)
        self.inp = self.pipe.stdin
        self.out = self.pipe.stdout
        self.queue = MPR.Queue()
        self.rdr = MPR.Process(target=read_file, args=(self.out, self.queue))
        self.rdr.start()
        init = self.queue.get()
        assert 'aspell' in init.lower(), ('aspell probably failed to start: \n'
                                          '%s' % init)
        # all characters except alphabetic ones are separators
        self._rxspl = re.compile(r'[ _0123456789\W]+', re.M | re.I | re.UNICODE)

    def __del__(self):
        """Close the pipe"""
        self.close()

    def close(self):
        """Close the pipe"""
        self.inp.close()
        self.out.close()
        self.rdr.terminate()
        self.pipe.terminate()

    def _clean(self, string):
        """clean 'string' before passing it to aspell"""
        if type(string) is not str:
            string = str(string, ENCODING)
        uwords = self._rxspl.split(string.strip())
        uwords = [i for i in uwords if i and not i[0].isdigit()]
        string = ' '.join([i for i in uwords if len(i) > 1])
        return string.strip()

    def send(self, string):
        """Send 'string' and return the response of aspell"""
        string = self._clean(string)
        logdbg and logdbg('check: %r' % string)
        words = string.split()
        logdbg and logdbg('words: %r' % words)
        resp = []
        if not words:
            return words, resp
        self.inp.write(string + os.linesep)
        self.inp.flush()
        while True:
            try:
                resp.append(self.queue.get_nowait())
                #logdbg and logdbg('returns: %r' % resp[-1])
            except queue.Empty:
                if len(resp) == 0 or resp[-1] != '':
                    continue
                break
        if len(resp) != len(words) + 1:
            loginfo("warning: expected answer of aspell:\n words=%r\n resp=%r"
                    % (words, resp))
        #logdbg and logdbg('resp: %r' % zip(words, resp))
        return words, resp

    def check(self, string):
        """Return the unknown words"""
        words, resp = self.send(string)
        unknown = [w for w, r in zip(words, resp) if r.strip() != '*']
        return unknown

def get_cata_msg(catamess):
    """Import a messages file"""
    import importlib
    cata_msg = {}
    try:
        d = {}
        mod = __import__("code_aster.Messages.%s" % catamess, d, d, [catamess])
        importlib.reload(mod)
        cata_msg = getattr(mod, 'cata_msg', {})
    except UnicodeDecodeError:
        UTMESS('F', 'CATAMESS_1',
               valk=("Encodage invalide pour le fichier de messages : '%s'" % catamess,
                     traceback.format_exc()))
    except Exception:
        UTMESS('F', 'CATAMESS_1',
               valk=("Nom du fichier de messages : '%s'" % catamess,
                     traceback.format_exc()))
    return cata_msg

def check_format(checker, catamess, idmess, msg, typ):
    """Check the format used for the given type 'typ'."""
    dre = {
        'integer' : (REI, ['d', 'i']),
        'real'    : (RER, ['f', 'g', 'e', 'F', 'G', 'E']),
        'string'  : (REK, ['s', ]),
        'other1'   : (RE1, ['ktout', ]),
        'other2'   : (RE2, []),
    }
    expr, l_auth = dre[typ]
    arg = expr.findall(msg)
    unauth = set(arg).difference(l_auth)
    if len(unauth) > 0:
        checker.error("%s : invalid format for type '%s' : %s" % (idmess, typ, tuple(unauth)))

def check_msg(checker, catamess, msg, key, lang):
    """Check a message."""
    idmess = "%s_%s" % (catamess, key)
    # check type : a string expected
    if type(msg) is not str:
        checker.error(lang, "%s has a wrong type" % idmess)
    if msg.strip() == '' and catamess != "vide":
        checker.error("%s is empty : use VIDE_1 for that!" % idmess)
    assert is_int(key), 'unexpected key : %s' % key
    # check unauthorized formatting
    for re_unauth in RE_UNAUTH:
        mat = re_unauth.search(msg)
        if mat:
            checker.info("unrecommanded formatting in %s : %s" % (idmess, mat.groups()))
    # check formatting
    txt = None
    try:
        txt = msg % VALUES
    except Exception as exc:
        trace = repr(exc)
        checker.error("%s can not be formatted :\nmessage: %r\n%s"
                      % (idmess, msg, trace))
    # check arguments
    for typ in ('integer', 'real', 'string', 'other1', 'other2'):
        check_format(checker, catamess, idmess, msg, typ)
    if txt and lang == 'fr':
        logdbg and logdbg('idmess: %s' % idmess)
        unknown = checker.aspell.check(txt)
        if unknown:
            idmess = "%s_%s" % (catamess, key)
            unknown = [convert(word) for word in unknown]
            checker.warning_spell(idmess, unknown)

_pws = None
def get_personal_dict():
    """Build the dictionnary for Code_Aster : keywords + personal dict."""
    global _pws
    if _pws:
        return _pws
    cnt = ['personal_ws-1.1 fr 0 %s' % ENCODING, ]
    dictdir = osp.join(os.environ['HOME'], 'dev', 'codeaster', 'devtools',
                       'share', 'spell')
    cata = osp.join(dictdir, 'code_aster_cata.aspell.per')
    if osp.exists(cata):
        # ignore the first line
        with open(cata, 'r') as fper:
            cnt.extend(fper.read().splitlines()[1:])
    else:
        raise IOError("no such file: {0}\nAn updated devtools repository is "
                      "required!".format(cata))
    cawl = osp.join(dictdir, 'code_aster_dict.aspell.per')
    if osp.exists(cawl):
        # ignore the first line
        with open(cawl, 'r') as fper:
            cnt.extend(fper.read().splitlines()[1:])
    else:
        raise IOError("no such file: {0}\nAn updated devtools repository is "
                      "required!".format(cawl))
    fd, _pws = tempfile.mkstemp(dir=os.getcwd())
    with open(fd, 'w') as fobj:
        fobj.write(os.linesep.join([line for line in cnt if line.strip()]))
    return _pws

def check_catamess(checker, lang, l_cata):
    """Check all the messages files"""
    checker.set_current_lang(lang)
    checker.set_current_mod('-')
    loginfo("<i18n> lang=%s, domain=%s, localedir=%s" % (LO.current_lang, LO.domain, LO.localedir))
    tr = LO.translation(lang)
    if lang != 'fr' and not isinstance(tr, gettext.GNUTranslations):
        checker.warning("no translation object for language '%s'" % lang)
        return
    pwl = get_personal_dict()
    if not pwl and lang == 'fr':
        checker.error("Code_Aster personal dict not found: %s" % pwl)
    for catamess in l_cata:
        checker.set_current_mod(catamess)
        loginfo("<%s> checking %s..." % (lang, catamess))
        if catamess == 'dvp':
            continue
        cata_msg = get_cata_msg(catamess)
        for key, msg in list(cata_msg.items()):
            if type(msg) == dict:
                msg  = msg['message']
            check_msg(checker, catamess, msg, key, lang)

def timekeeper(pid, delay):
    """Kill 'pid' if it times out"""
    time.sleep(delay)
    UTMESS('E', 'CATAMESS_1',
           valk=("""
The process %d timed out after %d seconds.

It probably blocks reading the response of aspell on its stdout...
Try run with INFO=2 to have all the details.
""" % (pid, delay), """Interruption : kill the main process!"""))
    os.kill(pid, signal.SIGTERM)

def supv002_ops(self, ERREUR, **kwargs):
    """Fake macro-command to check messages"""
    global logdbg
    if kwargs.get('INFO') == 2:
        logdbg = loginfo
    # existing errors
    previous_errors = set(ERREUR)
    os.environ['LANG'] = 'fr_FR.utf8'
    # remove all LC_xxxx variables
    keys = [k for k in list(os.environ.keys()) if k.startswith('LC_')]
    for k in keys:
        del os.environ[k]
    msgdir = osp.dirname(code_aster.Messages.__file__)
    LCATA = [osp.basename(osp.splitext(cata)[0]) for cata in glob(osp.join(msgdir, '*.py'))]
    #LCATA = [osp.basename(osp.splitext(cata)[0]) for cata in glob(osp.join(msgdir, 'mecanonline9.py'))]

    # check for installation problem: http://bugs.python.org/issue3770
    do_check = True
    try:
        MPR.Queue()
    except ImportError as exc:
        if 'sem_open implementation' in str(exc):
            do_check = False
            print("\n  <A> Problem detected ! supv002a can not run on this machine\n\n")
    # FIXME temporarly disabled!
    # do_check = False
    if do_check:
        try:
            aspell = AspellCall(get_personal_dict(), 'fr', ENCODING)
            checker = Checker(aspell)
            checker.set_ignored(ERREUR)
            # check default/native messages
            check_catamess(checker, 'fr', LCATA)
            # check translated messages in english
            check_catamess(checker, 'en', LCATA)
            # close aspell
            aspell.close()
        except OSError as exc:
            checker = Checker(None)
            checker.error("Can not start aspell: {0}".format(exc))

        nberr = checker.count_errors()
        errors = checker.get_errors() + """\nNumber of errors : %d""" % nberr
        allw = checker.get_all_words()
        nbwrn = len(allw)
        warns = checker.get_warnings()
        torm = list(previous_errors.difference(allw))
        torm.sort()
        new = checker.get_unknown_words()
        nbnew = len(new)
    else:
        nberr = nbnew = 0
        nbwrn = len(previous_errors)
        warns = ''
        errors = []
        torm = []

    if kwargs.get('unittest'):
        print(warns)
        print(errors)
        return 1

    if nberr > 0:
        UTMESS('A', 'CATAMESS_1', valk=("%6d erreurs" % nberr, errors))
    if kwargs.get('INFO') == 2:
        warns = checker.get_all_warnings()
    if warns:
        UTMESS('A', 'CATAMESS_1',
               valk=("Liste des alarmes et des erreurs par message", warns))
    if nbnew > 0:
        UTMESS('A', 'CATAMESS_1',
               valk=("Liste des nouvelles erreurs introduites à corriger :",
                     str(new)))
    if torm:
        UTMESS('A', 'CATAMESS_1',
               valk=("Liste des erreurs qui n'apparaissent plus "
                     "(à supprimer du mot-clé ERREUR) :", str(torm)))

    __tab = CREA_TABLE(LISTE=_F(PARA='NBERR', LISTE_I=nberr))

    TEST_TABLE(REFERENCE='ANALYTIQUE',
               VALE_CALC_I=0,
               VALE_REFE_I=0,
               NOM_PARA='NBERR',
               TABLE=__tab,)

    __tnew = CREA_TABLE(LISTE=_F(PARA='NBNEW_ERR', LISTE_I=nbnew))

    TEST_TABLE(REFERENCE='ANALYTIQUE',
               VALE_CALC_I=0,
               VALE_REFE_I=0,
               NOM_PARA='NBNEW_ERR',
               TABLE=__tnew,)

    __tabw = CREA_TABLE(LISTE=_F(PARA='NBWARN', LISTE_I=nbwrn))

    TEST_TABLE(CRITERE='ABSOLU',
               REFERENCE='ANALYTIQUE',
               VALE_CALC_I=len(previous_errors),
               VALE_REFE_I=0,
               PRECISION=len(previous_errors),
               NOM_PARA='NBWARN',
               TABLE=__tabw,
               )
    return

if __name__ != '__main__':
    from code_aster.Cata.Syntax import MACRO, SIMP
    from code_aster.Supervis.ExecuteCommand import UserMacro
    supv_cata = MACRO(nom='SUPV002', op=supv002_ops,
                      ERREUR = SIMP(statut='o',typ='TXM', max='**',),
                      INFO = SIMP(statut='f',typ='I', defaut=1, into=(1, 2),),
    )
    SUPV002 = UserMacro("SUPV002", supv_cata, supv002_ops)
else:
    # run as unittest
    # PYTHONPATH=$PYTHONPATH:/home/courtois/dev/codeaster/install/std/lib/python3.6/site-packages
    # ASTER_ROOT=/opt/aster
    # python -i astest/supv002a.33
    #logdbg = loginfo
    #aspell = AspellCall(get_personal_dict(), 'fr', 'utf-8')
    #unk = aspell.check("On ne peut pas vérifier les fotes d'orthografe.")
    #assert len(unk) == 2, unk
    #del aspell
    supv002_ops(None, [], unittest=True)


from django.urls import path
from . import views
from . import tkinter4

app_name = 'login'

urlpatterns = [
    path('', views.index, name='index'),
    path('auth/', views.auth, name='auth'),
    path('<str:user_id>/<int:box_id>/', views.box_detail, name='box_detail'),
    path('<str:user_id>/<int:box_id>/<int:traffic_id>', views.traffic_detail, name='traffic_detail'),
    path('image_slide/', views.traffic, name='image_slide'),
    path('gin/', views.traffic2, name='gin'),

    # path('kakao/', views.kakao, name='kakao'),
]


"""
Exercise:
Use recursion to write a function that accepts an array of numbers and
returns a new array containing just the even numbers.
"""

# Solution:
def extract_even(arr):
    # base case
    if len(arr) == 0:
        return []

    # recursive case
    if arr[0] % 2 == 0:
        return [arr[0]] + extract_even(arr[1:])
    else:
        return extract_even(arr[1:])

# test the function
my_arr = [1, 2, 3, 4, 5, 6]
result = extract_even(my_arr)
print(result)  # [2, 4, 6]

my_second_arr = [1, 3, 5, 7, 9, 10]
result = extract_even(my_second_arr)
print(result)  # [10]


#variables
name = 'julie'
print(name)

name = 'suarez'
age = 20
height_in_cm = 100.0
weight_in_kg = 60
print(name,height_in_cm,weight_in_kg)

first_name = 'julie'
last_name = 'suarez'
print(first_name + last_name )

my_text = 'I love Nalugya Vanessa to'
number = 100
print(my_text + str(100))

Dorm = 'Mpaga'
name1 = 'Julie'
name2 = 'Vanesa'
name3 = 'Angel'
name4 = 'Trinity'
#print(f'The Mpaga called{Dorm} has girls like{name1},{name2},{name3} and {name4}' )

#input

#dictionaries
capitals = {'uganda':'kampala','kenya':'nairobi','tanzania':'dodoma'}
print(capitals.keys())
print(capitals.values())
print(capitals.get('uganda'))
print(capitals.pop('kenya'))
print(capitals.popitem())


from re import A
from android import python_act
from android.runnable import run_on_ui_thread
from jnius import autoclass, cast

from android_api import activity, SDK_INT

AndroidString = autoclass('java.lang.String')
Context = autoclass('android.content.Context')
NotificationBuilder = autoclass('android.app.Notification$Builder')
NotificationManager = autoclass('android.app.NotificationManager')
Drawable = autoclass("{}.R$mipmap".format(activity.getPackageName()))
PendingIntent = autoclass('android.app.PendingIntent')
Intent = autoclass('android.content.Intent')
Toast = autoclass('android.widget.Toast')
BitmapFactory = autoclass('android.graphics.BitmapFactory')

class AndroidNotification():

	def __init__(self):
		self._ns = None
		self._channel_id = activity.getPackageName()

	def _get_notification_service(self):
		if not self._ns:
			self._ns = cast(NotificationManager, activity.getSystemService(
				Context.NOTIFICATION_SERVICE
			))
		return self._ns

	def _build_notification_channel(self, name):

		if SDK_INT < 26:
			return
		channel = autoclass('android.app.NotificationChannel')
		app_channel = channel(
			self._channel_id, name, NotificationManager.IMPORTANCE_DEFAULT
		)
		self._get_notification_service().createNotificationChannel(
			app_channel
		)
		return app_channel

	@run_on_ui_thread
	def _toast(self, message):
		Toast.makeText(
			activity,
			cast('java.lang.CharSequence', AndroidString(message)),
			Toast.LENGTH_LONG
		).show()

	@staticmethod
	def _set_icons(notification, icon=None):
		app_icon = Drawable.icon
		notification.setSmallIcon(app_icon)

		bitmap_icon = app_icon
		if icon is not None:
			bitmap_icon = BitmapFactory.decodeFile(icon)
			notification.setLargeIcon(bitmap_icon)
		elif icon == '':
			# we don't want the big icon set,
			# only the small one in the top panel
			pass
		else:
			bitmap_icon = BitmapFactory.decodeResource(
				python_act.getResources(), app_icon
			)
			notification.setLargeIcon(bitmap_icon)

	def _build_notification(self, title):
		if SDK_INT < 26:
			noti = NotificationBuilder(activity)
		else:
			self._channel = self._build_notification_channel(title)
			noti = NotificationBuilder(activity, self._channel_id)
		return noti

	@staticmethod
	def _set_open_behavior(notification):
		app_context = activity.getApplication().getApplicationContext()
		notification_intent = Intent(app_context, python_act)
		notification_intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
		notification_intent.setAction(Intent.ACTION_MAIN)
		notification_intent.addCategory(Intent.CATEGORY_LAUNCHER)

		pending_intent = PendingIntent.getActivity(
			app_context, 0, notification_intent, 0
		)
		notification.setContentIntent(pending_intent)
		notification.setAutoCancel(True)

	def _open_notification(self, notification):
		if SDK_INT >= 16:
			notification = notification.build()
		else:
			notification = notification.getNotification()

		self._get_notification_service().notify(0, notification)

	def _notify(self, **kwargs):
		noti = None
		message = kwargs.get('message').encode('utf-8')
		ticker = kwargs.get('ticker').encode('utf-8')
		title = AndroidString(
			kwargs.get('title', '').encode('utf-8')
		)
		icon = kwargs.get('app_icon')
		if kwargs.get('toast'):
			self._toast(message)
			return
		else:
			noti = self._build_notification(title)

		noti.setContentTitle(title)
		noti.setContentText(AndroidString(message))
		noti.setTicker(AndroidString(ticker))
		self._set_icons(noti, icon=icon)
		self._set_open_behavior(noti)

		self._open_notification(noti)

def instance():
	return AndroidNotification()

def notify(title='', message='', app_icon='', app_name='', timeout=10, ticker='', toast=False):
	my_notification = AndroidNotification()
	my_notification._notify(title=title, message=message,
		app_icon=app_icon, app_name=app_name,
		timeout=timeout, ticker=ticker, toast=toast)


# Jordan Ristow
# Implementing a stack using a linked list

class LinkedStack:

    class _Node:
        __slots__ = '_element', '_next'

        def __init__(self, element, next):
            self._element = element
            self._next = next

    def __init__(self):
        self._head = None
        self._size = 0

    def __len__(self):
        return self._size

    def isEmpty(self):
        return self._size == 0

    def push(self, e):
        self._head = self._Node(e, self._head)
        self._size += 1

    def top(self):
        if self.isEmpty():
            raise ValueError('Stack is empty')
        return self._head._element

    def pop(self):
        if self.isEmpty():
            raise ValueError('Stack is empty')
        value = self._head._element
        self._head = self._head._next
        self._size -= 1
        return value

def main():
    s = LinkedStack()  #creates the stack
    print('Is the stack empty? ', s.isEmpty())  #Testing the isEmpty function
    repetitions = int(input('How many elements do you want to add to the stack? '))
    for i in range(repetitions):  #building up the stack based on the number of elements the user selected
        n = int(input('Please enter a number to add to the stack: '))
        s.push(n)

    print('What value is at the top of the stack? ',s.top()) #Returns the top value of the stack without deleting it

    for i in range(repetitions): #decomposing the stack by popping the first value off one by one
        print('Current length of the stack: ', s.__len__())
        print('Top value: ', s.pop())

    print('What value is at the top of the stack?')
    s.top() #testing the empty ValueError of the top function

main()


import pandas as pd # Biblioteca usada para criação de dataframe

quakes = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/earthquakes-23k.csv') # Cria o dataframe com dados do arquivo

import plotly.graph_objects as go # Chama o módulo go

# Cria a figura e suas configurações
fig = go.Figure(go.Densitymapbox(lat=quakes.Latitude, lon=quakes.Longitude, z=quakes.Magnitude, # Chama os dados de lat/long
                                 radius=10))
fig.update_layout(mapbox_style="stamen-terrain", mapbox_center_lon=180) # Adiciona no layout o estilo do mapa
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}) # Configura no layout as proporções
fig.show() # Plota o gráfico

Plotly/Mapa de Calor no Globo Terrestre/Exemplo 2/MAPA DE CALOR COM MAPAS ex.2.py


import os
import time
import subprocess

# 定义要检查的文件名
file_name = "mp_auth.json"

# 获取文件的最后修改时间
if os.path.exists(file_name):
    file_stat = os.stat(file_name)
    file_mtime = file_stat.st_mtime

    # 计算文件的最后修改时间与当前时间的差值，以小时为单位
    current_time = time.time()
    time_difference_hours = (current_time - file_mtime) / 3600

    # 如果文件的最后修改时间超过82小时，则执行 main.py
    if time_difference_hours > 82:
        subprocess.run(["python3", "main.py"])
else:
    print(f"文件 {file_name} 不存在")


##Intergers

# 0b10 -> binary

# 0o10 -> Octal

# 0x10 -> hex

# int(3.5) = 3  "cast to integer"

##floats float(7) -> 7.0

## None = null

## bool True False

# bool(0) = False any other is True

if True:
    print("It is True")

h = 42
if h > 50:
    print("Greater than 50")
else:
    print("Less than or equal to 50")

c = 5
while c != 0:
    print(c)
    c -= 1

## if remainder is 0 then the program stops

while True:
    response = input()
    if int(response) % 7 == 0:
        break


# -*- coding: utf-8 -*-
from flask import Blueprint, request, redirect, jsonify, g, render_template
from common.libs.Helper import ops_render, getCurrentDate, iPagination, getDictField
from application import app, db
from common.models.news.News import News
from common.libs.UrlManager import UrlManager
from decimal import Decimal
from sqlalchemy import or_
route_news = Blueprint( 'news_page',__name__ )

# 通知首页
@route_news.route( "/index" )
def index():
    resp_data = {}
    req = request.values
    page = int(req['p']) if ('p' in req and req['p']) else 1  # 当前页号，默认为1
    query = News.query

    # 用户搜索处理
    if 'mix_kw' in req:
        rule = or_(News.name.ilike("%{0}%".format(req['mix_kw'])),
                   News.tags.ilike("%{0}%".format(req['mix_kw'])))  # 需要导入 sqlalchemy import or_   or 查询
        # ilike 不区分大小写 对用户名或手机号查询 or为混合查询
        query = query.filter(rule)
    # 用户账号有效无效查询
    if 'status' in req and int(req['status']) > -1:
        query = query.filter(News.status == int(req['status']))

    if 'cat_id' in req and int(req['cat_id']) > 0:
        query = query.filter(News.cat_id == int(req['cat_id']))

    # 分页参数
    page_params = {
        'total': query.count(),  # 统计账号总数
        'page_size': app.config['PAGE_SIZE'],  # 每页显示账号数
        'page': page,
        'display': app.config['PAGE_DISPLAY'],  # 展示总页数
        'url': request.full_path.replace("&p={}".format(page), "")
    }
    pages = iPagination(page_params)  # 分页操作
    offset = (page - 1) * app.config['PAGE_SIZE']  # 偏移量，第二页从50开始，第三页从101开始
    list = query.order_by(News.id.desc()).offset(offset).limit(app.config['PAGE_SIZE']).all()  # 使用uid字段倒序排  # .all()  为取出所有的数据 然后存到列表list

    # cat_mapping = getDictField(FoodCat,"id","id",[])
    resp_data['list'] = list
    resp_data['pages'] = pages
    resp_data['search_con'] = req  # 搜索框内容
    resp_data['status_mapping'] = app.config['STATUS_MAPPING']
    # resp_data['cat_mapping'] = cat_mapping
    resp_data['current'] = 'index'
    return ops_render( "news/index.html", resp_data)

# 通知内容详情展示页面
@route_news.route( "/info" )
def info():
    resp_data = {}
    req = request.args  # 参数多时用values ,参数少时用args
    id = int(req.get("id",0))
    reback_url = UrlManager.buildUrl("/news/index")
    if id < 1:
        return redirect(reback_url)
    info = News.query.filter_by(id=id).first()
    if not info:
        return redirect(reback_url)

    # stock_change_list = FoodStockChangeLog.query.filter(FoodStockChangeLog.food_id == id).order_by(FoodStockChangeLog.id.desc()).all()
    resp_data['info'] = info
    # resp_data['stock_change_list'] = stock_change_list
    resp_data['current'] = 'index'
    return ops_render( "news/info.html", resp_data )

# 通知详情编辑
@route_news.route( "/set",methods=["GET", "POST"] )
def set():
    if request.method == "GET":
        resp_data = {}
        req = request.args  # 参数多时用values ,参数少时用args
        id = req['id'] if 'id' in req else 0
        info = News.query.filter_by(id=id).first()
        if info and info.status !=1:
            return redirect(UrlManager.buildUrl("/news/index"))

        resp_data['info'] = info
        resp_data['current'] = 'index'
        return ops_render("news/set.html", resp_data)

    # 下面是POST处理
    resp = {'code': 200, 'msg': '操作成功', 'data': ''}
    req = request.values                                    # 参数多时用values ,参数少时用args
    id = req['id'] if 'id' in req else 0                    # 获取当前用户id
    name = req['name'] if 'name' in req else ''
    summary = req['summary'] if 'summary' in req else ''
    tags = req['tags'] if 'tags' in req else ''

    if name is None or len(name) < 1:
        resp['code'] = -1
        resp['msg'] = "请输入符合规范的名称"
        return jsonify(resp)
    if summary is None or len(summary) < 3:
        resp['code'] = -1
        resp['msg'] = "请输入符合规范的内容描述"
        return jsonify(resp)
    food_info = News.query.filter_by(id=id).first()
    if food_info:
        model_food = food_info
    else:
        model_food = News()
        model_food.status = 1
        model_food.created_time = getCurrentDate()

    model_food.name = name
    model_food.summary = summary
    model_food.tags = tags
    model_food.updated_time = getCurrentDate()
    db.session.add(model_food)
    try:
        db.session.commit()
    except Exception as e:
        print(e)
        resp['code'] = -1
        resp['msg'] = "提交数据库出错"
        db.session.rollback()

    return jsonify(resp)

# 删除恢复通知操作
@route_news.route("/ops", methods=["POST"])
def ops():
    resp = {'code': 200, 'msg': '操作成功', 'data': ''}
    req = request.values

    id = req['id'] if 'id' in req else 0
    act = req['act'] if 'act' in req else ''
    if not id:
        resp['code'] = -1
        resp['msg'] = "请选择要操作的通知"
        return jsonify(resp)
    if act not in ['remove', 'recover']:
        resp['code'] = -1
        resp['msg'] = "操作有误，请重试"
        return jsonify(resp)

    food_info = News.query.filter_by(id=id).first()
    if not food_info:
        resp['code'] = -1
        resp['msg'] = "指定通知不存在，请重试"
        return jsonify(resp)
    if act == "remove":
        food_info.status = 0
    elif act == "recover":
        food_info.status = 1

    # 提交信息
    food_info.update_time = getCurrentDate()
    db.session.add(food_info)
    db.session.commit()
    return jsonify(resp)


"""
This module is the main application entry point
"""
import sys
import os
path = sys.argv[0].split("\\")[:-1]
os.chdir("\\".join(path))
from gui.gui import GUI

def run_app(argv):
    """RUN!!!"""
    app = GUI(debug=True)
    if len(argv) > 1 and ".bci" in argv[1]:
        app.run_file(argv[1])
    else:
        app.run()

run_app(sys.argv)


import os
from pydub import AudioSegment
import threading

genres = 'blues classical country disco hiphop metal pop reggae rock'
genres = genres.split()

class myThread(threading.Thread):
    def __init__(self, genre):
        threading.Thread.__init__(self)
        self.genre = genre

    def run(self):
        print("Started " + self.genre)
        split_files(self.genre)
        print("Finished " + self.genre)

def split_files(g):
    j = 0
    for filename in os.listdir(os.path.join('Z:/Egyetem/önlab2_msc/raw_audio/yt_dataset_2', f"{g}")):
        song = os.path.join(f'Z:/Egyetem/önlab2_msc/raw_audio/yt_dataset_2/{g}', f'{filename}')
        j = j + 1
        for w in range(0, 1000):
            t1 = 3 * (w) * 1000
            t2 = t1 + 6000
            new_audio = AudioSegment.from_wav(song)
            new = new_audio[t1:t2]
            new.export(f'Z:/Egyetem/önlab2_msc/splitted_audiofiles/audio6sec_overlap3s_yt_dataset2/{g}/{g + str(j) + str(w)}.wav',
                       format="wav")

threads = []
for g in genres:
    threads.append(myThread(g))

for t in threads:
    t.start()


from odoo import api, fields, models

class L10nEsVatBookLine(models.Model):
    _name = "l10n.es.vat.book.line"
    _description = "Spanish VAT book line"
    _order = "exception_text asc, entry_number asc, invoice_date asc, ref asc"

    def _selection_special_tax_group(self):
        return self.env["l10n.es.vat.book.line.tax"].fields_get(
            allfields=["special_tax_group"]
        )["special_tax_group"]["selection"]

    ref = fields.Char("Reference")
    entry_number = fields.Integer()
    external_ref = fields.Char("External Reference")

    line_type = fields.Selection(
        selection=[
            ("issued", "Issued"),
            ("received", "Received"),
            ("rectification_issued", "Refund Issued"),
            ("rectification_received", "Refund Received"),
        ],
    )
    invoice_date = fields.Date()

    partner_id = fields.Many2one(comodel_name="res.partner", string="Empresa")
    vat_number = fields.Char(string="NIF")

    vat_book_id = fields.Many2one(comodel_name="l10n.es.vat.book", string="Vat Book id")

    move_id = fields.Many2one(comodel_name="account.move", string="Invoice")

    move_id = fields.Many2one(comodel_name="account.move", string="Journal Entry")
    tax_line_ids = fields.One2many(
        comodel_name="l10n.es.vat.book.line.tax",
        inverse_name="vat_book_line_id",
        string="Tax Lines",
        copy=False,
    )

    exception_text = fields.Char()

    base_amount = fields.Float(
        string="Base",
    )
    total_amount = fields.Float(
        string="Total",
    )
    special_tax_group = fields.Selection(
        selection=_selection_special_tax_group,
        string="Special group",
        help="Special tax group as R.Eq, IRPF, etc",
    )

    @api.depends("tax_id")
    def _compute_tax_rate(self):
        for rec in self:
            rec.tax_rate = rec.tax_id.amount


"""
Author: Rambod Azimi
This Python code will make use of numpy and scikit-learn libraries to implement and utilize the Logistic Regression
machine learning algorithm.
"""

import numpy as np
from sklearn.linear_model import LogisticRegression

# defining our training set examples
X_train = np.array([[0.5, 1.5], [1, 1], [1.5, 0.5], [3, 0.5], [2, 2], [1, 2.5]]) # 6 training examples with 2 features
y_train = np.array([0, 0, 0, 1, 1, 1]) # targets

logistic_regression_model = LogisticRegression()
logistic_regression_model.fit(X_train, y_train) # fit the model on the training data by caliing fit function

prediction = logistic_regression_model.predict(X_train)

for i in range(X_train.shape[0]):
    print(f"Prediction value: {prediction[i]} \t Actual value: {y_train[i]}")

print(f"Accuracy of the model: {logistic_regression_model.score(X_train, y_train)* 100}%")


from flask import request
from flask_restful import Resource
from app.models import db, Customers, CustomersSchema

customers_schema = CustomersSchema(many=True)
customer_schema = CustomersSchema()

class CustomersResource(Resource):
    def get(self):
        key = request.args.get('key')

        if key:
            customers = Customers.query.filter(Customers.type==key).all()
        else:
            customers = Customers.query.all()

        customers = customers_schema.dump(customers).data
        return {'status': 'success', 'message': customers[0]['values']}, 200


from librec_auto.core.cmd import Cmd
from librec_auto.core.util import Files
from librec_auto.core import ConfigCmd
from librec_auto.core.util import Status, StudyStatus
import optuna
import joblib
import numpy as np

class OptimizationFunction():
    def __init__(self, type, new_value, old_value, fairness, acceptable_threshold = 0.9):
        self.type = type
        self.store = {"additive":self.additive, "multiplicative":self.multiplicative, "exponential":self.exponential}
        self.new_value = new_value
        self.old_value = old_value
        self.fairness = fairness
        self.acceptable_threshold = acceptable_threshold

        self.store[type]()

    def additive(self):
        self.value = max(0,(self.new_value - self.acceptable_threshold*float(self.old_value))) + self.fairness

    def multiplicative(self):
        self.value = min(1,(1-(self.acceptable_threshold*float(self.old_value)-self.new_value))) * self.fairness

    def exponential(self):
        self.value = max(0,(self.new_value - self.acceptable_threshold*float(self.old_value))) ** self.fairness

class TellCmd(Cmd):
    def __init__(self, args, config, current_exp_no, study, trial, metric, direction, old_librec_value_command = None, new_val = None, optimize_val = None, files = None, optimization_type = "additive"):
        # print("inside tell")
        self.config = config
        self.args = args
        self.current_exp_no = current_exp_no
        self.study = study
        self.trial = trial
        self.metric = metric
        self.direction = direction
        self.status = 3
        self.files = files
        self.optimization_type = optimization_type
        self.title_map = {'auc': 'AUCEvaluator', 'ap': 'AveragePrecisionEvaluator','arhr': 'AverageReciprocalHitRankEvaluator','diversity': 'DiversityEvaluator',
        'hitrate': 'HitRateEvaluator','idcg': 'IdealDCGEvaluator','ndcg': 'NormalizedDCGEvaluator',
        'precision': 'PrecisionEvaluator', 'recall': 'RecallEvaluator', 'rr': 'ReciprocalRankEvaluator',
        'featurediversity': 'DiversityByFeaturesEvaluator', 'novelty': 'NoveltyEvaluator', 'entropy': 'EntropyEvaluator',
        'icov': 'ItemCoverageEvaluator', 'dppf': 'DiscountedProportionalPFairnessEvaluator', 'dpcf': 'DiscountedProportionalCFairnessEvaluator',
        'giniindex': 'GiniIndexEvaluator', 'mae': 'MAEEvaluator','mpe': 'MPEEvaluator','mse': 'MSEEvaluator','rmse': 'RMSEEvaluator',
        'csp': 'CStatisticalParityEvaluator', 'psp': 'PStatisticalParityEvaluator','miscalib': 'MiscalibrationEvaluator','nonpar': 'NonParityUnfairnessEvaluator','valunfairness': 'ValueUnfairnessEvaluator',
        'absunfairness'
        : 'AbsoluteUnfairnessEvaluator','overestimate': 'OverestimationUnfairnessEvaluator','underestimate': 'UnderestimationUnfairnessEvaluator','ppr': 'PPercentRuleEvaluator'
        }

        self.old_librec_value_command = old_librec_value_command

        self.optimize_val = optimize_val

        self.new_val = new_val

    def __str__(self):
        return f"TellCmd()"

    def show(self):
        print(str(self))

    def dry_run(self, config):
        print(f'librec-auto (DR): Running Tell command {self}')

    def get_data(self):
        store_val = ''

        i = 0
        for sub_paths in self.config._files.get_exp_paths_iterator():

            study_status = StudyStatus(self.config)
            if self.optimize_val is not None:
                old_val = self.optimize_val
                store_val = None
                store_psp = None
                if self.metric in self.title_map:
                    for sub_paths in self.config._files.get_exp_paths_iterator():

                        if i != self.current_exp_no:
                            i += 1
                            continue
                        status = Status(sub_paths)
                        store_val = status.get_metric_info(status._log, BBO = True)[self.title_map[self.metric]]
                        store_psp = status.get_metric_info(status._log, BBO = True)[self.title_map["psp"]]
                        break

                store_new_val = store_val
                print(store_val, store_psp,"get_data")
                # store_val = max(0,(store_new_val - 0.95*float(old_val))) + self.new_val._previous_status["psp.py"]
                value_object = OptimizationFunction(self.optimization_type, store_new_val,old_val, store_psp)
                store_val = value_object.value
                s = str(self.config._files.get_exp_paths(self.current_exp_no)._path_dict["output"])[:-10] + "output_combo.txt"

                with open(s,"w+") as f:
                    f.write(str(store_val))
            else:
                if self.metric in self.title_map:
                    for sub_paths in self.config._files.get_exp_paths_iterator():

                        if i != self.current_exp_no:
                            i += 1
                            continue
                        status = Status(sub_paths)
                        store_val = status.get_metric_info(status._log, BBO = True)[self.title_map[self.metric]]
                        break
                else:
                    # print(study_status._experiments.values())
                    # for exp in study_status._experiments.values():
                    #     print(exp._metric_avg)

                    store_val = study_status.get_metric_averages(self.metric)[0]
            break

        return float(store_val)

    def execute(self, command):
        data = self.get_data()
        pruned_trial = False
        if self.trial.should_prune():
            pruned_trial = True

        file_num = str(self.current_exp_no)
        while len(file_num) < 5:
            file_num = "0" + str(file_num)
        path = str(self.files._study_path) + "/exp" + file_num
        joblib.dump(self.study, path+ "/study.pkl")
        if pruned_trial:
            self.study.tell(self.trial, state=optuna.trial.TrialState.PRUNED)
        else:
            self.study.tell(self.trial, data)


# Construct a Trie from a Collection of Patterns
import itertools as it
import sys

from collections import defaultdict

class Trie:
    def __init__(self, patterns):
        self._node_id_generator = it.count()
        self._root = next(self._node_id_generator)
        self._adjacency_list = self._build_trie_from_patterns(patterns)

    def edges(self):
        for node, neighbours in self._adjacency_list.items():
            for neighbour, label in neighbours:
                yield f'{node}->{neighbour}:{label}'

    def _build_trie_from_patterns(self, patterns):
        adjacency_list = defaultdict(list)
        for pattern in patterns:
            self._add_pattern_to_trie(adjacency_list, pattern)
        return dict(adjacency_list)

    def _add_pattern_to_trie(self, adjacency_list, pattern):
        current_node = self._root
        for letter in pattern:
            node = _get_neighbour_with_given_label(adjacency_list, current_node, letter)
            if node is None:
                next_node = next(self._node_id_generator)
                adjacency_list[current_node].append((next_node, letter))
                current_node = next_node
            else:
                current_node = node

def _get_neighbour_with_given_label(adjacency_list, current_node, letter):
    neighbours = adjacency_list.get(current_node, [])
    for node, label in neighbours:
        if label == letter:
            return node
    return None

def main():
    patterns = sys.stdin.read().splitlines()
    trie = Trie(patterns)
    for edge in trie.edges():
        print(edge)

if __name__ == '__main__':
    main()


import numpy as np
import scipy.signal as signal
import scipy.ndimage as ndimg
# import cupyx.scipy.ndimage as cim
# import cupy as cp
from definitions import BITSHIFT
import cv2

shift_left_float = lambda arr: (arr*(2**BITSHIFT)).astype('int64')
shift_left_int = lambda arr: (arr.astype('int64') << BITSHIFT)
shift_right_float = lambda arr: (arr/(2**BITSHIFT)).astype('int64')
shift_right_int = lambda arr: (arr.astype('int64') >> BITSHIFT)

# def kernel_filter(img_, kernel_, confun='space'):
#     img = shift_left_int(img_)
#     kernel = shift_left_float(kernel_)
#     if confun=="fft":
#         result = signal.fftconvolve(img,
#                                        kernel,
#                                        mode='same')
#     else:
#         result = signal.fftconvolve(img, kernel, mode='same')
#     result = shift_right_int(result)
#     return result

def kernel_filter(img, kernel, confun='space'):
    # img = shift_left_int(img_)
    # kernel = shift_left_float(kernel)
    result = cv2.filter2D(img, -1, kernel)
    # result = shift_right_int(result)
    return result

# def detect_edges(img, kernel, threshold, confun='space'):
#     edges = kernel_filter(img, kernel, confun)
#     # edges = np.abs(edges)
#     mask = edges > (threshold * edges.mean())
#     return edges, mask

def detect_edges(img, threshold, dir='h'):
    if dir=='h':
        edges = cv2.Sobel(img, cv2.CV_32F, 1, 0)
    elif dir=='v':
        edges = cv2.Sobel(img, cv2.CV_32F, 0, 1)
    edges = cv2.convertScaleAbs(edges)
    mask = edges > (threshold * edges.mean())
    return edges, mask

# def detect_edges(img, min, max):
#     edges = cv2.Canny(img, min, max)
#     return edges


import numpy as np
import math
from matplotlib import pyplot as plt
from sklearn.preprocessing import normalize, StandardScaler
from sklearn import datasets

def main2d():
	means = (0, 0)
	cov = np.array([[1.9, 1.2], [1.2, 1.9]])
	A = np.random.multivariate_normal(means, cov, 100).transpose()
	C = normalize(A)
	eig_value, eig_vectors = np.linalg.eig(np.cov(C))
	Z = eig_vectors.T.dot(C)
	print(np.cov(Z))
	fig = plt.figure(1)
	ax = fig.add_subplot()
	ax.scatter(C[0], C[1], c='b', marker='.')
	len0 = math.sqrt(math.fabs(eig_value[0]))
	len1 = math.sqrt(math.fabs(eig_value[1]))
	t = np.linspace(0, 1, 100)
	x_v = t * eig_vectors[0][0] * len0
	y_v = t * eig_vectors[1][0] * len0
	ax.plot(x_v, y_v, linewidth=5, c='r')
	x_v = t * eig_vectors[0][1] * len1
	y_v = t * eig_vectors[1][1] * len1
	ax.plot(x_v, y_v, linewidth=5, c='r')
	ax.set_xlabel('x')
	ax.set_ylabel('y')
	ax.set_title('данные и собсвенные вектора матрицы ковариации')
	plt.show()

def main3d():
	means = (0, 0, 0)
	cov = np.array([[7, 5, 3], [5, 9, 3], [3, 3, 5]])
	A = np.random.multivariate_normal(means, cov, 100).transpose()
	C = normalize(A)
	print(np.cov(C))
	print(np.mean(C, axis=1))
	eig_value, eig_vectors = np.linalg.eig(np.cov(C))
	Z = eig_vectors.T.dot(C)
	print(np.cov(Z))
	fig = plt.figure(1)
	ax = fig.add_subplot(111, projection='3d')
	ax.scatter(C[0], C[1], C[2], c='b', marker='.')
	len0 = math.sqrt(math.fabs(eig_value[0]))
	len1 = math.sqrt(math.fabs(eig_value[1]))
	len2 = math.sqrt(math.fabs(eig_value[2]))
	t = np.linspace(0, 1, 100)
	x_ev = t * eig_vectors[0][0] * len0
	y_ev = t * eig_vectors[1][0] * len0
	z_ev = t * eig_vectors[2][0] * len0
	ax.plot(x_ev, y_ev, z_ev, linewidth=1, c='r')
	x_ev = t * eig_vectors[0][1] * len1
	y_ev = t * eig_vectors[1][1] * len1
	z_ev = t * eig_vectors[2][1] * len1
	ax.plot(x_ev, y_ev, z_ev, linewidth=1, c='r')
	x_ev = t * eig_vectors[0][2] * len2
	y_ev = t * eig_vectors[1][2] * len2
	z_ev = t * eig_vectors[2][2] * len2
	ax.plot(x_ev, y_ev, z_ev, linewidth=1, c='r')
	ax.set_xlabel('x')
	ax.set_ylabel('y')
	ax.set_zlabel('z')
	ax.set_title('данные и собсвенные вектора матрицы ковариации')
	plt.show()

def german_process():
	n = 1000
	p = 24
	matrix = np.loadtxt('../data/german.data-numeric')
	cols = [i for i in range(p)]
	classes = []
	col_i = 0
	A = np.zeros((p, n))
	for i in range(n):
		for j in range(p):
			A[j][col_i] = matrix[i][cols[j]]
		col_i += 1
		classes.append(matrix[i][p])
	A = StandardScaler().fit_transform(A.transpose()).transpose()
	eig_value, eig_vectors = np.linalg.eig(np.cov(A))
	Z = eig_vectors.T.dot(A)
	print(np.cov(Z))
	fig = plt.figure(2)
	ax = fig.add_subplot(111, projection='3d')
	colors = ['r', 'b']
	cov_x = np.cov(A)
	cov_z = np.cov(Z)
	d_x = []
	d_z = []
	for i in range(p):
		d_x.append(cov_x[i][i])
		d_z.append(cov_z[i][i])
	for i in range(n):
		ax.scatter(Z[0][i], Z[1][i], Z[2][i], c=colors[int(classes[i] - 1)], marker='.')
	[print("{:.5f}".format(f), end=' ') for f in sorted(d_x, key=lambda x: -x)]
	print()
	[print("{:.5f}".format(f), end=' ') for f in sorted(d_z, key=lambda x: -x)]
	print()
	print(sum(d_z))
	plt.title('данные german')
	plt.show()

def iris_process():
	iris = datasets.load_iris()
	A = StandardScaler().fit_transform(X=np.array(iris.data[:, :])).transpose()
	classes = iris.target
	eig_value, eig_vectors = np.linalg.eig(np.cov(A))
	Z = eig_vectors.T.dot(A)
	points = Z.transpose()

	print("A:")
	print(np.cov(A))
	print(np.cov(Z))
	mtr = np.identity(4)
	for i in range(4):
		mtr[i][i] = math.sqrt(eig_value[i])
	print(eig_vectors.dot(mtr))

	colors = ['r', 'g', 'b']
	markers = ['*', 'o', '^']
	for i in range(points.shape[0]):
		x = points[i]
		c = classes[i]
		plt.plot(x[0], x[1], f'{colors[c]}{markers[c]}')
	plt.title('данные Iris')
	plt.show()

if __name__ == "__main__":
	main2d()
	main3d()
	german_process()
	iris_process()


"""
Скрипт для генерации контента произведения исскуства
"""
from pymongo import MongoClient

DOC_IDS = "_id"
DOC_EXTERNAL_IDS = "external_ids"
DOC_DOCS = "docs"
DOC_NAME = 'name'
DOC_TYPE = 'type'
DOC_AUTHOR = 'author'
DOC_DATE = 'date'
DOC_AUDIO = 'audio'
DOC_IMG = 'img'
DOC_DESCRIPTION = 'description'

CONNECTION = MongoClient()
ARTS = CONNECTION['pushkin']['art']

try:
    print("Добавляем элементы датасета!")
    while True:
        print("=========================")
        name = input("Введите название: ")
        art_type = input("Введите тип предмета: ")
        ids = input("Введите id предмета: ")
        author = input("Введите автора предмета: ")
        date = input("Введите дату создания: ")
        audio = input("Введите ссылку на аудио запись: ")
        img = input("Введите ссылку на картинку: ")
        description = input("Введите краткое описание")
        docs = []
        while True:
            command = input("Введите ключевую фразу или Enter для выхода")
            if not command:
                break
            docs.append(command)
        art = {
            DOC_EXTERNAL_IDS: ids,
            DOC_NAME: name,
            DOC_TYPE: art_type,
            DOC_AUTHOR: author,
            DOC_AUDIO: audio,
            DOC_IMG: img,
            DOC_DATE: date,
            DOC_DESCRIPTION: description,
            DOC_DOCS: docs
        }
        print("ADDING: ", art)
        ARTS.insert_one(art)
        print("ADDED")
except KeyboardInterrupt:
    CONNECTION.close()


import pandas as pd
from rdflib import URIRef, BNode, Literal, Graph
from rdflib.namespace import RDF, RDFS, FOAF, XSD
from rdflib import Namespace
import numpy as np
import math
import sys
import argparse
import json
import html
import requests
from openpyxl import Workbook, load_workbook
from openpyxl.styles import Font
import openpyxl

with open("/Users/nakamurasatoru/git/d_sat/u-renja/static/iiif2/collection/top.json") as f:
    df2 = json.load(f)

manifests = df2["manifests"]

images = {}

for m in manifests:
    metadata = m["metadata"]

    num = -1
    identifier = ""

    for obj in metadata:
        if "Description" in obj["label"]:
            num = obj["value"][0].split("通番: ")[1]

            num = int(num)

        elif "identifier" in obj["label"]:
            identifier = obj["value"]

    if num not in images:
        images[num] = []

    images[num].append({
        "id": m["@id"],
        "identifier": identifier
    })

'''
for num in images:
    print(num, images[num])
'''

#############

url = "https://taishozo.github.io/db/iiif/kandomokuroku/manifest.json"

df = requests.get(url).json()

indexMap = {}

canvases = df["sequences"][0]["canvases"]
for i in range(len(canvases)):
    c = canvases[i]
    res = c["images"][0]["resource"]["@id"]
    if "p." in res:
        p = res.split("p.")[1].split(".")[0]
        indexMap[p] = i + 1

'''
for p in indexMap:
    print(p, indexMap[p], int(p) - int(indexMap[p]))
'''

hyperlink = Font(underline='single', color='0563C1')

def read_excel(path):
    # df = pd.read_excel(path, sheet_name=0, header=None, index_col=None, engine="openpyxl")

    wb = load_workbook(path)
    ws = wb.active

    r_count = ws.max_row
    c_count = ws.max_column

    map = {}

    for i in range(0, c_count):
        label = ws.cell(row=1+1, column=i+1).value
        map[i] = label

    for j in range(2, r_count):
        id = ws.cell(row=j+1, column=0+1).value

        if id == "" or id == None:
            continue

        経典番号 =  ws.cell(row=j+1, column=1+1).value
        枝番 = ws.cell(row=j+1, column=9+1).value
        if 枝番 == None:
            枝番 = ""

        e1 = ws.cell(row=j+1, column=8+1).value if ws.cell(row=j+1, column=8+1).value != None else ws.cell(row=j+1, column=8+1).value # ""
        e2 = ws.cell(row=j+1, column=10+1).value if ws.cell(row=j+1, column=10+1).value != None else ws.cell(row=j+1, column=10+1).value # ""
        e3 = ws.cell(row=j+1, column=11+1).value if ws.cell(row=j+1, column=11+1).value != None else ws.cell(row=j+1, column=11+1).value # ""
        e4 = ws.cell(row=j+1, column=12+1).value if ws.cell(row=j+1, column=12+1).value != None else ws.cell(row=j+1, column=12+1).value # ""
        e5 = ws.cell(row=j+1, column=13+1).value if ws.cell(row=j+1, column=13+1).value != None else ws.cell(row=j+1, column=13+1).value # ""

        uri_sat = "https://21dzk.l.u-tokyo.ac.jp/SAT2018/"+e1+枝番+"_."+str(e2).zfill(2)+"."+str(e3).zfill(4)+e4+str(e5).zfill(2)+".html"

        title = ws.cell(row=j+1, column=3+1).value

        ws.cell(row=j+1, column=3+1).value = '=HYPERLINK("{}", "{}")'.format(uri_sat, title)
        ws.cell(row=j+1, column=3+1).font = hyperlink

        num1 = ws.cell(row=j+1, column=114+1).value #df.iloc[j, 114]
        if num1 != "" and num1 != None:
            ws.cell(row=j+1, column=114+1).value = '=HYPERLINK("https://taishozo.github.io/u-renja/search/?main[refinementList][通番]={}", "{}")'.format(num1, num1)
            ws.cell(row=j+1, column=114+1).font = hyperlink

            # df.iloc[j, 114] = "[{}]({})".format(num1, num1)

        num2 = ws.cell(row=j+1, column=118+1).value #df.iloc[j, 114]
        if num2 != "" and num2 != None:
            ws.cell(row=j+1, column=118+1).value = '=HYPERLINK("https://taishozo.github.io/u-renja/search/?main[refinementList][通番]={}", "{}")'.format(str(num2).zfill(4), num2)
            ws.cell(row=j+1, column=118+1).font = hyperlink

        kando = ws.cell(row=j+1, column=122+1).value #df.iloc[j, 114]
        if kando != "" and kando != None:
            pos = int(kando) - 152
            ws.cell(row=j+1, column=122+1).value = '=HYPERLINK("http://codh.rois.ac.jp/software/iiif-curation-viewer/demo/?manifest=https://taishozo.github.io/db/iiif/kandomokuroku/manifest.json&pos={}", "{}")'.format(pos, int(kando))
            ws.cell(row=j+1, column=122+1).font = hyperlink

        # 画像リンク
        for c in range(0, 3):

            folder1 = ws.cell(row=j+1, column=127+1 + 4 * c).value #df.iloc[j, 114]

            if folder1 != "" and folder1 != None:

                x = ws.cell(row=j+1, column=128+1 + 4 * c).value
                y = ws.cell(row=j+1, column=129+1 + 4 * c).value

                # print(folder1, x, y)

                uuid1 = folder1 + "_" + str(x).zfill(4) + "_" + str(y).zfill(4)

                num1 = int(num1)

                num = num1

                if uuid1 == "u-renja1524-1525_0003_0005":
                    num += 1

                if uuid1 == "u-renja1663-1668_0015_0017":
                    num += 1

                if uuid1 == "u-renja1663-1668_0017_0019":
                    num += 2

                if num in images:

                    value1 = ""

                    arr = images[num]

                    for a in arr:
                        if uuid1 == a["identifier"]:
                            value1 = a["id"]

                    if value1 == "":
                        print("missing", uuid1, "value1", value1, "num", num)
                    else:
                        ws.cell(row=j+1, column=126+1 + 4 * c).value = '=HYPERLINK("http://codh.rois.ac.jp/software/iiif-curation-viewer/demo/?manifest={}", "{}")'.format(value1, ws.cell(row=j+1, column=126+1 + 4 * c).value)
                        ws.cell(row=j+1, column=126+1 + 4 * c).font = hyperlink
                        # df.iloc[j, 126] = "[{}]({})".format(df.iloc[j, 126], value1)

    wb.save('../static/metadata/data.xlsx')

with open("config.json") as f:
    config = json.load(f)

filename = config["filename"]

path = "data/" + filename

# data1 = read_excel(path)
read_excel(path)


# Practice Problem : Twitter Sentiment Analysis by Analytics Vidhya

#The objective of this task is to detect hate speech in tweets. Formally, given a training sample of tweets and labels,
#where label ‘1’ denotes the tweet is racist/sexist and label ‘0’ denotes the tweet is not racist/sexist, your objective
# is to predict the labels on the given test dataset.
#Note: The evaluation metric from this practice problem is F1-Score.

#Importing Libraries
import re #cleaning the text
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import string
import nltk
import warnings

#NLTK is a leading platform for building Python programs to work with human language data

#Importing dataset
dataset = pd.read_csv('train.csv')
testdata = pd.read_csv('test.csv')

#To see the first few rows of the train dataset
dataset.head()

dataset.info()

#breakdown of how many tweets are ‘0’s and how many tweets are ‘1’s.
dataset['label'].value_counts()

#Initial data cleaning requirements that we can think of after looking at the top 5 records:
#The Twitter handles are already masked as @user due to privacy concerns. So, these Twitter handles are hardly giving any information about the nature of the tweet.
#We can also think of getting rid of the punctuations, numbers and even special characters since they wouldn’t help in differentiating different kinds of tweets.
#Most of the smaller words do not add much value. For example, ‘pdx’, ‘his’, ‘all’. So, we will try to remove them as well from our data.
#Once we have executed the above three steps, we can split every tweet into individual words or tokens which is an essential step in any NLP task.
#In the 4th tweet, there is a word ‘love’. We might also have terms like loves, loving, lovable, etc. in the rest of the data. These terms are often used in the same
#context. If we can reduce them to their root word, which is ‘love’, then we can reduce the total number of unique words in our data without losing a significant amount of information.

#Tweets Preprocessing and Cleaning
#The objective of this step is to clean noise those are less relevant to find the sentiment of tweets such as punctuation,
#special characters, numbers, and terms which don’t carry much weightage in context to the text.

#a user-defined function to remove unwanted text patterns from the tweets.
# The function returns the same input string but without the given pattern

#combine train and test set
combi = dataset.append(testdata, ignore_index=True)

## importing regular expression library ## clean tweet text by removing links, special characters etc
def remove_pattern(input_txt, pattern):
    r = re.findall(pattern, input_txt)
    for i in r:
        input_txt = re.sub(i, '', input_txt)

    return input_txt

# remove twitter handles (@user)
combi['tweet'] = np.vectorize(remove_pattern)(combi['tweet'], "@[\w]*")

# remove special characters, numbers, punctuations
combi['tweet'] = combi['tweet'].str.replace("[^a-zA-Z#]", " ")

#Removing Short Words
combi['tweet'] = combi['tweet'].apply(lambda x: ' '.join([w for w in x.split() if len(w)>3]))

#Tokenization is the act of breaking up a sequence of strings into pieces such as words, keywords, phrases, symbols and other elements called tokens.
#Tokens can be individual words, phrases or even whole sentences. In the process of tokenization, some characters like punctuation marks are discarded
tokenized_tweet = combi['tweet'].apply(lambda x: x.split())

#Stemming is a rule-based process of stripping the suffixes (“ing”, “ly”, “es”, “s” etc) from a word.
#For example, For example – “play”, “player”, “played”, “plays” and “playing” are the different variations of the word – “play”.

from nltk.stem.porter import *
stemmer = PorterStemmer()

tokenized_tweet = tokenized_tweet.apply(lambda x: [stemmer.stem(i) for i in x]) # stemming

#Now let’s stitch these tokens
for i in range(len(tokenized_tweet)):
    tokenized_tweet[i] = ' '.join(tokenized_tweet[i])

combi['tweet'] = tokenized_tweet

#---------------------------------------------------------------------------------------------------------------

##Story Generation and Visualization from Tweets
#A wordcloud is a visualization wherein the most frequent words appear in
#large size and the less frequent words appear in smaller sizes.

all_words = ' '.join([text for text in combi['tweet']])
from wordcloud import WordCloud
wordcloud = WordCloud(width=800, height=500, random_state=21, max_font_size=110).generate(all_words)

plt.figure(figsize=(10, 7))
plt.imshow(wordcloud, interpolation="bilinear")

#Words in non racist/sexist tweets
normal_words =' '.join([text for text in combi['tweet'][combi['label'] == 0]])

wordcloud = WordCloud(width=800, height=500, random_state=21, max_font_size=110).generate(normal_words)
plt.figure(figsize=(10, 7))
plt.imshow(wordcloud, interpolation="bilinear")

#Racist/Sexist Tweets
negative_words = ' '.join([text for text in combi['tweet'][combi['label'] == 1]])
wordcloud = WordCloud(width=800, height=500,random_state=21, max_font_size=110).generate(negative_words)
plt.figure(figsize=(10, 7))
plt.imshow(wordcloud, interpolation="bilinear")

#Understanding the impact of Hashtags on tweets sentiment
#Hashtags in twitter are synonymous with the ongoing trends on twitter at any particular point in time.
# function to collect hashtags
def hashtag_extract(x):
    hashtags = []
    # Loop over the words in the tweet
    for i in x:
        ht = re.findall(r"#(\w+)", i)
        hashtags.append(ht)

    return hashtags

# extracting hashtags from non racist/sexist tweets
HT_regular = hashtag_extract(combi['tweet'][combi['label'] == 0])

# extracting hashtags from racist/sexist tweets
HT_negative = hashtag_extract(combi['tweet'][combi['label'] == 1])

# unnesting list
HT_regular = sum(HT_regular,[])
HT_negative = sum(HT_negative,[])

#Non-Racist/Sexist Tweets

a = nltk.FreqDist(HT_regular)
d = pd.DataFrame({'Hashtag': list(a.keys()),
                  'Count': list(a.values())})
# selecting top 10 most frequent hashtags
d = d.nlargest(columns="Count", n = 10)
plt.figure(figsize=(13,7))
ax = sns.barplot(data=d, x= "Hashtag", y = "Count")

#Racist/Sexist Tweets

b = nltk.FreqDist(HT_negative)
e = pd.DataFrame({'Hashtag': list(b.keys()), 'Count': list(b.values())})
# selecting top 10 most frequent hashtags
e = e.nlargest(columns="Count", n = 10)
plt.figure(figsize=(13,7))
ax = sns.barplot(data=e, x= "Hashtag", y = "Count")

#As expected, most of the terms are negative with a few neutral terms as well.
#So,it’s not a bad idea to keep these hashtags in our data as they contain useful information.

#-------------------------------------------------------------------------------------------------

#Extracting Features from Cleaned Tweets

#Bag-of-Words Features
#Bag-of-Words features can be easily created using sklearn’s CountVectorizer function.
#We will set the parameter max_features = 1000 to select only top 1000 terms ordered by term frequency across the corpus.

from sklearn.feature_extraction.text import CountVectorizer
bow_vectorizer = CountVectorizer(max_df=0.90, min_df=2, max_features=1000, stop_words='english')
# bag-of-words feature matrix
bow = bow_vectorizer.fit_transform(combi['tweet'])

#TF-IDF Features
from sklearn.feature_extraction.text import TfidfVectorizer
tfidf_vectorizer = TfidfVectorizer(max_df=0.90, min_df=2, max_features=1000, stop_words='english')
# TF-IDF feature matrix
tfidf = tfidf_vectorizer.fit_transform(combi['tweet'])

#Building log model using Bag-of-Words features
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score

train_bow = bow[:31962,:]
test_bow = bow[31962:,:]

# splitting data into training and validation set
xtrain_bow, xvalid_bow, ytrain, yvalid = train_test_split(train_bow, dataset['label'], random_state=42, test_size=0.3)

lreg = LogisticRegression()
lreg.fit(xtrain_bow, ytrain) # training the model

prediction1 = lreg.predict_proba(xvalid_bow) # predicting on the validation set
prediction_int1 = prediction1[:,1] >= 0.3 # if prediction is greater than or equal to 0.3 than 1 else 0
prediction_int1 = prediction_int1.astype(np.int)

f1_score(yvalid, prediction_int1) # calculating f1 score
#f1 = 0.53078

# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(yvalid, prediction_int1)
cm
prediction_test = lreg.predict_proba(test_bow) # predicting on the testset
prediction_test = prediction_test[:,1] >= 0.3 # if prediction is greater than or equal to 0.3 than 1 else 0
prediction_test = prediction_test.astype(np.int)

#Export submission file

testdata['label'] = prediction_test
submission = testdata[['id','label']]
submission.to_csv('sub_log_bow.csv', index=False) # writing data to a CSV file

#Building log model using TF-IDF features
train_tfidf = tfidf[:31962,:]
test_tfidf = tfidf[31962:,:]

xtrain_tfidf = train_tfidf[ytrain.index]
xvalid_tfidf = train_tfidf[yvalid.index]

lreg.fit(xtrain_tfidf, ytrain)

prediction2 = lreg.predict_proba(xvalid_tfidf)
prediction_int2 = prediction2[:,1] >= 0.3
prediction_int2 = prediction_int2.astype(np.int)

f1_score(yvalid, prediction_int2)
#f1 = 0.54465
prediction_test = lreg.predict_proba(test_bow) # predicting on the testset
prediction_test = prediction_test[:,1] >= 0.3 # if prediction is greater than or equal to 0.3 than 1 else 0
prediction_test = prediction_test.astype(np.int)

#Export submission file

testdata['label'] = prediction_test
submission = testdata[['id','label']]
submission.to_csv('sub_log_tfidf.csv', index=False) # writing data to a CSV file

# K-Nearest Neighbors (K-NN)
#Building log model using Bag-of-Words features
# Fitting K-NN to the Training set
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors = 5, metric = 'minkowski', p = 2)
knn.fit(xtrain_bow, ytrain)
# Predicting the Test set results
y_pred = knn.predict_proba(xvalid_bow)
prediction_int3 = y_pred[:,1] >= 0.3 # if prediction is greater than or equal to 0.3 than 1 else 0
prediction_int3 = prediction_int3.astype(np.int)
f1_score(yvalid, prediction_int3)
#f1 = 0.43578
#Building model using TF-IDF features

knn.fit(xtrain_tfidf, ytrain)
y_pred2 = knn.predict_proba(xvalid_tfidf)
prediction_int4 = y_pred2[:,1] >= 0.3
prediction_int4 = prediction_int4.astype(np.int)

f1_score(yvalid, prediction_int4)
#f1 = 0.45124

# Naive Bayes
## Error - A sparse matrix was passed, but dense data is required.
##Use X.toarray() to convert to a dense numpy array.
# Fitting Naive Bayes to the Training set
'''from sklearn.naive_bayes import GaussianNB
nb = GaussianNB()
nb.fit(xtrain_bow, ytrain)
# Predicting the Test set results
y_pred3 = nb.predict_proba(xvalid_bow)
prediction_int5 = y_pred3[:,1] >= 0.3
prediction_int5 = y_pred3.astype(np.int)
f1_score(yvalid, prediction_int5)

#Building model using TF-IDF features
nb.fit(xtrain_tfidf, ytrain)
y_pred4 = nb.predict_proba(xvalid_tfidf)
prediction_int6 = y_pred4[:,1] >= 0.3
prediction_int6 = prediction_int6.astype(np.int)

f1_score(yvalid, prediction_int6)
'''

# Fitting SVM to the Training set
from sklearn.svm import SVC
svm = SVC()
svm.fit(xtrain_bow, ytrain)

#Building model using TF-IDF features
svm.fit(xtrain_tfidf, ytrain)

# Applying k-Fold Cross Validation
from sklearn.model_selection import cross_val_score
accuracies = cross_val_score(estimator = svm, X = xtrain_bow, y = ytrain, cv = 10)
accuracies.mean()
accuracies.std()

# Applying Grid Search to find the best model and the best parameters from bag of words
from sklearn.model_selection import GridSearchCV
parameters = [{'C': [1, 10, 100, 1000], 'kernel': ['linear']},
              {'C': [1, 10, 100, 1000], 'kernel': ['rbf'], 'gamma': [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]}]
grid_search = GridSearchCV(estimator = svm,
                           param_grid = parameters,
                           scoring = 'f1',
                           cv = 10,
                           n_jobs = -1)
grid_search = grid_search.fit(xtrain_bow, ytrain)
best_f1 = grid_search.best_score_
best_parameters = grid_search.best_params_
#best f1 = 0.5953
#best parameters = c = 10, gamma = 0.2, kernel = rbf
#We will again do Grid Search with parametrs close to the above result
# Applying Grid Search to find the best model and the best parameters
from sklearn.model_selection import GridSearchCV
parameters = [
              {'C': [5, 10, 15, 20], 'kernel': ['rbf'], 'gamma': [0.15, 0.16, 0.17, 0.18, 0.19, 0.2, 0.21, 0.22, 0.23, 0.24, 0.25,]}]
grid_search = GridSearchCV(estimator = svm,
                           param_grid = parameters,
                           scoring = 'f1',
                           cv = 10,
                           n_jobs = -1)
grid_search = grid_search.fit(xtrain_bow, ytrain)
best_f1 = grid_search.best_score_
best_parameters = grid_search.best_params_
#best f1 = 0.6013
#best parameters = c = 10, gamma = 0.15, kernel = rbf

from sklearn.model_selection import GridSearchCV
parameters = [
              {'C': [8, 9, 10, 11, 12], 'kernel': ['rbf'], 'gamma': [0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18]}]
grid_search = GridSearchCV(estimator = svm,
                           param_grid = parameters,
                           scoring = 'f1',
                           cv = 10,
                           n_jobs = -1)
grid_search = grid_search.fit(xtrain_bow, ytrain)
best_f1 = grid_search.best_score_
best_parameters = grid_search.best_params_
#best f1 = 0.6213
#best parameters = c = 11, gamma = 0.14, kernel = rbf
#We will go with this parameters

svm = SVC(kernel = 'rbf', random_state = 0, gamma = 0.14, C =11)
svm.fit(xtrain_bow, ytrain)
y_pred5 = svm.predict(xvalid_bow)
prediction_int7 = y_pred5.astype(np.int)
f1_score(yvalid, prediction_int7)

#prediction on test set
test_pred = svm.predict(test_bow)
test_pred_int = test_pred.astype(np.int)
testdata['label'] = test_pred_int
submission = testdata[['id','label']]
submission.to_csv('svmrbfbow.csv', index=False) # writing data to a CSV file

# Applying Grid Search to find the best model and the best parameters from tfidf
from sklearn.model_selection import GridSearchCV
parameters = [{'C': [1, 10, 100, 1000], 'kernel': ['linear']},
              {'C': [1, 10, 100, 1000], 'kernel': ['rbf'], 'gamma': [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]}]
grid_search = GridSearchCV(estimator = svm,
                           param_grid = parameters,
                           scoring = 'f1',
                           cv = 10,
                           n_jobs = -1)
grid_search = grid_search.fit(xtrain_tfidf, ytrain)
best_f1 = grid_search.best_score_
best_parameters = grid_search.best_params_
#best f1 = 0.6146
#best parameters = c = 10, gamma = 0.5, kernel = rbf
#We will again do Grid Search with parametrs close to the above result
# Applying Grid Search to find the best model and the best parameters
from sklearn.model_selection import GridSearchCV
parameters = [
              {'C': [5, 10, 15, 20], 'kernel': ['rbf'], 'gamma': [0.45, 0.46, 0.47, 0.48, 0.49, 0.5, 0.51, 0.52, 0.53, 0.54, 0.55,]}]
grid_search = GridSearchCV(estimator = svm,
                           param_grid = parameters,
                           scoring = 'f1',
                           cv = 10,
                           n_jobs = -1)
grid_search = grid_search.fit(xtrain_tfidf, ytrain)
best_f1 = grid_search.best_score_
best_parameters = grid_search.best_params_
#best f1 = 0.6166
#best parameters = c = 10, gamma = 0.51, kernel = rbf

svm = SVC(kernel = 'rbf', random_state = 0, gamma = 0.51, C =10)
#Building model using TF-IDF features
svm.fit(xtrain_tfidf, ytrain)
y_pred6 = svm.predict(xvalid_tfidf)
prediction_int8 = y_pred6.astype(np.int)
f1_score(yvalid, prediction_int8)

#prediction on test set
test_pred = svm.predict(test_tfidf)
test_pred_int = test_pred.astype(np.int)
testdata['label'] = test_pred_int
submission = testdata[['id','label']]
submission.to_csv('svmrbftfidf.csv', index=False) # writing data to a CSV file

# Fitting Decision Tree Classification to the Training set
from sklearn.tree import DecisionTreeClassifier
tree = DecisionTreeClassifier(criterion = 'entropy', random_state = 0)
tree.fit(xtrain_bow, ytrain)
y_pred7 = tree.predict(xvalid_bow)
prediction_int9 = y_pred7.astype(np.int)
f1_score(yvalid, prediction_int9)
#f1 linear = 0.50385

#Building model using TF-IDF features
tree.fit(xtrain_tfidf, ytrain)
y_pred8 = svm.predict(xvalid_tfidf)
prediction_int10 = y_pred8.astype(np.int)
f1_score(yvalid, prediction_int10)
#f1 linear = 0.3919

#Building random forest model using Bag-of-Words features
from sklearn.ensemble import RandomForestClassifier

rf=RandomForestClassifier(n_estimators=1024,criterion='entropy',random_state=0)
rf.fit(xtrain_bow, ytrain) # training the model

predict_valid = rf.predict_proba(xvalid_bow) # predicting on the validation set
valid_predict_int = predict_valid[:,1] >= 0.3 # if prediction is greater than or equal to 0.3 than 1 else 0
valid_predict_int = valid_predict_int.astype(np.int)
f1_score(yvalid,valid_predict_int) # calculating f1 score
#f1 (n =64) = 0.5427

#prediction on test set
test_pred = rf.predict_proba(test_bow)
test_pred_int = test_pred[:,1] >= 0.3
test_pred_int = test_pred_int.astype(np.int)
testdata['label'] = test_pred_int
submission = testdata[['id','label']]
submission.to_csv('rfbow64.csv', index=False) # writing data to a CSV file

#Building model using TF-IDF features
rf=RandomForestClassifier(n_estimators=128,criterion='entropy',random_state=0)
rf.fit(xtrain_tfidf, ytrain) # training the model

predict_valid = rf.predict_proba(xvalid_tfidf) # predicting on the validation set
valid_predict_int = predict_valid[:,1] >= 0.3 # if prediction is greater than or equal to 0.3 than 1 else 0
valid_predict_int = valid_predict_int.astype(np.int)
f1_score(yvalid,valid_predict_int) # calculating f1 score
#f1 128 = 0.5777

#prediction on test set
test_pred = rf.predict_proba(test_tfidf)
test_pred_int = test_pred[:,1] >= 0.3
test_pred_int = test_pred_int.astype(np.int)
testdata['label'] = test_pred_int
submission = testdata[['id','label']]
submission.to_csv('rf1024.csv', index=False) # writing data to a CSV file

#Emoticons ##Use of emoticons is very prevalent throughout the web, more so on micro- blogging sites.

# Repeating Characters
#People often use repeating characters while using colloquial language,
#like "I’m in a hurrryyyyy", "We won, yaaayyyyy!" As our final pre-processing step,
# we replace characters repeating more than twice as two characters.


from cataelem.Tools.base_objects import LocatedComponents, ArrayOfComponents, SetOfNodes, ElrefeLoc
from cataelem.Tools.base_objects import Calcul, Element
import cataelem.Commons.physical_quantities as PHY
import cataelem.Commons.located_components as LC
import cataelem.Commons.parameters as SP
import cataelem.Commons.mesh_types as MT
from cataelem.Options.options import OP

#----------------
# Modes locaux :
#----------------

DDL_MECA = LocatedComponents(phys=PHY.DEPL_R, type='ELNO',
    components=('DX','DY','DZ',))

CFORCEF  = LocatedComponents(phys=PHY.FORC_F, type='ELEM',
    components=('FX','FY','FZ',))

EFORCNO  = LocatedComponents(phys=PHY.FORC_R, type='ELNO',
    components=('FX','FY','FZ',))

NGEOMER  = LocatedComponents(phys=PHY.GEOM_R, type='ELNO',
    components=('X','Y',))

EGGEOP_R = LocatedComponents(phys=PHY.GEOM_R, type='ELGA', location='RIGI',
    components=('X','Y','W',))

CTEMPSR  = LocatedComponents(phys=PHY.INST_R, type='ELEM',
    components=('INST',))

CPRESSF  = LocatedComponents(phys=PHY.PRES_F, type='ELEM',
    components=('PRES',))

EPRESNO  = LocatedComponents(phys=PHY.PRES_R, type='ELNO',
    components=('PRES',))

MVECTUR  = ArrayOfComponents(phys=PHY.VDEP_R, locatedComponents=DDL_MECA)

#------------------------------------------------------------
class MEFOSE2(Element):
    """Please document this element"""
    meshType = MT.SEG2
    elrefe =(
            ElrefeLoc(MT.SE2, gauss = ('RIGI=FPG2',),),
        )
    calculs = (

        OP.CHAR_MECA_FF1D2D(te=195,
            para_in=((SP.PFF1D2D, CFORCEF), (SP.PGEOMER, NGEOMER),
                     (SP.PTEMPSR, CTEMPSR), ),
            para_out=((SP.PVECTUR, MVECTUR), ),
        ),

        OP.CHAR_MECA_FR1D2D(te=194,
            para_in=((SP.PFR1D2D, EFORCNO), (SP.PGEOMER, NGEOMER),
                     ),
            para_out=((SP.PVECTUR, MVECTUR), ),
        ),

        OP.CHAR_MECA_PRES_F(te=193,
            para_in=((SP.PGEOMER, NGEOMER), (SP.PPRESSF, CPRESSF),
                     (SP.PTEMPSR, CTEMPSR), ),
            para_out=((SP.PVECTUR, MVECTUR), ),
        ),

        OP.CHAR_MECA_PRES_R(te=192,
            para_in=((SP.PGEOMER, NGEOMER), (SP.PPRESSR, EPRESNO),
                     ),
            para_out=((SP.PVECTUR, MVECTUR), ),
        ),

        OP.COOR_ELGA(te=478,
            para_in=((SP.PGEOMER, NGEOMER), ),
            para_out=((OP.COOR_ELGA.PCOORPG, EGGEOP_R), ),
        ),

        OP.TOU_INI_ELGA(te=99,
            para_out=((OP.TOU_INI_ELGA.PGEOM_R, EGGEOP_R), ),
        ),

        OP.TOU_INI_ELEM(te=99,
            para_out=((OP.TOU_INI_ELEM.PGEOM_R, LC.CGEOM2D), ),
        ),

        OP.TOU_INI_ELNO(te=99,
            para_out=((OP.TOU_INI_ELNO.PGEOM_R, NGEOMER), (OP.TOU_INI_ELNO.PNEUT_F, LC.ENNEUT_F),
                     (OP.TOU_INI_ELNO.PNEUT_R, LC.ENNEUT_R), (OP.TOU_INI_ELNO.PPRES_R, EPRESNO),
                     ),
        ),

    )

#------------------------------------------------------------
class MEFOSE3(MEFOSE2):
    """Please document this element"""
    meshType = MT.SEG3
    elrefe =(
            ElrefeLoc(MT.SE3, gauss = ('RIGI=FPG4',),),
        )