2018-04-02 01:07:50 +02:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2018-04-25 09:07:46 +02:00
|
|
|
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
## ======================= user define =======================
|
2018-04-02 01:07:50 +02:00
|
|
|
repo_dir = 'C:\\Users\\Aki\\source\\repos\\acoustic_model'
|
|
|
|
curr_dir = repo_dir + '\\acoustic_model'
|
2018-04-25 09:07:46 +02:00
|
|
|
forced_alignment_module = 'C:\\Users\\Aki\\source\\repos\\forced_alignment'
|
|
|
|
|
|
|
|
|
2018-04-02 01:07:50 +02:00
|
|
|
sys.path.append(os.path.join(os.path.dirname(sys.path[0]), curr_dir))
|
2018-04-25 09:07:46 +02:00
|
|
|
sys.path.append(forced_alignment_module)
|
|
|
|
from forced_alignment import convert_phone_set
|
2018-04-02 01:07:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
def make_hcopy_scp_from_filelist_in_fame(FAME_dir, dataset, feature_dir, hcopy_scp):
|
|
|
|
""" Make a script file for HCopy using the filelist in FAME! corpus. """
|
|
|
|
filelist_txt = FAME_dir + '\\fame\\filelists\\' + dataset + 'list.txt'
|
|
|
|
with open(filelist_txt) as fin:
|
|
|
|
filelist = fin.read()
|
|
|
|
filelist = filelist.split('\n')
|
|
|
|
|
|
|
|
with open(hcopy_scp, 'w') as fout:
|
|
|
|
for filename_ in filelist:
|
|
|
|
filename = filename_.replace('.TextGrid', '')
|
|
|
|
|
|
|
|
if len(filename) > 3: # remove '.', '..' and ''
|
|
|
|
wav_file = FAME_dir + '\\fame\\wav\\' + dataset + '\\' + filename + '.wav'
|
|
|
|
mfc_file = feature_dir + '\\' + filename + '.mfc'
|
|
|
|
|
|
|
|
fout.write(wav_file + '\t' + mfc_file + '\n')
|
|
|
|
|
|
|
|
|
|
|
|
def make_filelist(input_dir, output_txt):
|
|
|
|
""" Make a list of files in the input_dir. """
|
|
|
|
filenames = os.listdir(input_dir)
|
|
|
|
|
|
|
|
with open(output_txt, 'w') as fout:
|
|
|
|
for filename in filenames:
|
|
|
|
fout.write(input_dir + '\\' + filename + '\n')
|
|
|
|
|
|
|
|
|
|
|
|
def get_phonelist(lexicon_file):
|
|
|
|
""" Make a list of phones which appears in the lexicon. """
|
|
|
|
|
|
|
|
with open(lexicon_file, "rt", encoding="utf-8") as fin:
|
|
|
|
lines = fin.read()
|
|
|
|
lines = lines.split('\n')
|
|
|
|
phonelist = set([])
|
|
|
|
for line in lines:
|
|
|
|
line = line.split('\t')
|
|
|
|
if len(line) > 1:
|
|
|
|
pronunciation = set(line[1].split())
|
|
|
|
phonelist = phonelist | pronunciation
|
|
|
|
return phonelist
|
|
|
|
|
|
|
|
|
|
|
|
def find_phone(lexicon_file, phone):
|
|
|
|
""" Search where the phone is used in the lexicon. """
|
|
|
|
with open(lexicon_file, "rt", encoding="utf-8") as fin:
|
|
|
|
lines = fin.read()
|
|
|
|
lines = lines.split('\n')
|
|
|
|
|
|
|
|
extracted = []
|
|
|
|
for line in lines:
|
|
|
|
line = line.split('\t')
|
|
|
|
if len(line) > 1:
|
|
|
|
pron = line[1]
|
|
|
|
if phone in pron:
|
|
|
|
extracted.append(line)
|
2018-04-25 09:07:46 +02:00
|
|
|
return extracted
|
|
|
|
|
|
|
|
|
|
|
|
def ipa2famehtk_lexicon(lexicon_file_in, lexicon_file_out):
|
|
|
|
""" Convert a lexicon file from IPA to HTK format for FAME! corpus. """
|
|
|
|
|
|
|
|
lexicon_in = pd.read_table(lexicon_file_in, names=['word', 'pronunciation'])
|
|
|
|
with open(lexicon_file_out, "w", encoding="utf-8") as fout:
|
|
|
|
for word, pronunciation in zip(lexicon_in['word'], lexicon_in['pronunciation']):
|
|
|
|
pronunciation_no_space = pronunciation.replace(' ', '')
|
|
|
|
pronunciation_famehtk = convert_phone_set.ipa2famehtk(pronunciation_no_space)
|
|
|
|
if 'ceh' not in pronunciation_famehtk and 'sh' not in pronunciation_famehtk:
|
|
|
|
fout.write("{0}\t{1}\n".format(word.upper(), pronunciation_famehtk))
|
|
|
|
|
|
|
|
|
|
|
|
def combine_lexicon(lexicon_file1, lexicon_file2, lexicon_out):
|
|
|
|
""" Combine two lexicon files and sort by words. """
|
|
|
|
|
|
|
|
with open(lexicon_file1, "rt", encoding="utf-8") as fin:
|
|
|
|
lines1 = fin.read()
|
|
|
|
lines1 = lines1.split('\n')
|
|
|
|
with open(lexicon_file2, "rt", encoding="utf-8") as fin:
|
|
|
|
lines2 = fin.read()
|
|
|
|
lines2 = lines2.split('\n')
|
|
|
|
|
|
|
|
lex1 = pd.read_table(lexicon_file1, names=['word', 'pronunciation'])
|
|
|
|
lex2 = pd.read_table(lexicon_file2, names=['word', 'pronunciation'])
|
|
|
|
lex = pd.concat([lex1, lex2])
|
|
|
|
lex = lex.sort_values(by='word', ascending=True)
|
|
|
|
lex.to_csv(lexicon_out, index=False, header=False, encoding="utf-8", sep='\t')
|