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 .sourcePitch = convert2Pitch.object
\r
52 Formula... self*'.frequencyFactor'; Normalize Pitch
\r
53 Rename... SourcePitch
\r
55 select Pitch SourcePitch
\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
79 select DTW DTW'.inFile$'
\r
80 .countDistance = .countDistance + 1
\r
81 .sumDistance = .sumDistance + distance
\r
82 .sumSqrDistance = .sumSqrDistance + distance^2
\r
84 .inFile$ = replace$(.inFile$, "9", "3", 0)
\r
85 if .pinyin$ = .inFile$
\r
86 .correctDistance = distance
\r
90 # printline 'distance' - '.inFile$'
\r
93 if distance < .smallestDistance
\r
94 .smallestDistance = distance
\r
95 sgc_ToneProt.choiceReference$ = "'.inFile$'"
\r
99 if .keepIntermediates = 0
\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
113 printline Match: '.pinyin$' <== 'sgc_ToneProt.choiceReference$' small='.smallestDistance' Z='.zDistance'
\r
116 if .zDistance < .biasDistance
\r
117 sgc_ToneProt.choiceReference$ = .pinyin$
\r
118 .smallestDistance = .correctDistance
\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
131 select Pitch '.inFile$'
\r
132 if .keepIntermediates = 0
\r
138 select Table ToneList
\r
139 if .test_word$ <> "" and .test_word$ <> "REUSEPITCH"
\r
142 if .test_word$ <> "" and .test_word$ <> "REUSEPITCH"
\r
143 plus Pitch SourcePitch
\r
145 if .keepIntermediates = 0
\r