Project management of NTIS P1 Cybernetic Systems and Department of Cybernetics | WiKKY

Project

General

Profile

Task #3921 » asf_phn_corr.py

Hanzlíček Zdeněk, 27.05.2016 14:00

 
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3

    
4
import argparse
5
from asflight import AsfLight
6

    
7
# --------------------
8

    
9

    
10
def main():
11

    
12
    # all consonants
13
    phn_cons = [ 'b','c','C','d','D','w','W','f','g','h','x','G','j','k','l','L','m','M','H','n','J','N','p','r','P','R','Q','s','S','t','T','v','z','Z','!' ]
14

    
15
    # consonants without r, l, P, L
16
    phn_cons_m_left = [ 'b','c','C','d','D','w','W','f','g','h','x','G','j','k','m','M','H','n','J','N','p','R','Q','s','S','t','T','v','z','Z','!' ]
17

    
18
    # consonants without r, l, P, L, J
19
    phn_cons_m_right = [ 'b','c','C','d','D','w','W','f','g','h','x','G','j','k','m','M','H','n','N','p','R','Q','s','S','t','T','v','z','Z','!' ]
20

    
21
    parser = argparse.ArgumentParser( description = 'Convert ASF file to MLF.' )
22

    
23
    parser.add_argument( type=str, dest="asf_in", help="input ASF file", metavar="ASF_IN" )
24

    
25
    parser.add_argument( type=str, dest="asf_out", help="input ASF file", metavar="ASF_OUT" )
26

    
27
    parser.add_argument( "-c", "--code-page", type=str, dest="code_page",
28
                         help="encoding of all files, default value: %(default)s", default="utf-8" )
29

    
30
    # parse arguments
31
    args = parser.parse_args()
32

    
33
    asf = AsfLight( args.asf_in )
34

    
35
    for utt_name, asf_labels in asf.utts.iteritems():
36

    
37
        n_labels = len( asf_labels )
38

    
39
        for lab_idx in range( len( asf_labels ) ):
40

    
41
            curr_lab = asf_labels[ lab_idx ]
42
            prev_lab = asf_labels[ lab_idx-1 ] if ( lab_idx > 0 ) else None
43
            next_lab = asf_labels[ lab_idx+1 ] if ( lab_idx+1 < n_labels ) else None
44

    
45
            word_beg = ( curr_lab['word'] != '.' )
46
            word_end = ( next_lab is None ) or ( next_lab['word'] != '.' )
47

    
48
            if curr_lab['phone'] == 'n':
49
                if ( not word_end ) and ( next_lab['phone'] in [ 'g', 'k' ] ):
50
                    curr_lab['phone'] = 'N'
51

    
52
            elif curr_lab['phone'] == 'm':
53
                if ( not word_beg ) and ( prev_lab['phone'] in phn_cons_m_left ) and ( word_end or ( next_lab['phone'] in phn_cons_m_right ) ):
54
                    curr_lab['phone'] = 'H'
55

    
56
                elif ( not word_end ) and ( next_lab['phone'] in [ 'f', 'v' ] ):
57
                    curr_lab['phone'] = 'M'
58

    
59
            elif curr_lab['phone'] == 'x':
60
                if next_lab and ( next_lab['phone'] in [ 'b','d','D','w','W','g','h','G','z','Z' ] ):  # voiced paired consonants without 'v'
61
                    curr_lab['phone'] = 'G'
62

    
63
            elif curr_lab['phone'] == 'l':
64
                if ( not word_beg ) and ( prev_lab['phone'] in phn_cons ) and ( word_end or ( next_lab['phone'] in phn_cons ) ):
65
                    curr_lab['phone'] = 'L'
66

    
67
            elif curr_lab['phone'] == 'r':
68
                if ( not word_beg ) and ( prev_lab['phone'] in phn_cons ) and ( word_end or ( next_lab['phone'] in phn_cons ) ):
69
                    curr_lab['phone'] = 'P'
70

    
71
    asf.write( args.asf_out )
72

    
73

    
74
#
75
# ----------------------------------
76
#
77

    
78
# run the main program
79
if ( __name__ == "__main__" ):
80
    main()
(2-2/2)