Added shasums
[sgc2.git] / ToneProt / ToneRecognition.praat
bloba2e117aea4541a074340958953772a29479b007d
1 #! praat\r
2 #\r
3 # Construct all tone patterns and look for the one closest to the given one\r
4 #\r
5 \r
6 procedure FreeToneRecognition .pinyin$ .test_word$ .exclude$ .upperRegister .freqRange .durScale .startSyllable\r
7     # Clean up input\r
8     if .pinyin$ <> ""\r
9         .pinyin$ = replace_regex$(.pinyin$, "^\s*(.+)\s*$", "\1", 1)\r
10         .pinyin$ = replace_regex$(.pinyin$, "5", "0", 0)\r
11                 # Missing neutral tones\r
12                 call add_missing_neutral_tones '.pinyin$'\r
13                 .pinyin$ = add_missing_neutral_tones.pinyin$\r
14     endif\r
16     .referenceFrequency = 300\r
17     .frequencyFactor = .referenceFrequency / .upperRegister\r
19     .referenceExt$ = "pitch"\r
20     \r
21     # Bias Z-normalized value of the distance difference between smallest and correct\r
22     .biasDistance = 1.1\r
23     if index_regex(config.strict$, "[^0-9]") <= 0\r
24                 .tmp = 'config.strict$'\r
25                 if .tmp = 0\r
26                         .biasDistance = 1.7\r
27                 elsif .tmp = 1\r
28                         .biasDistance = 1.1\r
29                 elsif .tmp = 2\r
30                         .biasDistance = 0.6\r
31                 elsif .tmp = 3\r
32                         .biasDistance = 0.3\r
33                 endif\r
34     endif\r
35     \r
36     # Debugging\r
37     .keepIntermediates = 0\r
38     .debug = 0\r
40     # Generate reference tones\r
41     call toneScript '.pinyin$' '.upperRegister' '.freqRange' '.durScale' Pitch_'.startSyllable'\r
43     # Convert input to Pitch\r
44     if .test_word$ <> "" and .test_word$ <> "REUSEPITCH"\r
45         Read from file... '.test_word$'\r
46         Rename... Source\r
47     endif\r
48     if .test_word$ <> "REUSEPITCH"\r
49         select Sound Source\r
50                 call convert2Pitch 'sgc_ToneProt.minimumPitch' 'sgc_ToneProt.maximumPitch'\r
51                 .sourcePitch = convert2Pitch.object\r
52         Formula... self*'.frequencyFactor'; Normalize Pitch\r
53         Rename... SourcePitch\r
54     endif\r
55     select Pitch SourcePitch\r
57     .countDistance = 0\r
58     .sumDistance = 0\r
59     .sumSqrDistance = 0\r
60     .correctDistance = -1\r
62     .smallestDistance=999999\r
63     sgc_ToneProt.choiceReference$ = "empty"\r
64     select Table ToneList\r
65     .listLength = Get number of rows\r
66     for .i from 1 to .listLength\r
67         select Table ToneList\r
68         .inFile$ = Get value... '.i' Word\r
69         # Broken third tones are still third tones\r
71         if (.exclude$ = "" or rindex_regex(.inFile$, .exclude$) <= 0) and rindex_regex(.inFile$, "[\d]") > 0 \r
72             referenceName$ = .inFile$\r
73             select Pitch '.inFile$'\r
74             plus Pitch SourcePitch\r
75             .dtw = noprogress To DTW... 24 10 yes yes no restriction\r
76             Rename... DTW'.inFile$'\r
77             distance = Get distance (weighted)\r
78             # Clean up\r
79             select DTW DTW'.inFile$'\r
80             .countDistance = .countDistance + 1\r
81             .sumDistance = .sumDistance + distance\r
82             .sumSqrDistance = .sumSqrDistance + distance^2\r
83             \r
84                         .inFile$ = replace$(.inFile$, "9", "3", 0)\r
85             if .pinyin$ = .inFile$\r
86                 .correctDistance = distance\r
87             endif\r
89             if .debug > 0\r
90                 # printline 'distance' - '.inFile$'\r
91             endif\r
93             if distance < .smallestDistance\r
94                 .smallestDistance = distance\r
95                 sgc_ToneProt.choiceReference$ = "'.inFile$'"\r
96             endif\r
99             if .keepIntermediates = 0\r
100                 Remove\r
101             endif\r
102         endif\r
103     endfor\r
104     \r
105     if .countDistance > 1\r
106         .meanDistance = .sumDistance / .countDistance\r
107         .varDistance = (.sumSqrDistance - .sumDistance^2/.countDistance)/(.countDistance - 1)\r
108         .stdDistance = sqrt(.varDistance)\r
109         .diffDistance = .correctDistance - .smallestDistance\r
110         .zDistance = .diffDistance/.stdDistance\r
112         if .debug > 0\r
113             printline Match: '.pinyin$' <== 'sgc_ToneProt.choiceReference$' small='.smallestDistance' Z='.zDistance'\r
114         endif\r
116         if .zDistance < .biasDistance\r
117             sgc_ToneProt.choiceReference$ = .pinyin$\r
118             .smallestDistance = .correctDistance\r
119         endif\r
120     endif\r
121     \r
123     # Clean up\r
124     for .i from 1 to .listLength\r
125         select Table ToneList\r
126         .inFile$ = Get value... '.i' Word\r
128         if (.exclude$ = "" or rindex_regex(.inFile$, .exclude$) <= 0) and rindex_regex(.inFile$, "[\d]") > 0 \r
130             # Clean up\r
131             select Pitch '.inFile$'\r
132             if .keepIntermediates = 0\r
133                 Remove\r
134             endif\r
135         endif\r
136     endfor\r
138     select Table ToneList\r
139     if .test_word$ <> "" and .test_word$ <> "REUSEPITCH"\r
140         plus Sound Source\r
141     endif\r
142     if .test_word$ <> "" and .test_word$ <> "REUSEPITCH"\r
143         plus Pitch SourcePitch\r
144     endif\r
145     if .keepIntermediates = 0\r
146         Remove\r
147     endif\r
149 endproc\r