import math
from time import time as _time
from typing import Generator, Tuple

_YEAR = 33177600
_MOON = 2764800
_DAY = 86400
_HOUR = 3600
_MINUTE = 60
_SECOND = 1
_EORZEA_MINUTE = 60
_EORZEA_BELL = 60
_EORZEA_SUN = 24
_EORZEA_MOON = 32
_EORZEA_YEAR = 12
_EORZEA_TIME_CONST = 3600.0 / 175.0
_MILLISECOND_EORZEA_PER_MINUTE = (2 + 11/12) * 1000

class EorzeaTime:
    """
    EorzeaTime(moon, sun, hour, minute)
    """

    __slots__ = '_moon', '_sun', '_hour', '_minute', '_phase', '_guardian'

    def __new__(cls, moon: int, sun: int, hour: int, minute: int):
        instance = super().__new__(cls)
        instance._hour, instance._minute = _check_time_field(hour, minute)
        moon, instance._sun = _check_date_field(moon, sun)
        instance._moon = _calculate_moon(moon)
        instance._phase = _check_phase_field(_calculate_phase(sun))
        instance._guardian = _the_twelve(moon)
        return instance

    @property
    def moon(self):
        return self._moon

    @property
    def sun(self):
        return self._sun

    @property
    def hour(self):
        return self._hour

    @property
    def minute(self):
        return self._minute

    @property
    def phase(self):
        return self._phase

    @property
    def guardian(self):
        return self._guardian

    @classmethod
    def now(cls):
        """
        get current EorzeaTime

        Returns
        -------
        [EorzeaTime object]
            current EorzeaTime
        """

        t = _time()
        return cls._fromtimestamp(t)

    @classmethod
    def _fromtimestamp(cls, t: float):
        et = t * _EORZEA_TIME_CONST
        moon = math.ceil(et / _MOON % _EORZEA_YEAR)
        sun = math.ceil(et / _DAY % _EORZEA_MOON)
        hh = int(et / _HOUR % _EORZEA_SUN)
        mm = int(et / _MINUTE % _EORZEA_BELL)
        return cls(moon, sun, hh, mm)

    @classmethod
    def weather_period(cls, step=5) -> Generator[float, int, None]:
        """
        generate weather period

        Parameters
        ----------
        step : int, optional
            quantity of period you want, by default 5

        Returns
        -------
        Generator[float, int, None]
            a generator of weather period
        """

        period = _weather_period_generator(cls._weather_period_start(), step)
        return period

    @classmethod
    def _weather_period_start(cls) -> float:
        t = _time()
        et = t * _EORZEA_TIME_CONST
        lt = int(et / 28800) * 28800 / _EORZEA_TIME_CONST
        return lt

    def _cls_to_str(self) -> str:
        return "{}({}, {}, {:02d}, {:02d}, {:.2f}, {})".format(
            self.__class__.__qualname__,
            self.moon, self.sun,
            self.hour, self.minute, self.phase,
            self.guardian)

    def __repr__(self):
        return self._cls_to_str()

    def __str__(self):
        return self._cls_to_str()

def check_int(func):
    def wrap(*values):
        for value in values:
            if not isinstance(value, int):
                raise TypeError("integer argument required")
        return func(*values)
    return wrap

def _weather_period_generator(min, step: int) -> Generator[float, int, None]:
    if not isinstance(step, int):
        raise TypeError("integer argument required")
    n, i = 0, min
    while n < step:
        yield i
        i += 1400
        n += 1

def _the_twelve(moon: int) -> str:
    the_twelve = [
        "Halone",
        "Menphina",
        "Thaliak",
        "Nymeia",
        "Llymlaen",
        "Oschon",
        "Byregot",
        "Rhalgr",
        "Azeyma",
        "Nald'thal",
        "Nophica",
        "Althyk"
    ]
    return the_twelve[moon - 1]

def _calculate_moon(moon: int) -> str:
    th = ["First", "Second", "Third", "Fourth", "Fifth", "Sixth"]
    M_th = th[math.ceil(moon / 2) - 1]
    M_type = _astral_or_embral(moon)
    return "{} {} Moon".format(M_th, M_type)

def _astral_or_embral(moon: int) -> str:
    return "Astral" if moon % 2 else "Embral"

def _calculate_phase(sun: int) -> int:
    if sun <= 20:
        return 1 - int(abs(20 - sun) / 4) / 4
    if sun > 20:
        return int(abs(36 - sun) / 4) / 4

def _calculate_pphase(sun: int) -> int:
    if sun <= 20:
        return round((sun - 1) / 19, 2)
    if sun >= 20:
        return round(1 - (sun - 20) / 13, 2)

@check_int
def _check_time_field(hour: int, minute: int) -> Tuple[int, int]:
    if not 0 <= hour <= 23:
        raise ValueError('hour must be in 0..23', hour)
    if not 0 <= minute <= 59:
        raise ValueError('minute must be in 0..59', minute)
    return hour, minute

@check_int
def _check_date_field(moon: int, sun: int) -> Tuple[int, int]:
    if not 1 <= moon <= 12:
        raise ValueError('moon must be in 1..12', moon)
    if not 1 <= sun <= 32:
        raise ValueError('sun must be in 1..32', sun)
    return moon, sun

def _check_phase_field(phase: float) -> float:
    if not 0 <= phase <= 1:
        raise ValueError('phase must be in 0..1', phase)
    return phase

if __name__ == "__main__":
    while True:
        t = str(EorzeaTime.now())
        print("\r"+t, end="")


from doubly import Link
from functools import partial
from util.link import links_from_list

def test_iter():
    linker = partial(links_from_list, Link)
    ll = linker([2, 4])
    assert set(ll) == {2, 4}

def test_insert():
    ll = Link()
    assert not ll.search(5)

    ll.insert(Link(5))
    assert ll.search(5).datum == 5

def test_search():
    linker = partial(links_from_list, Link)
    ll = linker([2, 4])

    assert not ll.search(1)
    assert not ll.search(3)
    assert ll.search(2)
    assert ll.search(4)

def test_delete():
    linker = partial(links_from_list, Link)
    ll = linker([2])

    assert ll.search(2)

    ll.delete(Link(2))
    assert not ll.search(2)


# Copyright (c) 2020  PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

from ...registry import RECOGNIZERS
from .base import BaseRecognizer
import paddle
from paddlevideo.utils import get_logger

logger = get_logger("paddlevideo")

@RECOGNIZERS.register()
class Recognizer2D(BaseRecognizer):
    """2D recognizer model framework."""

    def forward_train(self, imgs, labels, reduce_sum, **kwargs):
        """Define how the model is going to train, from input to output.
        """
        # As the num_segs is an attribute of dataset phase, and didn't pass to build_head phase, should obtain it from imgs(paddle.Tensor) now, then call self.head method.

        batches = imgs.shape[0]
        imgs = paddle.reshape(imgs, [-1]+imgs.shape[2:])
        num_segs = imgs.shape[0] // batches
        feature = self.extract_feature(imgs)
        cls_score = self.head(feature, num_segs)
        #labels = labels.squeeze()
        #XXX: unsqueeze label to [label] ?
        loss_metrics = self.head.loss(cls_score, labels, reduce_sum, **kwargs)
        return loss_metrics

    def forward_valid(self, imgs):
        """Define how the model is going to valid, from input to output."""
        #XXX add testing code.
        imgs = paddle.reshape(imgs, [-1]+imgs.shape[2:])
        num_segs = imgs.shape[0] // batches
        feature = self.extract_feature(imgs)
        cls_score = self.head(feature, num_segs)

        return cls_score


import matplotlib.pyplot as plt
import numpy as np

#x = np.random.normal(size = 1000)
#plt.hist(x, density=True, bins=30) # density
#plt.ylabel('Probability')
#plt.show()

x = np.arange(2)
plt.title("Primary Key constraint v2 performance")
barlist = plt.bar(x, height= [150,18])
plt.xticks(x, ['success','invalid'])

plt.ylabel(ylabel = "Time (ms)")
plt.xlabel(xlabel = "Result of the check")

barlist[0].set_color('g')
barlist[1].set_color('r')
plt.show()


# Copyright 2021 auto_anki
# MIT License

""" wordprocessing.py """
import string
from collections import OrderedDict, Counter
import sys
import re
import spacy
import numpy as np

def keyword_extractor(data: list) -> list:
    """
    Function to extract keywords from the headers and paragraphs of slides

    :param data: The list of dictionaries of the form
    :type: [{"Header":"", "Paragraph":"", slide:int}]
    :return: The list of dictionaries with keywords extracted of the form
    :rtype: [{"Header":"",
            "Paragraph":"",
            "Header_keywords": [],
            "Paragraph_keywords": [],
            slide:int}]
    """
    try:
        nlp = spacy.load("en_core_web_lg")
    except OSError:
        sys.exit()
    pos_tag = ["NOUN"]
    dep_tag = ["nsubj"]
    for slide in data:
        doc_header = nlp(slide["Header"].lower())
        doc_paragraph = nlp(slide["Paragraph"].lower())
        header_keywords = []
        paragraph_keywords = []
        for token in doc_header:
            if token.text in nlp.Defaults.stop_words or token.is_punct:
                continue
            if token.pos_ in pos_tag or token.dep_ in dep_tag:
                word = re.sub(r"[^0-9a-zA-Z]+", " ", token.text)
                word = word.strip()
                if len(word) >= 3:
                    header_keywords.append(word)
        for token in doc_paragraph:
            if token.text in nlp.Defaults.stop_words or token.is_punct:
                continue
            if token.pos_ in pos_tag or token.dep_ in dep_tag:
                word = re.sub(r"[^a-zA-Z]+", " ", token.text)
                word = word.strip()
                if len(word) >= 3:
                    paragraph_keywords.append(word)
        slide["Header_keywords"] = header_keywords
        slide["Paragraph_keywords"] = paragraph_keywords
    return data

def duplicate_word_removal(data: list) -> list:
    """
    Removes duplicate words

    :param data: The list of dictionaries of the form
    :type: [{"Header":"", "Header_keywords": [], "Paragraph_keywords": [], slides:[int]}]
    :return: The list of dictionaries with duplicate keywords removed of the form
    :rtype: [{"Header":"", "Header_keywords": [], "Paragraph_keywords": [], slides:[int]}]

    """
    for dictionary in data:
        ordered_headers = list(OrderedDict.fromkeys(dictionary['Header_keywords']))
        dictionary['Header_keywords'] = ordered_headers

        ordered_paragraph = list(OrderedDict.fromkeys(dictionary['Paragraph_keywords']))
        dictionary['Paragraph_keywords'] = ordered_paragraph
    return data

def merge_slide_with_same_headers(data: list) -> list:
    """
    Function to merge slides with the same header.

    :param data: The list of dictionaries of the form
    :type: [{"Header":"",
            "Paragraph":"",
            "Header_keywords": [],
            "Paragraph_keywords": [],
            slide:int}]
    :return: The list of dictionaries where slides containing the same header are merged
    :rtype: [{"Header":"", "Header_keywords": [], "Paragraph_keywords": [], slides:[int]}]
    """
    merged = []
    headers = []
    for slide in data:
        if slide["Header"] not in headers:
            headers.append(slide["Header"])
            paragraph_keywords = []
            slide_numbers = []
            for data_1 in [data_2 for data_2 in data if data_2["Header"] == slide["Header"]]:
                paragraph_keywords += data_1["Paragraph_keywords"]
                slide_numbers.append(data_1["slide"])
            merged.append({"Header": slide["Header"], "Header_keywords": slide["Header_keywords"],
                           "Paragraph_keywords": paragraph_keywords, "slides": slide_numbers})
    return merged

def merge_slide_with_same_slide_number(data: list) -> list:
    """
    Function to merge slides with the same slide number into a single one.

    :param data: The list of dictionaries of the form
    :type: [{"Header":"",
            "Paragraph":"",
            "Header_keywords": [],
            "Paragraph_keywords": [],
            slide:int}]
    :return: The list of dictionaries where slides containing the same slide number are merged
    :rtype: [{"Header":"", "Header_keywords": [], "Paragraph_keywords": [], slide:int}]
    """
    merged = []
    slide_number = []
    for slide in data:
        if slide["slide"] not in slide_number:
            slide_number.append(slide["slide"])
            header_keywords = []
            paragraph_keywords = []
            for data_1 in [data_2 for data_2 in data if data_2["slide"] == slide["slide"]]:
                header_keywords += data_1["Header_keywords"]
                paragraph_keywords += data_1["Paragraph_keywords"]
            merged.append({"Header": slide["Header"], "Header_keywords": header_keywords,
                           "Paragraph_keywords": paragraph_keywords,
                           "slide": slide["slide"]})
    return merged

def construct_search_query(data: list) -> list:
    """
    Constructs a search query given a PDF data

    :param data: The list of data
    :type: list
    :return: List of words to search
    :rtype: list
    """
    header_keywords = []
    paragraph_keywords = []
    for item in data:
        header_keywords += item["Header_keywords"] * len(item["slides"])
        paragraph_keywords += item["Paragraph_keywords"] * len(item["slides"])
    header_counts = Counter(header_keywords)
    paragraph_counts = Counter(paragraph_keywords)
    header_mean = np.array(list(header_counts.values())).mean()
    paragraph_mean = np.array(list(paragraph_counts.values())).mean()
    header_search = []
    paragraph_search = []
    for key, value in header_counts.items():
        if value > header_mean:
            header_search.append(key)
    for key, value in paragraph_counts.items():
        if value > paragraph_mean:
            paragraph_search.append(key)
    return header_search + paragraph_search

def extract_noun_chunks(data: list) -> list:
    """

    Extracts nouns using Spacy

    :param data: list of PDF data
    :type: list
    :return: list of data with nouns extracted
    :rtype: list
    """
    try:
        nlp = spacy.load("en_core_web_lg")
    except OSError:
        sys.exit()
    for slide in data:
        doc_header_noun_chunks = nlp(slide["Header"].lower()).noun_chunks
        doc_paragraph_noun_chunks = nlp(slide["Paragraph"].lower()).noun_chunks
        header_keywords = []
        paragraph_keywords = []
        for token in doc_header_noun_chunks:
            processed_words = []
            words = token.text.split()
            for word in words:
                word = re.sub(r"[^a-zA-Z]+", "", word).strip()
                if word in nlp.Defaults.stop_words or word in string.punctuation:
                    continue
                if len(word) >= 3:
                    processed_words.append(word)
            if len(processed_words) >= 2:
                header_keywords.append(" ".join(processed_words))
        for token in doc_paragraph_noun_chunks:
            processed_words = []
            words = token.text.split()
            for word in words:
                word = re.sub(r"[^a-zA-Z]+", "", word).strip()
                if word in nlp.Defaults.stop_words or word in string.punctuation:
                    continue
                if len(word) >= 3:
                    processed_words.append(word)
            if len(processed_words) >= 2:
                paragraph_keywords.append(" ".join(processed_words))
        slide["Header_keywords"] = header_keywords
        slide["Paragraph_keywords"] = paragraph_keywords
    return data


import tensorflow as tf
import numpy as np

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

save_file = './model.ckpt'

sess = tf.InteractiveSession()

x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])

x_image = tf.reshape(x, [-1,28,28,1])

def max_pool_2x2(x):
  return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
                        strides=[1, 2, 2, 1], padding='VALID')

def weight_variable(shape):
  initial = tf.truncated_normal(shape, stddev=0.1)
  return tf.Variable(initial)

def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)

def conv2d(x, W):
  return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='VALID')

kernel_size = 3
databit = 255
precision = 5

conv_filter1 = 8
conv_filter2 = 16

unit1 = 120
unit2 = 84

W_conv1 = weight_variable([kernel_size, kernel_size, 1, conv_filter1])
b_conv1 = bias_variable([8])

#h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_conv1 = conv2d(x_image, W_conv1) + b_conv1
h_conv1 = tf.nn.relu(h_conv1)
h_pool1 = max_pool_2x2(h_conv1)

W_conv2 = weight_variable([kernel_size, kernel_size, conv_filter1, conv_filter2])
b_conv2 = bias_variable([16])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)

h_pool2_flat = tf.reshape(h_pool2, [-1, 5*5*conv_filter2])
W_fc1 = weight_variable([5 * 5 * conv_filter2, unit1])
b_fc1 = bias_variable([unit1])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)

keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

W_fc2 = weight_variable([unit1, unit2])
b_fc2 = bias_variable([unit2])
h_fc2 = tf.nn.relu(tf.matmul(h_fc1, W_fc2) + b_fc2)

W_fc3 = weight_variable([unit2, 10])
b_fc3 = bias_variable([10])
y_conv=tf.nn.softmax(tf.matmul(h_fc2, W_fc3) + b_fc3)

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[1]))

train_step = tf.train.AdamOptimizer(0.0001).minimize(cross_entropy)

correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess.run(tf.global_variables_initializer())

saver = tf.train.Saver()

for i in range(0, 12000):
  batch = mnist.train.next_batch(100)
  if i%100 == 0:

    #train_accuracy = accuracy.eval(feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0})

    train_accuracy = accuracy.eval(feed_dict={x:batch[0], y_: batch[1]})

    #if i % display_step == 0 or i == 1:
    #    saver.save(sess, './save/mdc_session', global_step=i)

    print("step %d, training accuracy %g"%(i, train_accuracy))

  if i%2000 == 0:
      W_conv1_re = sess.run(W_conv1).reshape(9, 8)
      np.savetxt('./mnist_training_weight/W_conv1_int_%d.txt'%(i), W_conv1_re)
      W_conv2_re = sess.run(W_conv2).reshape(9, 128)
      np.savetxt('./mnist_training_weight/W_conv2_int_%d.txt'%(i), W_conv2_re)
      np.savetxt('./mnist_training_weight/W_fc1_int_%d.txt'%(i), sess.run(W_fc1))
      np.savetxt('./mnist_training_weight/W_fc2_int_%d.txt'%(i), sess.run(W_fc2))
      np.savetxt('./mnist_training_weight/W_fc3_int_%d.txt'%(i), sess.run(W_fc3))

  #train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
  train_step.run(feed_dict={x: batch[0], y_: batch[1]})
saver.save(sess, save_file)
#print("test accuracy %g"%accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))

print("test accuracy %g"%accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels}))


'''
Author: Aslan
Type: Practice, also a useless implementation. This problem is not categorical but my attempt is for categorical...
What: Deep-learning, policy gradient, gradient descent
Entropy: https://towardsdatascience.com/demystifying-entropy-f2c3221e2550
Cross entropy: https://towardsdatascience.com/demystifying-cross-entropy-e80e3ad54a8, https://ml-cheatsheet.readthedocs.io/en/latest/loss_functions.html
'''

import tensorflow as tf
import numpy as np
import gym

# Openai
env = gym.make('Pendulum-v0')
env = env.unwrapped
env.seed(1)

# Parameters
state_size = 3
action_size = 2
max_epochs = 200
learnRate = 0.01
gamma = 0.95

# Reward handling
def rewardHandle(reward_to_handle):
    adjusted_rewards = np.zeros_like(reward_to_handle)
    cumulative = 0.0
    for i in reversed(range(len(reward_to_handle))):
        cumulative = cumulative * gamma + reward_to_handle[i]
        adjusted_rewards[i] = cumulative
    mean = np.mean(adjusted_rewards)
    std = np.std(adjusted_rewards)
    adjusted_rewards = (adjusted_rewards - mean) / std

    return adjusted_rewards

with tf.name_scope("inputs"):
    input_ = tf.placeholder(tf.float32, [None, state_size], name="input_")
    actions = tf.placeholder(tf.float32, [None, action_size], name="actions")
    adjusted_rewards = tf.placeholder(tf.float32, [None,], name="adjusted_rewards")

    # Build layers
    with tf.name_scope("f1"):
        fc1 = tf.contrib.layers.fully_connected(inputs=input_,
                                                num_outputs=16,
                                                activation_fn=tf.nn.relu,
                                                weights_initializer=tf.contrib.layers.xavier_initializer())

    with tf.name_scope("f2"):
        fc2 = tf.contrib.layers.fully_connected(inputs=fc1,
                                                num_outputs=16,
                                                activation_fn=tf.nn.relu,
                                                weights_initializer=tf.contrib.layers.xavier_initializer())

    with tf.name_scope("f3"):
        fc3 = tf.contrib.layers.fully_connected(inputs=fc2,
                                                num_outputs=action_size,
                                                activation_fn=None,
                                                weights_initializer=tf.contrib.layers.xavier_initializer())

    with tf.name_scope("softmax"):
        action_distribution = tf.nn.softmax(fc3)

    with tf.name_scope("loss"):
        neg_log_prob = tf.nn.softmax_cross_entropy_with_logits_v2(logits=fc3, labels=actions)
        loss = tf.reduce_mean(neg_log_prob * adjusted_rewards)

    with tf.name_scope("train"):
        train_opt = tf.train.AdamOptimizer(learning_rate=learnRate).minimize(loss)

# Training
ep_states, ep_actions, ep_rewards = [], [], []
max_reward = 0

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for epochs in range(max_epochs):
        state = env.reset()
        env.render()

        for _ in range(200):

            # non-deterministic env
            actions_prob_dist = sess.run(action_distribution, feed_dict={input_: state.reshape([1, 3])})
            action = np.random.choice(range(actions_prob_dist.shape[1]), p=actions_prob_dist.ravel())

            if action == 0:
                oAction = [1,-2,2]
            else:
                oAction = [-1,-2,2]

            new_state, reward, done, extra = env.step(oAction)
            ep_states.append(state)
            action_ = np.zeros(action_size)
            action_[action] = 1
            ep_actions.append(action_)
            ep_rewards.append(reward)

        input_reward = rewardHandle(ep_rewards)
        sess.run(train_opt, feed_dict={input_: np.vstack(np.array(ep_states)),
                                        actions: np.vstack(np.array(ep_actions)),
                                        adjusted_rewards: input_reward})

        max_reward = max(max_reward, np.sum(ep_rewards))
        print("Max reward so far: ", max_reward)
        ep_states, ep_actions, ep_rewards = [], [], []
        state = new_state


import heapq
N, M = map(int, input().split())
if N >= M:
    print(0)
    exit()
X = list(map(int, input().split()))
X.sort(reverse=True)
dist = []
for i in range(1, M):
    dist.append(X[i] - X[i - 1])
dist = list(map(lambda x: (-1) * abs(x), dist))
heapq.heapify(dist)
#print(dist)
for _ in range(N - 1):
    heapq.heappop(dist)
ans = -1 * sum(dist)
print(ans)


from django.db import models
from django.contrib.comments.models import Comment
from django.db.models.signals import pre_save, post_save, post_delete
from customsetting.utility import remove_bad_char
from django.db import close_connection
from django.core.cache import cache

def count_comments( sender, instance, **kwargs ):
    """
    Signal Listener to count all comments of a particular question whenever a comment is posted.
    """

    if instance.content_type.name == 'question':
        from content.models import Question
#        close_connection()
        count = Comment.objects.using( 'htcMaster' ).filter( object_pk = instance.object_pk, content_type = instance.content_type ).count()
        question = Question.objects.get( pk = instance.object_pk )
        question.anscount = count
        question.save()
    if instance.content_type.name in ['article','question'] and cache.has_key( 'question_article_frontend' ):
        cache.delete( 'question_article_frontend' )

def modify_comment( sender, instance, **kwargs ):
    #Truncate the URLS and Phone Numbers when Question is posted.
    #Get the question object
    from content.models import Question
    if isinstance( instance, Question ):
        if not instance.author.is_staff:
            instance.content = remove_bad_char( instance.content )
    else:
        if not instance.user.is_staff:
            instance.comment = remove_bad_char( instance.comment )

class NewComment( Comment ):
    abuse_count = models.IntegerField( default = 0, blank = True, null = True )
    class Meta:
        verbose_name_plural = "Comments"
        ordering = ['-submit_date']

    def save(instance, *args, **kwargs):
        super(NewComment,instance).save()
        if instance.content_type.name == 'question':
            from content.models import Question
            close_connection()
            count = Comment.objects.using('htcMaster').filter(object_pk=instance.object_pk,content_type=instance.content_type).count()
            question = Question.objects.get(pk=instance.object_pk)
            question.anscount = count
            question.save()
        if instance.content_type.name in ['article','question'] and cache.has_key('question_article_frontend'):
            cache.delete('question_article_frontend')

pre_save.connect(modify_comment, sender=NewComment)

#post_save.connect(count_comments, sender=NewComment)

post_delete.connect( count_comments, sender = Comment )


from user_data.models import School, Department, Term, EventType, Teacher, Class, Group, User, SyllabusEvent
from django.utils import timezone
import datetime

class init_db_with_test_data():

    def main(self):
        #school model
        poly = School(name='Cal Poly Pomona', city='Pomona', state='California', pic_key='logo1')
        poly.save()
        diego = School(name='UCSD', city='SanDiego', state='California', pic_key='logo2')
        diego.save()
        berkeley = School(name='UC Berkeley', city='Berkeley', state='California', pic_key='logo3')
        berkeley.save()
        fullerton = School(name='Cal State Fullerton', city='Fullerton', state='California', pic_key='logo4')
        fullerton.save()

        #department model
        cs = Department(department_name='Computer Science')
        cs.save()
        lit = Department(department_name='Literature')
        lit.save()
        bio = Department(department_name='Biology')
        bio.save()
        business = Department(department_name='Business')
        business.save()
        math = Department(department_name='Math')
        math.save()

        #term model
        fall = Term(term='FALL')
        fall.save()
        winter = Term(term='WINTER')
        winter.save()
        spring = Term(term='SPRING')
        spring.save()
        summer = Term(term='SUMMER')
        summer.save()

        #eventtype model
        hw = EventType(event_type='homework')
        hw.save()
        midterm = EventType(event_type='midterm')
        midterm.save()
        final = EventType(event_type='final')
        final.save()
        event = EventType(event_type='event')
        event.save()

        #teacher model
        teacher1 = Teacher(first_name='Sam', last_name='Baker', department=cs, school=poly,
                           rating=70.5, website='website.com')
        teacher1.save()
        teacher2 = Teacher(first_name='Rushi', last_name='Kri', department=math, school=poly,
                           rating=None, website='website2.com')
        teacher2.save()
        teacher3 = Teacher(first_name='Barbra', last_name='Ledger', department=bio, school=diego,
                           rating=99.2, website=None)
        teacher3.save()
        teacher4 = Teacher(first_name='Damian', last_name='Wayne', department=lit, school=diego,
                           rating=52, website='website3.com')
        teacher4.save()
        teacher5 = Teacher(first_name='Kelly', last_name='Mo', department=cs, school=berkeley,
                           rating=29.9, website='website4.com')
        teacher5.save()
        teacher6 = Teacher(first_name='Deb', last_name='Rider', department=business, school=berkeley,
                           rating=92.5, website=None)
        teacher6.save()
        teacher7 = Teacher(first_name='Ken', last_name='Sa', department=cs, school=fullerton,
                           rating=42.4, website='website5.com')
        teacher7.save()
        teacher8 = Teacher(first_name='Tet', last_name='Ser', department=lit, school=fullerton,
                           rating=None, website=None)
        teacher8.save()

        #class model
        algorithm = Class(department=cs, class_description='Algorithms', class_number='CS450', school=poly,
                          teacher=teacher1, term=winter, year=2018)
        algorithm.save()
        data = Class(department=cs, class_description='Data Structures', class_number='CS240', school=berkeley,
                          teacher=teacher5, term=None, year=2018)
        data.save()
        software = Class(department=cs, class_description='Software Development', class_number='CS480', school=fullerton,
                          teacher=teacher7, term=fall, year=None)
        software.save()
        calc = Class(department=math, class_description='Calculus', class_number='MAT230', school=poly,
                          teacher=teacher2, term=spring, year=2017)
        calc.save()
        animal = Class(department=bio, class_description='Animal Health', class_number='Bio320', school=diego,
                          teacher=teacher3, term=None, year=2017)
        animal.save()
        poem = Class(department=lit, class_description='Great Poets', class_number='LIT110', school=diego,
                          teacher=teacher4, term=winter, year=2017)
        poem.save()
        market = Class(department=business, class_description='Intro to Marketing', class_number='=CBE101', school=berkeley,
                          teacher=teacher6, term=summer, year=2016)
        market.save()
        haiku = Class(department=lit, class_description='Haiku Analysis', class_number='LIT100', school=fullerton,
                          teacher=teacher8, term=None, year=None)
        haiku.save()

        #group model
        css = Group(group_name='Computer Science Society')
        css.save()
        dps = Group(group_name='Dead Poet Society')
        dps.save()
        bff = Group(group_name='Biology Friends Forever')
        bff.save()

        #user model
        user1 = User(id='234d3', first_name='Justin', last_name='Corcuera', username='yaboi', email=None, pic_key=None,
                     school=poly)
        user1.save()
        user1.classes.add(algorithm)
        user1.classes.add(calc)
        user1.groups.add(css)
        user2 = User(id='438d2', first_name='Guy', last_name='Fieri', username='flame', email='cooking@flavortown.com',
                     pic_key='png1', school=poly)
        user2.save()
        user2.groups.add(css)
        user3 = User(id='19fg3', first_name='Mo', last_name='Smalls', username='idot', email='j@uc.edu', pic_key=None,
                     school=diego)
        user3.save()
        user3.classes.add(animal)
        user3.groups.add(bff)
        user4 = User(id='8yf74', first_name='Kal', last_name='Ces', username='ieatpants', email=None, pic_key='png2',
                     school=diego)
        user4.save()
        user4.classes.add(poem)
        user4.groups.add(dps)
        user5 = User(id='ef584', first_name='Jiren', last_name='Palin', username='amenee', email='d@uc.edu', pic_key='png3',
                     school=berkeley)
        user5.save()
        user5.classes.add(market)
        user6 = User(id='f7873', first_name='Los', last_name='Marcy', username='amen', email='Kaleid@calstate.edu',
                     pic_key='png4',school=fullerton)
        user6.save()
        user7 = User(id='fgm0s', first_name='Zed', last_name='Darcy', username='denk', email='Kkeekd@calstate.edu',
                     pic_key='png4', school=fullerton)
        user7.save()
        user7.classes.add(haiku)
        user7.groups.add(dps)

        #timezone setting
        now = datetime.datetime.now()
        due_date = timezone.make_aware(now)
        due_date1 = due_date + datetime.timedelta(days=7)
        due_date2 = due_date + datetime.timedelta(days=13)
        due_date3 = due_date + datetime.timedelta(days=29)

        # syllabusevent model
        event1 = SyllabusEvent(event_name='Algorithms Final', date=due_date1)
        event1.save()
        event1.from_class.add(algorithm)
        event1.event_type.add(final)
        event2 = SyllabusEvent(event_name='CSS Meeting', date=due_date2)
        event2.save()
        event2.from_group.add(css)
        event2.event_type.add(event)
        event3 = SyllabusEvent(event_name='Study Session with friend', date=due_date3)
        event3.save()
        event3.from_user.add(user2)
        event3.event_type.add(event)


#A queen is standing on an n X n chessboard. The chessboard's rows are numbered from 1 to n, going from bottom to top; its columns are numbered from 1 to n, going from left to right. Each square on the board is denoted by a tuple, (r,c) , describing the row, r, and column, c, where the square is located. The queen is standing at position (rq,cq) and, in a single move, she can attack any square in any of the eight directions (left, right, up, down, or the four diagonals). There are K obstacles on the chessboard preventing the queen from attacking any square that has an obstacle blocking the the queen's path to it. Given the queen's position and the locations of all the obstacles, find and print the number of squares the queen can attack from her position at (rq, cq)

#!/bin/python3

import sys

total = 0
n,k = input().strip().split(' ')
n,k = [int(n),int(k)]
rQueen,cQueen = input().strip().split(' ')
rQueen,cQueen = [int(rQueen),int(cQueen)]

hObstacle = [0, n + 1]
vObstacle = [n + 1 ,0]
pSlope = [rQueen - min(cQueen - 1, rQueen - 1) - 1, rQueen + min(n - rQueen, n - cQueen) + 1]
nSlope = [rQueen + min(cQueen - 1, n - rQueen) + 1, rQueen - min(rQueen - 1, n - cQueen) - 1]

for a0 in range(k):
    rObstacle,cObstacle = input().strip().split(' ')
    rObstacle,cObstacle = [int(rObstacle),int(cObstacle)]

    rDif = rObstacle - rQueen
    cDif = cObstacle - cQueen

    if(rObstacle == rQueen):
        if(cObstacle < cQueen):
            hObstacle[0] = cObstacle
        else:
            hObstacle[1] = cObstacle
    elif(cObstacle == cQueen):
        if(rObstacle > rQueen):
            vObstacle[0] = rObstacle
        else:
            vObstacle[1] = rObstacle
    elif(abs(rDif) == abs(cDif)):
        if(pSlope[0] < rObstacle and rDif < 0 and cDif < 0):
            pSlope[0] = rObstacle
        elif( pSlope[1] > rObstacle and rDif > 0 and cDif > 0):
            pSlope[1] = rObstacle
        elif(nSlope[0] > rObstacle and rDif > 0 and cDif < 0):
            nSlope[0] = rObstacle
        elif(nSlope[1] < rObstacle and rDif < 0 and cDif > 0):
            nSlope[1] = rObstacle

total += abs(pSlope[0] - pSlope[1]) - 2
total += abs(nSlope[0] - nSlope[1]) - 2

total += abs(hObstacle[0] - hObstacle[1]) - 2
total += abs(vObstacle[0] - vObstacle[1]) - 2

print(total)


from pathlib import Path

import cv2
import numpy as np

import depthai as dai

if __name__ == "__main__":
    model_path = Path(__file__).parent / 'custom_ops/out'
    pipeline = dai.Pipeline()
    # Source
    camera = pipeline.createColorCamera()
    camera.setPreviewSize(300, 300)
    camera.setCamId(0)
    camera.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P)
    camera.setInterleaved(True)
    # Ops
    detection = pipeline.createNeuralNetwork()
    blob_path = model_path / 'model.blob'
    detection.setBlobPath(f'{blob_path.as_posix()}')
    camera.preview.link(detection.input)
    # Link Outputs for Detection
    x_out = pipeline.createXLinkOut()
    x_out.setStreamName('custom')
    detection.out.link(x_out.input)

    device = dai.Device(pipeline)
    device.startPipeline()

    frame_buffer = device.getOutputQueue(name='custom', maxSize=4)

    while True:
        frame = frame_buffer.get()
        # Returns a list
        layer = frame.getFirstLayerFp16()
        layer = np.array(layer, dtype=np.uint8)
        shape = (300, 300, 3)
        frame_data = layer.reshape(shape)
        cv2.imshow('Image', frame_data)
        if cv2.waitKey(1) == ord('q'):
            break


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2011-2013, Yu-chen Kao
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

__author__ = "Yu-chen Kao (cybeliak)"

from ..lib.common import *

class ApplyUttmapToFeat(nz.AdvJob):

    @nz.hook('define_things')
    def init(self):
        self.i.define('feat', ag.things.KaldiFeat)
        self.i.define('uttmap', ag.things.UttMergeMap)
        self.o.define('feat', ag.things.KaldiFeatDescriptor)

    @nz.hook('run')
    def go(self):
        self.o.feat.param["samprate"] = self.i.feat.param["samprate"]
        self.o.feat.param["nutt"] = self.i.uttmap.param["nutt"]
        self.o.feat.param['feattype'] = self.i.feat.param['feattype']
        self.o.feat.param['ndim'] = self.i.feat.param['ndim']

        self.o.feat.param["cmdscp"] = self.i.feat.get_cmd_scp()
        self.o.feat.param["cmdlen"] = self.i.feat.get_cmd_len()

        self.o.feat.param["transscp"] = self.pm.PSC(r"""#!bash
            | gawk '{if(NR==FNR){wav[$1]=$2}else{print $1, wav[$2]}}'
                - <( %@[ri.uttmap.get_cmd] )
            | csort.sh
            """)

        self.o.feat.param["translen"] = self.pm.PSC(r"""#!bash
            | gawk '{if(NR==FNR){len[$1]=$2}else{print $1, len[$2]}}'
                - <( %@[ri.uttmap.get_cmd] )
            """)

        if "cmdfeat" in self.i.feat.param:
            self.o.feat.param["cmdfeat"] = self.i.feat.param["cmdfeat"]


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

from odoo.tests import TransactionCase, tagged, Form
from odoo.exceptions import ValidationError
from odoo import fields
from .test_sale_common import TestCommonSaleNoChart
import logging

_logger = logging.getLogger(__name__)

@tagged('ipx_product_fob_compute', 'post_install', '-at_install')
class TestSaleOrderLine(TestCommonSaleNoChart):
    def setUp(self):
        super(TestSaleOrderLine, self).setUp()

        self.uom_unit = self.env.ref('uom.product_uom_unit')

        self.product = self.env['product'].create({
            'name':'Producto',
            'type':'consu',
            'uom_id':self.uom_unit.id,
            'uom_po_id':self.uom_unit.id,
        })

        self.sale_order = self.env['sale.order'].create({

        })


import json
from pathlib import Path
from unittest import TestCase
from unittest.mock import patch

from ledgerwallet.manifest import AppManifest

class ManifestTest(TestCase):
    def setUp(self):
        self.data_dir = Path(__file__).parent / "data"
        self.json_path = self.data_dir / "manifest.json"
        self.assertTrue(self.json_path.is_file())
        self.manifest = AppManifest(str(self.json_path))

    def test___init__(self):
        self.assertEqual(self.manifest.path, str(self.json_path.parent))
        with self.json_path.open() as filee:
            self.assertEqual(self.manifest.json, json.load(filee))

    def test___init__error(self):
        with self.assertRaises(AssertionError):
            AppManifest(str(self.data_dir / "empty_manifest.json"))  # missing key
        with self.assertRaises(FileNotFoundError):
            AppManifest("/this/path/should/not/exist")  # file does not exist (right?)

    def test_app_name(self):
        self.assertEqual(self.manifest.app_name, "some name")

    def test_data_size(self):
        self.assertEqual(self.manifest.data_size, 42)

    def test_get_application_flags(self):
        self.assertEqual(self.manifest.get_application_flags(), 0x9999)

    def test_get_binary(self):
        self.assertEqual(self.manifest.get_binary(), str(self.data_dir / "some binary"))

    def test_get_target_id(self):
        self.assertEqual(self.manifest.get_target_id(), 0x1234)

    def test_properties_with_minimal_manifest(self):
        manifest = AppManifest(str(self.data_dir / "minimal_manifest.json"))
        with self.assertRaises(KeyError):
            manifest.app_name
        self.assertEqual(manifest.data_size, 0)
        self.assertEqual(manifest.get_application_flags(), 0)
        self.assertEqual(manifest.get_binary(), str(self.data_dir / "some binary"))
        self.assertEqual(manifest.get_target_id(), 0x1234)
        self.assertEqual(manifest.serialize_parameters(), b"")

    def test_serialize_parameters(self):
        # fmt: off
        expected = bytes.fromhex(
            "01" +  # BolosTag 'AppName'
            "09" + "736f6d65206e616d65" +  # "some name"
            "02" +  # BolosTag 'Version'
            "0b" + "322e392e342d6465627567" +  # 2.9.4-debug
            "03" +  # BolosTag 'Icon'
            "04" + "01020304" +  # mocked icon
            "04" +  # BolosTag 'DerivationPath'
            "23" +  # 35: following size
            "05" +  # secp256k1 (1) + ed25519 (4)
            "03" + "8000002c80000000000000ff" +                # "44'/0'/255"
            "05" + "8000002c80000000800000000000000100000190"  # "44'/0'/0'/1/400"
        )
        # fmt: on
        with patch(
            "ledgerwallet.manifest.icon_from_file", lambda x: b"\x01\x02\x03\x04"
        ):
            result = self.manifest.serialize_parameters()
        self.assertEqual(result, expected)


from Api.apiFactory import ApiFactory
import app, utils,logging
import pytest

@pytest.mark.run(order=0)
class TestUserApi:

    def test_get_token(self):
        res = ApiFactory.get_user_api().get_token_api()
        logging.info('请求地址：{}'.format(res.url))
        logging.info('响应数据：{}'.format(res.json()))
        utils.common_assert_code(res)
        assert len(res.json().get('token')) > 0
        app.headers['token'] = res.json().get('token')
        print('app.headers:{}'.format(app.headers))

    def test_verify_token(self):
        """响应"""
        res = ApiFactory.get_user_api().verify_token_api()
        logging.info('请求地址：{}'.format(res.url))
        logging.info('响应数据：{}'.format(res.json()))
        utils.common_assert_code(res)
        assert res.json().get('isValid') is True

    def test_user_addr(self):
        """用户地址"""
        # 获取响应
        res = ApiFactory.get_user_api().user_addr_api()
        logging.info('请求地址：{}'.format(res.url))
        logging.info('响应数据：{}'.format(res.json()))
        utils.common_assert_code(res)
        assert False not in [i in res.text for i in ['上海市', '浦东新区', '大白', '001号', '15962620132']]


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

def splitvals(val, t):
    if isinstance(val, set) or isinstance(val, tuple) or isinstance(val, list):
        for i in val:
            splitvals(i, t)
    else:
        t.append(val)

def invert_dict(source_dict):
    tmp_dict = dict()
    new_dict = dict()
    if source_dict:
        for k, v in source_dict.items():
            arr = []
            splitvals(v, arr)
            tmp_dict[k] = arr
        for k, v in tmp_dict.items():
            for el in v:
                if el not in new_dict.keys():
                    new_dict[el] = k
                else:
                    if not isinstance(new_dict[el], list):
                        new_dict[el] = [new_dict[el]]
                    new_dict[el].append(k)
    return new_dict


import networkx as nx
from pyvis.network import Network

import re

import spacy
import wikipedia

from py_babelnet.calls import BabelnetAPI
import py_babelnet

import pandas as pd

import os
from dotenv import load_dotenv

import json
import sys

import time
import socket
from json.decoder import JSONDecodeError

#initialize the api-key
def init_bn_api():

    load_dotenv()
    key = os.getenv("BABELNET_KEY")
    if key is None:
        raise ValueError("No BABELNET_KEY found in .env-file. Please check your .env and try again.")
    api = BabelnetAPI(key)

    return api

#initialize the values needed for the program - read all neccessary data from a .env-file
def init_values():

    load_dotenv()
    similarity_threshold = float(os.getenv("SIMILARITY_THRESHOLD"))
    wikipedia_sentences = int(os.getenv("WIKIPEDIA_SENTENCES"))
    location_size = int(os.getenv("LOCATION_SIZE"))
    org_size = int(os.getenv("ORG_SIZE"))
    entity_size = int(os.getenv("OTHER_ENTITY_SIZE"))
    unlabelled_edges = os.getenv("EDGES_WITHOUT_RELATIONSHIPTYPE", 'False').lower() in ('true', '1', 't')

    if similarity_threshold is None:
        raise ValueError("No SIMILARITY_THRESHOLD found in .env-file. Please check your .env-file and try again.")
    if wikipedia_sentences is None:
        raise ValueError("No WIKIPEDIA_SENTENCES found in .env-file. Please check your .env-file and try again.")
    if location_size is None:
        raise ValueError("No LOCATION_SIZE found in .env-file. Please check your .env-file and try again.")
    if org_size is None:
        raise ValueError("No ORG_SIZE found in .env-file. Please check your .env-file and try again.")
    if entity_size is None:
        raise ValueError("No OTHER_ENTITY_SIZE found in .env-file. Please check your .env-file and try again.")
    if unlabelled_edges is None:
        raise ValueError("No EDGES_WITHOUT_RELATIONSHIPTYPE found in .env-file. Please check your .env-file and try again.")

    return similarity_threshold, wikipedia_sentences, location_size, org_size, entity_size, unlabelled_edges

#read the articles from the given files and return them in a string
def read_articles(filenames: list):

    all_articles = ""
    for filename in filenames:
        # try to read article from a json file
        if filename.endswith(".json"):
            try:
                with open(filename) as f:
                    data = json.load(f)
            except IOError:
                print("Error: File {} does not appear to exist.".format(filename))
            except JSONDecodeError :
                print("Error: File {} is not in JSON-format.".format(filename))
            else:
                try:
                    article = data["text"]
                except AttributeError:
                    print("No text object found in json-file {}".format(filename))
                else:
                    all_articles += article
        # try to read article from a .txt-file
        elif filename.endswith(".txt"):
            try:
                with open(filename) as f:
                    data = f.read()
            except IOError:
                print("Error: File {} does not appear to exist.".format(filename))
            else:
                all_articles += data

    if all_articles == "":
        raise ValueError("No articles found.")

    return all_articles

#extract entity names from article
def extract_article_names(article: spacy.tokens.doc.Doc, sim_threshold = 0.8):
    # labels of entities that we dont want as nodes
    unwanted_label = ["DATE", "TIME", "ORDINAL", "CARDINAL"]
    #in the end, nodes will contain all relevant entities and their labels
    nodes = {}
    #the similarity dict has an entry for each entity in the network. These entities are dictionaries themselves, which include the similarity between the entity and all other entities.
    # Example:
    # similarities = {"Donald Trump": {"Barack Obama": 0.7, "Angela Merkel": 0.5},
    #                 "Barack Obama": {"Donald Trump": 0.7, "Angela Merkel": 0.6},
    #                 "Angela Merkel": {"Donald Trump": 0.5, "Barack Obama": 0.6}}
    similarities = {}

    for ent in article.ents:
        #remove all 's at the end of entity labels
        base_form_ent = re.sub(r"'s$", '', ent.text)
        if (base_form_ent not in nodes):
            nodes[base_form_ent] = {"label": ent.label_}
        if (ent.label_ not in unwanted_label) and (ent.has_vector):
            similarities[base_form_ent] = {}
            for other_ent in article.ents:
                base_form_other_ent = re.sub(r"'s$", '', other_ent.text)
                # add base_form_ent and base_form_other_ent to similarities
                if (other_ent.label_ not in unwanted_label) and (other_ent.has_vector) and (base_form_ent != base_form_other_ent):
                    if base_form_ent in similarities:
                        similarities[base_form_ent][base_form_other_ent] = ent.similarity(other_ent)
                    else:
                        similarities[base_form_ent] = {base_form_other_ent : ent.similarity(other_ent)}
    return reduce_nodes(similarities, nodes, sim_threshold)

# remove nodes which are very similar to another node which also contains their label
def reduce_nodes(similarities: dict, nodes: dict, sim_threshold = 0.8):

    extracted_entities = []
    for entity in similarities:
        #append entity name to list if the name is not part of the name of another entity and they have a high similarity
        append = True
        for other_entity in similarities[entity]:
            if entity in other_entity:
                try:
                    if similarities[entity][other_entity] > sim_threshold:
                        append = False
                        break
                except KeyError:
                    raise KeyError("There doesn't seem to be a similarity score for {} and {}.".format(entity, other_entity))
        for other_entity in extracted_entities:
            if entity.lower() == other_entity.lower():
                append = False
                break
        if append == True:
            extracted_entities.append(entity)
    #only keep those nodes which are in extracted_entities
    nodes = {key: nodes[key] for key in extracted_entities}

    return extracted_entities, similarities, nodes

#delete all entries in similarity dict where at least one of the entities is not in extracted_entities
def clean_similarities(similarities: dict, extracted_entities: list):

    try:
        similarities =  { key: similarities[key] for key in extracted_entities }
    except KeyError:
        raise KeyError("Some key doesn't seem to be in similarities, but appears in extracted_entities!")
    for entity in similarities:
        deletelist = []
        for other_entity in similarities[entity]:
            if other_entity not in extracted_entities:
                deletelist.append(other_entity)
        for unwanted_key in deletelist:
            del similarities[entity][unwanted_key]
    #divide the word similarity by three for the similarity measure
    for ent in similarities:
        for other_ent in similarities[ent]:
            similarities[ent][other_ent] /= 3

    return similarities

#add nodes to the network, including their labels and plot their sizes accordingly
def add_nodes(network: nx.Graph(), nodes: dict, location_size, company_size, entity_size):

    for entity in nodes:
        try:
            if nodes[entity]["label"] == "GPE" or nodes[entity]["label"] == "LOC":
                network.add_node(entity, size = location_size)

            elif nodes[entity]["label"] == "ORG":
                network.add_node(entity, size = company_size)
            else:
                network.add_node(entity, size = entity_size)
        except TypeError:
            raise TypeError("The node {} doesn't seem to have a label!".format(entity))

    return network

#add an edge between all nodes above a certain similarity threshold to the network
def add_edges_with_high_sim(network: nx.Graph(), extracted_entities: list, similarities: dict, high_sim_value : float):
    #edges which don't appear in the network already. Those edges are colored red.
    new_edges = []
    #already_added will contain all edges which have been added to the network before.
    #those edges are going to be colored in black
    already_added = []

    for entity in extracted_entities:
        for other_entity in extracted_entities:
            if entity != other_entity:
                try:
                    entity_sim = similarities[entity][other_entity]
                except KeyError:
                    raise KeyError("There doesn't seem to be a similarity score for {} and {}.".format(entity, other_entity))
                except TypeError:
                    raise TypeError("The similarity dict has an unexpected format.")
                else:
                    if entity_sim >= high_sim_value:
                        if (entity, other_entity) in network.edges:
                            already_added.append((entity, other_entity))
                        else:
                            new_edges.append((entity, other_entity))

    network.add_edges_from(new_edges, color = "red", title = "word similarity")
    network.add_edges_from(already_added, color = "black", title = "word similarity")

    return network

#returns a list of all overlapping entries in two lists (in this case: lists of entity_ids)
def find_overlapping_ids(entity_ids: list, other_entity_ids: list):
    overlap = list(set(entity_ids) & set(other_entity_ids))
    return overlap

#determines a list of babelnet_ids for an entity
def find_babelnet_ids(entity: str, api: py_babelnet.calls.BabelnetAPI):
    bn_ids = []

    try:
        entity_ids = api.get_synset_ids(lemma = entity, searchLang = "EN", targetLang = "EN")
    except ValueError :
        raise ValueError("Not enough BabelCoins to determine entity IDs.")
    except socket.error:
        print("Connection Error - please check your internet connection and try again.")
        raise

    if entity_ids:
        #message occurs when running out of babel coins or giving an invalid api-key
        if "message" not in entity_ids:
            for entity_id in entity_ids:
                bn_ids.append(entity_id["id"])
        else:
            raise ValueError(entity_ids["message"] + " Please consider checking your .env as well.")
    return bn_ids

#finds all ID belonging to the extracted entities in the BabelNet Network and saves them in bn_ids
#additionally extracts all edges belonging to those IDs and saves them in bn_info
def get_babelnet_info(extracted_entities: list, api: py_babelnet.calls.BabelnetAPI, bn_info: dict, bn_ids: dict):
    #find all IDS for the entity name in the babelnet network
    for entity in extracted_entities:
        if entity not in bn_ids:
            bn_ids[entity] = find_babelnet_ids(entity, api)

        # find all outgoing edges from the entities in the babelnet network
        if (entity not in bn_info) and (entity in bn_ids):
            entity_ids = bn_ids[entity]
            entity_edges = pd.DataFrame(columns = ["target", "name"])

            for entity_id in entity_ids:
                try:
                    edges = api.get_outgoing_edges(id = entity_id)
                    edges_df = pd.DataFrame(edges)
                except ValueError:
                    raise ValueError("Not enough BabelCoins to determine outgoing edges.")
                except socket.error:
                    print("Connection Error - please check your internet connection and try again.")
                    raise
                if not edges_df.empty:
                    edges_df["name"] = (pd.DataFrame(edges_df["pointer"].tolist()))["name"]
                    edges_df = edges_df.drop(columns = ["weight", "language", "pointer", "normalizedWeight"])
                    #edges_df now only contains the columns "target" and "name", where "name" is the edge-label
                    entity_edges = pd.concat([entity_edges, edges_df])
            #bn_info is now a dictionary of dataframes containing all edges found in the babelnet network
            bn_info[entity] = entity_edges

    return bn_info, bn_ids

#finds all edges between two of our entities from the BN-Network
def find_babelnet_edges(bn_ids: dict, bn_info: dict):
    edges = pd.DataFrame(columns = ["from", "to", "label"])
    for entity in bn_ids:
        for entity_id in bn_ids[entity]:
            for other_entity in bn_info:
                if entity != other_entity:
                    other_entity_edges = bn_info[other_entity]
                    #add an edge if any id of entity is a target in the edge-dataframe of the other entity
                    try:
                        if other_entity_edges["target"].str.contains(entity_id).any():
                            incident_edges = other_entity_edges.loc[(other_entity_edges["target"] == entity_id)]
                            incident_edges = incident_edges.drop_duplicates()
                            incident_edges = incident_edges.reset_index()
                            #if there is any edge with a different label than "Semantically related form", remove all edges with the label "Semantically related form"
                            if not incident_edges["name"].str.contains("Semantically related form").all():
                                incident_edges = incident_edges[incident_edges["name"] != "Semantically related form"]
                            outg_edges = pd.DataFrame(columns = ("from", "to", "label"))
                            outg_edges["from"] = [other_entity]*len(incident_edges)
                            outg_edges["to"] = [entity]*len(incident_edges)
                            outg_edges["label"] = incident_edges["name"]
                            edges = pd.concat([edges, outg_edges], ignore_index = True)
                    except TypeError:
                        raise TypeError("bn_info seems to be in a wrong format. It should be a dict of key - dataframe pairs.")
                    except KeyError:
                        raise KeyError("The dataframes in bn_info need to have columns called 'name' and 'target' ")

    edges = edges.drop_duplicates()
    edges = edges.reset_index()
    return edges

#add the babelnet edges to the network and update similarity measure
def add_babelnet_edges(network: nx.Graph(), edges: pd.DataFrame, similarity_measure: dict, unlabelled_edges: bool):
    #use sim_update to update the similarity_measure: add 1/3 if an edge in the babel-net network exists or if an edge with a label exists (if unlabelled_edges = false)
    new_edges = []
    sim_update = {}
    if not unlabelled_edges:
        edges = edges[edges["label"] != "Semantically related form"]
    for index, row in edges.iterrows():
        add = True
        try:
            if ([row["from"], row["to"]]) not in network.edges:
                color = "red"
                new_edges.append((row["from"], row["to"]))
                new_edges.append((row["to"], row["from"]))
            else:
                if((row["from"], row["to"]) not in new_edges):
                    color = "black"
                else:
                    add = False
        except KeyError:
            raise KeyError("The edges need to have columns called 'from' and 'to'.")
        #only add new edges
        if add == True:
            try:
                network.add_edge(row["from"], row["to"], title = row["label"], color = color)
            except KeyError:
                raise KeyError("The edge-dataframe needs to have columns called 'from', 'to' and 'label'.")

        if row["from"] in sim_update:
            if row["to"] not in sim_update[row["from"]]:
                sim_update[row["from"]].append(row["to"])
        else:
            sim_update[row["from"]] = [row["to"]]

        if row["to"] in sim_update:
            if row["from"] not in sim_update[row["to"]]:
                sim_update[row["to"]].append(row["from"])
        else:
            sim_update[row["to"]] = [row["from"]]

    # update similarity measure
    for entity in sim_update:
        if entity not in similarity_measure:
            similarity_measure[entity] = {}
        for other_entity in sim_update[entity]:
            if other_entity not in similarity_measure[entity]:
                similarity_measure[entity][other_entity] = 1/3
            else:
                similarity_measure[entity][other_entity] += 1/3

    return network, similarity_measure

#creates a list of sentences from a text
def split_sentences(text: str):
    # split sentences and questions
    text = re.split('[.?!]', text)
    clean_sent = []
    for sent in text:
        clean_sent.append(sent)
    return clean_sent

#extract entities from the wikipedia-summary of the article-entities and calculate their similarity
def calculate_wikipedia_similarity(nlp: spacy.language, extracted_entities: list, num_sentences = 3):

    unwanted_label = ["DATE", "ORDINAL", "CARDINAL", "PERCENT"]
    #wiki_ents is a dictionary where the keys are the article entities and the values are lists of all entities which appear in the first (num_sentences) of their wikipedia-summary
    wiki_ents = {}
    #wiki_sentences is a dictionary where the keys are the article entities and the values are lists of the split sentences of their wikipedia-summaries.
    wiki_sentences = {}
    #sim_wiki is a dictionary with entity keys and dictionaries as values. These dictionaries contain the wikipedia-similarity between the pairs of nodes.
    sim_wiki = {}
    for entity in extracted_entities:
        wiki_ents[entity] = []
        wiki_sentences[entity] = []
        try:
            descr = wikipedia.summary(entity, sentences = num_sentences)
        except (wikipedia.exceptions.DisambiguationError, wikipedia.exceptions.PageError):
            try:
                descr = wikipedia.summary(wikipedia.suggest(entity), sentences = num_sentences)
                print("Wikipedia error for entity ", entity, " use ", wikipedia.suggest(entity), " instead")
            except (wikipedia.exceptions.DisambiguationError, wikipedia.exceptions.PageError, TypeError, ValueError):
                print("Wikipedia disambiguation error occured for {}, skipping this entity".format(entity))
                continue
            except socket.error:
                print("Connection Error - please check your internet connection and try again.")
                raise
        except socket.error:
            print("Connection Error - please check your internet connection and try again.")
            raise

        split_descr = split_sentences(descr)
        split_descr = split_descr[:num_sentences]
        wiki_sentences[entity] = split_descr

        #calculate similarity for overlapping entities and store the occuring entities in wiki_ents
        sentence_number = 1
        for sentence in split_descr:
            entity_descr = nlp(sentence)
            for ent in entity_descr.ents:
                if (ent.label_ not in unwanted_label) and (ent.text != entity):
                    wiki_ents[entity].append(ent.text)
                    if entity not in sim_wiki:
                        sim_wiki[entity] = {ent.text: 1/sentence_number}
                    else:
                        if ent not in sim_wiki[entity]:
                            sim_wiki[entity][ent.text] = 1/sentence_number
            sentence_number = sentence_number + 1
        for item in wiki_ents:
            wiki_ents[item] = list(dict.fromkeys(wiki_ents[item]))

    return wiki_ents, sim_wiki

#add the similarity score of two entities to the similarity measure dict
def add_similarity(entity: str, other_entity: str, sim_score: float, similarity_measure:dict):

    if entity in similarity_measure:
        try:
            if other_entity in similarity_measure[entity]:
                similarity_measure[entity][other_entity] += sim_score
            else:
                similarity_measure[entity][other_entity] = sim_score
        except TypeError:
            print("The similarity dict has an unexpected format.")
            raise TypeError()
    else:
        similarity_measure[entity] = {other_entity : sim_score}
    return similarity_measure

#add the direct edges from wikipedia to the network and update the similarity measure
def find_direct_wikipedia_edges(network: nx.Graph(), wiki_ents: dict, extracted_entities: list, similarity_measure: dict, sim_wiki: dict, api: py_babelnet.calls.BabelnetAPI):
    new_edges = []
    already_added = []
    #dict where each lists of ids are assigned to entities
    wiki_ids = {}
    for entity in wiki_ents:
        for summ_entity in wiki_ents[entity]:
            if summ_entity != entity:
                if summ_entity in extracted_entities:
                    if ([entity, summ_entity]) not in network.edges:
                        new_edges.append((entity, summ_entity))
                    else:
                        already_added.append((entity, summ_entity))
                    # update the similarity measure with the max. wikipedia-similarity of the pair of entities
                    try:
                        if summ_entity not in sim_wiki:
                            sim_score = sim_wiki[entity][summ_entity]/3
                        elif entity not in sim_wiki[summ_entity]:
                            sim_score = sim_wiki[entity][summ_entity]/3
                        else:
                            sim_score = max(sim_wiki[entity][summ_entity], sim_wiki[summ_entity][entity])/3
                    except TypeError:
                        print("The similarity dict has an unexpected format.")
                        raise TypeError()

                    similarity_measure = add_similarity(entity, summ_entity, sim_score, similarity_measure)
                    similarity_measure = add_similarity(summ_entity, entity, sim_score, similarity_measure)

                #create wiki_ids: babelnet IDs of the wikipedia entities of the article of summ_entity
                #stop if there are not enough babel coins to find the wikipedia-ids -> this will be excepted in main(), where we will then continue without the wikipedia-edges
                elif summ_entity not in wiki_ids:
                    try:
                        wiki_ids[summ_entity] = find_babelnet_ids(summ_entity, api)
                    except ValueError:
                        print("Not enough BabelCoins - Continuing without the Wikipedia edges.")
                        raise ValueError()
                    except socket.error:
                        print("Connection Error - please check your internet connection and try again.")
                        raise

    network.add_edges_from(new_edges, color = "red", title = "wikipedia direct")
    network.add_edges_from(already_added, color = "black", title = "wikipedia direct")
    return wiki_ids, similarity_measure, network

#compare the BN-Ids of the wikipedia entities to those of the BN-Entities and substitute those with overlapping IDs.
def remove_duplicate_wikipedia_ents(bn_ids: dict, wiki_ents: dict, wiki_ids: dict):
    #bn_id_list includes all bnIds
    bn_id_list = []
    # wikiIds_retrieved: dict. This has article entities as keys and lists of ids as values.
    # Those ID-lists are obtained by comparing the BN_IDs of wikipedia-entities to the article BN-IDs. If there is an overlap, both entities are considered equal.
    # We can then substitute the wikipedia-entities with overlapping IDs for the article entities
    wiki_ids_retrieved = {}
    # wiki_entities_retrieved has wikipedia-entities as keys and lists of the article-entities with overlapping IDs as values.
    # we can later use this to connect entities to the retrieved article-entities instead of the wikipedia-entities
    wiki_entities_retrieved = {}
    for entity in bn_ids:
        bn_id_list.extend(bn_ids[entity])
    for entity in wiki_ents:
        for summ_ent in wiki_ents[entity]:
            added = False
            if summ_ent in wiki_ids:
                #check if the wikipedia entity has overlapping IDs with one of the BN-Entities
                if any(summ_ent_id in bn_id_list for summ_ent_id in wiki_ids[summ_ent]):
                    for label in bn_ids:
                        if label != entity:
                            #if this is the overlapping entity
                            if any(summ_ent_id in bn_ids[label] for summ_ent_id in wiki_ids[summ_ent]):
                                added = True
                                if label not in wiki_ids_retrieved:
                                    wiki_ids_retrieved[label] = set(wiki_ids[summ_ent])
                                else:
                                    wiki_ids_retrieved[label].update(set(bn_ids[label]))
                                if entity not in wiki_entities_retrieved:
                                    wiki_entities_retrieved[entity] = {label}
                                else:
                                    wiki_entities_retrieved[entity].add(label)
                if not added:
                    if entity in wiki_entities_retrieved:
                        wiki_entities_retrieved[entity].add(summ_ent)
                    else:
                        wiki_entities_retrieved[entity] = {summ_ent}
                    if entity in wiki_ids_retrieved:
                        wiki_ids_retrieved[entity].update(set(wiki_ids[summ_ent]))
                    else:
                        wiki_ids_retrieved[entity] = set(wiki_ids[summ_ent])

    return wiki_ids_retrieved, wiki_entities_retrieved

#add the indirect wikipedia edges to the network
def find_indirect_wikipedia_edges(network: nx.Graph(), wiki_ents_retrieved: dict, wikiIds_retrieved: dict):
    new_edges = []
    already_added = []

    for entity in wiki_ents_retrieved:
        for other_entity in wiki_ents_retrieved:
            if entity != other_entity:
                for summ_entity in wiki_ents_retrieved[entity]:
                    for summ_other_entity in wiki_ents_retrieved[other_entity]:
                        #if both entities have an overlapping wikipedia-summary-entity, add an indirect edge to the network
                        if summ_entity == summ_other_entity and summ_entity != entity and summ_entity != other_entity:
                            if (entity, summ_entity) in network.edges:
                                already_added.append((entity, summ_entity))
                            else:
                                new_edges.append((entity, summ_entity))
                            if (other_entity, summ_entity) in network.edges:
                                already_added.append((other_entity, summ_entity))
                            else:
                                new_edges.append((other_entity, summ_entity))
                        else:
                            #if the wikipedia-summary entities have overlapping BabelNet IDs, treat them as equal and add an indirect edge to the network
                            if (summ_entity in wikiIds_retrieved) and (summ_other_entity in wikiIds_retrieved):
                                if any(summ_ids in wikiIds_retrieved[summ_other_entity] for summ_ids in wikiIds_retrieved[summ_entity]) and summ_entity != entity and summ_entity != other_entity:
                                    if (entity, summ_entity) in network.edges:
                                        already_added.append((entity, summ_entity))
                                    else:
                                        new_edges.append((entity, summ_entity))
                                    if (other_entity, summ_entity) in network.edges:
                                        already_added.append((other_entity, summ_entity))
                                    else:
                                        new_edges.append((other_entity, summ_entity))

    network.add_edges_from(new_edges, color = "red", title = "wikipedia indirect")
    network.add_edges_from(already_added, color = "black", title = "wikipedia indirect")
    return network

#reset the colors of the edges of the network
def reset_edgecolors(network: nx.Graph()):
    for edge in network.edges:
        network.edges[edge].pop("color", None)
    return network

#compute the wikipedia networks or skip them if not enough babelcoins
def compute_wikipedia_networks(network: nx.Graph(), bn_ids: dict, extracted_entities: list, similarity_measure: dict, api: py_babelnet.calls.BabelnetAPI, nlp: spacy.language, wikipedia_sentences: int):
    wiki_ents, sim_wiki = calculate_wikipedia_similarity(nlp, extracted_entities, wikipedia_sentences)
    pyvis_network = Network()
    try:
        wiki_ids, similaritiy_measure, network = find_direct_wikipedia_edges(network, wiki_ents, extracted_entities, similarity_measure, sim_wiki, api)
    except ValueError:
        raise ValueError
    else:
        pyvis_network.from_nx(network)
        pyvis_network.save_graph("wikipedia_network_direct_{}.html".format(time.strftime("%Y%m%d-%H%M%S")))
        network = reset_edgecolors(network)
        wikiIds_retrieved, wiki_entities_retrieved = remove_duplicate_wikipedia_ents(bn_ids, wiki_ents, wiki_ids)
        network = find_indirect_wikipedia_edges(network, wiki_entities_retrieved, wikiIds_retrieved)
        pyvis_network = Network()
        pyvis_network.from_nx(network)
        pyvis_network.save_graph("wikipedia_network_indirect_{}.html".format(time.strftime("%Y%m%d-%H%M%S")))
        network = reset_edgecolors(network)
        return network

#add the similarity of two nodes as a value to the edges between two nodes
def add_similarity_to_edges(network: nx.Graph(), similarity_measure: dict):
    for edge in network.edges:
        network.edges[edge]["weight"] = 0
    for entity in similarity_measure:
        for other_entity in similarity_measure[entity]:
            if ([entity, other_entity] in network.edges):
                network.edges[entity, other_entity]["weight"] = similarity_measure[entity][other_entity]
                if network.edges[entity, other_entity]["weight"] > 0:
                    weight_label = ", weight: {:.2f}".format(similarity_measure[entity][other_entity])
                    if "title" in network.edges[entity, other_entity] and not pd.isna(network.edges[entity, other_entity]["title"]):
                        if weight_label not in network.edges[entity, other_entity]["title"]:
                            network.edges[entity, other_entity]["title"] += weight_label
                    else:
                        network.edges[entity, other_entity]["title"] = weight_label
    return network

#filter network for edges with a similarity larger than a given sim_threshold
def filter_similarity(network: nx.Graph(), sim_threshold = 0.8):
    sim_network = nx.Graph([(u, v, properties) for u, v, properties in network.edges(data = True) if "weight" in properties and properties["weight"] > sim_threshold])
    return sim_network

#filter network for nodes with certain labels
def filter_node_labels(network: nx.Graph(), labels: list):
    filtered_network = nx.Graph([(u, v, properties) for u, v, properties in network.edges(data = True) if any(label in u.lower() or label in v.lower() for label in labels)])
    return filtered_network

#filter network for edges with certain labels
def filter_edge_labels(network: nx.Graph(), labels: list):
    filtered_network = nx.Graph([(u, v, properties) for u, v, properties in network.edges(data = True) if "title" in properties and not pd.isna(properties["title"]) and any(label in properties['title'].lower() for label in labels)])
    return filtered_network

#interactively filters the network
def filtering_process(network: nx.Graph()):
    exit = False

    while exit == False:
        filter_type = input("Do you wish to filter your network? \n [n] - filter nodes with a certain label \n [e] - filter edges with a certain label \n [s] - filter edges over a certain similarity threshold \n [other] - quit \n").lower()
        #differentiate between the different filter types
        if filter_type == "n":
            node_labels = [item for item in input("Please enter the node labels for which you want to filter the network: \n").split()]
            filtered_node_network = filter_node_labels(network, node_labels)
            #dont save the network if it is empty
            if len(filtered_node_network.nodes) == 0:
                print("No nodes found for labels {}.\n".format(node_labels))
            else:
                pyvis_network = Network()
                pyvis_network.from_nx(filtered_node_network)
                pyvis_network.save_graph("filtered_node_nw_{}.html".format(time.strftime("%Y%m%d-%H%M%S")))

        elif filter_type == "e":
            edge_labels = [item for item in input("Please enter the edge labels for which you want to filter the network: \n").split()]
            filtered_edge_network = filter_edge_labels(network, edge_labels)
            if (len(filtered_edge_network.edges) == 0):
                print("No edges found for labels {}!\n".format(edge_labels))
            else:
                pyvis_network = Network()
                pyvis_network.from_nx(filtered_edge_network)
                pyvis_network.save_graph("filtered_edge_nw_{}.html".format(time.strftime("%Y%m%d-%H%M%S")))

        elif filter_type == "s":
            try:
                similarity_score = [float(item) for item in (input("Please enter the similarity thresholds for which you want to filter the network: \n").split())]
            except ValueError:
                print("Please enter a valid similarity threshold (for example 0.6)")
            else:
                for sim in similarity_score:
                    sim_network = filter_similarity(network, sim)
                    if len(sim_network.edges) == 0:
                        print("No edges found for similarity score {}.\n".format(sim))
                    else:
                        pyvis_network = Network()
                        pyvis_network.from_nx(sim_network)
                        pyvis_network.save_graph("filtered_similarity_{}_nw_{}.html".format(sim, time.strftime("%Y%m%d-%H%M%S")))
        else:
            exit = True

def __main__():

    nlp = spacy.load('en_core_web_lg')

    #initialize babel net api
    api = init_bn_api()
    bn_info = {}
    bn_ids = {}
    similarity_measure = {}
    network = nx.Graph()
    pyvis_network = Network()
    sim_threshold, wikipedia_sentences, location_size, org_size, entity_size, unlabelled_edges = init_values()
    articles = ""

    if len(sys.argv) > 1:
        paths = sys.argv[1:]

    else:
        paths = [item for item in input("Please enter a path to an article: \n").split()]

    while articles == "":
        try:
            articles = read_articles(paths)
        except ValueError:
            paths = [item for item in input("No articles found. Please enter a valid path to an article. Enter 'quit' if you wish to stop.\n").split()]
        if "quit" in paths:
            return
    nlp_article = nlp(articles)

    #create nodes
    extracted_entities, similarities, nodes = extract_article_names(nlp_article)
    similarity_measure = clean_similarities(similarities, extracted_entities)
    network = add_nodes(network, nodes, location_size, org_size, entity_size)
    #add edges between nodes with similarity larger than sim_threshold
    network = add_edges_with_high_sim(network, extracted_entities, similarity_measure, sim_threshold)
    pyvis_network.from_nx(network)
    pyvis_network.save_graph("wordsimilarity_network_{}.html".format(time.strftime("%Y%m%d-%H%M%S")))
    network = reset_edgecolors(network)

    #collect info from babelnet and find edges in the network
    bn_info, bn_ids = get_babelnet_info(extracted_entities, api, bn_info, bn_ids)
    edges = find_babelnet_edges(bn_ids, bn_info)
    #Create the babelnet network
    network, similarity_measure = add_babelnet_edges(network, edges, similarity_measure, unlabelled_edges)
    pyvis_network = Network()
    pyvis_network.from_nx(network)
    pyvis_network.save_graph("babelnet_network_{}.html".format(time.strftime("%Y%m%d-%H%M%S")))
    network = reset_edgecolors(network)
    #If we dont run out of babel-coins, find the wikipedia-edges. Else, skip this part.
    try:
        network = compute_wikipedia_networks(network, bn_ids, extracted_entities, similarity_measure, api, nlp, wikipedia_sentences)
    except ValueError :
        pass

    #add the similarity score to the edge-labels
    network = add_similarity_to_edges(network, similarity_measure)
    pyvis_network = Network()
    pyvis_network.from_nx(network)
    pyvis_network.save_graph("weighted_network_{}.html".format(time.strftime("%Y%m%d-%H%M%S")))
    network = reset_edgecolors(network)

    filtering_process(network)

if __name__ == "__main__":
    __main__()


"""
Author: Brady Trenary
Program: validate_input_in_function.py

prompts user for name and score, prints values to console
"""
import unittest

def score_input(test_name, test_score=0, invalid_message='Invalid test score, try again!'):
    """
    Function that accepts and returns a string....
    :param test_name: The name of a test/exam
    :type test_name: String
    :param test_score: A value between 0 and 100 representing a test score
    :type test_score: int
    :param invalid_message: Message displayed when an invalid score is provided
    :type invalid_message: String
    :return: formatted string with test and test score
    :rtype: String
    """
    try:

        if 0 <= test_score <= 100:
            # print(f"{test_name}: {test_score}")
            return f"{test_name}: {test_score}"
        else:
            return invalid_message
    except TypeError:
        raise TypeError

if __name__ == '__main__':
    try:
        print(score_input("test1", "45"))
    except TypeError:
        print("not a valid score!")


# coding: utf-8

__all__ = ["search_video"]

from src.core.comm import check_data, extra_tag
from src.core.init import PriorityQueue

def search_video(path, cfg):
    """
    search video according to file type and exclude folder
    Returns: file path list

    """
    file_type = cfg.resource.file_type
    excluded = [path.joinpath(e) for e in cfg.resource.exclude_folders]
    files = []
    for file in path.rglob("*"):
        exs = [n for n in excluded if n in file.parents]
        if not exs and file.suffix.lower() in file_type:
            files.append(file)
    return files

def get_metadata(file, number, cfg):
    """
    get metadata from plugin according to number

    have been tested
    """
    # priority init， get sorted plugin
    priority = PriorityQueue.get(number, cfg.comm.priority)
    while not priority.empty():
        # noinspection PyBroadException
        website = priority.pop().capitalize()
        data = Registry.get(website, number, cfg)
        if check_data(data):
            return extra_tag(file, data, cfg.resource)


s1 = input()
s2 = input()

l = s2.split(" ")
l.sort()

boolean = True

for i in range(1, len(l)):
    if int(l[i]) % int(l[0]) != 0:
        boolean = False
        break

if boolean:
    print(l[0])
else:
    print(-1)


from wsgiref.util import *
from json import loads

from .server import Server, HttpException

def query_dict(environ):
    query_string = environ["QUERY_STRING"]
    if not query_string: return {}
    terms = (term.split('=') for term in query_string.split('&'))
    return {key: val for [key, val] in terms}

def request_body(environ):
    size = int(environ.get("CONTENT_LENGTH", 0))
    body = environ["wsgi.input"].read(size).decode()
    return {
        "text/html": str,
        "text/plain": str,
        "text/json": loads
    }[environ["CONTENT_TYPE"]](body)

class RestServer(Server):
    def __init__(self, **containers):
        handlers = {name: ContainerRestHandler(cont) for (name, cont) in containers.items()}
        super().__init__(**handlers)

class RestHandler:
    def get(self, environ):
        pass

    def put(self, environ):
        pass

    def post(self, environ):
        pass

    def delete(self, environ):
        pass

    def __call__(self, environ):
        return {
            "GET": self.get,
            "PUT": self.put,
            "POST": self.post,
            "DELETE": self.delete
        }[environ["REQUEST_METHOD"]](environ)

class ContainerRestHandler(RestHandler):
    def __init__(self, container):
        self.container = container

    def post(self, environ):
        body = request_body(environ)

        try:
            key = self.container.add(body)

        except KeyError as e:
            raise HttpException(409, {}, e.args[0])

        else:
            return (201, {"Location": str(key)}, "")

    def get(self, environ):
        key = shift_path_info(environ)
        query = query_dict(environ)

        if not key:
            if not query:
                return (200, {}, list(self.container))

            else:
                elems = self.container
                for (key, val) in query.items():
                    if key.startswith("min_"):
                        key = key[4:]
                        elems = elems.where(lambda elem: int(elem[key]) >= val)
                    elif key.startswith("max_"):
                        key = key[4:]
                        elems = elems.where(lambda elem: int(elem[key]) <= val)
                    else:
                        elems = elems.where(lambda elem: str(elem[key]) == val)
                return (200, {}, list(elems))

        else:
            try:
                return (200, {}, self.container[key])

            except KeyError as e:
                raise HttpException(404, {}, e.args[0])

    def put(self, environ):
        key = shift_path_info(environ)
        body = request_body(environ)

        try:
            self.container[key] = body

        except KeyError:
            raise HttpException(404, {}, e.args[0])

        else:
            return (200, {}, "")

    def delete(self, environ):
        key = shift_path_info(environ)

        try:
            del self.container[key]

        except KeyError:
            raise HttpException(404, {}, e.args[0])

        else:
            return (200, {}, "")


import scrapy

class BitcoinFeesSpider(scrapy.Spider):
    name = 'bitcoinfees'
    start_urls = [
        'https://bitcoinfees.net/',
    ]

    def parse(self, response):
        fee_rates_table_body = response.css('tbody')
        for fee_rate_row in fee_rates_table_body.css('tr'):
            yield {
                'feeRate': fee_rate_row.css('tr').css('td:first-child::text').get(),
                'transactions': fee_rate_row.css('tr').css('td .progress .progress-bar::text').get(),
                'time': fee_rate_row.css('tr').css('td:last-child::text').get(),
            }


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

from __future__ import absolute_import

import copy
import bson
import datetime
import logging
from eduid_userdb.db import BaseDB
from eduid_userdb.exceptions import UserHasUnknownData, DocumentOutOfSync, DocumentDoesNotExist

__author__ = 'lundberg'

# Store all data received from the op

class Proof(object):
    def __init__(self, data, raise_on_unknown=True):
        self._data_in = copy.deepcopy(data)  # to not modify callers data
        self._data = dict()

        # things without setters
        # _id
        _id = self._data_in.pop('_id', None)
        if _id is None:
            _id = bson.ObjectId()
        if not isinstance(_id, bson.ObjectId):
            _id = bson.ObjectId(_id)
        self._data['_id'] = _id
        # eppn
        eppn = self._data_in.pop('eduPersonPrincipalName')
        self._data['eduPersonPrincipalName'] = eppn

        # authn_resp
        authn_resp = self._data_in.pop('authn_resp')
        self._data['authn_resp'] = authn_resp

        # token_resp
        token_resp = self._data_in.pop('token_resp')
        self._data['token_resp'] = token_resp

        # userinfo
        userinfo = self._data_in.pop('userinfo')
        self._data['userinfo'] = userinfo

        self.modified_ts = self._data_in.pop('modified_ts', None)

        if len(self._data_in) > 0:
            if raise_on_unknown:
                raise UserHasUnknownData('User {!s} unknown data: {!r}'.format(
                    self.eppn, self._data_in.keys()
                ))
            # Just keep everything that is left as-is
            self._data.update(self._data_in)

    def __repr__(self):
        return '<eduID {!s}: {!s}>'.format(self.__class__.__name__, self.eppn)

    @property
    def _id(self):
        """
        Get the vetting datas _id

        :rtype: bson.ObjectId
        """
        return self._data['_id']

    @property
    def eppn(self):
        """
        Get the user's eppn

        :rtype: str | unicode
        """
        return self._data['eduPersonPrincipalName']

    @property
    def auth_resp(self):
        """
        Get the user's auth_resp

        :rtype: dict
        """
        return self._data['auth_resp']

    @property
    def token_resp(self):
        """
        Get the user's token_resp

        :rtype: dict
        """
        return self._data['token_resp']

    @property
    def userinfo(self):
        """
        Get the user's userinfo

        :rtype: dict
        """
        return self._data['userinfo']

    @property
    def modified_ts(self):
        """
        :return: Timestamp of last modification in the database.
                 None if User has never been written to the database.
        :rtype: datetime.datetime | None
        """
        return self._data.get('modified_ts')

    @modified_ts.setter
    def modified_ts(self, value):
        """
        :param value: Timestamp of modification.
                      Value None is ignored, True is short for datetime.utcnow().
        :type value: datetime.datetime | True | None
        """
        if value is None:
            return
        if value is True:
            value = datetime.datetime.utcnow()
        self._data['modified_ts'] = value

    def to_dict(self):
        res = copy.copy(self._data)  # avoid caller messing with our _data
        return res

class ProofDB(BaseDB):

    ProofDataClass = Proof

    def __init__(self, db_uri, db_name='eduid_oidc_proofing', collection='proofs'):
        BaseDB.__init__(self, db_uri, db_name, collection)

    def get_proofs_by_eppn(self, eppn, raise_on_missing=True):
        """
        Locate a vetting data in the db given the user's eppn.

        :param eppn: eppn
        :param raise_on_missing: Raise exception if True else return None

        :type eppn: str | unicode
        :type raise_on_missing: bool

        :return: VettingDataClass instance | None
        :rtype: VettingDataClass | None

        :raise self.DocumentDoesNotExist: No user match the search criteria
        """

        data = self._get_documents_by_attr('eduPersonPrincipalName', eppn, raise_on_missing)
        return [self.ProofDataClass(item) for item in data]

    def save(self, proof, check_sync=True):
        """

        :param proof: VettingDataClass object
        :param check_sync: Ensure the document hasn't been updated in the database since it was loaded

        :type proof: VettingDataClass
        :type check_sync: bool

        :return:
        """

        modified = proof.modified_ts
        proof.modified_ts = True  # update to current time
        if modified is None:
            # document has never been modified
            result = self._coll.insert(proof.to_dict())
            logging.debug("{!s} Inserted new state {!r} into {!r}): {!r})".format(
                self, proof, self._coll_name, result))
        else:
            test_doc = {'_id': proof._id}
            if check_sync:
                test_doc['modified_ts'] = modified
            result = self._coll.update(test_doc, proof.to_dict(), upsert=(not check_sync))
            if check_sync and result['n'] == 0:
                db_ts = None
                db_state = self._coll.find_one({'_id': proof._id})
                if db_state:
                    db_ts = db_state['modified_ts']
                logging.debug("{!s} FAILED Updating state {!r} (ts {!s}) in {!r}). "
                              "ts in db = {!s}".format(self, proof, modified, self._coll_name, db_ts))
                raise DocumentOutOfSync('Stale state object can\'t be saved')
            logging.debug("{!s} Updated state {!r} (ts {!s}) in {!r}): {!r}".format(
                self, proof, modified, self._coll_name, result))


import json

import requests

LUNA_PACIFIC_ENDPOINT = "https://luna.devhi.me/pacific"
NOTICE_ARTICLE_PREFIX = "/article/notice"

def store_notice_article(**kwargs):
    body = {
        "no": kwargs.get("no"),
        "subject": kwargs.get("subject"),
        "content": kwargs.get("content"),
        "hit": kwargs.get("hit"),
        "writer": kwargs.get("writer"),
        "url": kwargs.get("url"),
        "created_at": kwargs.get("created_at"),
        "modified_at": kwargs.get("modified_at"),
    }
    res = requests.post(LUNA_PACIFIC_ENDPOINT + NOTICE_ARTICLE_PREFIX, data=body)
    if res:
        res = json.loads(res.text)
        print("Response of store_notice_article : ", str(res))
    return res


# -*- coding: utf-8 -*-
"""
Created on Mon Aug 10 10:15:34 2020

@author: Ale
"""

from time import perf_counter
import matplotlib.pyplot as plt
import sys
from numpy import zeros, float32
import numpy as np

from scipy import linalg

def matriz_laplaciana(N, dtype=float32):

    Matrizlp = zeros((N,N), dtype=np.single)

    for i in range (N):

        for j in range (N):

            if i==j:
                Matrizlp[i,j]=2

            if i+1==j:
                Matrizlp[i,j]=-1

            if i-1==j:
                Matrizlp[i,j]=-1

    return Matrizlp

Tamañodelamatriz = [2,5,10,15,20,30,40,50,60,100,200,500,1000,2000,3000]

dt = []
memoria = []

Cantidadcorridas = 10

for numerocorrida in range(Cantidadcorridas):

    corrida = open(f"Corrida{numerocorrida}.txt","w")

    for N in Tamañodelamatriz:

         a=matriz_laplaciana(N,dtype=float32)

         t1 = perf_counter()

         X = linalg.inv(a,overwrite_a=True)

         t2 = perf_counter()

         deltatiempo = t2 - t1

         memoria1 = (sys.getsizeof(a))

         print(f"Para un N = {N}")

         print(f"El tiempo entre ambos es {deltatiempo} s")

         print(f"La memoria utilizada es de {memoria1} bytes")
         print("--------------------------------------")

         dt.append(deltatiempo)
         memoria.append(memoria1)

         corrida.write(f"{N} {deltatiempo} {memoria1}\n")
         corrida.flush()

plt.figure()

plt.subplot(2,1,1)

x = [10,20,50,100,200,500,1000,2000,5000,10000,20000]
y1 = [0.1e-3,1e-3,1e-2,0.1,1.,10.,60,60*10,]
yl = ["0.1 ms","1 ms","10 ms","0.1 s","1 s","10 s","1 min","10 min"]
y2 = [10**3,10**4,10**5,10**6,10**7,10**8,10**9,10**10,]
yt = ["1 KB","10 KB","100 KB","1 MB","10 MB","100 MB","1 GB","10 GB","100 GB"]

for i in range(Cantidadcorridas):

    memoriausada=[]
    tiempos = []
    archivos = (f"Corrida{i}.txt")
    apertura = open(archivos,'r')
    todas_las_lineas = apertura.readlines()

    for j in range(len(Tamañodelamatriz)):

        fila = []
        fila.append(todas_las_lineas[j])
        fila = fila[0].split(" ")
        tiempos.append(float(fila[1]))
        memoriausada.append(int(fila[2]))

    apertura.close()
    plt.loglog(Tamañodelamatriz,tiempos,"-o")

plt.xticks(x,[])
plt.yticks(y1,yl)
plt.grid(True)
plt.ylabel("Tiempo Transcurrido (s)")
plt.title("Rendimiento A@B=X")

plt.subplot(2,1,2)
plt.loglog(Tamañodelamatriz,memoriausada,"-o")
plt.xticks(x,x,rotation=45)
plt.grid(True)
plt.yticks(y2,yt)
plt.ylabel("Uso de Memoria")
plt.xlabel("Tamaño de la matriz (N)")
plt.axhline(y=16000000000, xmin=0.001, xmax=0.9999,color="red",ls="--")

plt.show()


_author__ = "Marco Clerici"

import sys
from config import es_constants
from apps.es2system import es2system
from lib.python import es_logging as log
logger = log.my_logger("apps.es2system.ingest_archive")

try:
    command = str(sys.argv[1])
except:
    logger.fatal("An argument should be provided: status/start/stop")
    exit(1)

# Define pid file and create daemon
pid_file = es_constants.ingest_archive_pid_filename
daemon = es2system.IngestArchiveDaemon(pid_file, dry_run=True)

status = False
if command == "status":
    status = daemon.status()
    logger.info("Current status of the IngestArchive Service: %s" % status)
    print("Current status of the Service: %s" % status)

if command == "start":
    logger.info("Starting IngestArchive service")
    daemon.start()
    status = daemon.status()

if command == "stop":
    logger.info("Stopping IngestArchive service")
    daemon.stop()
    status = daemon.status()

if command == "restart":
    logger.info("Restarting IngestArchive service")
    daemon.restart()
    status = daemon.status()


import sys,time

import pygame

class Ship:
    def __init__(self, screen):
        """initialize the ship and its starting position"""
        self.screen = screen
        # load the ship and its rect
        self.image = pygame.image.load('image/ship.png')
        self.rect = self.image.get_rect()
        self.screenRect = self.screen.get_rect()
        # Start each new ship at the bottom center of the screen.
        self.rect.centerx = self.screenRect.centerx
        self.rect.bottom = self.screenRect.bottom
        # movement flag
        self.move_right = False
        self.move_left = False
        self.move_up = False
        self.move_down = False
        # set speed
        self.speed = 3

    def blit(self):
        """draw a ship at its current location"""
        self.screen.blit(self.image, self.rect)

    def update(self, stat, ship_hit):
        if self.move_right and self.rect.right < 1200:
            self.rect.centerx += self.speed
        if self.move_left and self.rect.left > 0:
            self.rect.centerx -= self.speed
        if self.move_up and self.rect.top > 0:
            self.rect.bottom -= self.speed
        if self.move_down and self.rect.bottom < 700:
            self.rect.bottom += self.speed
        # ship_life
        if stat.ship_life < 0:
            # print("game over")
            stat.game_play = False
        if ship_hit:
            stat.ship_life -= 1
            time.sleep(0.3)
        if stat.new_life >= 10:
            stat.new_life -= 10
            stat.ship_life += 1