-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilters.go
147 lines (131 loc) · 4.49 KB
/
filters.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package symbolset
// symbol set filters for accent/stress placement
import (
"fmt"
"regexp"
"strings"
)
func preFilter(ss SymbolSet, trans string, fromType Type) (string, error) {
if fromType == IPA {
return filterBeforeMappingFromIPA(ss, trans)
} else if fromType == CMU {
return filterBeforeMappingFromCMU(ss, trans)
}
return trans, nil
}
func postFilter(ss SymbolSet, trans string, toType Type) (string, error) {
if toType == IPA {
return filterAfterMappingToIPA(ss, trans)
} else if toType == CMU {
return filterAfterMappingToCMU(ss, trans)
}
return trans, nil
}
var ipaIndepStressRe = fmt.Sprintf("[%s%s]", ipaAccentI, ipaSecStress)
var ipaAccentI = "\u02C8"
var ipaAccentII = "\u0300"
var ipaSecStress = "\u02CC"
var ipaLength = "\u02D0"
//var ipaSyllDelim = "."
//var cmuString = "cmu"
func filterBeforeMappingFromIPA(ss SymbolSet, trans string) (string, error) {
// IPA: ˈba`ŋ.ka => ˈ`baŋ.ka"
// IPA: ˈɑ̀ː.pa => ˈ`ɑː.pa
trans = strings.Replace(trans, ipaAccentII+ipaLength, ipaLength+ipaAccentII, -1)
s := ipaAccentI + "(" + ss.ipaPhonemeRe.String() + "+)" + ipaAccentII
repl, err := regexp.Compile(s)
if err != nil {
return "", fmt.Errorf("couldn't compile regexp from string '%s' : %v", s, err)
}
res := repl.ReplaceAllString(trans, ipaAccentI+ipaAccentII+"$1")
return res, nil
}
/*
func canMapToIPA(ss SymbolSet, trans string) (bool, error) {
symbols, err := ss.SplitIPATranscription(trans)
if err != nil {
return false, err
}
nSyllabic := 0
foundDelim := false
syllabicReS := "^(" + ss.ipaSyllabicRe.String() + ")$"
syllabicRe, err := regexp.Compile(syllabicReS)
if err != nil {
return false, fmt.Errorf("cannot create ipa syllabic regexp from string /%s/", syllabicReS)
}
for _, sym := range symbols {
if syllabicRe.MatchString(sym) {
nSyllabic = nSyllabic + 1
} else if sym == ipaSyllDelim {
foundDelim = true
}
}
if foundDelim == false && nSyllabic > 1 {
return false, nil
}
return true, nil
}
*/
func filterAfterMappingToIPA(ss SymbolSet, trans string) (string, error) {
// only filter stress if the symbol set has a syllable delimiter
if len(filterSymbolsByCat(ss.Symbols, []SymbolCat{SyllableDelimiter})) == 0 {
return trans, nil
}
// create an error if the input transcription contains more than one syllabic, but no syllable delimiter
// canMap, err := canMapToIPA(ss, trans)
// if err != nil {
// return "", err
// }
// if !canMap {
// return trans, fmt.Errorf("cannot map transcription to IPA /%s/", trans)
// }
// IPA: /ə.ba⁀ʊˈt/ => /ə.ˈba⁀ʊt/
s := "(" + ss.ipaNonSyllabicRe.String() + "*)(" + ss.ipaSyllabicRe.String() + ")(" + ipaIndepStressRe + ")"
repl, err := regexp.Compile(s)
if err != nil {
return "", fmt.Errorf("couldn't compile regexp from string '%s' : %v", s, err)
}
trans = repl.ReplaceAllString(trans, "$3$1$2")
// IPA: /ə.bˈa⁀ʊt/ => /ə.ˈba⁀ʊt/
s = "(" + ss.ipaNonSyllabicRe.String() + "*)(" + ipaIndepStressRe + ")(" + ss.ipaSyllabicRe.String() + ")"
repl, err = regexp.Compile(s)
if err != nil {
return "", fmt.Errorf("couldn't compile regexp from string '%s' : %v", s, err)
}
trans = repl.ReplaceAllString(trans, "$2$1$3")
// IPA: əs.ˈ̀̀e ...
// IPA: /'`pa.pa/ => /'pa`.pa/
accentIIConditionForAfterMapping := ipaAccentI + ipaAccentII
if strings.Contains(trans, accentIIConditionForAfterMapping) {
s := ipaAccentI + ipaAccentII + "(" + ss.ipaNonSyllabicRe.String() + "*)(" + ss.ipaSyllabicRe.String() + ")"
repl, err := regexp.Compile(s)
if err != nil {
return "", fmt.Errorf("couldn't compile regexp from string '%s' : %v", s, err)
}
res := repl.ReplaceAllString(trans, ipaAccentI+"$1$2"+ipaAccentII)
trans = res
}
// IPA: /'paː`.pa/ => /'pa`ː.pa/
trans = strings.Replace(trans, ipaLength+ipaAccentII, ipaAccentII+ipaLength, -1)
return trans, nil
}
func filterBeforeMappingFromCMU(ss SymbolSet, trans string) (string, error) {
re, err := regexp.Compile("([^ ]+)([012])")
if err != nil {
return "", err
}
trans = re.ReplaceAllString(trans, "$2 $1")
return trans, nil
}
func filterAfterMappingToCMU(ss SymbolSet, trans string) (string, error) {
s := "([012]) ((?:" + ss.NonSyllabicRe.String() + " )*)(" + ss.SyllabicRe.String() + ")"
repl, err := regexp.Compile(s)
if err != nil {
return "", fmt.Errorf("couldn't compile regexp from string '%s' : %v", s, err)
}
trans = repl.ReplaceAllString(trans, "$2$3$1")
trans = strings.Replace(trans, " 1", "1", -1)
trans = strings.Replace(trans, " 2", "2", -1)
trans = strings.Replace(trans, " 0", "0", -1)
return trans, nil
}