Exemplo n.º 1
0
def add_stress_to_full(full_file, out_file, out_full_no_time_file, name):

    # print name

    pattern = re.compile(r"""(?P<start>.+)\s(?P<end>.+)\s.+
        /A:.+\-(?P<cur_phone_position>.+)_.+\+.+
        /B:.+
        """,re.VERBOSE)

    count = 0

    o = []

    for line in Utility.read_file_line_by_line(full_file):
        match = re.match(pattern, line)
        if match:
            cur_phone_position = match.group('cur_phone_position')
            # print cur_phone_position
            if (cur_phone_position == '1') | (cur_phone_position == 'x'):
                count = count + 1
                # syl_id = '{}_{}'.format(name, count)
                # print syl_id

            pre_syl = '{}_{}'.format(name, count-1)
            cur_syl = '{}_{}'.format(name, count)
            suc_syl = '{}_{}'.format(name, count+1)

            if pre_syl not in db:
                pre_stress = 'x'
            else:
                pre_stress = db[pre_syl]['stress']

            if cur_phone_position == 'x':
                cur_stress = 'x'
            else :
                cur_stress = db[cur_syl]['stress']

            if suc_syl not in db:
                suc_stress = 'x'
            else:
                suc_stress = db[suc_syl]['stress']

            stress_context = '/I:{}-{}+{}'.format(pre_stress, cur_stress, suc_stress)
            # print stress_context

            context = '{}{}'.format(Utility.trim(line), stress_context)
            # print context

            o.append(context)

    o_no_time = get_remove_time(o)
    # print o_no_time

    if (not (len(Utility.read_file_line_by_line(full_file)) == len(o))) & (not (len(Utility.read_file_line_by_line(full_file)) == len(o_no_time))):
        print name

    Utility.write_to_file_line_by_line(out_file, o)
    Utility.write_to_file_line_by_line(out_full_no_time_file, o_no_time)

    pass
Exemplo n.º 2
0
    def gen_dur_and_name_list_for_phone(syllable_label_path, name,
                                        phone_duration_path):

        pattern = re.compile(r""".+/A:.+/D:.+\-(?P<num_phone>.+)\+.+/E:.+""",
                             re.VERBOSE)

        dur_list = []
        for idx, line in enumerate(
                Utility.read_file_line_by_line(phone_duration_path)):
            spl = Utility.trim(line).split(' ')

            frame = int(spl[1]) - int(spl[0])
            frame = frame / 50000

            dur_list.append(frame)

        names = []
        for idx, line in enumerate(
                Utility.read_file_line_by_line(syllable_label_path)):
            spl = Utility.trim(line).split(' ')

            match = re.match(pattern, line)
            if match:
                num_phone = match.group('num_phone')
                if num_phone == 'x':
                    num_phone = 1
                else:
                    num_phone = int(num_phone)

            for phone_index in xrange(num_phone):
                names.append('{}_{}_{}'.format(name, (idx + 1), phone_index))

        return (dur_list, names)
Exemplo n.º 3
0
def gen_phone_duration_in_syllable(filepath, outpath_file):

    pattern = re.compile(
        r"""(?P<start>.+)\s(?P<end>.+)\s.+\-(?P<curphone>.+)\+.+/A:.+\-(?P<cur_phone_index>.+)_.+\+.+/B:.+/D:.+\-(?P<phone_number>.+)\+.+/E:.+""",
        re.VERBOSE)
    # 87545000 108545472 n^-sil+X/A:3_10-x_x+x_x/B:0-x+x/C:4_29-x_x+x_x/D:3-x+x/E:18-x+x/F:10_4-x_x+x_x/G:x_29_18/H:2-x+x
    # 85143750 87545000 aa-n^+sil/A:2_9-3_10+x_x/B:0-0+x/C:3_28-4_29+x_x/D:3-3+x/E:17-18+x/F:2_1-10_4+x_x/G:x_29_18/H:45-2+x

    main_duration = []
    temp_dur = []

    for line in Utility.read_file_line_by_line(filepath):
        # print line
        match = re.match(pattern, line)
        if match:

            start = float(match.group('start'))
            end = float(match.group('end'))

            phone_index = match.group('cur_phone_index')
            phone_number = match.group('phone_number')

            # print phone_index, phone_number, start, end

            if phone_index == 'x':
                temp_dur = []
                temp_dur.append(end - start)
                main_duration.append(temp_dur)
            elif phone_index == '1':
                temp_dur = []
                temp_dur.append(end - start)
            elif phone_index == phone_number:
                temp_dur.append(end - start)
                main_duration.append(temp_dur)
            else:
                temp_dur.append(end - start)

    # print main_duration

    c = 0

    for m in main_duration:
        # print m
        c = c + len(m)

    if c != len(Utility.read_file_line_by_line(filepath)):
        print 'No equal', filepath

    Utility.save_obj(main_duration, outpath_file)

    pass
    def cal_duration_distortion_from_label_and_npy_list(org_path, syn_path):

        dur_true_list = []
        dur_pred_list = []

        for file in Utility.list_file(org_path):
            if file.startswith('.'): continue

            base = Utility.get_basefilename(file)
            syn = numpy.load('{}/{}.npy'.format(syn_path, base))

            for idx, line in enumerate(
                    Utility.read_file_line_by_line('{}/{}'.format(
                        org_path, file))):

                if ('sil' in line) | ('pau' in line):
                    continue

                # print syn[idx]
                o = line.split(' ')

                dur_true_list.append(1000 * (float(o[1]) - float(o[0])) /
                                     10000000)
                dur_pred_list.append(1000 * syn[idx])

        rmse = numpy.sqrt(
            sklearn.metrics.mean_squared_error(dur_true_list, dur_pred_list))
        return rmse, len(dur_true_list), len(dur_pred_list)
Exemplo n.º 5
0
def get_last_syllable(filepath, name):

    # sil-sil+sil/A:X-X+X/S:l-x+z^/B:x-x+3/C:x_x-x_x+1_1/D:x-x+2/E:x-x+1/F:x_x-x_x+2_1/G:x_35_23/H:x-x+45

    global out

    pattern = re.compile(r"""(?P<start>.+)\s(?P<end>.+)\s.+\-(?P<curphone>.+)\+.+/A:.+/C:.+\-(?P<cur_position_syl>.+)_.+\+.+/D:.+/F:.+\-.+_(?P<cur_num_syl>.+)\+.+/G:.+""",re.VERBOSE)

    count = 0

    for line in Utility.read_file_line_by_line(filepath):

        count = count + 1

        match = re.match(pattern, line)
        if match:
           cur_position_syl = match.group('cur_position_syl')
           cur_num_syl = match.group('cur_num_syl')

           iden = '{}_{}'.format(name, count)
           # print iden

           if cur_position_syl == 'x': continue

           if ( cur_position_syl == cur_num_syl ) & ( ( cur_num_syl != '1' ) ):
                # print iden
                out.append(iden)

    pass
Exemplo n.º 6
0
def set_pre_suc():
    tones = ['01234']

    name_list_path = '/home/h1/decha/Dropbox/python_workspace/Inter_speech_2016/playground/list_file_for_preceeding_suceeding/list_gpr_file/'

    for t in tones:
        path = '/home/h1/decha/Dropbox/Inter_speech_2016/Syllable_object/mix_object/current_version/all_vowel_type/syllable_object_{}.pickle'.format(
            t)
        print path

        syl_management = Utility.load_obj(path)
        for syl in syl_management.syllables_list:
            if 'manual' in syl.name_index: continue

            name = syl.name_index.split('_')
            file_tar = '{}/{}/{}.lab'.format(name_list_path, name[2][0],
                                             name[2])
            list_file = Utility.read_file_line_by_line(file_tar)
            for idx, l in enumerate(list_file):
                f = Utility.trim(l)
                if f == syl.name_index:
                    # print '--------------------'
                    preceeding = Utility.trim(list_file[idx - 1])
                    # print f
                    succeeding = Utility.trim(list_file[idx + 1])
                    # print '--------------------'
                    syl.set_preceeding_succeeding_name_index(
                        preceeding, succeeding)

            # sys.exit()

        Utility.save_obj(syl_management, path)
    def gen_syllable_tag(syllable_label_path, tone, start_set, end_set, tag):
        # Format a_tscsda01_3
        #         'tscsd_manual'
        lf0_tags = []

        for set in Utility.char_range(start_set, end_set):
            path = '{}/{}'.format(syllable_label_path, set)

            count = Utility.count_valid_file(path)

            for i in range(1, count + 1):

                filepath = '{}/tscsd_stust_{}{}.stresslab'.format(
                    path, set, Utility.fill_zero(i, 2))
                syllable_count = 0
                for line in Utility.read_file_line_by_line(filepath):
                    syllable_count += 1

                    if tone == '01234':
                        lf0_tags.append('{}_{}_{}{}_{}'.format(
                            set, tag, set, Utility.fill_zero(i, 2),
                            syllable_count))
                    else:
                        if line[0] == tone:
                            lf0_tags.append('{}_{}_{}{}_{}'.format(
                                set, tag, set, Utility.fill_zero(i, 2),
                                syllable_count))

        return lf0_tags

        pass
def fix_database(db_file, change_list_file, out_file):

    global db 
    db = None
    db = Utility.load_obj(db_file)

    change_list = []

    less_than = None

    for line in Utility.read_file_line_by_line(change_list_file):
        if 'tsc' in line:
            n = Utility.trim(line).replace(' ', '_')
            change_list.append(n)
        elif '<' in line:
            # print line
            less_than = line.split(' ')[1]
            pass

    # print change_list
    # print less_than

    if (len(change_list) == 0) | (less_than == None):
        raise 'Change list file false'

    new_list = change_stress(change_list, less_than)

    Utility.save_obj(new_list, out_file)

    pass
def get_org_syllable_list(filename):

    dur_list = []
    num_phone_list = []

    for line in Utility.read_file_line_by_line(filename):
        pattern = re.compile(
            r"""(?P<start>.+)\s(?P<end>.+)\s.+/D:.+\-(?P<num_phone>.+)\+.+/E:.+""",
            re.VERBOSE)
        match = re.match(pattern, line)
        if match:
            num_phone = match.group('num_phone')

            start = match.group('start')
            end = match.group('end')

            # print start, end, num_phone

            d = float(int(end) - int(start)) / 10000000
            # print d
            dur_list.append(d)
            num_phone_list.append(num_phone)

    return (dur_list, num_phone_list)

    pass
Exemplo n.º 10
0
def read_rmse_file(rmse_file):

    param_old = []
    rmse_old = 100000000.0
    line_old = ''
    const = ''
    thefile = Utility.read_file_line_by_line(rmse_file)

    # print rmse_file

    for idx, line in enumerate(thefile):
        if 'const' in line:
            const = Utility.trim(line)

            l = thefile[idx + 1].split(' ')
            current_param = [l[0], l[1], l[2], l[3], l[4]]

        if 'Only' in line:
            spl = line.split(' ')
            rmse = float(spl[5])

            if rmse < rmse_old:
                rmse_old = rmse
                param_old = current_param
                line_old = line

    return (rmse_old, param_old, line_old, const)
Exemplo n.º 11
0
def gen_file_list():

    outpath = '/home/h1/decha/Dropbox/python_workspace/Inter_speech_2016/playground/list_file_for_preceeding_suceeding/list_gpr_file/'

    label_path = '/work/w2/decha/Data/GPR_data/label/03_GPR_syllable_level/full/tsc/sd/'
    start_set = 'a'
    end_set = 'j'

    for sett in Utility.char_range(start_set, end_set):
        set_path = '{}/{}/'.format(label_path, sett)

        out_set_path = '{}/{}/'.format(outpath, sett)
        Utility.make_directory(out_set_path)

        for f in Utility.list_file(set_path):
            if f.startswith('.'): continue
            file_path = '{}/{}'.format(set_path, f)
            count = 0
            # print f
            file_number = f[6] + f[7]

            out_list = []

            for line in Utility.read_file_line_by_line(file_path):
                # print Utility.trim(line)
                out = ''
                if 'sil-sil+sil/A:' in line:
                    out = 'sil'
                elif 'pau-pau+pau/A:' in line:
                    out = 'pau'
                else:
                    count += 1
                    out = 'tscsd_gpr_{}{}_{}'.format(sett, file_number, count)
                # print out
                out_list.append(out)

            if len(out_list) != len(Utility.read_file_line_by_line(file_path)):
                print file_path

            out_file_name = '{}/{}{}.lab'.format(out_set_path, sett,
                                                 file_number)
            # print out_file_name

            Utility.write_to_file_line_by_line(out_file_name, out_list)
def load_fix_list(path):
    out = []
    for f in Utility.list_file(path):
        if f.startswith('.'): continue
        for line in Utility.read_file_line_by_line('{}/{}'.format(path, f)):
            # print Utility.trim(line)
            out.append(Utility.trim(line))

    return out
    pass
Exemplo n.º 13
0
def gen_tonal_part_duration(phone_level_label, pattern, start_set, end_set,
                            outpath):

    for sett in Utility.char_range(start_set, end_set):
        set_path = '{}/{}/'.format(phone_level_label, sett)
        for f in Utility.list_file(set_path):
            if f.startswith('.'): continue
            file_path = '{}/{}'.format(set_path, f)

            phone_frame_list = []
            syllable_count = 0

            for line in Utility.read_file_line_by_line(file_path):
                match = re.match(pattern, line)
                if match:
                    start_time = match.group('start_time')
                    end_time = match.group('end_time')

                    if match.group('phone_position_in_syllable') == 'x':
                        continue

                    phone_position_in_syllable = int(
                        match.group('phone_position_in_syllable'))
                    phone_number_in_syllable = int(
                        match.group('phone_number_in_syllable'))

                    frame = (float(end_time) - float(start_time)) / 50000

                    if phone_position_in_syllable == 1:
                        phone_frame_list = []
                        phone_frame_list.append(frame)
                    elif phone_position_in_syllable == phone_number_in_syllable:
                        phone_frame_list.append(frame)
                        if phone_number_in_syllable == 2:
                            phone_frame_list.append(0)

                        syllable_count += 1
                        print phone_frame_list
                        outfile = '{}/{}/{}/{}_dur.npy'.format(
                            outpath, sett,
                            f.split('.')[0], syllable_count)
                        print outfile
                        Utility.make_directory('{}/{}/{}/'.format(
                            outpath, sett,
                            f.split('.')[0]))
                        Utility.save_obj(phone_frame_list, outfile)
                    elif phone_position_in_syllable == 2:
                        phone_frame_list.append(frame)

                else:
                    print 'Not match', f

                pass
def gen_dur_and_name_list(label_path, name):
    dur_list = []
    names = []
    for idx, line in enumerate(Utility.read_file_line_by_line(label_path)):
        spl = Utility.trim(line).split(' ')

        frame = int(spl[1]) - int(spl[0])
        frame = frame / 50000

        dur_list.append(frame)

        names.append('{}_{}'.format(name, (idx + 1)))

    return (dur_list, names)
Exemplo n.º 15
0
def gen_all_method(base):

    print base

    mono = '{}/{}.lab'.format(mono_label, base)
    stress_tone = '{}/{}.npy'.format(stress_list, base)

    lf0_out = np.array([])

    for line, (st, t) in zip(Utility.read_file_line_by_line(mono),
                             np.load(stress_tone)):
        # print line, st, t

        l = Utility.trim(line)
        spl = l.split(' ')

        stress_type = 'unstress'
        if st == '1':
            stress_type = 'stress'

        if spl[2] in ['sil', 'pau']:
            target_file = original_file
        else:
            config = result_450['{}_{}'.format(stress_type, t)]

            if config['beta'] == '':
                target_file = original_file
            else:
                target_file = np.load(
                    '{}/tone_{}/Alpha_1.0_Beta_{}_Gamma_{}/lf0/{}.npy'.format(
                        config['basepath'], t, config['beta'], config['gamma'],
                        base))

        start = int(float(spl[0]) / 50000.0)
        end = int(float(spl[1]) / 50000.0)

        # print target_file.shape

        target_lf0 = target_file[start:end]

        lf0_out = np.append(lf0_out, target_lf0, axis=0)

    print lf0_out.shape

    np.save('{}/{}.npy'.format(outpath, base), lf0_out)

    # sys.exit()

    pass
Exemplo n.º 16
0
    def get_all_context_names(filepath):

        pattern = re.compile(r""".+-\sname:\s(?P<name>.+begin.*)""",
                             re.VERBOSE)

        for line in Utility.read_file_line_by_line(filepath):

            match = re.match(pattern, line)

            if match is None:
                continue

            print match.group('name')

        pass
def load_mono(mono_file):

    m = []

    for line in Utility.read_file_line_by_line(mono_file):
        spl = line.split(' ')

        ph = Utility.trim(spl[2])
        # print ph

        m.append(ph)

    # print m
    return m

    pass
Exemplo n.º 18
0
def gen_json_data():
    outpath = '/home/h1/decha/Dropbox/python_workspace/Inter_speech_2016/playground/generate_json/latent_data/'
    obj = Utility.load_obj(
        '/home/h1/decha/Dropbox/Inter_speech_2016/Syllable_object/mix_object/current_version/all_vowel_type/syllable_object_01234.pickle'
    )
    start_set, end_set = 'a', 'j'
    base_path = '/home/h1/decha/Dropbox/python_workspace/Inter_speech_2016/playground/list_file_for_preceeding_suceeding/list_gpr_file/'
    for sett in Utility.char_range(start_set, end_set):
        set_path = '{}/{}/'.format(base_path, sett)

        for f in Utility.list_file(set_path):
            if f.startswith('.'): continue

            file_path = '{}/{}'.format(set_path, f)
            out_list = []
            for line in Utility.read_file_line_by_line(file_path):
                name = Utility.trim(line)
                # "duration" "syllable_context"
                duration = ''
                syllable_context = ''

                d = dict()

                if name == 'sil':
                    syllable_context = 'sil-sil-sil-x'
                    duration = [
                        0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
                    ]
                elif name == 'pau':
                    syllable_context = 'pau-pau-pau-x'
                    duration = [
                        0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
                    ]
                else:
                    syl = obj.get_syllable_by_name_index(name)
                    syllable_context = '{}-{}-{}-{}'.format(
                        syl.consonant, syl.vowel, syl.final_consonant,
                        syl.tone)
                    duration = syl.single_space_latent.tolist()

                d['duration'] = duration
                d['syllable_context'] = syllable_context
                out_list.append(d)

            outfile_path = '{}/tscsd{}.json'.format(outpath, f)
            Utility.save_json(outfile_path, out_list)
def plot_syllable(lab_path, lf0_path, out_set_path, name, plot_set_out):

    lines = Utility.read_file_line_by_line(lab_path)
    lf0 = Utility.read_lf0_into_ascii(lf0_path)

    print lf0

    path = '{}/{}/'.format(out_set_path, name)

    Utility.make_directory(path)

    for idx, line in enumerate(lines):

        line = Utility.trim(line)
        spl = line.split(' ')
        # print spl

        start = float(spl[0]) / 50000
        end = float(spl[1]) / 50000

        syl = spl[2]

        # print start, end, syl

        if end > len(lf0):
            end = len(lf0) - 1

        cur_lf0 = lf0[start:end]
        # print len(cur_lf0)

        o = '{}_{}'.format((idx + 1), syl)

        out_name = '{}/{}.lf0'.format(path, o)
        print out_name
        # Utility.write_to_file_line_by_line(out_name, cur_lf0)

        Utility.make_directory('{}/{}/'.format(plot_set_out, name))

        plot_out_file_path = '{}/{}/{}.eps'.format(plot_set_out, name, o)

        plot(cur_lf0, plot_out_file_path)

    print len(lf0)

    pass
def run_gen_mono(utt_set):

    set_path = '{}/{}/'.format(utterance_path, utt_set)

    set_syllable_base_path = '{}/{}/'.format(syllable_base, utt_set)

    out_set_path = '{}/{}/'.format(output_path, utt_set)
    Utility.make_directory(out_set_path)

    for i in xrange(1, 51):
        utt_file = Utility.yaml_load('{}/tscsd{}{}.utt.yaml'.format(
            set_path, utt_set, Utility.fill_zero(i, 2)))
        # print utt_file

        out_file = '{}/tscsd{}{}.lab'.format(out_set_path, utt_set,
                                             Utility.fill_zero(i, 2))

        stress_list = []
        recursion(utt_file, stress_list)

        syllable_time_label = Utility.read_file_line_by_line(
            '{}/tscsd{}{}.lab'.format(set_syllable_base_path, utt_set,
                                      Utility.fill_zero(i, 2)))
        # print stress_list, len(stress_list)
        # print len(syllable_time_label)
        if len(syllable_time_label) != len(stress_list):
            print utt_set, i
            # print 'Error'
            # sys.exit()

        out = []
        for idx, line in enumerate(syllable_time_label):
            # print line, stress_list[idx]
            o = '{}::{}'.format(
                Utility.trim(line).replace('-', '_').replace('+', '_'),
                stress_list[idx])
            # print o
            out.append(o)

        Utility.write_to_file_line_by_line(out_file, out)

        # sys.exit()

    pass
    def load_mono_ori_list_in_sec(file_path):

        pattern = re.compile(
            r"""(?P<start>.+)\s+(?P<end>.+)\s+(?P<curphone>.+)""", re.VERBOSE)

        phone_list = []
        dur_list = []

        for line in Utility.read_file_line_by_line(file_path):
            match = re.match(pattern, line)
            if match:
                phone = match.group('curphone')
                start = match.group('start')
                end = match.group('end')

                phone_list.append(phone)
                dur_list.append((float(end) - float(start)) / 10000000)

        return (phone_list, dur_list)
def get_dir_list_HMM(dur_path):

    dur = []

    for d in Utility.read_file_line_by_line(dur_path):
        if 'state' in d:
            continue

        pattern = re.compile(r""".+\sduration=(?P<frame>.+)\s\(frame\).+""",
                             re.VERBOSE)
        match = re.match(pattern, d)
        if match:
            frame = match.group('frame')
            # print frame
            dur.append([float(frame) * 50000 / 10000000])

    return dur

    pass
Exemplo n.º 23
0
    def read_file_to_W(label_file):

        pattern = re.compile(
            r"""(?P<start>.+)\s(?P<end>.+)\s.+/A:.+/D:.+\-(?P<phone_num>.+)\+.+/E:.+""",
            re.VERBOSE)

        phone_list = []

        for line in Utility.read_file_line_by_line(label_file):
            # print line
            match = re.match(pattern, line)
            if match:
                phone_num = match.group('phone_num')
                # print phone_num
                if phone_num == 'x':
                    phone_list.append(1)
                else:
                    phone_list.append(int(phone_num))

        row = len(phone_list)
        column = sum(phone_list)

        # print row, column

        w = []

        cur = 0

        for i in phone_list:
            r = np.zeros(column)
            r[cur:cur + i] = 1
            w.append(r)
            cur = cur + i

        w = np.array(w)
        # print w

        # for idx, i in enumerate(phone_list):
        #     print i, w[idx]

        return w
        pass
    def load_ori_list_in_sec_and_num_phone_list(file_path):

        pattern = re.compile(
            r"""(?P<start>.+)\s(?P<end>.+)\s.+/A:.+/D:.+\-(?P<phone_num>.+)\+.+/E:.+""",
            re.VERBOSE)

        num_phone_list = []
        dur_list = []

        for line in Utility.read_file_line_by_line(file_path):
            match = re.match(pattern, line)
            if match:
                phone_num = match.group('phone_num')
                start = match.group('start')
                end = match.group('end')

                num_phone_list.append(phone_num)
                dur_list.append((float(end) - float(start)) / 10000000)

        return (num_phone_list, dur_list)
def run_gen(base_path, start_set, end_set, pattern, outpath):

    out = []

    for sett in Utility.char_range(start_set, end_set):
        set_path = '{}/{}/'.format(base_path, sett)
        for f in Utility.list_file(set_path):
            if f.startswith('.'): continue
            file_path = '{}/{}'.format(set_path, f)
            # print file_path

            count = 0
            # tscsd_gpr_g37_13

            prefix = 'tscsd_gpr'

            lines = Utility.read_file_line_by_line(file_path)

            for idx, line in enumerate(lines):
                # print line
                match = re.match(pattern, line)
                if match:
                    phone = match.group('curphone')
                    # print phone

                    if phone not in ['sil', 'pau']:
                        count += 1
                        # print f
                        name_index = '{}_{}{}_{}'.format(
                            prefix, sett, f[6:8], count)

                        if ('sil-sil+sil/A:' in lines[idx + 1]) | (
                                'pau-pau+pau/A:' in lines[idx + 1]):
                            print name_index
                            out.append(name_index)

    print len(out)
    outpath_file = '{}/gpr_followed_by_sil_list.npy'.format(outpath)
    Utility.save_obj(out, outpath_file)

    pass
    def find_stress_info(stress_path, name):

        spl = name.split('_')
        sett = spl[0][5]
        syl_index = spl[1]

        filename = spl[0]

        # print sett, syl_index

        for s in Utility.list_file(stress_path):
            if s[0] == sett:
                file_path = '{}/{}/{}.lab'.format(stress_path, s, filename)
                # print file_path

                lines = Utility.read_file_line_by_line(file_path)
                ss = lines[int(syl_index) - 1].split(' ')

                return Utility.trim(ss[3])

                break
Exemplo n.º 27
0
def gen_new_file(file_path, out_file):
    print file_path

    count = 0

    out = []

    base = Utility.get_basefilename(file_path)
    print base

    for line in Utility.read_file_line_by_line(file_path):

        count = count + 1

        name = '{}_{}'.format(base, count)
        # print name
        # print db['tscsdj46_2']

        # sys.exit()

        stress = 0
        syl = 'x_x_x_x'
        if name in db:
            if name in multi_level_list:
                stress = multi_level_list[name]['stress']
            else:
                stress = 0
            syl = '{}_{}_{}_{}'.format(db[name]['consonant'],
                                       db[name]['vowel'],
                                       db[name]['finalconsonant'],
                                       db[name]['tone'])

        if stress == 2: print name

        spl = line.split(' ')
        o = '{} {} {}_{}_{}'.format(spl[0], spl[1], syl, count, stress)
        # print o
        out.append(o)

    Utility.write_to_file_line_by_line(out_file, out)
def gen_stress(filepath, outpath):

    # sil-l+xx/A:x_x-1_1+2_2/B:x-3+0/C:x_x-1_1+1_2/D:x-3+3/E:x-1+2/F:x_x-3_1+6_2/G:x_18_12/H:x-47+47/I:x-0+0

    out = []

    pattern = re.compile(
        r""".+/A:.+\-(?P<cur_phone_position>.+)_.+\+.+/B:.+\-(?P<tone>.+)\+.+/C:.+/I:.+\-(?P<stress>.+)\+.+""",
        re.VERBOSE)

    for line in Utility.read_file_line_by_line(filepath):
        match = re.match(pattern, line)
        if match:
            cur_phone_position = match.group('cur_phone_position')
            stress = match.group('stress')
            tone = match.group('tone')

            # print line, cur_phone_position, stress

            if cur_phone_position in ['1', 'x']:
                out.append((stress, tone))

    np.save(outpath, np.array(out))
def lf0_distortion_syn_is_gpr_format(org_path, syn_path, stress_list,
                                     mono_label):

    lf0_true_list = []
    lf0_pred_list = []

    lf0_true_stress_list = []
    lf0_pred_stress_list = []

    for base in Utility.list_file(org_path):

        if base.startswith('.'):
            continue

        b = Utility.get_basefilename(base)
        stress = np.load('{}/{}.npy'.format(stress_list, b))
        mono_file = Utility.read_file_line_by_line('{}/{}.lab'.format(
            mono_label, b))

        stress_index = np.array([])

        for st, mono in zip(stress, mono_file):
            spl = mono.split(' ')
            start = int(spl[0]) / 50000
            end = int(spl[1]) / 50000

            if (st[0] == '1') & (st[1] == '{}'.format(tone)):
                stress_index = np.append(stress_index,
                                         np.arange(start, end),
                                         axis=0)

        # Load Original
        original_file = os.path.join(org_path, base)
        original_vector = numpy.loadtxt(
            Utility.read_lf0_into_ascii(original_file))

        # Load Synthesis
        synthesis_file = '{}/{}.npy'.format(syn_path,
                                            Utility.get_basefilename(base))
        synthesis_vector = numpy.load(synthesis_file)
        synthesis_vector = synthesis_vector.reshape(len(synthesis_vector))

        # print synthesis_vector
        synthesis_vector = np.nan_to_num(synthesis_vector)
        synthesis_vector[np.where(synthesis_vector <= 0.0)] = UNDEF_VALUE

        # print synthesis_vector
        # sys.exit()

        for idx, (lf0_original, lf0_synthesis) in enumerate(
                zip(original_vector, synthesis_vector)):
            if lf0_original == UNDEF_VALUE:
                continue
            if lf0_synthesis == UNDEF_VALUE:
                continue

            lf0_true_list.append(lf0_original)
            lf0_pred_list.append(lf0_synthesis)

            if idx in stress_index:
                lf0_true_stress_list.append(lf0_original)
                lf0_pred_stress_list.append(lf0_synthesis)

    # rmse = numpy.sqrt(sklearn.metrics.mean_squared_error(lf0_true_list, lf0_pred_list)) * 1000 / numpy.log(2)
    rmse = numpy.sqrt(
        sklearn.metrics.mean_squared_error(
            lf0_true_list, lf0_pred_list)) * 1200 / numpy.log(2)
    print('All LF0 RMSE: {:f} in {} frames'.format(rmse, len(lf0_true_list)))

    rmse = numpy.sqrt(
        sklearn.metrics.mean_squared_error(
            lf0_true_stress_list, lf0_pred_stress_list)) * 1200 / numpy.log(2)
    print('Only stress LF0 RMSE: {:f} in {} frames'.format(
        rmse, len(lf0_true_stress_list)))

    pass
    def dur_distortion(org_path, syn_path):

        dur_true_list = []
        dur_pred_list = []

        for base in Utility.list_file(org_path):

            if base.startswith('.'):
                continue

            basename = base.split('.')
            #             print basename
            # Load Original
            org_file = os.path.join(org_path,
                                    '{}{}'.format(basename[0], '.lab'))
            org_duration_vector = Utility.read_file_line_by_line(org_file)

            syn_file = os.path.join(syn_path,
                                    '{}{}'.format(basename[0], '.dur'))
            syn_duration_vector_temp = Utility.read_file_line_by_line(syn_file)

            syn_duration_vector = []

            for vec in syn_duration_vector_temp:
                if not "state" in vec:
                    syn_duration_vector.append(vec)

            pattern = re.compile(
                r"""(?P<start>.+)\s(?P<end>.+)\s.+\-(?P<curphone>.+)\+.+/A:.+""",
                re.VERBOSE)
            syn_pattern = re.compile(
                r""".+\-(?P<phone>.+)\+.+/A:.+\sduration=(?P<duration>.+)\s\(frame\).+""",
                re.VERBOSE)

            #X-sil+ph/A:x_x-x_x+1_1/B:x-x+0/C:x_x-x_x+1_1/D:x-x+2/E:x-x+1/F:x_x-x_x+9_4/G:x_22_14/H:x_x-56_1+2_1.state[2]: duration=5 (frame), mean=4.609594e+00

            if len(org_duration_vector) != len(syn_duration_vector):
                print "Not equal"

            for org, syn in zip(org_duration_vector, syn_duration_vector):

                match = re.match(pattern, org)

                if match.group('curphone') in ["sil", "pau"]:
                    continue

                phone = match.group('curphone')
                duration = (int(match.group('end')) -
                            int(match.group('start'))) / 50000

                syn_match = re.match(syn_pattern, syn)
                syn_duration = int(syn_match.group('duration'))

                #                 print phone, duration, syn_match.group('phone') , syn_duration

                dur_true_list.append(1000 * duration * 0.005)
                dur_pred_list.append(1000 * syn_duration * 0.005)

        if len(dur_true_list) != len(dur_pred_list):
            print "Not equal"

        rmse = numpy.sqrt(
            sklearn.metrics.mean_squared_error(dur_true_list, dur_pred_list))
        print('Duration RMSE: {:f} in {} phones'.format(
            rmse, len(dur_true_list)))