import tweepy

from authorization_tokens import *

# import random
random_number = 3

if random_number == 0:
        #option1
        #Pick a phrase randomly from a list of phrases:
        phrase_list = ["Hi my name is Kasper",
                        "Kasper is my name",
                        "I like Twitter Bots"]
        random_number = random.randrange( len(phrase_list))
        message = phrase_list[random_number]
elif random_number == 1:
        #option 2
        # Create a sentence template with some blanks, and
        #randomly pick a word from a list to fill in each blank.
        word_list = ["apples", "bananas", "carrots", "dates"]

        string_template = "Some people think {} are good, but I like {}."

        random_number = random.randrange( len(word_list))
        word1 = word_list[random_number]

        random_number = random.randrange( len(word_list))
        word2 = word_list[random_number]

        message = string_template.format(word1, word2)
elif random_number == 2:
        #option 3
        #Randomly pick a template from a list, then randomly pick word_list
        #from a word list, and use the words to fill in the string_template

        word_list = ["people", "video games", "sports", "people"]
        template_list = ["If you like {}, you'll love {}.",
                        "You might think I'm a {} person, but actually I love {}.",
                        "You'd never guess it, but I like {} even more than {}."]

        random_number = random.randrange( len(template_list))
        template = template_list[random_number]

        random_number = random.randrange( len(word_list))
        word1 = word_list[random_number]

        random_number = random.randrange( len(word_list))
        word2 = word_list[random_number]

        message = template.format(word1, word2)
elif random_number == 3:
    # Kasper Bielecki 09/12/2020
    # Dungeons and Dragans Charachter Generator
    # Based on a Java Program I worked on between the dates of 04/17/2019 - 09/12/2020
    #*************CHECKLIST*************
    #
    # - Physical Attributes -
    # [*] Race
    # [*] Sub Race
    # [*] Sex
    # [*] Height
    # [*] Weight
    # [*] Age and agebracket
    # [] Eyes
    # [] Skin
    # [] Hair
    #
    # - Character Traits -
    # [*] Alignment
    # [*] Class
    # [] Background
    # [] Proficiencies
    # [] Character Name
    # [] Skills
    # [] Languages
    # [] Personality Traits
    # [] Ideals
    # [] Bonds
    # [] Flaws
    #
    # - Statistics -
    # [] Level
    # [*] Abilities
    # [*] Modifiers
    # [] Saving Throws
    # [] Hit Points
    # [] Hit Dice
    # [] Perception
    # [] Spell Casting Ability
    # [] Spell Save DC
    # [] Spell Attack Bonus
    # [] Spell Casting Class
    #
    # - Others -
    # [] Attacks
    # [] Equipment
    # [] Spells
    #
    #*******************************
    import random

    #04 Sided Die
    def D4():
    	x = (random.randrange(1,5))
    	return x

    #06 Sided Die
    def D6():
    	x = (random.randrange(1,7))
    	return x

    #08 Sided Die
    def D8():
    	x = (random.randrange(1,9))
    	return x

    #10 Sided Die
    def D10():
    	x = (random.randrange(1,11))
    	return x

    #Ability Score Roll
    def ability_score_roll():
        roll1 = D6()
        roll2 = D6()
        roll3 = D6()
        roll4 = D6()

        rolls = [
        roll1,
        roll2,
        roll3,
        roll4
        ]

        rolls.sort()

        ability_score = rolls[1] + rolls[2] + rolls[3]
        return ability_score

    def modifier_(ability):
        if ability == 1:
            return "(-5)"
        elif ability == 2 or ability == 3:
            return "(-4)"
        elif ability == 4 or ability == 5:
            return "(-3)"
        elif ability == 6 or ability == 7:
            return "(-2)"
        elif ability == 8 or ability == 9:
            return "(-1)"
        elif ability == 10 or ability == 11:
            return "(+0)"
        elif ability == 12 or ability == 13:
            return "(+1)"
        elif ability == 14 or ability == 15:
            return "(+2)"
        elif ability == 16 or ability == 17:
            return "(+3)"
        elif ability == 18 or ability == 19:
            return "(+4)"
        elif ability == 20 or ability == 21:
            return "(+5)"
        elif ability == 22 or ability == 23:
            return "(+6)"
        elif ability == 24 or ability == 25:
            return "(+7)"
        elif abilit == 26 or ability == 27:
            return "(+8)"
        elif ability == 28 or ability == 29:
            return "(+9)"
        elif ability == 30:
             return "(+10)"

    #Race
    race_list = [
    "Dragonborn",
    "Dwarf",
    "Elf",
    "Gnome",
    "Half-Elf",
    "Halfling",
    "Half-Orc",
    "Human",
    "Tiefling"
    ]
    random_number = random.randrange( len(race_list))
    race = race_list[random_number]

    #Sub Race
    if race == "Dwarf":
        subrace_list = [
        "Hill",
        "Mountain"
        ]
        random_number = random.randrange( len(subrace_list))
        subrace = subrace_list[random_number]

    elif race == "Elf":
        subrace_list = [
        "High",
        "Wood",
        "Drow"
        ]
        random_number = random.randrange( len(subrace_list))
        subrace = subrace_list[random_number]

    elif race == "Halfling":
        subrace_list = [
        "Lightfoot",
        "Stout"
        ]
        random_number = random.randrange( len(subrace_list))
        subrace = subrace_list[random_number]

    elif race == "Human":
        subrace_list = [
        "Calishite",
        "Chondathan",
        "Damaran",
        "Illuskan",
        "Mulan",
        "Rashemi",
        "Shou",
        "Tethyrian",
        "Turami"
        ]
        random_number = random.randrange( len(subrace_list))
        subrace = subrace_list[random_number]

    elif race == "Dragonborn":
        subrace_list = [
        "Black",
        "Blue",
        "Brass",
        "Bronze",
        "Copper",
        "Gold",
        "Green",
        "Red",
        "Silver",
        "White"
        ]
        random_number = random.randrange( len(subrace_list))
        subrace = subrace_list[random_number]

    elif race == "Gnome":
        subrace_list = [
        "Forrest",
        "Rock"
        ]
        random_number = random.randrange( len(subrace_list))
        subrace = subrace_list[random_number]

    else:
        subrace = None

    #Class
    class_list = [
    "Barbarian",
    "Bard",
    "Cleric",
    "Druid",
    "Fighter",
    "Monk",
    "Paladin",
    "Ranger",
    "Rogue"
    ]
    random_number = random.randrange( len(class_list))
    class_ = class_list[random_number]

    #Sex
    sex_list = [
    "Male",
    "Female"
    ]
    random_number = random.randrange( len(sex_list))
    sex = sex_list[random_number]

    #Height and Weight
    if race == "Dragonborn":
        roll = D8() + D8()
        feet = int((66 + roll) / 12)
        inches = (66 + roll) % 12
        weight = ((roll * (D6()+D6())) + 175)

    elif race == "Dwarf":
        if subrace == "Hill":
            roll = D4() + D4()
            feet = int((44 + roll) / 12)
            inches = (44 + roll) % 12
            weight = ((roll * (D6()+D6())) + 115)

        elif subrace == "Mountain":
            roll = D4() + D4()
            feet = int((48 + roll) / 12)
            inches = (48 + roll) % 12
            weight = ((roll * (D6()+D6())) + 130)

    elif race == "Elf":
        if subrace == "High":
            roll = D10() + D10()
            feet = int((54 + roll) / 12)
            inches = (54 + roll) % 12
            weight = ((roll * D4()) + 90)

        elif subrace == "Wood":
            roll = D10() + D10()
            feet = int((54 + roll) / 12)
            inches = (54 + roll) % 12
            weight = ((roll * D4()) + 100)

        elif subrace == "Drow":
            roll = D6() + D6()
            feet = int((53 + roll) / 12)
            inches = (53 + roll) % 12
            weight = ((roll * D6()) + 75)

    elif race == "Gnome":
        roll = D4() + D4()
        feet = int((35 + roll) / 12)
        inches = (35 + roll) % 12
        weight = ((roll * 1) + 35)

    elif race == "Half-Elf":
        roll = D8() + D8()
        feet = int((57 + roll) / 12)
        inches = (57 + roll) % 12
        weight = ((roll * (D4()+D4())) + 110)

    elif race == "Halfling":
        roll = D4() + D4()
        feet = int((31 + roll) / 12)
        inches = (31 + roll) % 12
        weight = ((roll * 1) + 35)

    elif race == "Half-Orc":
        roll = D10() + D10()
        feet = int((58 + roll) / 12)
        inches = (58 + roll) % 12
        weight = ((roll * (D6()+D6())) + 140)

    elif race == "Human":
        roll = D10() + D10()
        feet = int((56 + roll) / 12)
        inches = (56 + roll) % 12
        weight = ((roll * (D4()+D4())) + 110)

    elif race == "Tiefling":
        roll = D8() + D8()
        feet = int((57 + roll) / 12)
        inches = (57 + roll) % 12
        weight = ((roll * (D4()+D4())) + 110)

    height_template = '''{}'{}"'''
    height, weight = (height_template.format(feet, inches), weight)

    #Age and Age Bracket
    if race == "Human":
        age = (random.randrange(6,91))
        if age < 18:
            age_bracket = "years old - Childhood"
        elif age >= 18 and age < 30:
            age_bracket = "years old - Young Adult"
        elif age >= 30 and age < 65:
            age_bracket = "years old - Settled Adult"
        elif age >= 65 and age <= 90:
            age_bracket = "years old - Elderly"

    elif race == "Dwarf":
        age = (random.randrange(6,351))
        if age < 18:
            age_bracket = "years old - Childhood"
        elif age >= 18 and age < 50:
            age_bracket = "years old - Young Adult"
        elif age >= 50 and age < 200:
            age_bracket = "years old - Settled Adult"
        elif age >= 200 and age <= 350:
            age_bracket = "years old - Elderly"

    elif race == "Elf":
        age = (random.randrange(6,751))
        if age < 100:
            age_bracket = "years old - Childhood"
        elif age >= 100 and age < 450:
            age_bracket = "years old - Young Adult"
        elif age >= 450 and age < 650:
            age_bracket = "years old - Settled Adult"
        elif age >= 650 and age <= 750:
            age_bracket = "years old - Elderly"

    elif race == "Halfling":
        age = (random.randrange(6,251))
        if age < 20:
            age_bracket = "years old - Childhood"
        elif age >= 20 and age < 40:
            age_bracket = "years old - Young Adult"
        elif age >= 40 and age < 150:
            age_bracket = "years old - Settled Adult"
        elif age >= 150 and age <= 250:
            age_bracket = "years old - Elderly"

    elif race == "Dragonborn":
        age = (random.randrange(1,81))
        if age < 15:
            age_bracket = "years old - Childhood"
        elif age >= 15 and age < 20:
            age_bracket = "years old - Young Adult"
        elif age >= 20 and age < 50:
            age_bracket = "years old - Settled Adult"
        elif age >= 50 and age <= 80:
            age_bracket = "years old - Elderly"

    elif race == "Gnome":
        age = (random.randrange(6,401))
        if age < 20:
            age_bracket = "years old - Childhood"
        elif age >= 20 and age < 40:
            age_bracket = "years old - Young Adult"
        elif age >= 40 and age < 200:
            age_bracket = "years old - Settled Adult"
        elif age >= 200 and age <= 400:
            age_bracket = "years old - Elderly"

    elif race == "Half-Elf":
        age = (random.randrange(6,181))
        if age < 20:
            age_bracket = "years old - Childhood"
        elif age >= 20 and age < 50:
            age_bracket = "years old - Young Adult"
        elif age >= 50 and age < 150:
            age_bracket = "years old - Settled Adult"
        elif age >= 150 and age <= 180:
            age_bracket = "years old - Elderly"

    elif race == "Half-Orc":
        age = (random.randrange(1,76))
        if age < 14:
            age_bracket = "years old - Childhood"
        elif age >= 14 and age < 45:
            age_bracket = "years old - Young Adult"
        elif age >= 45 and age < 60:
            age_bracket = "years old - Settled Adult"
        elif age >= 60 and age <= 75:
            age_bracket = "years old - Elderly"

    elif race == "Tiefling":
        age = (random.randrange(6,101))
        if age < 18:
            age_bracket = "years old - Childhood"
        elif age >= 18 and age < 40:
            age_bracket = "years old - Young Adult"
        elif age >= 40 and age < 70:
            age_bracket = "years old - Settled Adult"
        elif age >= 70 and age <= 100:
            age_bracket = "years old - Elderly"

    #Alignment
    alignment_list = [
    "Lawful Good",
    "Neutral Good",
    "Chaotic Good",
    "Lawful Neutral",
    "True Neutral",
    "Chaotic Neutral",
    "Lawful Evil",
    "Neutral Evil",
    "Chaotic Evil"
    ]
    random_number = random.randrange( len(alignment_list))
    alignment = alignment_list[random_number]

    #Abilities
    ability1 = ability_score_roll()
    ability2 = ability_score_roll()
    ability3 = ability_score_roll()
    ability4 = ability_score_roll()
    ability5 = ability_score_roll()
    ability6 = ability_score_roll()

    abilities = [ability1,ability2,ability3,ability4,ability5,ability6]
    abilities.sort()

    if class_ == "Barbarian":
        strength = abilities[4]
        dexterity = abilities[3]
        constitution = abilities[5]
        intelligence = abilities[0]
        wisdom = abilities[1]
        charisma = abilities[2]
    elif class_ == "Bard":
        strength = abilities[1]
        dexterity = abilities[4]
        constitution = abilities[3]
        intelligence = abilities[0]
        wisdom = abilities[2]
        charisma = abilities[5]
    elif class_ == "Cleric":
        strength = abilities[4]
        dexterity = abilities[1]
        constitution = abilities[3]
        intelligence = abilities[0]
        wisdom = abilities[5]
        charisma = abilities[2]
    elif class_ == "Druid":
        strength = abilities[0]
        dexterity = abilities[4]
        constitution = abilities[3]
        intelligence = abilities[1]
        wisdom = abilities[5]
        charisma = abilities[2]
    elif class_ == "Fighter":
        strength = abilities[5]
        dexterity = abilities[3]
        constitution = abilities[4]
        intelligence = abilities[0]
        wisdom = abilities[1]
        charisma = abilities[2]
    elif class_ == "Monk":
        strength = abilities[0]
        dexterity = abilities[5]
        constitution = abilities[3]
        intelligence = abilities[1]
        wisdom = abilities[4]
        charisma = abilities[2]
    elif class_ == "Paladin":
        strength = abilities[5]
        dexterity = abilities[2]
        constitution = abilities[4]
        intelligence = abilities[0]
        wisdom = abilities[1]
        charisma = abilities[3]
    elif class_ == "Ranger":
        strength = abilities[0]
        dexterity = abilities[5]
        constitution = abilities[3]
        intelligence = abilities[1]
        wisdom = abilities[4]
        charisma = abilities[2]
    elif class_ == "Rogue":
        strength = abilities[3]
        dexterity = abilities[5]
        constitution = abilities[2]
        intelligence = abilities[0]
        wisdom = abilities[1]
        charisma = abilities[4]
    elif class_ == "Sorcerer":
        strength = abilities[0]
        dexterity = abilities[4]
        constitution = abilities[3]
        intelligence = abilities[1]
        wisdom = abilities[2]
        charisma = abilities[5]
    elif class_ == "Warlock":
        strength = abilities[1]
        dexterity = abilities[3]
        constitution = abilities[4]
        intelligence = abilities[0]
        wisdom = abilities[2]
        charisma = abilities[5]
    elif class_ == "Wizard":
        strength = abilities[0]
        dexterity = abilities[3]
        constitution = abilities[4]
        intelligence = abilities[5]
        wisdom = abilities[2]
        charisma = abilities[1]

    if race == "Dwarf":
        constitution = constitution + 2
        if subrace == "Hill":
            wisdom = wisdom + 1
        elif subrace == "Mountain":
            strength = strength + 2
    elif race == "Elf":
        dexterity = dexterity + 2
        if subrace == "High":
            intelligence = intelligence + 1
        elif subrace == "Drow":
            charisma = charisma + 1
        elif subrace == "Wood":
            wisdow = wisdom + 1
    elif race == "Halfling":
        dexterity = dexterity + 2
        if subrace == "Lightfoot":
            charisma = charisma + 1
        elif subrace == "Stout":
            constitution = constitution + 1
    elif race == "Human":
        strength = strength + 1
        dexterity = dexterity + 1
        constitution = constitution + 1
        intelligence = intelligence + 1
        wisdom = wisdom + 1
        charisma = charisma + 1
    elif race == "Dragonborn":
        strength = strength + 2
        charisma = charisma + 1
    elif race == "Gnome":
        intelligence = intelligence + 2
        if subrace == "Forrest":
            dexterity = dexterity + 1
        elif subrace == "Rock":
            constitution = constitution + 1
    elif race == "Half-Elf":
        charisma = charisma + 2
        abilitiy_choices = ["strength","dexterity","constitution","intelligence","wisdom","charisma"]
        random_number = random.randrange( len(abilitiy_choices))
        if abilitiy_choices[random_number] == "strength":
            strength = strength +1
            abilities.pop(0)
            random_number = random.randrange( len(abilitiy_choices))
            if abilitiy_choices[random_number] == "dexterity":
                dexterity = dexterity + 1
            elif abilitiy_choices[random_number] == "constitution":
                constitution = constitution + 1
            elif abilitiy_choices[random_number] == "intelligence":
                intelligence = intelligence + 1
            elif abilitiy_choices[random_number] == "wisdom":
                wisdom = wisdom + 1
            elif abilitiy_choices[random_number] == "charisma":
                charisma = charisma + 1
        elif abilitiy_choices[random_number] == "dexterity":
            dexterity = dexterity +1
            abilities.pop(1)
            random_number = random.randrange( len(abilitiy_choices))
            if abilitiy_choices[random_number] == "strength":
                strength = strength + 1
            elif abilitiy_choices[random_number] == "constitution":
                constitution = constitution + 1
            elif abilitiy_choices[random_number] == "intelligence":
                intelligence = intelligence + 1
            elif abilitiy_choices[random_number] == "wisdom":
                wisdom = wisdom + 1
            elif abilitiy_choices[random_number] == "charisma":
                charisma = charisma + 1
        elif abilitiy_choices[random_number] == "constitution":
            constitution = constitution +1
            abilities.pop(2)
            random_number = random.randrange( len(abilitiy_choices))
            if abilitiy_choices[random_number] == "strength":
                strength = strength + 1
            elif abilitiy_choices[random_number] == "dexterity":
                dexterity = dexterity + 1
            elif abilitiy_choices[random_number] == "intelligence":
                intelligence = intelligence + 1
            elif abilitiy_choices[random_number] == "wisdom":
                wisdom = wisdom + 1
            elif abilitiy_choices[random_number] == "charisma":
                charisma = charisma + 1
        elif abilitiy_choices[random_number] == "intelligence":
            intelligence = intelligence +1
            abilities.pop(3)
            random_number = random.randrange( len(abilitiy_choices))
            if abilitiy_choices[random_number] == "strength":
                strength = strength + 1
            elif abilitiy_choices[random_number] == "dexterity":
                dexterity = dexterity + 1
            elif abilitiy_choices[random_number] == "constitution":
                constitution = constitution + 1
            elif abilitiy_choices[random_number] == "wisdom":
                wisdom = wisdom + 1
            elif abilitiy_choices[random_number] == "charisma":
                charisma = charisma + 1
        elif abilitiy_choices[random_number] == "wisdom":
            wisdom = wisdom +1
            abilities.pop(4)
            random_number = random.randrange( len(abilitiy_choices))
            if abilitiy_choices[random_number] == "strength":
                strength = strength + 1
            elif abilitiy_choices[random_number] == "dexterity":
                dexterity = dexterity + 1
            elif abilitiy_choices[random_number] == "constitution":
                constitution = constitution + 1
            elif abilitiy_choices[random_number] == "intelligence":
                intelligence = intelligence + 1
            elif abilitiy_choices[random_number] == "charisma":
                charisma = charisma + 1
        elif abilitiy_choices[random_number] == "charisma":
            charisma = charisma +1
            abilities.pop(5)
            random_number = random.randrange( len(abilitiy_choices))
            if abilitiy_choices[random_number] == "strength":
                strength = strength + 1
            elif abilitiy_choices[random_number] == "dexterity":
                dexterity = dexterity + 1
            elif abilitiy_choices[random_number] == "constitution":
                constitution = constitution + 1
            elif abilitiy_choices[random_number] == "intelligence":
                intelligence = intelligence + 1
            elif abilitiy_choices[random_number] == "wisdom":
                wisdom = wisdom + 1
    elif race == "Half-Orc":
        strength = strength + 2
        constitution = constitution + 1
    elif race == "Tiefling":
        intelligence = intelligence +1
        charisma = charisma + 2

    strength_modifier = modifier_(strength)
    dexterity_modifier = modifier_(dexterity)
    constitution_modifier = modifier_(constitution)
    intelligence_modifier = modifier_(intelligence)
    wisdom_modifier = modifier_(wisdom)
    charisma_modifier = modifier_(charisma)
    #Print
    if subrace == None:
        # print("Race:",race,
        # "\nClass:",class_,
        # "\nAlignment:",alignment,
        # "\nSex:",sex,
        # "\nHeight:",height,
    	# "\nWeight:",weight,"lbs",
        # "\nAge:",age,age_bracket,
        # "\nStrength:",strength,strength_modifier,
        # "\nDexterity:",dexterity,dexterity_modifier,
        # "\nConstitution:",constitution,constitution_modifier,
        # "\nIntelligence:",intelligence,intelligence_modifier,
        # "\nWisdom:",wisdom,wisdom_modifier,
        # "\nCharisma:",charisma,charisma_modifier,)

        template = "Race: {}\nClass: {}\nAlignment: {}\nSex: {}\nHeight: {}\nWeight: {} Lbs\nAge: {} {}\nStrength: {} {}\nDexterity: {} {}\nConstitution {} {}\nIntelligence: {} {}\nWisdom: {} {}\nCharisma {} {}"
        message = template.format(race,class_,alignment,sex,height,weight,age,age_bracket,strength,strength_modifier,dexterity,dexterity_modifier,constitution,constitution_modifier,intelligence,intelligence_modifier,wisdom,wisdom_modifier,charisma,charisma_modifier)
    else:
        # print("Race:",subrace,race,
        # "\nClass:",class_,
        # "\nAlignment:",alignment,
        # "\nSex:",sex,
        # "\nHeight:",height,
    	# "\nWeight:",weight,"lbs",
        # "\nAge:",age,age_bracket,
        # "\nStrength:",strength,strength_modifier,
        # "\nDexterity:",dexterity,dexterity_modifier,
        # "\nConstitution:",constitution,constitution_modifier,
        # "\nIntelligence:",intelligence,intelligence_modifier,
        # "\nWisdom:",wisdom,wisdom_modifier,
        # "\nCharisma:",charisma,charisma_modifier,)
        template = "Race: {} {}\nClass: {}\nAlignment: {}\nSex: {}\nHeight: {}\nWeight: {} Lbs\nAge: {} {}\nStrength: {} {}\nDexterity: {} {}\nConstitution {} {}\nIntelligence: {} {}\nWisdom: {} {}\nCharisma {} {}"
        message = template.format(subrace,race,class_,alignment,sex,height,weight,age,age_bracket,strength,strength_modifier,dexterity,dexterity_modifier,constitution,constitution_modifier,intelligence,intelligence_modifier,wisdom,wisdom_modifier,charisma,charisma_modifier)

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

api.update_status(message)
print("Done.")


#===========================================#
#                                           #
#                                           #
#----------CROSSWALK RECOGNITION------------#
#-----------WRITTEN BY N.DALAL--------------#
#-----------------2017 (c)------------------#
#                                           #
#                                           #
#===========================================#

#Copyright by N. Dalal, 2017 (c)

#Licensed under the MIT License:

#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:

#The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.

#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.

import numpy as np
import cv2
import math
import scipy.misc
import PIL.Image
import statistics
import timeit
import glob
from sklearn import linear_model, datasets

#==========================#
#---------functions--------#
#==========================#

#get a line from a point and unit vectors
def lineCalc(vx, vy, x0, y0):
    scale = 10
    x1 = x0+scale*vx
    y1 = y0+scale*vy
    m = (y1-y0)/(x1-x0)
    b = y1-m*x1
    return m,b

#the angle at the vanishing point
def angle(pt1, pt2):
    x1, y1 = pt1
    x2, y2 = pt2
    inner_product = x1*x2 + y1*y2
    len1 = math.hypot(x1, y1)
    len2 = math.hypot(x2, y2)
    print(len1)
    print(len2)
    a=math.acos(inner_product/(len1*len2))
    return a*180/math.pi

#vanishing point - cramer's rule
def lineIntersect(m1,b1, m2,b2) :
    #a1*x+b1*y=c1
    #a2*x+b2*y=c2
    #convert to cramer's system
    a_1 = -m1
    b_1 = 1
    c_1 = b1

    a_2 = -m2
    b_2 = 1
    c_2 = b2

    d = a_1*b_2 - a_2*b_1 #determinant
    dx = c_1*b_2 - c_2*b_1
    dy = a_1*c_2 - a_2*c_1

    intersectionX = dx/d
    intersectionY = dy/d
    return intersectionX,intersectionY

#process a frame
def process(im):
    start = timeit.timeit() #start timer

    #initialize some variables
    x = W
    y = H

    radius = 250 #px
    thresh = 170
    bw_width = 170

    bxLeft = []
    byLeft = []
    bxbyLeftArray = []
    bxbyRightArray = []
    bxRight = []
    byRight = []
    boundedLeft = []
    boundedRight = []

    #1. filter the white color
    lower = np.array([170,170,170])
    upper = np.array([255,255,255])
    mask = cv2.inRange(im,lower,upper)

    #2. erode the frame
    erodeSize = int(y / 30)
    erodeStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (erodeSize,1))
    erode = cv2.erode(mask, erodeStructure, (-1, -1))

    #3. find contours and  draw the green lines on the white strips
    _ , contours,hierarchy = cv2.findContours(erode,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE )

    for i in contours:
        bx,by,bw,bh = cv2.boundingRect(i)

        if (bw > bw_width):

            cv2.line(im,(bx,by),(bx+bw,by),(0,255,0),2) # draw the a contour line
            bxRight.append(bx+bw) #right line
            byRight.append(by) #right line
            bxLeft.append(bx) #left line
            byLeft.append(by) #left line
            bxbyLeftArray.append([bx,by]) #x,y for the left line
            bxbyRightArray.append([bx+bw,by]) # x,y for the left line
            cv2.circle(im,(int(bx),int(by)),5,(0,250,250),2) #circles -> left line
            cv2.circle(im,(int(bx+bw),int(by)),5,(250,250,0),2) #circles -> right line

    #calculate median average for each line
    medianR = np.median(bxbyRightArray, axis=0)
    medianL = np.median(bxbyLeftArray, axis=0)

    bxbyLeftArray = np.asarray(bxbyLeftArray)
    bxbyRightArray = np.asarray(bxbyRightArray)

    #4. are the points bounded within the median circle?
    for i in bxbyLeftArray:
        if (((medianL[0] - i[0])**2 + (medianL[1] - i[1])**2) < radius**2) == True:
            boundedLeft.append(i)

    boundedLeft = np.asarray(boundedLeft)

    for i in bxbyRightArray:
        if (((medianR[0] - i[0])**2 + (medianR[1] - i[1])**2) < radius**2) == True:
            boundedRight.append(i)

    boundedRight = np.asarray(boundedRight)

    #5. RANSAC Algorithm

    #select the points enclosed within the circle (from the last part)
    bxLeft = np.asarray(boundedLeft[:,0])
    byLeft =  np.asarray(boundedLeft[:,1])
    bxRight = np.asarray(boundedRight[:,0])
    byRight = np.asarray(boundedRight[:,1])

    #transpose x of the right and the left line
    bxLeftT = np.array([bxLeft]).transpose()
    bxRightT = np.array([bxRight]).transpose()

    #run ransac for LEFT
    model_ransac = linear_model.RANSACRegressor(linear_model.LinearRegression())
    ransacX = model_ransac.fit(bxLeftT, byLeft)
    inlier_maskL = model_ransac.inlier_mask_ #right mask

    #run ransac for RIGHT
    ransacY = model_ransac.fit(bxRightT, byRight)
    inlier_maskR = model_ransac.inlier_mask_ #left mask

    #draw RANSAC selected circles
    for i, element in enumerate(boundedRight[inlier_maskR]):
       # print(i,element[0])
        cv2.circle(im,(element[0],element[1]),10,(250,250,250),2) #circles -> right line

    for i, element in enumerate(boundedLeft[inlier_maskL]):
       # print(i,element[0])
        cv2.circle(im,(element[0],element[1]),10,(100,100,250),2) #circles -> right line

    #6. Calcuate the intersection point of the bounding lines
    #unit vector + a point on each line
    vx, vy, x0, y0 = cv2.fitLine(boundedLeft[inlier_maskL],cv2.DIST_L2,0,0.01,0.01)
    vx_R, vy_R, x0_R, y0_R = cv2.fitLine(boundedRight[inlier_maskR],cv2.DIST_L2,0,0.01,0.01)

    #get m*x+b
    m_L,b_L=lineCalc(vx, vy, x0, y0)
    m_R,b_R=lineCalc(vx_R, vy_R, x0_R, y0_R)

    #calculate intersention
    intersectionX,intersectionY = lineIntersect(m_R,b_R,m_L,b_L)

    #7. draw the bounding lines and the intersection point
    m = radius*10
    if (intersectionY < H/2 ):
        cv2.circle(im,(int(intersectionX),int(intersectionY)),10,(0,0,255),15)
        cv2.line(im,(x0-m*vx, y0-m*vy), (x0+m*vx, y0+m*vy),(255,0,0),3)
        cv2.line(im,(x0_R-m*vx_R, y0_R-m*vy_R), (x0_R+m*vx_R, y0_R+m*vy_R),(255,0,0),3)

    #8. calculating the direction vector
    POVx = W/2 #camera POV - center of the screen
    POVy = H/2 # camera POV - center of the screen

    Dx = -int(intersectionX-POVx) #regular x,y axis coordinates
    Dy = -int(intersectionY-POVy) #regular x,y axis coordinates

    #focal length in pixels = (image width in pixels) * (focal length in mm) / (CCD width in mm)
    focalpx = int(W * 4.26 / 6.604) #all in mm

    end = timeit.timeit() #STOP TIMER
    time_ = end - start

    print('DELTA (x,y from POV):' + str(Dx) + ',' + str(Dy))
    return im,Dx,Dy

#=============================#
#---------MAIN PROGRAM--------#
#=============================#
#initialization
cap = cv2.VideoCapture('inputVideo.mp4') #load a video

W = cap.get(3) #get width
H = cap.get(4) #get height

#Define a new resolution
ratio = H/W
W = 800
H = int(W * ratio)

#setup the parameters for saving the processed file
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('processedVideo.mp4',fourcc, 15.0, (int(W),int(H)))

Dx = []
Dy = []
after =0
DxAve =0
Dxold =0
DyAve =0
Dyold =0
i = 0
state = ""

while(cap.isOpened()):
    ret, frame = cap.read()
    img = scipy.misc.imresize(frame, (H,W))

    #draw camera's POV
    cv2.circle(img,(int(W/2),int(H/2)),5,(0,0,255),8)

    try:
        processedFrame,dx,dy = process(img)

        if (i < 6):
            Dx.append(dx)
            Dy.append(dy)
            i=i+1

        else:
            DxAve = sum(Dx)/len(Dx)
            DyAve = sum(Dy)/len(Dy)
            del Dx[:]
            del Dy[:]
            i=0

        if (DyAve > 30) and (abs(DxAve) < 300):
            #check if the vanishing point and the next vanishing point aren't too far from each other
            if (((DxAve - Dxold)**2 + (DyAve - Dyold)**2) < 150**2) == True:  ##distance 150 px max
                cv2.line(img,(int(W/2),int(H/2)),(int(W/2)+int(DxAve),int(H/2)+int(DyAve)),(0,0,255),7)

                #walking directions
                if abs(DxAve) < 80 and DyAve > 100 and abs(Dxold-DxAve) < 20:
                    state = 'Straight'
                    cv2.putText(img,state,(50,50), cv2.FONT_HERSHEY_PLAIN, 3,(0,0,0),2,cv2.LINE_AA)

                elif DxAve > 80 and DyAve > 100 and abs(Dxold-DxAve) < 20:
                    state = 'Right'
                    cv2.putText(img,state,(50,50), cv2.FONT_HERSHEY_PLAIN, 3,(0,0,255),2,cv2.LINE_AA)

                elif DxAve < 80 and DyAve > 100 and abs(Dxold-DxAve) < 20:
                    state = 'Left'
                    cv2.putText(img,state,(50,50), cv2.FONT_HERSHEY_PLAIN, 3,(0,0,255),2,cv2.LINE_AA)
            else:
                cv2.line(img,(int(W/2),int(H/2)),(int(W/2)+int(Dxold),int(H/2)+int(Dyold)),(0,0,255),)

            #walking directions
            if state == 'Straight':
                cv2.putText(img,state,(50,50), cv2.FONT_HERSHEY_PLAIN, 3,(0,0,0),2,cv2.LINE_AA)
            else:
                cv2.putText(img,state,(50,50), cv2.FONT_HERSHEY_PLAIN, 3,(0,0,255),2,cv2.LINE_AA)

            Dxold = DxAve
            Dyold = DyAve

    except:
        print('Failed to process frame')

    #show & save
    img = cv2.imshow('Processed',processedFrame)
    out.write(processedFrame)

    if cv2.waitKey(1) & 0xFF == ord('q') or cv2.waitKey(1) & 0xFF == ord('Q'):
        break

out.release()
cap.release()
cv2.destroyAllWindows()


## Created mkwalter9 10/11/20
import os, sys
import numpy as np
import pandas as pd
from pprint import pprint
from parse_artbnk_html import PageParser
from image_parser import ImageFetcher
import logging
import time

logger = logging.getLogger()

run_id = str(time.time())

try:
    PAGES_DIR = str(sys.argv[1])
except:
    PAGES_DIR = 'pages'
logger.info(' || Using pages inside directory ' + PAGES_DIR)

IMAGES_DIR = 'images'
DATADIR = 'records'

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

def get_img(row, fetcher):
    row['image_file'] = fetcher.fetch_image(row['img_url'])
    return row
########################## MAIN ###########################

pages = os.listdir(PAGES_DIR)

main_df = pd.DataFrame()

parser = PageParser(PAGES_DIR)
for page in pages:
    feats = parser.scrape_feats(page)
    df=pd.DataFrame(feats)

    imager = ImageFetcher(df.iloc[0]['Artist Name'])

    df = df.apply(get_img, args=(imager,),axis=1)
    print(df.head())
    main_df=main_df.append(df)

print(main_df.describe())
main_df.to_csv("{}/artist_dataset_{}.csv".format(DATADIR, run_id), sep="|")


def removeVowels(string,count):
    """
    Recursive function that that for each iteration loops through the given string and removes vowels if found
    """
    vowels = ["a","e","i","o","u","A","E","I","O","U"]
    while count <=8:
        #Stops recursion once it has gone through all the possible vowels
        if vowels[count] in string:
            #Is the vowel at the position of count within the list in the string
            for letter in string:
                #loops through all letters in the string
                string = string.replace(vowels[count],"")
                #if it finds a letter that is a vowel it removes it
                count +=1
                #Adds one to the count moving the next cycle to the next vowel
            return removeVowels(string,count)
            #Starts recurisve function again
        else:
            #if there isnt that vowel in the string it moves to the next one in the list
            count +=1
            return removeVowels(string,count)
            #Starts recurisve function again
    else:
        #outputs the final ammended string without vowels
        return string

def main(string):
    count=0
    #starts at 0 due to pythons 0 based indexing of arrays
    print(removeVowels(string,count))
    #Starts recurisve function

main("Beautiful")
#Initialises main program function


import requests
import random
from bs4 import BeautifulSoup
# from config import Search_list

def main():
    cat_url = 'https://www.pexels.com/search/cat'
    req = requests.get(cat_url)
    # Check status code
    if req.status_code == requests.codes.ok:
        bsParsingResult = BeautifulSoup(req.text, "html.parser")
        # CSS selector
        selectionResult = bsParsingResult.select('div.photos__column')
        article_num = random.randint(0, 2)
        articleResult = selectionResult[article_num].select('div.hide-featured-badge > article')
        img_num = random.randint(0, len(articleResult) - 1)
        url = articleResult[img_num].get('data-photo-modal-image-src')
        return url
    else:
        print(req.status_code)
        return '503 Server'

if __name__ == '__main__':
    main()


from django.urls import path
from . import views

urlpatterns = [
        path('', views.index, name='index'),
        path('edit/<int:id>', views.edit, name='edit'),
        path('remove-benefit/<int:id>', views.remove_benefit, name='remove_benefit'),

        path('get-data', views.get_data, name='get_data'),
        path('get-data/', views.get_data, name='get_data'),
]


import datetime
import json
import os
import xarray as xr
import pandas as pd
import numpy as np
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
import zipfile
import portalocker
import gzip

from blsgov.conf import *

DATE_FORMAT = '%Y-%m-%d'

def get_dbs(request,  last_time=None):
    with portalocker.Lock(BLSGOV_DB_LIST_FILE_NAME + '.lock', flags=portalocker.LOCK_SH | portalocker.LOCK_NB):
        with gzip.open(BLSGOV_DB_LIST_FILE_NAME, 'r') as f:
            dbs = f.read()
    return HttpResponse(dbs, content_type='application/json')

def get_db_meta(request, last_time=None):
    id = request.GET['id']
    db_path = os.path.join(BLSGOV_DIR, id.lower())

    with portalocker.Lock(db_path + '.lock', flags=portalocker.LOCK_SH | portalocker.LOCK_NB):
        with gzip.open(os.path.join(db_path, BLSGOV_META_FILE_NAME), 'rt') as f:
            meta = f.read()
        return HttpResponse(meta, content_type='application/json')

def get_series_meta(request, last_time=None):
    id = request.GET['id']
    last_series_id = request.GET.get("last_series", "")
    db_path = os.path.join(BLSGOV_DIR, id.lower())

    with portalocker.Lock(db_path + '.lock', flags=portalocker.LOCK_SH | portalocker.LOCK_NB):
        files = os.listdir(db_path)
        files = [{
            "from": f.split('.')[1],
            "to": f.split('.')[2],
            "name": f
        } for f in files if f.startswith('series.')]
        files.sort(key=lambda f: f['from'])
        if last_series_id is None:
            series_file = files[0]
        else:
            series_file = next((f for f in files if f['to'] > last_series_id), None)
        if series_file is None:
            return HttpResponse('[]', content_type='application/json')

        series_path = os.path.join(db_path, series_file['name'])
        with gzip.open(series_path, 'rt') as f:
            series = f.read()
        series = json.loads(series)

        if last_series_id is not None:
            series = [s for s in series if s['id'] > last_series_id]

        return HttpResponse(json.dumps(series, indent=1), content_type='application/json')

def get_series_data(request, last_time=None):
    args = request.GET

    id = args['id']

    min_date = args.get('min_date', '2007-01-01')
    min_date = datetime.datetime.strptime(min_date, DATE_FORMAT).date() if min_date is not None else datetime.date(2007, 1, 1)

    max_date = args.get('max_date')
    max_date = datetime.datetime.strptime(max_date, DATE_FORMAT).date() if max_date is not None else datetime.date.today()

    if last_time is not None:
        last_time = datetime.datetime.strptime(last_time.split('T')[0], DATE_FORMAT).date()
        if last_time < max_date:
            max_date = last_time

    res = get_data_series(id, max_date, min_date, 'data')

    return HttpResponse(json.dumps(res, indent=1), content_type='application/json')

def get_series_aspect(request, last_time=None):
    args = request.GET

    id = args['id']

    min_date = args.get('min_date', '2007-01-01')
    min_date = datetime.datetime.strptime(min_date, DATE_FORMAT).date() if min_date is not None else datetime.date(2007, 1, 1)

    max_date = args.get('max_date')
    max_date = datetime.datetime.strptime(max_date, DATE_FORMAT).date() if max_date is not None else datetime.date.today()

    if last_time is not None:
        last_time = datetime.datetime.strptime(last_time.split('T')[0], DATE_FORMAT).date()
        if last_time < max_date:
            max_date = last_time

    res = get_data_series(id, max_date, min_date, 'aspect')

    return HttpResponse(json.dumps(res, indent=1), content_type='application/json')

def get_data_series(id, max_date, min_date, prefix):
    db_id = id[:2].lower()

    prefix = prefix + '.'
    db_path = os.path.join(BLSGOV_DIR, db_id.lower())

    with portalocker.Lock(db_path + '.lock', flags=portalocker.LOCK_SH | portalocker.LOCK_NB):
        files = os.listdir(db_path)
        files = [{
            "from": f.split('.')[1],
            "to": f.split('.')[2],
            "name": f
        } for f in files if f.startswith(prefix)]
        data_file = next((f for f in files if f['from'] <= id <= f['to']), None)
        if data_file is None:
            return []
        path = os.path.join(db_path, data_file['name'])
        with zipfile.ZipFile(path, 'r') as z:
            try:
                content = z.read(id + '.json')
            except KeyError:
                return []
        content = content.decode()
        content = json.loads(content)

        for p in content:
            p['pub_date'] = get_pub_date(p)
        content = [p for p in content if min_date <= p['pub_date'] <= max_date]
        for p in content:
            p['pub_date'] = p['pub_date'].isoformat()

        return content

def get_pub_date(p):
    PUB_OFFSET = 14
    if p['period'][0] == 'A':
        return datetime.date(p['year'] + 1, 1, 1) - datetime.timedelta(days=1) + datetime.timedelta(days=PUB_OFFSET)
    month = None
    if p['period'][0] == 'M':
        month = int(p['period'][1:])
    elif p['period'][0] == 'Q':
        month = int(p['period'][1:]) * 3
    elif p['period'][0] == 'S':
        month = int(p['period'][1:]) * 6
    else:
        raise Exception("wrong period")
    if month >= 12:
        return datetime.date(p['year'] + 1, 1, 1) - datetime.timedelta(days=1) + datetime.timedelta(days=PUB_OFFSET)
    else:
        return datetime.date(p['year'], month + 1, 1) - datetime.timedelta(days=1) + datetime.timedelta(days=PUB_OFFSET)


# THIS WORKS!
import numpy as np
from skimage.measure import find_contours
from numpy.linalg import eig, inv

class SOEllipse(object):
    '''
    #
    # Conic Ellipse fitter
    #     exploiting the quadratic curve nature of the ellipse
    #

    advantages: fast

    disadvantages: does not have flexibility

    '''
    @staticmethod
    def fitEllipse(x,y):
        #
        # Take a set of x,y points at fit an ellipse to it
        #
        x = x[:,np.newaxis]
        y = y[:,np.newaxis]
        D =  np.hstack((x*x, x*y, y*y, x, y, np.ones_like(x)))
        S = np.dot(D.T,D)
        C = np.zeros([6,6])
        C[0,2] = C[2,0] = 2; C[1,1] = -1
        E, V =  eig(np.dot(inv(S), C))
        n = np.argmax(np.abs(E))
        a = V[:,n]

        return a
    @staticmethod
    def ellipse_center(a):
        b,c,d,f,g,a = a[1]/2, a[2], a[3]/2, a[4]/2, a[5], a[0]
        num = b*b-a*c
        x0=(c*d-b*f)/num
        y0=(a*f-b*d)/num

        return np.array([x0,y0])
    @staticmethod
    def ellipse_angle_of_rotation( a ):
        b,c,d,f,g,a = a[1]/2, a[2], a[3]/2, a[4]/2, a[5], a[0]

        return 0.5*np.arctan(2*b/(a-c))
    @staticmethod
    def ellipse_axis_length( a ):
        b,c,d,f,g,a = a[1]/2, a[2], a[3]/2, a[4]/2, a[5], a[0]
        up = 2*(a*f*f+c*d*d+g*b*b-2*b*d*f-a*c*g)
        down1=(b*b-a*c)*( (c-a)*np.sqrt(1+4*b*b/((a-c)*(a-c)))-(c+a))
        down2=(b*b-a*c)*( (a-c)*np.sqrt(1+4*b*b/((a-c)*(a-c)))-(c+a))
        res1=np.sqrt(up/down1)
        res2=np.sqrt(up/down2)
        return np.array([res1, res2])

def follow_contour(xx,yy,arr,level):

    dx = np.unique(xx[0,:])[1] - np.unique(xx[0,:])[0]
    xmin = np.min(np.unique(xx[0,:]))

    dy = np.unique(yy[:,0])[1] - np.unique(yy[:,0])[0]
    ymin = np.min(np.unique(yy[:,0]))

    res = find_contours(arr,level)

    #c = cntr.Cntr(xx,yy,arr)
    #res = c.trace(level)
    #
    xcon = []
    ycon = []
    try:
        for i in range(0,len(res[0])):
            xcon.append(dx*res[0][i][0] + xmin)
            ycon.append(dy*res[0][i][1] + ymin)
    except:
        print('No countour found at {}'.format(level))
        #
    XCON = np.array(xcon)
    YCON = np.array(ycon)
    return YCON,XCON

def make_ellipse(xcontours,ycontours):
    #
    # use parametric conic to get basic parameters
    #
    ell = SOEllipse.fitEllipse(xcontours,ycontours)
    phi = SOEllipse.ellipse_angle_of_rotation(ell)
    xcenter,ycenter = SOEllipse.ellipse_center(ell)
    alength = SOEllipse.ellipse_axis_length(ell)
    a = np.max(alength)
    b = np.min(alength)
    return a,b,phi,xcenter,ycenter


def wordListConfig(wordList):      # Loads the word list, sorts it, and
     wordsFile = open('randomlist.txt','r')
     for line in wordsFile:
          wordList.append(line.rstrip())
     wordList.sort()

def writeToJS(wordList):
     jsFile = open('randomlist.js','w')
     jsFile.write('var wordList = [\n')
     for line in wordList:
          jsFile.write('"' + line + '",\n')
     jsFile.write('];')
     jsFile.close()

wordList = []
wordListConfig(wordList)
writeToJS(wordList)


#!/usr/bin/env python

"""Run pre-commit hooks with manual stages."""

from __future__ import print_function

import subprocess
import sys

TRACE_PREFIX = "+"

RUN_HOOKS_COMMAND = [
    "pre-commit",
    "run",
    "--hook-stage",
    "manual",
]

def grok_args(args):
    """
    Figure out whether we have any arguments and extract the program "name".

    :Args:
        args
            A list of program arguments, including the program "name" as the
            zero-th argument (see `sys.argv`:py:attr:)

    :Returns:
        A best guess at (`prog`, `args`), where:

        - `prog` is the program "name"
        - `args` is the list of program arguments, beginning with the first
          argument (i.e., _not_ including the program name)
    """
    if not args:
        args = sys.argv
    try:
        prog = args[0]
    except IndexError:
        prog = None
    return (prog, args[1:])

def filter_args(args):
    """Add needed arguments to `args` only when needed."""
    for arg in ["-a", "--all-files", "-h", "--help", "--files"]:
        if arg in args:
            return args
    return ["--all-files"] + args

def main():
    """Run the pre-commit command to run manual hooks."""
    (_prog, args) = grok_args(sys.argv)

    if not args:
        print(
            "ERROR: Please supply the name of at least one hook to run manually.",
            file=sys.stderr,
        )
        sys.exit(1)

    args = filter_args(args)
    command = RUN_HOOKS_COMMAND + args
    print(" ".join([TRACE_PREFIX] + command), file=sys.stderr)
    status = subprocess.call(command)
    sys.exit(status)

if __name__ == "__main__":
    main()


from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.utils.html import format_html

# Create your models here.
class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    mobile_no = models.CharField(max_length=50)
    address = models.CharField(max_length=100, blank=True)

@receiver(post_save, sender=User)                                   ###################################
def create_user_profile(sender, instance, created, **kwargs):       # Thus, we are hooking the methods
    if created:                                                     # to the User model,
        Profile.objects.create(user=instance)                       # whenever a save event occurs
                                                                    ###################################
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()

class Product(models.Model):
    PRODUCT_CHOICES = [

        ('Cosmetics', (
            ('Hair Care', 'Hair Care'),
            ('Skin Care', 'Skin Care'),
            ('Beauty Care', 'Beauty Care'),
            )
        ),
        ('Digital', (
            ('E-Book', 'E-Book'),
            ('Kindle Edition', 'Kindle Edition'),
            ('Software', 'Software'),
            )
        ),
        ('Electronics', (
            ('Monitor', 'Monitor'),
            ('Mouse', 'Mouse'),
            ('Keyboard', 'Keyboard'),
            ('Headphone', 'Headphone'),
            ('Earphone', 'Earphone'),
            ('Laptop', 'Laptop'),
            ('TV', 'TV'),
            ('Pen Drive', 'Pen Drive'),
            ('Router', 'Router'),
            ('AC', 'AC'),
            ('Tablets', 'Tablets'),
            ('Camera', 'Camera'),
            )
        ),
        ('Bag', 'Bag'),
        ('Wallet', 'Wallet'),
        ('T-Shirt', 'T-Shirt'),
        ('Jeans', 'Jeans'),
        ('Wrist Watch', 'Wrist Watch'),
        ('Belt', 'Belt'),
        ('Sunglass', 'Sunglass'),
        ('Shirt', 'Shirt'),
        ('Trouser', 'Trouser'),
        ('Shorts', 'Shorts'),
        ('Book', 'Book'),
        ('None', 'None'),
    ]

    TYPE_CHOICES = [
        ('Wired', 'Wired'),
        ('Wireless', 'Wireless'),
        ('SLR', 'SLR'),
        ('DSLR', 'DSLR'),
        ('None', 'None'),

    ]

    FOR_CHOICES = [
        ('Men', 'Men'),
        ('Women', 'Women'),
        ('Kids', 'Kids'),
        ('Boys', 'Boys'),
        ('Girls', 'Girls'),
        ('All', 'All'),
        ('None', 'None'),
    ]

    title = models.CharField(max_length=255, blank=True)
    price = models.DecimalField(max_digits=8, decimal_places=2, blank=True)
    offer = models.BooleanField(default=False, null=True)
    description = models.TextField(blank=True, null=True)
    category = models.CharField(
        choices=PRODUCT_CHOICES,
        max_length=20,
        blank=True
    )
    sub_category = models.CharField(
        choices=TYPE_CHOICES,
        max_length=20,
        blank=True
    )
    usable_by = models.CharField(choices=FOR_CHOICES, max_length=6, blank=True)
    quantity = models.IntegerField(blank=True, null=True)
    image = models.ImageField(upload_to='products', blank=True)
    added_at = models.DateTimeField(auto_now=True)
    updated_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

    def show_image(self):
        return format_html('<img src="/media/%s" width="50" />'%self.image)


from itertools import cycle
from typing import Callable, List

import pandas as pd

class SlotBased:

    def __init__(self, methods: List[Callable[[int, int], pd.Series]], slots: List[int] = None):
        self.methods = methods
        self.total_slots = sum(slots) if slots is not None else len(methods)
        self.slots = slots if slots is not None else cycle([1])

    def __call__(self, movie_id: int, n: int = 5):
        scores = []
        # for each method, execute method, collect scores
        for method, n_slots in zip(self.methods, self.slots):
            method_result = method(movie_id, self.total_slots)
            # require as many results as there are slots as otherwise there might be too many duplicates
            method_scores = method_result.nlargest(self.total_slots).index.tolist()
            scores.append(method_scores)

        self._remove_duplicates(scores, self.slots)

        result = []
        # select (at most) n_slots items from each scores,
        # where n_slots is the number of slots assigned to that method
        for scores_a, slots_a in zip(scores, self.slots):
            result.extend(scores_a[:slots_a])

        # return resulting movies with an artificial decreasing score
        return pd.Series(data=range(len(result), 0, -1), index=result)

    @staticmethod
    def _remove_duplicates(scores, slots):
        # scores_sets = [set(s) for s in scores]

        # remove from each pairwise combination
        for index, (scores_a, slots_a) in enumerate(zip(scores, slots)):
            # pre calculate set representation
            relevant_a = set(scores_a)
            for scores_b, slots_b in zip(scores[index + 1:], slots[index + 1:]):
                # find common movies
                common = relevant_a.intersection(scores_b)
                # remove all common movies from ONE of the sets its in
                for item in common:
                    # remove it from the set that has more "surplus" space
                    # (more recommendations than it needs to fill its slots)
                    if (len(scores_a) - slots_a) > (len(scores_b) - slots_b):
                        scores_a.remove(item)
                    else:
                        scores_b.remove(item)


import os
import sys

import argparse
import string
import numpy as np
from PIL import Image, ImageDraw, ImageFont
from bs4 import BeautifulSoup, NavigableString
import editdistance
import random

import torch
from torch import nn
from torch.backends import cudnn
import torch.nn.functional as F
from torch.utils.data import DataLoader

from dataset import AlignCollate, PILDataset
from model import Model
from utils import CTCLabelConverter, AttnLabelConverter

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
IMG_EXT = {'.jpg', '.tif', '.tiff', '.png'}

def get_already_done(done_path):
    done = set()
    if not os.path.isfile(done_path):
        return done
    with open(done_path, 'r', encoding='utf-8') as fp:
        for line in fp:
            done.add(line.rstrip())
    return done

def get_count(count_path):
    if not os.path.isfile(count_path):
        return 0
    else:
        return int(open(count_path, 'r', encoding='utf-8').readline().rstrip())

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--done_path', type=str, default='result/done.txt')
    parser.add_argument('--count_path', type=str, default='result/count.txt')
    parser.add_argument('--todo_file', type=str, default=None)
    parser.add_argument('--type', type=str, required=True, choices=['dingxiu', 'diaolong'])
    parser.add_argument('--input_path', type=str, default=None)
    parser.add_argument('--output_path', type=str, required=True)
    parser.add_argument('--shuffle', action='store_true', help='random shuffle input list')
    parser.add_argument('--strict', action='store_true', help='use strict check(len diff less than 2 and edit_dist less than 0.2).')
    parser.add_argument('--hold_acc_prob', type=float, default=0.33, help='If gt and pred is the same, hold it with this prob.')

    parser.add_argument('--workers', type=int, help='number of data loading workers', default=4)
    parser.add_argument('--batch_size', type=int, default=192, help='input batch size')
    parser.add_argument('--saved_model', required=True, help="path to saved_model to evaluation")
    """ Data processing """
    parser.add_argument('--batch_max_length', type=int, default=25, help='maximum-label-length')
    parser.add_argument('--imgH', type=int, default=32, help='the height of the input image')
    parser.add_argument('--imgW', type=int, default=100, help='the width of the input image')
    parser.add_argument('--rgb', action='store_true', help='use rgb input')
    parser.add_argument('--character', type=str, default='0123456789abcdefghijklmnopqrstuvwxyz', help='character label')
    parser.add_argument('--sensitive', action='store_true', help='for sensitive character mode')
    parser.add_argument('--PAD', action='store_true', help='whether to keep ratio then pad for image resize')
    """ Model Architecture """
    parser.add_argument('--Transformation', type=str, required=True, help='Transformation stage. None|TPS')
    parser.add_argument('--FeatureExtraction', type=str, required=True, help='FeatureExtraction stage. VGG|RCNN|ResNet')
    parser.add_argument('--SequenceModeling', type=str, required=True, help='SequenceModeling stage. None|BiLSTM')
    parser.add_argument('--Prediction', type=str, required=True, help='Prediction stage. CTC|Attn')
    parser.add_argument('--num_fiducial', type=int, default=20, help='number of fiducial points of TPS-STN')
    parser.add_argument('--input_channel', type=int, default=1, help='the number of input channel of Feature extractor')
    parser.add_argument('--output_channel', type=int, default=512,
                        help='the number of output channel of Feature extractor')
    parser.add_argument('--hidden_size', type=int, default=256, help='the size of the LSTM hidden state')

    parser.add_argument('--page_orient', type=str, choices=['horizontal', 'vertical', 'single'], default='horizontal',
                        help='page orientation, or single char')

    args = parser.parse_args()
    return args

def get_box(db_path):
    boxes = list()
    with open(db_path, 'r', encoding='utf-8') as fp:
        for line in fp:
            line = line.strip().split(',')
            if len(line) % 2 == 1:
                line = line[:-1]
            line = list(map(int, line))
            l = min(line[::2])
            r = max(line[::2])
            u = min(line[1::2])
            d = max(line[1::2])
            boxes.append((l, u, r, d))
    return boxes

def get_gt(html_path, type):
    gt = []
    if type == 'dingxiu':
        with open(html_path, 'r', encoding='utf-8') as fp:
            try:
                html_file = fp.read()
            except UnicodeDecodeError:
                return gt

        soup = BeautifulSoup(html_file, 'lxml')
        lines = soup.find_all('div', {'class': 'linespan'})
        for line in lines:
            contents = line.contents
            cur_line = ''
            br_cnt = 0
            for content in contents:
                if isinstance(content, NavigableString):
                    cur_line += content
                    br_cnt = 0
                else:
                    br_cnt += 1
                    if br_cnt > 1 and len(cur_line) > 0:
                        gt.append(cur_line)
                        cur_line = ''
            if len(cur_line) > 0:
                gt.append(cur_line)
                cur_line = ''
    elif type == 'diaolong':
        pass
    else:
        raise ValueError
    return gt

def demo(args, PIL_image_list, model, AlignCollate_demo, converter):
    dataset = PILDataset(args, PIL_image_list)
    demo_loader = DataLoader(dataset, batch_size=args.batch_size, shuffle=False,
                             num_workers=int(args.workers), collate_fn=AlignCollate_demo,
                             pin_memory=True)
    total_preds_str = []
    with torch.no_grad():
        for image_tensors, image_path_list in demo_loader:
            batch_size = image_tensors.size(0)
            image = image_tensors.to(device)
            # For max length prediction
            length_for_pred = torch.IntTensor([args.batch_max_length] * batch_size).to(device)
            text_for_pred = torch.LongTensor(batch_size, args.batch_max_length + 1).fill_(0).to(device)

            if 'CTC' in args.Prediction:
                preds = model(image, text_for_pred)
                # Select max probabilty (greedy decoding) then decode index to character
                preds_size = torch.IntTensor([preds.size(1)] * batch_size)
                _, preds_index = preds.max(dim=2)
                # preds_index = preds_index.view(-1)
                preds_str = converter.decode(preds_index, preds_size)
            else:
                preds, alphas = model(image, text_for_pred, is_train=False)
                _, preds_index = preds.max(dim=2)
                preds_str = converter.decode(preds_index, length_for_pred)

            for pred in preds_str:
                if 'Attn' in args.Prediction:
                    pred_EOS = pred.find('[s]')
                    pred = pred[:pred_EOS]  # prune after "end of sentence" token ([s])
                total_preds_str.append(pred)
    return total_preds_str

def check_relax(gt, pred):
    if abs(len(gt) - len(pred)) >= 2:
        return 0
    dis = editdistance.distance(gt, pred)
    if dis == 0:
        return 2
    elif dis < max(len(gt), len(pred)) * 0.5:
        return 1
    else:
        return 0

def check_strict(gt, pred):
    if abs(len(gt) - len(pred)) >= 2:
        return 0
    dis = editdistance.distance(gt, pred)
    if dis == 0:
        return 2
    elif dis < max(len(gt), len(pred)) * 0.3:
        return 1
    else:
        return 0

def get_match_idx(preds, gts, strict=False):
    if strict:
        check = check_strict
    else:
        check = check_relax
    matching_idxs = []
    preds_len = len(preds)
    gts_len = len(gts)
    current_pred_idx = 0
    for current_gt_idx in range(gts_len):
        flag = 0
        current_gt = gts[current_gt_idx]
        while True:
            current_pred = preds[current_pred_idx]
            check_res = check(current_gt, current_pred)
            if check_res > 0:
                matching_idxs.append((current_pred_idx, current_gt_idx, check_res))
                break
            else:
                current_pred_idx += 1
                flag += 1
            # max forward match distance = 7
            # if match fail, go back 3
            if flag == 7 or current_pred_idx == preds_len:
                current_pred_idx -= 10
                current_pred_idx = max(0, current_pred_idx)
                break
    return matching_idxs

def get_match_img(args, model, AlignCollate_demo, converter,
                  img_path, db_path, html_path, current_cnt):
    match_cnt = 0
    acc_cnt = 0
    should_cnt = 0
    boxes = get_box(db_path)
    gts = get_gt(html_path, args.type)
    PIL_image_list = list()
    img = Image.open(img_path).convert('L')
    for box in boxes:
        crop_img = img.crop(box)
        # crop_img.show()
        PIL_image_list.append(crop_img)
    pred = demo(args, PIL_image_list, model, AlignCollate_demo, converter)
    matching_idx = get_match_idx(pred, gts, args.strict)
    for p_idx, g_idx, check_res in matching_idx:
        if check_res == 1:
            img = PIL_image_list[p_idx]
            img_gt = gts[g_idx]
            img_save_path = os.path.join(args.output_path, 'imgs', str(current_cnt) + '.jpg')
            img.save(img_save_path)
            gt_save_path = os.path.join(args.output_path, 'gts', str(current_cnt) + '.txt')
            with open(gt_save_path, 'w', encoding='utf-8') as fp:
                fp.write(img_gt + '\n')
            current_cnt += 1
            match_cnt += 1
            should_cnt += 1
        elif check_res == 2:
            if random.random() < args.hold_acc_prob:
                img = PIL_image_list[p_idx]
                img_gt = gts[g_idx]
                img_save_path = os.path.join(args.output_path, 'imgs', str(current_cnt) + '.jpg')
                img.save(img_save_path)
                gt_save_path = os.path.join(args.output_path, 'gts', str(current_cnt) + '.txt')
                with open(gt_save_path, 'w', encoding='utf-8') as fp:
                    fp.write(img_gt + '\n')
                current_cnt += 1
                match_cnt += 1
            should_cnt += 1
            acc_cnt += 1
        else:
            raise ValueError('check_res should be 1 or 2')
    return match_cnt, acc_cnt, should_cnt, current_cnt

def init_model(args):
    """ model configuration """
    if 'CTC' in args.Prediction:
        converter = CTCLabelConverter(args.character)
    else:
        converter = AttnLabelConverter(args.character)
    args.num_class = len(converter.character)

    if args.rgb:
        args.input_channel = 3
    model = Model(args)
    print('model input parameters', args.imgH, args.imgW, args.num_fiducial, args.input_channel, args.output_channel,
          args.hidden_size, args.num_class, args.batch_max_length, args.Transformation, args.FeatureExtraction,
          args.SequenceModeling, args.Prediction)
    try:
        model = torch.nn.DataParallel(model).to(device)
    except RuntimeError:
        raise RuntimeError(device)

    # load model
    print('loading pretrained model from %s' % args.saved_model)
    model.load_state_dict(torch.load(args.saved_model, map_location=device))

    # prepare data. two demo images from https://github.com/bgshih/crnn#run-demo
    AlignCollate_demo = AlignCollate(imgH=args.imgH, imgW=args.imgW, keep_ratio_with_pad=args.PAD)
    model.eval()
    return converter, model, AlignCollate_demo

if __name__ == '__main__':
    args = parse_args()
    done = get_already_done(args.done_path)
    print('{} imgs already done.'.format(len(done)))
    cnt = get_count(args.count_path)
    print('{} lines has been extracted.'.format(cnt))
    if args.todo_file is None:
        print('no todo_file in args. Scan input_path.')
        todo_list = list()
        for root, dirs, files in os.walk(args.input_path):
            for file in files:
                if os.path.splitext(file)[-1].lower() not in IMG_EXT:
                    continue
                file_path = os.path.join(root, file)
                if file_path in done:
                    continue
                todo_list.append(file_path)
    else:
        print('load todo_file from {}.'.format(args.todo_file))
        todo_list = [s.rstrip() for s in open(args.todo_file, 'r', encoding='utf-8').readlines()]
        todo_list = [s for s in todo_list if s not in done]
    print('{} files to do.'.format(len(todo_list)), flush=True)
    if args.shuffle:
        print('shuffle todo files.')
        random.shuffle(todo_list)
    os.makedirs(os.path.join(args.output_path, 'imgs'), exist_ok=True)
    os.makedirs(os.path.join(args.output_path, 'gts'), exist_ok=True)

    if args.character in ['CN-s', 'CN-m', 'CN-l', 'CN-xl']:
        size = args.character.split('-')[-1]
        with open('charset/charset_' + size + '.txt', 'r', encoding='utf-8') as chars:
            charset = [c.strip() for c in chars]
        charset = ''.join(charset)
        args.character = charset
    elif args.sensitive:
        # opt.character += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
        args.character = string.printable[:-6]  # same with ASTER setting (use 94 char).
    else:
        raise ValueError

    cudnn.benchmark = True
    cudnn.deterministic = True
    args.num_gpu = torch.cuda.device_count()

    converter, model, AlignCollate_demo = init_model(args)

    for file_path in todo_list:
        db_result_path = os.path.splitext(file_path)[0] + '.txt'
        if not os.path.isfile(db_result_path):
            continue
        html_gt_path = os.path.splitext(file_path)[0] + '.html'
        print('started from No. {} '.format(cnt), end='')
        match_cnt, acc_cnt, should_cnt, cnt = get_match_img(
            args, model, AlignCollate_demo, converter, file_path, db_result_path, html_gt_path, cnt
        )
        print('{} should get {} imgs, get {} match imgs, {} imgs perfectly right.'.format(
            file_path, should_cnt, match_cnt, acc_cnt)
        )
        with open(args.done_path, 'a', encoding='utf-8') as fp:
            fp.write(file_path + '\n')
        with open(args.count_path, 'w', encoding='utf-8') as fp:
            fp.write(str(cnt) + '\n')


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 17 09:35:56 2020

@author: ida
"""

import pandas as pd
import re
import os
import spacy
import numpy as np
from string import punctuation

def calculate_precision_recall_f1(true_label_df, predict_label_df):

    true_label_df['Offset-raw'] = true_label_df['Offset-raw'].str.lower()
    true_label_df['filename'] = true_label_df['filename'].apply(str)

    true_label_list = true_label_df.values.tolist()

    predict_label_df['Offset-raw'] = predict_label_df['Offset-raw'].str.lower()
    predict_label_list = predict_label_df.values.tolist()

    intersection = [value for value in predict_label_list if value in true_label_list]

    if len(predict_label_df) != 0:
        precision = (len(intersection)/len(predict_label_df))
        recall = (len(intersection)/len(true_label_df))
        f1 = (2*precision*recall)/(precision+recall)
        print('True Positive\t: ', str(len(intersection)))
        print('False Positive\t: ', str(len(predict_label_df) - len(intersection)))
        print('False Negative\t: ', str(len(true_label_df) - len(intersection)))
        print('Precision\t: ' + str(precision))
        print('Recall\t\t: ' + str(recall))
        print('F1-score\t: ' + str(f1))

    tpdf = pd.DataFrame(intersection, columns=['Offset-raw', 'filename', 'ConnSpanList'])
    return tpdf

def run_heuristic_pdtb(path):

    connective_path = '../../data/pdtb-annotation-manual.csv'

    nlp = spacy.load("en")

    connective_list = pd.read_csv(connective_path)['connective'].values.tolist()

    pred_conn = []
    for file in os.listdir(path):
        try:
            f = open(path+file, "r").read()
        except:
            f = open(path+file, 'r', encoding="utf-8", errors="replace").read()
        doc = nlp(f)
        filename = file.split(".")[0]
#        print(filename)
        found = False
        for sent in doc.sents:
#            print(sent)
            for conn in connective_list:
                conn_list = conn.split(';')
                if len(conn_list) == 1:
                    word_list = conn_list[0].split(" ")
                    if len(word_list) == 1:
                        for word in sent:
                            if word.text.lower()==conn_list[0]:
                                pred_conn.append([word.text.lower(), filename, str(word.idx)+".."+str(word.idx+len(word.text))])
                    else:
                        sent_split = sent.text.lower().split(conn_list[0])
                        cur_len = 0
                        for i in range(len(sent_split)-1):
                            cur_len += len(sent_split[i])
                            pred_conn.append([conn_list[0], filename, str(doc[sent.start].idx+cur_len)+".."+str(doc[sent.start].idx+cur_len+len(conn_list[0]))])
                            cur_len += len(conn_list[0])
                else:
                    sent_words = [x.text.lower() for x in sent]
#                    print(sent_words)
                    intersection = list(set(sent_words).intersection(set(conn_list)))
                    if len(intersection) == 2:
#                        print(intersection)
                        first_part_idx = len(sent.text.lower().split(conn_list[0]+" ")[0])
                        second_part_idx = len(sent.text.lower().split(conn_list[1]+" ")[0])
                        if first_part_idx < second_part_idx:
                            pred_conn.append([conn, filename, str(doc[sent.start].idx+first_part_idx)+".."+str(doc[sent.start].idx+first_part_idx+len(intersection[0]))+
                                              ";"+str(doc[sent.start].idx+second_part_idx)+".."+str(doc[sent.start].idx+second_part_idx+len(intersection[1]))])
                        else:
                            pred_conn.append([conn, filename, str(doc[sent.start].idx+second_part_idx)+".."+str(doc[sent.start].idx+second_part_idx+len(intersection[1]))+
                                              ";"+str(doc[sent.start].idx+first_part_idx)+".."+str(doc[sent.start].idx+first_part_idx+len(intersection[0]))])
                        found = True
#                        break
#                if found:
#                    break
#            if found:
#                break
    pred_df = pd.DataFrame(pred_conn, columns = ['Offset-raw', 'filename', 'ConnSpanList'])

    return pred_df
#%%
def extract_spice_pred(pred_df):
    nlp = spacy.load("en")
    raw_path = "../../data/SPICE/raw/"

    all_pred = pd.DataFrame()
    for each_file in pred_df['filename'].unique():
#        print(each_file)
        raw_file_path = raw_path + each_file + '.txt'
        pred_file = pred_df[pred_df['filename']==each_file]
        pred_file['start'] = [int(pred_file.loc[x, 'ConnSpanList'].split("..")[0]) for x in pred_file.index]
        pred_file = pred_file.sort_values(['start']).reset_index()
        try:
            f_string = open(raw_file_path, 'r').read()
        except:
            f_string = open(raw_file_path, 'r', encoding="utf-8", errors="replace").read()
        doc = nlp(f_string.strip())

        docs = [sent for sent in doc.sents]
        results = []
        doc_pivot = 0
        pred_pivot = 0
        while pred_pivot != len(pred_file):
            if (len(doc[:docs[doc_pivot].end].text) > pred_file.loc[pred_pivot]['start']):
                results.append(docs[doc_pivot].text)
                pred_pivot += 1
            else:
                doc_pivot += 1
        pred_file['fullSent'] = results
        all_pred = all_pred.append(pred_file, ignore_index=True)
    return all_pred

def evaluate_spice(gold_df, pred_conn):

    print('Evaluating result...')
    pred_conn = extract_spice_pred(pred_conn)

    pred_dict = {}
    for i in range(len(pred_conn)):
        filename = pred_conn.loc[i]["filename"]
        conn = pred_conn.loc[i]["Offset-raw"].lower().strip()
        fullSent = pred_conn.loc[i]["fullSent"].lower().strip(punctuation).strip()
        if filename not in pred_dict:
            pred_dict[filename] = {}
            pred_dict[filename][conn] = [fullSent]
        elif conn not in pred_dict[filename]:
            pred_dict[filename][conn] = [fullSent]
        else:
            pred_dict[filename][conn].append(fullSent)

    #create dictionary from the gold:
    gold_dict = {}
    for i in gold_df.index:
        filename = gold_df.loc[i]['filename']
        conn = gold_df.loc[i]['Offset-raw'].lower().strip()
        fullSent = gold_df.loc[i]['fullSent'].lower().strip(punctuation).strip()
        if filename not in gold_dict:
            gold_dict[filename] = {}
            gold_dict[filename][conn] = [fullSent]
        elif conn not in gold_dict[filename]:
            gold_dict[filename][conn] = [fullSent]
        else:
            gold_dict[filename][conn].append(fullSent)

    match_connective = []
    i = 0
    not_found = []
    for filename in gold_dict:
        for conn in gold_dict[filename]:
            for gold_sent in gold_dict[filename][conn]:
                found = False
                if filename in pred_dict:
                    if conn in pred_dict[filename]:
                        for pred_sent in pred_dict[filename][conn]:
                            if (pred_sent in gold_sent or pred_sent in gold_sent):
                                match_connective.append([filename, conn, pred_sent])
                                found = True
                                break
                if found == False:
                    not_found.append(conn)
                i+=1
#                if i%100 == 0:
#                    print(i)

    prec = len(match_connective)/len(pred_conn)
    rec = len(match_connective)/len(gold_df)
    print("precision\t: ", prec)
    print("recall\t: " , rec)
    print("f1-score\t: ", 2*prec*rec/(prec+rec))

    tpdf = pd.DataFrame(match_connective, columns=['filename', 'Offset-raw', 'fullSent'])

    return gold_df, tpdf, pred_conn

#%%
def get_false_negatif_connective(pred, gold):
    pred_list = pred['Offset-raw'].apply(str).str.lower().values.tolist()
    gold_list = gold['Offset-raw'].apply(str).str.lower().values.tolist()

    diff = list(set(gold_list) - set(pred_list))

    return diff

#%%
if __name__ == "__main__":
    biopath = "../../data/BioDRB/Genia Raw/"
    tedpath = "../../data/Ted-Talk/raw/"
    wsjpath = "../../data/wsj_23/raw/"

#    print("Evaluate biodrb...")
#    pred_bio = run_heuristic_pdtb(biopath)
#    gold_bio = pd.read_csv("../../result-csv/biodrb_gold.csv")
#    tp_bio = calculate_precision_recall_f1(gold_bio, pred_bio)
#
#    print("Evaluate ted...")
#    pred_ted = run_heuristic_pdtb(tedpath)
#    gold_ted = pd.read_csv("../../result-csv/ted_gold.csv")
#    tp_ted = calculate_precision_recall_f1(gold_ted, pred_ted)
#
#    print("Evaluate wsj...")
#    pred_wsj = run_heuristic_pdtb(wsjpath)
#    gold_wsj = pd.read_csv("../../../Result_v2/wsj_23_gold.csv")
#    tp_wsj = calculate_precision_recall_f1(gold_wsj, pred_wsj)
#%%
    print("Evaluate spice...")
    spicepath = "../../data/SPICE/raw/"
    pred_spice = run_heuristic_pdtb(spicepath)
    gold_spice = pd.read_csv("../../result-csv/spice_gold.csv")
    gold, tp_spice, pred = evaluate_spice(gold_spice, pred_spice)


from flask import Flask, render_template, request
import json
import urllib.request as requests
import urllib.parse
import ssl
import cgi
import requests as rq
import configparser

app = Flask(__name__, static_url_path='/static')

#api från themoviedb.com

form = cgi.FieldStorage()
searchterm =  form.getvalue('searchbox')

cfg = configparser.ConfigParser()
cfg.read('api_key.cfg')
api_key = cfg.get('KEYS', 'api_key', raw='')
rapid_api_key = cfg.get('KEYS', 'rapid_api_key', raw='')

base_url = 'https://api.themoviedb.org/3/'
search_URL = 'https://api.themoviedb.org/3/search/movie?api_key='+api_key+'&query=' #Sök URL för vårt API.
topMovie_url = 'https://api.themoviedb.org/3/movie/top_rated?api_key='+api_key+'&language=en-US'

url = 'https://streaming-availability.p.rapidapi.com/get/basic'

def getQueryString(movie_id):
    return {'country':'se','tmdb_id':'movie/{}'.format(movie_id)}

def getMovieURL(movie_id):
    return ('https://api.themoviedb.org/3/movie/{}?api_key='+api_key+'&language=en-US').format(movie_id)

headers = {
    'x-rapidapi-key': rapid_api_key,
    'x-rapidapi-host': 'streaming-availability.p.rapidapi.com'
    }

@app.route('/app')
def search_function():
    '''
    Hämtar användarens input från sökfunktionen i index.html för att sedan göra en sökning med hjälp av API.
    '''
    search = request.args.get('searchbox')
    ssl._create_default_https_context =  ssl._create_unverified_context
    search = urllib.parse.quote(search)
    conn = requests.urlopen(search_URL+search)
    json_data = json.loads(conn.read())
    return render_template('search.html', data=json_data['results'])

@app.route('/movie/<movie_id>')
def movie_details(movie_id):
    '''
    Test route för att hämta streaming tillgänglighet för film.
    '''
    ssl._create_default_https_context =  ssl._create_unverified_context
    conn = requests.urlopen(getMovieURL(movie_id))
    json_data = json.loads(conn.read())
    network = rq.request('GET', url, headers=headers, params=getQueryString(movie_id))
    return render_template('movie.html', data=json_data, network_data=json.loads(network.text))

@app.route('/faq')
def faq():
    ssl._create_default_https_context =  ssl._create_unverified_context
    return render_template('faq.html')

@app.route('/')
def index():
    ssl._create_default_https_context =  ssl._create_unverified_context
    return render_template('index.html')

@app.route('/contact')
def contact():
    ssl._create_default_https_context =  ssl._create_unverified_context
    return render_template('contact.html')

@app.route('/top_rated_movies')
def topMovie_function():
    '''
    Funktion för att generera top 20 filmer.
    '''
    ssl._create_default_https_context =  ssl._create_unverified_context
    conn = requests.urlopen(topMovie_url)
    json_data = json.loads(conn.read())
    return render_template('topMovies.html', data=json_data['results'])

@app.route('/top_netflix')
def top_netflix():
    ssl._create_default_https_context =  ssl._create_unverified_context
    return render_template('topNetflix.html')

@app.route('/top_prime')
def top_prime():
    ssl._create_default_https_context =  ssl._create_unverified_context
    return render_template('topPrime.html')

@app.route('/top_hbo')
def top_hbo():
    ssl._create_default_https_context =  ssl._create_unverified_context
    return render_template('topHBO.html')

@app.route('/top_disney')
def top_disney():
    ssl._create_default_https_context =  ssl._create_unverified_context
    return render_template('topDisney.html')

if __name__ == '__main__':
    app.run(debug=True)


import sys
sys.path.append('..')
import Praktikum
import numpy as np
import scipy.io.wavfile

def mkwav(fin, fout):
    c = Praktikum.lese_lab_datei(fin)

    t = c[:,1]
    a = c[:,2]

    scaled = np.int16(a/np.max(np.abs(a)) * 32767)
    print(1/(t[1]-t[0]))
    scipy.io.wavfile.write(fout, 1/(t[1]-t[0]), scaled)

#for i in xrange(1,4):
#    for j in range(1,7):
#        mkwav("rohdaten_ml/stab%d_%d.lab" % (i, j),'/tmp/test%d_%d.wav' % (i, j))

#for i in xrange(1,9):
#    mkwav("rohdaten_ml/schwebung%d.lab" % i, "/tmp/schwebung%d.wav" % i)

for i in xrange(1,3):
    mkwav("rohdaten_br/rfkomplett%d.lab" % i, "/tmp/rf%d.wav" % i)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('experiments', '__first__'),
        ('tags', '__first__'),
        ('labs', '__first__'),
    ]

    operations = [
        migrations.CreateModel(
            name='Unit',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('sample', models.CharField(max_length=4096, verbose_name='sample')),
                ('active', models.BooleanField(default=True, verbose_name='active')),
                ('description', models.TextField(null=True, verbose_name='description', blank=True)),
                ('experiments', models.ManyToManyField(to='experiments.Experiment', verbose_name='experiments')),
                ('lab', models.ForeignKey(verbose_name='lab', to='labs.Lab')),
                ('parent', models.ManyToManyField(db_index=True, related_name='children', null=True, to='units.Unit', blank=True)),
                ('tags', models.ManyToManyField(to='tags.Tag', null=True, verbose_name='tags', blank=True)),
            ],
            options={
                'verbose_name': 'unit',
                'verbose_name_plural': 'units',
            },
        ),
        migrations.CreateModel(
            name='UnitFile',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('file', models.FileField(upload_to=b'unit_files')),
                ('content_type', models.CharField(max_length=256, null=True, blank=True)),
                ('outer_url', models.URLField()),
                ('thumbnail', models.FileField(upload_to=b'unit_thumbs')),
                ('outer_thumbnail_url', models.URLField()),
                ('timestamp', models.DateTimeField(auto_now=True)),
                ('parent', models.ForeignKey(verbose_name='unit', to='units.Unit')),
            ],
        ),
        migrations.CreateModel(
            name='UnitLink',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('link', models.URLField(verbose_name='url')),
                ('timestamp', models.DateTimeField(auto_now=True)),
                ('image', models.URLField(null=True, verbose_name='image', blank=True)),
                ('title', models.TextField(null=True, verbose_name='title', blank=True)),
                ('canonicalUrl', models.URLField(null=True, verbose_name='canonicalUrl', blank=True)),
                ('description', models.TextField(null=True, verbose_name='description', blank=True)),
                ('parent', models.ForeignKey(verbose_name='unit', to='units.Unit')),
            ],
        ),
    ]


"""

'r' (default) - leitura
'w' - escrita, se o arquivo existir, ele escreve por cima
'x' - abre para escrita, somente se o arquivo não existir... se existir, ele dá erro...
'a' - ele cria o arquivo se não existir, e ele existir, ele adiciona o conteúdo ...
mas adiciona sempre no FIM (default)
'+' exemplo: 'r+' - abre para o modo de atualização de leitura ou escrita...

"""
#Vamos testar o 'x'
try:
    with open('novo.txt', 'x') as arquivo:
        arquivo.write('Hello World kkkk')

except:
    print('O arquivo já existe...')

#Vamos testar o 'a'
arquivo = open('novo.txt','a')

arquivo.write('Mais um\n') #Por padrão adiciona no fim do arquivo

#quero adicionar no início... use o Seek lembra?
arquivo.seek(0)
arquivo.write('No início agora \n')

arquivo.close()

arq = open('new.txt','w')
arq.write('oi \ntchau \n')
arq.seek(0)
arq.close()

arq = open('new.txt','a')
arq.write("agora eu escrevi no inicio \n")
arq.close()


__author__ = 'Liang Li'
class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        max_profit1 = 0
        max_profit2 = 0
        min_price1 = (2 << 31)-1
        min_price2 = (2 << 31)-1
        for price in prices:
            max_profit2 = max(max_profit2, price-min_price2)
            min_price2 = min(min_price2, price-max_profit1)
            max_profit1 = max(max_profit1, price-min_price1)
            min_price1 = min(min_price1, price)
        return max_profit2


import actions

# A list of identifiers and member values that may not be used.
BANNED_IDENTIFIERS = ("newThread", )

# For "dangerous" elements, specifying True will throw an error on all
# detected instances of the particular object. Specifying a lambda function
# will allow the object to be referenced. If the object is called via a
# CallExpression, "a" will contain the raw arguments values and "t" will
# contain a reference to traverser._traverse_node(). "t" will always return a
# JSWrapper object. The return value of the lambda function will be used as
# the value for the "dangerous" property. Lastly, specifying a string functions
# identically to "True", except the string will be outputted when the error is
# thrown.

# GLOBAL_ENTITIES is also representative of the `window` object.
GLOBAL_ENTITIES = {
    "window": {"value": lambda: GLOBAL_ENTITIES},
    "document":
        {"value": {"createElement":
                       {"dangerous": lambda a,t: t(a[0]).get_literal_value() == "script"},
                   "createElementNS":
                       {"dangerous": lambda a,t: t(a[0]).get_literal_value() == "script"}}},

    # The nefariuos timeout brothers!
    "setTimeout": {"dangerous": actions._call_settimeout},
    "setInterval": {"dangerous": actions._call_settimeout},

    "encodeURI": {"readonly": True},
    "decodeURI": {"readonly": True},
    "encodeURIComponent": {"readonly": True},
    "decodeURIComponent": {"readonly": True},
    "escape": {"readonly": True},
    "unescape": {"readonly": True},
    "isFinite": {"readonly": True},
    "isNaN": {"readonly": True},
    "parseFloat": {"readonly": True},
    "parseInt": {"readonly": True},

    "eval": {"dangerous": True},
    "Function": {"dangerous": True},
    "Object": {"value": {"prototype": {"dangerous": True},
                         "constructor": # Just an experiment for now
                             {"value":lambda:GLOBAL_ENTITIES["Function"]}},},
    "String": {"value": {"prototype": {"dangerous": True}}},
    "Array": {"value": {"prototype": {"dangerous": True}}},
    "Number": {"value": {"prototype": {"dangerous": True}}},
    "Boolean": {"value": {"prototype": {"dangerous": True}}},
    "RegExp": {"value": {"prototype": {"dangerous": True}}},
    "Date": {"value": {"prototype": {"dangerous": True}}},

    "Math": {"readonly": True},

    "netscape":
        {"value": {"security":
                       {"value": {"PrivilegeManager":
                                      {"value": {"enablePrivilege":
                                                     {"dangerous": True}}}}}}},

    "navigator":
        {"value": {"wifi": {"dangerous": True},
                   "geolocation": {"dangerous": True}}},

    "Components":
        {"readonly": True,
         "value": {"utils":
                       {"value": {"evalInSandbox":
                                     {"dangerous": True},
                                  "import":
                                     {"dangerous":
                                         lambda a,t: a and a[0].contains("ctypes.jsm")}}},
                   "interfaces":
                       {"value": {"nsIProcess":
                                     {"dangerous": True},
                                  "nsIDOMGeoGeolocation":
                                     {"dangerous": True},
                                  "nsIX509CertDB":
                                     {"dangerous": True},
                                  "mozIJSSubScriptLoader":
                                     {"dangerous": True}}}}},
    "extensions": {"dangerous": True},
    "xpcnativewrappers": {"dangerous": True},

    # Global properties are inherently read-only, though this formalizes it.
    "Infinity": {"readonly": True},
    "NaN": {"readonly": True},
    "undefined": {"readonly": True},
    }


from django.conf.urls import include, url
from django.conf import settings
urlpatterns = [

    url(r'^', include('fform.urls')),
    url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
]
if not settings.DEBUG:
        urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)


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

def f(grid):
    def neighbor(grid, i, j):
        for x, y in [(i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)]:
            if 0 <= x < len(grid) and 0 <= y < len(grid[0]):
                yield (x, y)

    N = len(grid)
    currentNodes = set([(0, 0)])
    ans = grid[0][0]
    seen = set()

    while currentNodes:
        m = None
        v = None

        for n in currentNodes:
            if m is None:
                m = n
                v = grid[m[0]][m[1]]
            else:
                if v > grid[n[0]][n[1]]:
                    m = n
                    v = grid[n[0]][n[1]]

        currentNodes.remove(m)
        seen.add(m)

        if m[0] == N - 1 and m[1] == N - 1:
            return max(ans, v)

        for i, j in neighbor(grid, m[0], m[1]):
            if (i, j) in seen:
                continue
            currentNodes.add((i, j))
        ans = max(ans, v)

    return ans

grid = [[0,1],[2,3]]

print(f(grid))

grid = [[0,1,2,3,4],[24,23,22,21,5],[12,13,14,15,16],[11,17,18,19,20],[10,9,8,7,6]]
print(f(grid))


#!/usr/bin/env python3

import sys
import getopt
import argparse
import requests

action_add = 'add'
action_remove = 'remove'

def main():
    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers(title='action', dest='action',
                                       help='which action to perform',
                                       required=True)
    parser.add_argument('-s', '--server', help='url of the repo server',
                        default='https://repo.zenlinux.net')

    add_parser = subparsers.add_parser(action_add)
    add_parser.add_argument('pkg', help='path to the pkg file to add',
                            type=argparse.FileType('rb'))

    remove_parser = subparsers.add_parser(action_remove)
    remove_parser.add_argument('pkg', help='name of the pkg to remove')

    args = parser.parse_args()

    if args.action == action_add:
        add(args.pkg, args.server)
    elif args.action == action_remove:
        remove(args.pkg, args.server)

def add(pkg_file, server):
    response = requests.post(f'{server}/add', files={'file': pkg_file})
    print(response.text)

def remove(pkg_name, server):
    response = requests.post(f'{server}/remove', data={'pkg': pkg_name})
    print(response.text)

if __name__ == "__main__":
    main()


import socket

adr_server = ("localhost",10000)

def Controllo() -> str:
    cond = True
    while cond:
        s = input("> ")
        if s == "w" or s == "s" or s == "a" or s == "d":
            cond = False
            return s
        elif s == "esc":
            return s
        else:
            print("valore sbagliato")

with socket.socket(socket.AF_INET,socket.SOCK_DGRAM) as s:
    while True:

        stringa = Controllo()
        s.sendto(stringa.encode("ascii"), (adr_server))


class Solution:
    def rob(self, nums: List[int]) -> int:
        houseNum = len(nums)  # 房屋数量

        if houseNum == 0:
            return 0
        elif houseNum == 1:
            return nums[0]

        # 基本思路：
        # 1、第一个房子要是被偷窃，最后一个房子就不能被偷窃。同理，最后一个房子要是被偷窃，第一个房子就不能被偷窃
        # 2、其他房子，分为偷窃和不偷窃两种情况

        # firstcount[i]:代表在偷窃第一个房子的前提下，从第1个房子~第i个房子，盗匪能获取的最大利润
        # 此时最后一个房子一定不能被偷窃
        firstcount = [0] * (houseNum + 1)
        firstcount[1] = nums[0]
        for index in range(2, houseNum):
            firstcount[index] = max(firstcount[index - 1], firstcount[index - 2] + nums[index - 1])

        # notFirstcount[i]:代表在不偷窃第一个房子的前提下，从第1个房子~第i个房子，盗匪能获取的最大利润
        # 此时最后一个房子被偷窃,第一个房子不能被偷窃。其实就是按照上面的计算方法,逆序进行计算即可
        notFirstcount = [0] * (houseNum + 1)
        notFirstcount[houseNum - 1] = nums[houseNum - 1]
        for index in range(houseNum - 2, 0, -1):
            notFirstcount[index] = max(notFirstcount[index + 1], notFirstcount[index + 2] + nums[index])

        return max(notFirstcount[1], firstcount[-2])