3 # Construct all tone patterns and look for the one closest to the given one
\r
6 procedure FreeToneRecognition .pinyin$ .test_word$ .exclude$ .upperRegister .freqRange .durScale .startSyllable
\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
16 .referenceFrequency = 300
\r
17 .frequencyFactor = .referenceFrequency / .upperRegister
\r
19 .referenceExt$ = "pitch"
\r
21 # Bias Z-normalized value of the distance difference between smallest and correct
\r
23 if index_regex(config.strict$, "[^0-9]") <= 0
\r
24 .tmp = 'config.strict$'
\r
25 # Be lenient with longer words
\r
26 if .startSyllable > 0
\r
41 .keepIntermediates = 0
\r
44 # Generate reference tones
\r
45 call toneScript '.pinyin$' '.upperRegister' '.freqRange' '.durScale' Pitch_'.startSyllable'
\r
47 # Convert input to Pitch
\r
48 if .test_word$ <> "" and .test_word$ <> "REUSEPITCH"
\r
49 Read from file... '.test_word$'
\r
52 if .test_word$ <> "REUSEPITCH"
\r
54 call convert2Pitch 'sgc_ToneProt.minimumPitch' 'sgc_ToneProt.maximumPitch'
\r
55 if index(.pinyin$, "3") > 0
\r
56 select convert2Pitch.object
\r
57 .sourcePitch = Kill octave jumps
\r
58 select convert2Pitch.object
\r
61 .sourcePitch = convert2Pitch.object
\r
64 Formula... self*'.frequencyFactor'; Normalize Pitch
\r
65 Rename... SourcePitch
\r
68 select Pitch SourcePitch
\r
73 .correctDistance = -1
\r
75 .smallestDistance=999999
\r
76 sgc_ToneProt.choiceReference$ = "empty"
\r
77 select Table ToneList
\r
78 .listLength = Get number of rows
\r
79 for .i from 1 to .listLength
\r
80 select Table ToneList
\r
81 .inFile$ = Get value... '.i' Word
\r
82 # Broken third tones are still third tones
\r
83 # Creaky voice reduces 3rd tone distances
\r
85 if (.exclude$ = "" or rindex_regex(.inFile$, .exclude$) <= 0) and rindex_regex(.inFile$, "[\d]") > 0
\r
86 referenceName$ = .inFile$
\r
87 select Pitch '.inFile$'
\r
88 plus Pitch SourcePitch
\r
89 .dtw = noprogress To DTW... 24 10 yes yes no restriction
\r
90 Rename... DTW'.inFile$'
\r
91 distance = Get distance (weighted)
\r
93 select DTW DTW'.inFile$'
\r
94 .countDistance = .countDistance + 1
\r
95 .sumDistance = .sumDistance + distance
\r
96 .sumSqrDistance = .sumSqrDistance + distance^2
\r
98 .inFile$ = replace$(.inFile$, "9", "3", 0)
\r
99 if .pinyin$ = .inFile$
\r
100 .correctDistance = distance
\r
104 # printline 'distance' - '.inFile$'
\r
107 if distance < .smallestDistance
\r
108 # You cannot have a large range and misidentify it as 00
\r
109 if not (.freqRange >= 0.5 and index_regex(.inFile$, "0[^0-9]+0") > 0 and index_regex(.test_word$, "0[^0-9]+0") <= 0)
\r
110 .smallestDistance = distance
\r
111 sgc_ToneProt.choiceReference$ = "'.inFile$'"
\r
116 if .keepIntermediates = 0
\r
122 if .countDistance > 1
\r
123 .meanDistance = .sumDistance / .countDistance
\r
124 .varDistance = (.sumSqrDistance - .sumDistance^2/.countDistance)/(.countDistance - 1)
\r
125 .stdDistance = sqrt(.varDistance)
\r
126 .diffDistance = .correctDistance - .smallestDistance
\r
127 .zDistance = .diffDistance/.stdDistance
\r
130 printline Match: '.pinyin$' <== 'sgc_ToneProt.choiceReference$' small='.smallestDistance' Z='.zDistance'
\r
133 if .zDistance < .biasDistance
\r
134 sgc_ToneProt.choiceReference$ = .pinyin$
\r
135 .smallestDistance = .correctDistance
\r
141 for .i from 1 to .listLength
\r
142 select Table ToneList
\r
143 .inFile$ = Get value... '.i' Word
\r
145 if (.exclude$ = "" or rindex_regex(.inFile$, .exclude$) <= 0) and rindex_regex(.inFile$, "[\d]") > 0
\r
148 select Pitch '.inFile$'
\r
149 if .keepIntermediates = 0
\r
155 select Table ToneList
\r
156 if .test_word$ <> "" and .test_word$ <> "REUSEPITCH"
\r
159 if .test_word$ <> "" and .test_word$ <> "REUSEPITCH"
\r
160 plus Pitch SourcePitch
\r
162 if .keepIntermediates = 0
\r