-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCMUPronouncingDictionary.hs
More file actions
210 lines (175 loc) · 6.26 KB
/
Copy pathCMUPronouncingDictionary.hs
File metadata and controls
210 lines (175 loc) · 6.26 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
module CMUPronouncingDictionary (
Phoneme, Word(Word), Line(Line), Stress(U,E,D), PoemLine, Dictionary,
wordsToLine,
loadWords,
isStressPhoneme,
testWordTransfusion, testWordPox,
testWordVision, testWordFox,
testLineFoxPox, testLineVisionTransfusion,
testLineTransfusion, testLinePox,
testLineVision, testLineFox, CMUPronouncingDictionary.test,
syllables, phonemes
) where
import Prelude hiding (Word)
import Data.Char(toLower,toUpper,isAlpha)
import Data.List(isPrefixOf, isSuffixOf)
import Data.Map(Map)
import qualified Data.Map as Map
import Test.HUnit
type Phoneme = String
data Word =
Word String Int [Stress] [Phoneme] -- actual word, syllables, stress, phonemes
deriving (Eq, Show)
data Line = Line {
lwords :: [String]
,lsyllables :: Int
,lstress :: [Stress]
,lphonemes :: [Phoneme]}
deriving (Eq, Show)
type PoemLine = [Word]
data Stress = U | E | D
deriving (Show)
instance Eq Stress where
U == U = True
U == D = False
U == E = True
D == D = True
D == E = True
D == U = False
E == U = True
E == D = True
E == E = True
type Dictionary = Map String Word
-- | Converts a list of Words into a Line containing those words.
wordsToLine :: PoemLine -> Line
wordsToLine ws = Line words totalSyllables totalStress allPhons where
words = map wordOnly ws where
wordOnly (Word w _ _ _ ) = w
totalSyllables = foldr (\w n -> (syllables w) + n) 0 ws
totalStress = foldr(\w sts -> ((stressPattern (phonemes w)) ++ sts)) [] ws
allPhons = concatMap phonemes ws
-- | Converts a Word into a Line containing only that word
wordToLine :: Word -> Line
wordToLine (Word w n s ph) = Line [w] n s ph
testWordToLine :: Test
testWordToLine = "wordToLine" ~:
TestList $ zipWith (~?=) (map wordToLine ws) ls where
ws = [testWordFox, testWordVision]
ls = [testLineFox, testLineVision]
-- | Generates a dictionary based on the passed words
loadWords :: String -> [String] -> Dictionary
loadWords strDict strWords = Map.fromList wordList where
relevantDictionaryLines = filter (/= "") $ map findWordLine strWords
dictLines = lines strDict
-- | Returns "" if the word is not found
findWordLine w = if null ws then "" else head ws where
ws = findWordLines w
findWordLines w = filter (isPrefixOf ((filter isAlpha (map toUpper w))++" "))
dictLines
wordList = map wordTuple relevantDictionaryLines
wordTuple line = (strWord word, word) where
word = dictLineToWord line
strWord (Word text _ _ _) = text
testLoadWords :: Test
testLoadWords = "Test loadWords" ~: TestList[
loadWords testCorpus ["fox", "pox", "notindictionary"] ~?=
Map.fromList [("fox", testWordFox), ("pox", testWordPox)]
]
dictLineToWord :: String -> Word
dictLineToWord line = Word strWord syllableCount stress phonemes where
(strWord, phonemes) = case (words line) of
[] -> ("", [])
(x:xs) -> (map toLower x, xs)
stress = stressPattern phonemes
stressPhonemes = filter isStressPhoneme phonemes
syllableCount = length stressPhonemes
testDictLineToWord :: Test
testDictLineToWord = "Test dictLineToWord" ~: TestList [
dictLineToWord "FOX F AA1 K S" ~?= testWordFox
]
testWordFox :: Word
testWordFox = Word "fox" 1 [U] ["F", "AA1", "K", "S"]
testWordPox :: Word
testWordPox = Word "pox" 1 [U] ["P", "AA1", "K", "S"]
testWordVision :: Word
testWordVision = Word "vision" 2 [U, D] ["V", "IH1", "ZH", "AH0", "N"]
testWordTransfusion :: Word
testWordTransfusion = Word "transfusion" 3 [D, U, D] ["T", "R",
"AE0", "N", "S", "F", "Y", "UW1", "ZH", "AH0", "N"]
testLineFox :: Line
testLineFox = Line ["fox"] 1 [U] ["F", "AA1", "K", "S"]
testLineVision :: Line
testLineVision = Line ["vision"] 2 [U, D] ["V", "IH1", "ZH", "AH0", "N"]
testLineTransfusion = wordToLine testWordTransfusion
testLinePox = wordToLine testWordPox
testLineFoxPox :: Line
testLineFoxPox = Line ["fox","pox"] 2 [U, U]
["F", "AA1", "K", "S", "P", "AA1", "K", "S"]
testLineVisionTransfusion :: Line
testLineVisionTransfusion = Line ["vision","transfusion"] 5 [U, D, D, U, D]
["V", "IH1", "ZH", "AH0", "N", "T",
"R", "AE0", "N", "S", "F", "Y", "UW1",
"ZH", "AH0", "N"]
testCorpus :: String
testCorpus = "FOX F AA1 K S"
++ "\nPOX P AA1 K S"
++ "\nSOX S AA1 K S"
++ "\nVISION V IH1 ZH AH0 N"
++ "\nTRANSFUSION T R AE0 N S F Y UW1 ZH AH0 N"
++ "\nSOMETHING S AH1 M TH IH0 NG"
-- | Use to determine the number of syllables in a word
syllables :: Word -> Int
syllables (Word _ syl _ _ ) = syl
-- | Use to retrieve the phonemes associated with a word
phonemes :: Word -> [Phoneme]
phonemes (Word _ _ _ p) = p
testSyllables :: Test
testSyllables = "Test syllables" ~: TestList [
"transfusion" ~: syllables testWordTransfusion ~?= 3,
"vision" ~: syllables testWordVision ~?= 2,
"pox" ~: syllables testWordPox ~?= 1
]
-- | Returns the pattern of stresses. For example, a word with a
-- stressed, then two unstressed symbols will return [U, D, D]
stressPattern :: [Phoneme] -> [Stress]
stressPattern [] = []
stressPattern (p:ps) = pat ++ stressPattern ps where
pat = case lst of
'2' -> [U]
'1' -> [E]
'0' -> [D]
_ -> []
lst = if null p then '\0' else last p
testStressPattern :: Test
testStressPattern = "Test stressPattern" ~: TestList [
stressPattern ["A1", "A0", "AB", "A1"] ~?= [U,D,U]
]
-- | Use to retrieve the stressed phonemes in a word
stresses :: Word -> [Phoneme]
stresses (Word _ _ _ ph) = filter isStressPhoneme ph
testStresses :: Test
testStresses = "Test stresses" ~: TestList [
"transfusion" ~: stresses testWordTransfusion ~?= ["AE0", "UW1", "AH0"]
]
-- | Phonemes which end in 0, 1, or 2 are syllables
isStressPhoneme :: Phoneme -> Bool
isStressPhoneme s = any (\n -> isSuffixOf n s) ["0", "1", "2"]
testIsStressPhoneme :: Test
testIsStressPhoneme = "Test isStressPhoneme" ~: TestList [
isStressPhoneme "A" ~?= False,
isStressPhoneme "A1" ~?= True,
isStressPhoneme "A3" ~?= False,
isStressPhoneme "VERYLONG1" ~?= True
]
test :: IO ()
test = do
runTestTT (TestList [
testWordToLine,
testSyllables,
testStresses,
testStressPattern,
testIsStressPhoneme,
testDictLineToWord,
testLoadWords
] )
return ()