import cv2

def compress_and_remove_duplicate_frames(input_file, output_file):
    # Đọc video gốc
    video = cv2.VideoCapture(input_file)

    # Lấy thông số video gốc
    width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
    fps = int(video.get(cv2.CAP_PROP_FPS))

    # Tạo đối tượng VideoWriter để ghi video nén
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    output = cv2.VideoWriter(output_file, fourcc, fps, (width, height))

    # Khởi tạo biến lưu trữ khung hình trước đó
    prev_frame = None

    while True:
        ret, frame = video.read()
        if not ret:
            break

        # Kiểm tra khung hình hiện tại có giống khung hình trước đó không
        if prev_frame is not None and (frame == prev_frame).all():
            continue  # Bỏ qua nếu khung hình trùng lặp

        # Nén và ghi khung hình vào video nén
        output.write(frame)

        # Cập nhật khung hình trước đó
        prev_frame = frame.copy()

    # Giải phóng tài nguyên
    video.release()
    output.release()
    cv2.destroyAllWindows()

# Sử dụng hàm để nén và loại bỏ khung hình giống nhau
compress_and_remove_duplicate_frames('/home/maicg/Downloads/toan_canh_hop_quoc_hoi_ngay_27_5_ky_hop_thu_5_quoc_hoi_khoa_xv_vnews.mp4', 'output.mp4')


import pdfplumber

pot = 'jo_nesbø_Snømmanen.pdf'

with pdfplumber.open(pot) as pdf:
    with open('prebrana_knjiga.txt', 'a', encoding='utf-8') as dat:
        for page_number in range(905):
            st_dela = 0
            if (page_number + 1) not in {193, 362, 485, 767}:
                dat.write(f'({page_number + 1})')
                dat.write('\n')
                dat.write(pdf.pages[page_number].extract_text(x_tolerance=3, y_tolerance=3))
                dat.write('\n')
            else:
                slovar_posebnih_strani = {193: 1, 362: 2, 485: 3, 767: 4}
                dat.write(f'({page_number + 1})')
                dat.write('\n')
                dat.write(pdf.pages[page_number].extract_text(x_tolerance=3, y_tolerance=3))
                dat.write('\n')
                dat.write(f'KONEC DELA {slovar_posebnih_strani[page_number + 1]}')
                dat.write('\n')
with open('prebrana_knjiga.txt', 'a', encoding='utf-8') as dat:
    dat.write('KONEC')


from django.db import models
from django.conf import settings
from django.utils import timezone
from django.utils.text import slugify

# signals imports
from django.dispatch import receiver
from django.db.models.signals import (
pre_save,
post_save,
pre_delete,
post_delete,
m2m_changed,
)

User = settings.AUTH_USER_MODEL

@receiver(pre_save, sender=User)
def user_pre_save_receiver(sender, instance, *args, **kwargs):
    """
     before saved in the database
     """
    print(instance.username, instance.id)
# pre_save.connect(user_pre_save_receiver, sender=User)

@receiver(post_save, sender=User)
def user_post_save_receiver(sender, instance, created, *args, **kwargs):
    """
     after saved in the database
     """
    if created:
        print("send email to", instance.username)
        # trigger pre_save
        # instance.save()
        # trigger post_save
    else:
        print(instance.username, "was just saved")
# post_save.connect(user_post_save_receiver, sender = User)

class BlogPost(models.Model):
    title = models.CharField(max_length=120)
    slug = models.SlugField(blank=True, null=True)
    liked = models.ManyToManyField(User, blank=True)
    notify_users = models.BooleanField(default=False)
    notify_users_timestamp = models.DateTimeField(blank=True, null=True, auto_now_add=False)
    active = models.BooleanField(default=True)


# -*- coding: utf-8 -*-
# __author__ = 'fwy'
# last_modify:20200411

from biocluster.core.exceptions import OptionError
from biocluster.module import Module
import gevent
import re
import os
import unittest
import subprocess
from biocluster.config import Config
import pandas as pd

class HclusterHeatmapModule(Module):
    """
    该module为了基因组坐标转化功能，针对没有已经chain文件的分析项目开发的module,其目的功能是通过给定的
    已知fasta和目标fasta，通过互相比对生成chain文件，后通过liftover进行分析，来生成最终的结果文件
    """
    def __init__(self, work_id):
        super(HclusterHeatmapModule, self).__init__(work_id)
        options = [
            {"name":'otutable', 'type': 'infile', 'format': 'ref_rna_v2.common'},
            {"name": "sep", "type": 'string', "default": "tab"},
            {"name": 'log_change', "type": 'string'},
            {'name': 'scale', 'type': 'string'},
            {"name": 'cluster', 'type': 'bool'},
            {'name': 'gcd', 'type': 'string'},   #行距离算法
            {"name": 'gcm', 'type': 'string'},   #行聚类方式
            {"name": 'scd', 'type': 'string'},   #列距离算法
            {"name": 'scm', 'type': 'string'},   #列聚类方式
            {"name": 'title', 'type': 'string'},
            {'name': 'main_id', 'type': 'string'},
            {'name': 'update_info', 'type': 'string'}
        ]
        self.add_option(options)
        self.distance_col = self.add_tool('tool_lab.distance')
        self.cluster_col = self.add_tool('tool_lab.hcluster')
        self.distance_row = self.add_tool('tool_lab.distance')
        self.cluster_row = self.add_tool('tool_lab.hcluster')
        self.heatmap = self.add_tool('tool_lab.heatmap')

    def check_options(self):
        for k, v in self._options.items():
            self.logger.debug('{} -> {}'.format(k, v.value))
        else:
            return True

    def run(self):
        super(HclusterHeatmapModule, self).run()
        # if self.option('cluster') == False:
        #     self.run_heatmap()
        #     self.on_rely([self.heatmap], self.set_output)
        # else:
        #     self.run_heatmap()
        #
        #     self.on_rely([self.heatmap], self.run_distance_col)
        #     self.on_rely([self.heatmap], self.run_distance_row)
        self.run_heatmap()
        self.on_rely([self.cluster_col, self.cluster_row, self.heatmap], self.set_output)
    def run_distance_col(self):
        sep_in = 'tab'
        sep = {'tab': '\t', 'comma': ',', 'semicolon': ';', 'space': '\s+'}[sep_in]
        otutable = pd.read_table(self.heatmap.option('scale_table').path,sep=sep)
        otutable.to_csv(os.path.join(self.distance_col.work_dir, 'sample_otutable.txt'), sep='\t',index=False,header=True)
        otutable_path = os.path.join(self.distance_col.work_dir, 'sample_otutable.txt')
        a = otutable < 0
        c = a.values.flatten().tolist()
        # a = otutable.values.flatten().tolist()
        # c = [b < 0 for b in a]
        if any(c) and self.option('scd').lower() in ['bray_curtis', 'bray_curtis_faith', 'bray_curtis_magurran',
                                                          'canberra', 'hellinger', 'kulczynski', 'abund_jaccard', 'morisita_horn',
                                                          'soergel', 'specprof', 'binary_sorensen_dice', 'binary_hamming',
                                                          'binary_jaccard', 'binary_lennon', 'binary_ochiai', 'chisq']:
            self.set_error("原始表中或者标准化之后的表中含有负值，无法使用{}距离算法，请重新选择参数".format(self.option('scd')))
            # raise ValueError("原始表中或者标准化之后的表中含有负值，无法使用{}距离算法，请重新选择参数".format(self.option('scd')))
        self.distance_col.set_options({
            "method": self.option("scd"),
            "otutable": otutable_path,
            'sep': sep_in

        })
        self.distance_col.on('end', self.run_cluster_col)
        self.distance_col.run()

    def run_cluster_col(self):

        self.cluster_col.set_options({
            "dis_matrix": self.distance_col.option('dis_matrix'),
            "linkage": self.option('scm').lower()
        })
        # self.cluster_col.on('end', self.set_output)
        self.cluster_col.run()

    def run_distance_row(self):
        sep_in = 'tab'
        sep = {'tab': '\t', 'comma': ',', 'semicolon': ';', 'space': '\s+'}[sep_in]
        otutable = pd.read_table(self.heatmap.option('scale_table').path, sep=sep)
        otutable = otutable.T
        otutable.to_csv(os.path.join(self.distance_row.work_dir, 'gene_otutable.txt'), sep='\t',header=False)
        otutable_path = os.path.join(self.distance_row.work_dir, 'gene_otutable.txt')
        self.distance_row.set_options({
            "method": self.option("gcd"),
            "otutable": otutable_path,
            'sep': sep_in

        })
        self.distance_row.on('end', self.run_cluster_row)
        self.distance_row.run()

    def run_cluster_row(self):

        self.cluster_row.set_options({
            "dis_matrix": self.distance_row.option('dis_matrix'),
            "linkage": self.option('gcm').lower()
        })
        # self.cluster_row.on('end', self.set_output)
        self.cluster_row.run()

    def run_heatmap(self):
        sep = {'tab': '\t', 'comma': ',', 'semicolon': ';', 'space': '\s+'}[self.option('sep')]
        otutable = pd.read_table(self.option('otutable').path,sep=sep)
        otutable.to_csv(os.path.join(self.heatmap.work_dir, 'otutable.txt'), sep='\t',index=False,header=True)
        otutable_path = os.path.join(self.heatmap.work_dir, 'otutable.txt')
        self.heatmap.set_options({
            'otutable': otutable_path,
            'log_change': self.option('log_change'),
            'scale': self.option('scale')
        })
        if self.option('cluster') == False:
            self.heatmap.on('end', self.set_output)
        else:
            self.heatmap.on('end', self.run_distance_col)
            self.heatmap.on('end', self.run_distance_row)
        self.heatmap.run()
    def set_output(self):
        if self.option('cluster') == True:
            for file_name in os.listdir(self.cluster_col.output_dir):
                source = os.path.join(self.cluster_col.output_dir, file_name)
                link_name = os.path.join(self.output_dir, '{}_{}'.format('sample',file_name))
                if os.path.isfile(link_name):
                    os.remove(link_name)
                os.link(source, link_name)
            for file_name in os.listdir(self.cluster_row.output_dir):
                source = os.path.join(self.cluster_row.output_dir, file_name)
                link_name = os.path.join(self.output_dir, '{}_{}'.format('feature', file_name))
                if os.path.isfile(link_name):
                    os.remove(link_name)
                os.link(source, link_name)
            for file_name in os.listdir(self.heatmap.output_dir):
                source = os.path.join(self.heatmap.output_dir, file_name)
                link_name = os.path.join(self.output_dir, file_name)
                if os.path.isfile(link_name):
                    os.remove(link_name)
                os.link(source, link_name)
        else:
            for file_name in os.listdir(self.heatmap.output_dir):
                source = os.path.join(self.heatmap.output_dir, file_name)
                link_name = os.path.join(self.output_dir, file_name)
                if os.path.isfile(link_name):
                    os.remove(link_name)
                os.link(source, link_name)
        self.end()

    def end(self):
        super(HclusterHeatmapModule, self).end()

class TestFunction(unittest.TestCase):
    """
    This is test for the tool. Just run this script to do test.
    """
    def test(self):
        import random
        from mbio.workflows.single import SingleWorkflow
        from biocluster.wsheet import Sheet
        data = {
            "id": "cluster_heatmap" + str(random.randint(1, 10000)),
            "type": "module",
            "name": "tool_lab.hcluster_heatmap",
            "instant": False,
            "options": dict(
                gcd="euclidean",
                otutable="/mnt/ilustre/users/sanger-dev/workspace/20200508/GenesetCluster_tsg_37259_1072_2943/exp_matrix",
                gcm="complete",
                scd="euclidean",
                scm="single",
                log_change='log10',
                scale='zscore',
                cluster=True
            )
           }
        wsheet = Sheet(data=data)
        wf = SingleWorkflow(wsheet)
        wf.run()

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


import sys

def solve():
    N, K, tr, tc = tuple(map(int, input().split()))
    tr -= 1
    tc -= 1
    tr %= K
    tc %= K

    '''
        (tr, tc) = (2, 5) --> offset is 3
    '''

    if tc < tr: tc += K
    offset = tc - tr

    out = [['.' for _ in range(N)] for _ in range(N)]
    for rblock in range(N//K):
        for cblock in range(N//K):
            for k in range(K):
                r = rblock * K + k
                c = cblock * K + (k + offset) % K
                # print((rblock, cblock), k, (r,c), file=sys.stderr)
                out[r][c] = 'X'

    print('\n'.join(''.join(row) for row in out))

T = int(input())
for _ in range(T):
    solve()


import nltk

from nltk.corpus import words, names

# nltk.download()

nltk.download('words', quiet=True)
nltk.download('names', quiet=True)

def word_check(phrase):

  words_list = words.words()
  names_list = names.words()
  # print(words_list)

  word_list_test = phrase.split(' ')

  hits = 0
  num_words = 0

  for word in word_list_test:
      if word.lower() in words_list:
          # print(f'The word {word} is in the list')
          hits += 1
          num_words += 1
      else:
          # print(f'The word {word} is not in the list')
          num_words += 1

  # print(f"{hits/num_words * 100}%")
  return (hits/num_words * 100)

if __name__ == "__main__":
  print(word_check('It was the best of times, it was the worst of times.'))