# -*- coding: utf-8 -*-
import os
import re
import sys
from gmisclib import die
from gmisclib import g2_select
sys.path.insert(0, 'lib')
import get_phones
import prep_p_VY_fromphones
import prep_p_Vy

MIN_VY_PHONE_FREQ = 200
MIN_Vy_PHONE_FREQ = 10


def selected(info):
	return True

def select_Vy_datalist(data):
	return data

def select_VY_datalist(infolist):
	return [info for info in infolist if "ASR" in info['metadatafile']]


MAN_accept = [ re.compile("^manual_[A-Z]+[.]lab$") ]
MAN_deny = [ re.compile("^C[CV]_"), re.compile("_C[CV]_") ]

class find_manual_segmentations(get_phones.find_manual_segmentations_from_pat):
	def __init__(self, manDBlist):
		get_phones.find_manual_segmentations_from_pat.__init__(self, manDBlist, accept=MAN_accept, deny=MAN_deny)
		
		

def get_VY_transcription(info):
		phonefile = os.path.join(info['dbdir'], info.get('trans_real', info['trans_exp']))
		phonelist, priority = get_phones.read_phones_nt(phonefile)
		return (phonelist, priority, phonefile)



def prepare_VY_phones(info, pmap, phonology):
	return prep_p_VY_fromphones.prepare_phones(info, pmap, phonology, get_VY_transcription)

def prepare_Vy_phones(info, pmap, phonology, find_manual_segmentation):
	candidate_files = find_manual_segmentation(info)
	return prep_p_Vy.prepare_phones(info, pmap, phonology, candidate_files)


def read_VY(mdfn):
	die.info("Reading metadata from %s" % mdfn)
	return get_phones.read_Rhythm_metadata(mdfn)


def read_Vy(mdfn):
	return get_phones.read_Rhythm_metadata(mdfn)
