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
37 .keepIntermediates = 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
48 if .test_word$ <> "REUSEPITCH"
\r
50 call convert2Pitch 'sgc_ToneProt.minimumPitch' 'sgc_ToneProt.maximumPitch'
\r
51 if index(.pinyin$, "3") > 0
\r
52 select convert2Pitch.object
\r
53 .sourcePitch = Kill octave jumps
\r
54 select convert2Pitch.object
\r
57 .sourcePitch = convert2Pitch.object
\r
60 Formula... self*'.frequencyFactor'; Normalize Pitch
\r
61 Rename... SourcePitch
\r
63 select Pitch SourcePitch
\r
68 .correctDistance = -1
\r
70 .smallestDistance=999999
\r
71 sgc_ToneProt.choiceReference$ = "empty"
\r
72 select Table ToneList
\r
73 .listLength = Get number of rows
\r
74 for .i from 1 to .listLength
\r
75 select Table ToneList
\r
76 .inFile$ = Get value... '.i' Word
\r
77 # Broken third tones are still third tones
\r
79 if (.exclude$ = "" or rindex_regex(.inFile$, .exclude$) <= 0) and rindex_regex(.inFile$, "[\d]") > 0
\r
80 referenceName$ = .inFile$
\r
81 select Pitch '.inFile$'
\r
82 plus Pitch SourcePitch
\r
83 .dtw = noprogress To DTW... 24 10 yes yes no restriction
\r
84 Rename... DTW'.inFile$'
\r
85 distance = Get distance (weighted)
\r
87 select DTW DTW'.inFile$'
\r
88 .countDistance = .countDistance + 1
\r
89 .sumDistance = .sumDistance + distance
\r
90 .sumSqrDistance = .sumSqrDistance + distance^2
\r
92 .inFile$ = replace$(.inFile$, "9", "3", 0)
\r
93 if .pinyin$ = .inFile$
\r
94 .correctDistance = distance
\r
98 # printline 'distance' - '.inFile$'
\r
101 if distance < .smallestDistance
\r
102 .smallestDistance = distance
\r
103 sgc_ToneProt.choiceReference$ = "'.inFile$'"
\r
107 if .keepIntermediates = 0
\r
113 if .countDistance > 1
\r
114 .meanDistance = .sumDistance / .countDistance
\r
115 .varDistance = (.sumSqrDistance - .sumDistance^2/.countDistance)/(.countDistance - 1)
\r
116 .stdDistance = sqrt(.varDistance)
\r
117 .diffDistance = .correctDistance - .smallestDistance
\r
118 .zDistance = .diffDistance/.stdDistance
\r
121 printline Match: '.pinyin$' <== 'sgc_ToneProt.choiceReference$' small='.smallestDistance' Z='.zDistance'
\r
124 if .zDistance < .biasDistance
\r
125 sgc_ToneProt.choiceReference$ = .pinyin$
\r
126 .smallestDistance = .correctDistance
\r
132 for .i from 1 to .listLength
\r
133 select Table ToneList
\r
134 .inFile$ = Get value... '.i' Word
\r
136 if (.exclude$ = "" or rindex_regex(.inFile$, .exclude$) <= 0) and rindex_regex(.inFile$, "[\d]") > 0
\r
139 select Pitch '.inFile$'
\r
140 if .keepIntermediates = 0
\r
146 select Table ToneList
\r
147 if .test_word$ <> "" and .test_word$ <> "REUSEPITCH"
\r
150 if .test_word$ <> "" and .test_word$ <> "REUSEPITCH"
\r
151 plus Pitch SourcePitch
\r
153 if .keepIntermediates = 0
\r