# Path: analyzer/utils.py
# def window(seq, n=2):
# 	"""Returns a sliding window (of width n) over data from the iterable"""
# 	"   s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ...                   "
# 	it = iter(seq)
# 	result = tuple(islice(it, n))
# 	if len(result) == n:
# 		yield result
# 	for elem in it:
# 		result = result[1:] + (elem,)
# 		yield result
# 
# Path: analyzer/alignment_utils.py
# class Alignment(object):
# 	old_vertical_index = None
# 	old_horizontal_index = None
# 
# 	def __init__(self, alignment_list, vertical_index, horizontal_index, grid, traceback, subtitles=None):
# 		self.alignment_list = alignment_list
# 		self.vertical_index = vertical_index
# 		self.horizontal_index = horizontal_index
# 		self.grid = grid
# 		self.traceback = traceback
# 		self.subtitles = subtitles
# 
# Path: analyzer/script_entity.py
# class ScriptEntity(Model):
# 	def __init__(self, character, text, type="speech"):
# 		self.character = character
# 		self.type = type
# 		self.original_text = text
# 
# 		self.text = text.rstrip().replace('\n', ' ') if type == 'speech' else text
# 
# 	def __str__(self):
# 		return "type: {}, character: {}, text: \"{}\"".format(self.type, self.character, self.text)
# 
# 	def __repr__(self):
# 		return self.__str__()
# 
# 	@classmethod
# 	def from_dict(cls, data, from_camel=True):
# 		d = Model.from_dict(data, from_camel)
# 		return cls(d.get("character"), d.get("text"), type=d.get("type"))
# 
# 	@classmethod
# 	def from_dicts(cls, data, from_camel=True):
# 		return [cls.from_dict(d) for d in data]
# 
# Path: analyzer/string_utils.py
# def remove_punctuation(text):
# 	exclude = set(string.punctuation)
# 	exclude.add("'")
# 
# 	characters = []
# 	for character in text:
# 		if character not in exclude:
# 			characters.append(character)
# 		else:
# 			characters.append(" ")
# 
# 	cleaned_text = "".join(characters)
# 	var = re.sub(' +', ' ', cleaned_text)
# 
# 	return var


import editdistance
import numpy as np
from scipy.spatial import distance as dist
from tqdm import tqdm
from copy import deepcopy
from itertools import groupby
from analyzer.utils import window
from analyzer.alignment_utils import Alignment
from analyzer.script_entity import ScriptEntity
from analyzer.string_utils import remove_punctuation
			progress_bar.update(n)

	progress_bar.close()

	return grid, traceback


def calculate_backtrace(traceback):
	assert len(traceback) > 0
	assert len(traceback[0]) > 0

	m = len(traceback)
	n = len(traceback[0])

	alignment = []

	i, j = m - 1, n - 1
	alignment.append((i, j))

	while (i != 0) or (j != 0):
		coordinates = traceback[i][j]
		alignment.append(coordinates)
		i, j = coordinates

	alignment.reverse()

	return alignment


def convert_to_script_entities(script):

# Path: analyzer/utils.py
# def window(seq, n=2):
# 	"""Returns a sliding window (of width n) over data from the iterable"""
# 	"   s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ...                   "
# 	it = iter(seq)
# 	result = tuple(islice(it, n))
# 	if len(result) == n:
# 		yield result
# 	for elem in it:
# 		result = result[1:] + (elem,)
# 		yield result
# 
# Path: analyzer/alignment_utils.py
# class Alignment(object):
# 	old_vertical_index = None
# 	old_horizontal_index = None
# 
# 	def __init__(self, alignment_list, vertical_index, horizontal_index, grid, traceback, subtitles=None):
# 		self.alignment_list = alignment_list
# 		self.vertical_index = vertical_index
# 		self.horizontal_index = horizontal_index
# 		self.grid = grid
# 		self.traceback = traceback
# 		self.subtitles = subtitles
# 
# Path: analyzer/script_entity.py
# class ScriptEntity(Model):
# 	def __init__(self, character, text, type="speech"):
# 		self.character = character
# 		self.type = type
# 		self.original_text = text
# 
# 		self.text = text.rstrip().replace('\n', ' ') if type == 'speech' else text
# 
# 	def __str__(self):
# 		return "type: {}, character: {}, text: \"{}\"".format(self.type, self.character, self.text)
# 
# 	def __repr__(self):
# 		return self.__str__()
# 
# 	@classmethod
# 	def from_dict(cls, data, from_camel=True):
# 		d = Model.from_dict(data, from_camel)
# 		return cls(d.get("character"), d.get("text"), type=d.get("type"))
# 
# 	@classmethod
# 	def from_dicts(cls, data, from_camel=True):
# 		return [cls.from_dict(d) for d in data]
# 
# Path: analyzer/string_utils.py
# def remove_punctuation(text):
# 	exclude = set(string.punctuation)
# 	exclude.add("'")
# 
# 	characters = []
# 	for character in text:
# 		if character not in exclude:
# 			characters.append(character)
# 		else:
# 			characters.append(" ")
# 
# 	cleaned_text = "".join(characters)
# 	var = re.sub(' +', ' ', cleaned_text)
# 
# 	return var


# Path: analyzer/dtw_merger.py
import editdistance
import numpy as np
from scipy.spatial import distance as dist
from tqdm import tqdm
from copy import deepcopy
from itertools import groupby
from analyzer.utils import window
from analyzer.alignment_utils import Alignment
from analyzer.script_entity import ScriptEntity
from analyzer.string_utils import remove_punctuation

			progress_bar.update(n)

	progress_bar.close()

	return grid, traceback


def calculate_backtrace(traceback):
	assert len(traceback) > 0
	assert len(traceback[0]) > 0

	m = len(traceback)
	n = len(traceback[0])

	alignment = []

	i, j = m - 1, n - 1
	alignment.append((i, j))

	while (i != 0) or (j != 0):
		coordinates = traceback[i][j]
		alignment.append(coordinates)
		i, j = coordinates

	alignment.reverse()

	return alignment


def convert_to_script_entities(script):

	script_entities = [ScriptEntity.from_dict(script_dict) for script_dict in script]