# Path: swbd/labels/fisher/character.py
# def read_trans(label_paths, target_speaker):
#     """Read transcripts (*_trans.txt) & save files (.npy).
#     Args:
#         label_paths: list of paths to label files
#         target_speaker: A or B
#     Returns:
#         speaker_dict: dictionary of speakers
#             key (string) => speaker
#             value (dict) => dictionary of utterance infomation of each speaker
#                 key => utterance index
#                 value => [start_frame, end_frame, transcript]
#         char_set (set):
#         char_capital_set (set):
#         word_count_dict (dict):
#             key => word
#             value => the number of words in Fisher corpus
#     """
#     print('=====> Processing target labels...')
#     speaker_dict = OrderedDict()
#     char_set, char_capital_set = set([]), set([])
#     word_count_dict = {}
#     vocab_set = set([])
# 
#     for label_path in tqdm(label_paths):
#         utterance_dict = OrderedDict()
#         with open(label_path, 'r') as f:
#             utt_index = 0
#             session = basename(label_path).split('.')[0]
#             for line in f:
#                 line = line.strip().split(' ')
#                 if line[0] in ['#', '']:
#                     continue
#                 start_frame = int(float(line[0]) * 100 + 0.05)
#                 end_frame = int(float(line[1]) * 100 + 0.05)
#                 which_speaker = line[2].replace(':', '')
#                 if which_speaker != target_speaker:
#                     continue
#                 speaker = session + '-' + which_speaker
# 
#                 # Clean transcript
#                 transcript_original = ' '.join(line[3:]).lower()
#                 transcript = fix_transcript(transcript_original)
# 
#                 # Convert space to "_"
#                 transcript = re.sub(r'\s', SPACE, transcript)
# 
#                 # Skip silence, laughter, noise, vocalized-noise only utterance
#                 if transcript.replace(NOISE, '').replace(LAUGHTER, '').replace(VOCALIZED_NOISE, '').replace(SPACE, '') != '':
# 
#                     # Remove the first and last space
#                     if transcript[0] == SPACE:
#                         transcript = transcript[1:]
#                     if transcript[-1] == SPACE:
#                         transcript = transcript[:-1]
# 
#                     # Count words
#                     for word in transcript.split(SPACE):
#                         vocab_set.add(word)
#                         if word not in word_count_dict.keys():
#                             word_count_dict[word] = 0
#                         word_count_dict[word] += 1
# 
#                     # Capital-divided
#                     transcript_capital = ''
#                     for word in transcript.split(SPACE):
#                         if len(word) == 1:
#                             char_capital_set.add(word)
#                             transcript_capital += word
#                         else:
#                             # Replace the first character with the capital
#                             # letter
#                             word = word[0].upper() + word[1:]
# 
#                             # Check double-letters
#                             for i in range(0, len(word) - 1, 1):
#                                 if word[i:i + 2] in DOUBLE_LETTERS:
#                                     char_capital_set.add(word[i:i + 2])
#                                 else:
#                                     char_capital_set.add(word[i])
#                             transcript_capital += word
# 
#                     for c in list(transcript):
#                         char_set.add(c)
# 
#                     utterance_dict[str(utt_index).zfill(4)] = [
#                         start_frame, end_frame, transcript]
# 
#                     # for debug
#                     # print(transcript_original)
#                     # print(transcript)
# 
#                 utt_index += 1
# 
#             speaker_dict[speaker] = utterance_dict
# 
#     # Reserve some indices
#     for mark in [SPACE, HYPHEN, APOSTROPHE, LAUGHTER, NOISE, VOCALIZED_NOISE]:
#         for c in list(mark):
#             char_set.discard(c)
#             char_capital_set.discard(c)
# 
#     # for debug
#     # print(sorted(list(char_set)))
#     # print(sorted(list(char_capital_set)))
# 
#     return speaker_dict, char_set, char_capital_set, word_count_dict
# 
# Path: utils/measure_time_func.py
# def measure_time(func):
#     @functools.wraps(func)
#     def _measure_time(*args, **kwargs):
#         start = time.time()
#         func(*args, **kwargs)
#         elapse = time.time() - start
#         print("Takes {} seconds.".format(elapse))
#     return _measure_time
# 
# Path: utils/util.py
# def mkdir_join(path_to_dir, *dir_name):
#     """Concatenate root path and 1 or more paths, and make a new direcory if
#     the direcory does not exist.
#     Args:
#         path_to_dir (string): path to a diretcory
#         dir_name (string): a direcory name
#     Returns:
#         path to the new directory
#     """
#     if path_to_dir is None:
#         return path_to_dir
# 
#     path_to_dir = mkdir(path_to_dir)
#     for i in range(len(dir_name)):
#         if i == len(dir_name) - 1 and '.' in dir_name[i]:
#             path_to_dir = os.path.join(path_to_dir, dir_name[i])
#         else:
#             path_to_dir = mkdir(os.path.join(path_to_dir, dir_name[i]))
#     return path_to_dir


from os.path import join
from glob import glob
from swbd.labels.fisher.character import read_trans
from utils.measure_time_func import measure_time
from utils.util import mkdir_join
import sys
import unittest
#! /usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function


sys.path.append('../../')

fisher_path = '/n/sd8/inaguma/corpus/swbd/data/fisher'

# Search paths to transcript
label_paths = []
for trans_path in glob(join(fisher_path, 'data/trans/*/*.txt')):
    label_paths.append(trans_path)
label_paths = sorted(label_paths)


class TestLabelFisher(unittest.TestCase):

    def test(self):

        self.check()

    @measure_time
    def check(self):

        read_trans(label_paths=label_paths, target_speaker='A')

# Path: swbd/labels/fisher/character.py
# def read_trans(label_paths, target_speaker):
#     """Read transcripts (*_trans.txt) & save files (.npy).
#     Args:
#         label_paths: list of paths to label files
#         target_speaker: A or B
#     Returns:
#         speaker_dict: dictionary of speakers
#             key (string) => speaker
#             value (dict) => dictionary of utterance infomation of each speaker
#                 key => utterance index
#                 value => [start_frame, end_frame, transcript]
#         char_set (set):
#         char_capital_set (set):
#         word_count_dict (dict):
#             key => word
#             value => the number of words in Fisher corpus
#     """
#     print('=====> Processing target labels...')
#     speaker_dict = OrderedDict()
#     char_set, char_capital_set = set([]), set([])
#     word_count_dict = {}
#     vocab_set = set([])
# 
#     for label_path in tqdm(label_paths):
#         utterance_dict = OrderedDict()
#         with open(label_path, 'r') as f:
#             utt_index = 0
#             session = basename(label_path).split('.')[0]
#             for line in f:
#                 line = line.strip().split(' ')
#                 if line[0] in ['#', '']:
#                     continue
#                 start_frame = int(float(line[0]) * 100 + 0.05)
#                 end_frame = int(float(line[1]) * 100 + 0.05)
#                 which_speaker = line[2].replace(':', '')
#                 if which_speaker != target_speaker:
#                     continue
#                 speaker = session + '-' + which_speaker
# 
#                 # Clean transcript
#                 transcript_original = ' '.join(line[3:]).lower()
#                 transcript = fix_transcript(transcript_original)
# 
#                 # Convert space to "_"
#                 transcript = re.sub(r'\s', SPACE, transcript)
# 
#                 # Skip silence, laughter, noise, vocalized-noise only utterance
#                 if transcript.replace(NOISE, '').replace(LAUGHTER, '').replace(VOCALIZED_NOISE, '').replace(SPACE, '') != '':
# 
#                     # Remove the first and last space
#                     if transcript[0] == SPACE:
#                         transcript = transcript[1:]
#                     if transcript[-1] == SPACE:
#                         transcript = transcript[:-1]
# 
#                     # Count words
#                     for word in transcript.split(SPACE):
#                         vocab_set.add(word)
#                         if word not in word_count_dict.keys():
#                             word_count_dict[word] = 0
#                         word_count_dict[word] += 1
# 
#                     # Capital-divided
#                     transcript_capital = ''
#                     for word in transcript.split(SPACE):
#                         if len(word) == 1:
#                             char_capital_set.add(word)
#                             transcript_capital += word
#                         else:
#                             # Replace the first character with the capital
#                             # letter
#                             word = word[0].upper() + word[1:]
# 
#                             # Check double-letters
#                             for i in range(0, len(word) - 1, 1):
#                                 if word[i:i + 2] in DOUBLE_LETTERS:
#                                     char_capital_set.add(word[i:i + 2])
#                                 else:
#                                     char_capital_set.add(word[i])
#                             transcript_capital += word
# 
#                     for c in list(transcript):
#                         char_set.add(c)
# 
#                     utterance_dict[str(utt_index).zfill(4)] = [
#                         start_frame, end_frame, transcript]
# 
#                     # for debug
#                     # print(transcript_original)
#                     # print(transcript)
# 
#                 utt_index += 1
# 
#             speaker_dict[speaker] = utterance_dict
# 
#     # Reserve some indices
#     for mark in [SPACE, HYPHEN, APOSTROPHE, LAUGHTER, NOISE, VOCALIZED_NOISE]:
#         for c in list(mark):
#             char_set.discard(c)
#             char_capital_set.discard(c)
# 
#     # for debug
#     # print(sorted(list(char_set)))
#     # print(sorted(list(char_capital_set)))
# 
#     return speaker_dict, char_set, char_capital_set, word_count_dict
# 
# Path: utils/measure_time_func.py
# def measure_time(func):
#     @functools.wraps(func)
#     def _measure_time(*args, **kwargs):
#         start = time.time()
#         func(*args, **kwargs)
#         elapse = time.time() - start
#         print("Takes {} seconds.".format(elapse))
#     return _measure_time
# 
# Path: utils/util.py
# def mkdir_join(path_to_dir, *dir_name):
#     """Concatenate root path and 1 or more paths, and make a new direcory if
#     the direcory does not exist.
#     Args:
#         path_to_dir (string): path to a diretcory
#         dir_name (string): a direcory name
#     Returns:
#         path to the new directory
#     """
#     if path_to_dir is None:
#         return path_to_dir
# 
#     path_to_dir = mkdir(path_to_dir)
#     for i in range(len(dir_name)):
#         if i == len(dir_name) - 1 and '.' in dir_name[i]:
#             path_to_dir = os.path.join(path_to_dir, dir_name[i])
#         else:
#             path_to_dir = mkdir(os.path.join(path_to_dir, dir_name[i]))
#     return path_to_dir


# Path: swbd/test/test_label_fisher.py
from os.path import join
from glob import glob
from swbd.labels.fisher.character import read_trans
from utils.measure_time_func import measure_time
from utils.util import mkdir_join
import sys
import unittest

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

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function


sys.path.append('../../')

fisher_path = '/n/sd8/inaguma/corpus/swbd/data/fisher'

# Search paths to transcript
label_paths = []
for trans_path in glob(join(fisher_path, 'data/trans/*/*.txt')):
    label_paths.append(trans_path)
label_paths = sorted(label_paths)


class TestLabelFisher(unittest.TestCase):

    def test(self):

        self.check()

    @measure_time
    def check(self):

        read_trans(label_paths=label_paths, target_speaker='A')

        read_trans(label_paths=label_paths, target_speaker='B')