forked from jamiely/poetry-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoemAnalyzer.hs
More file actions
62 lines (51 loc) · 1.75 KB
/
Copy pathPoemAnalyzer.hs
File metadata and controls
62 lines (51 loc) · 1.75 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
module PoemAnalyzer(getWords, justWords) where
import CMUPronouncingDictionary
import Test.HUnit
import Data.Map (Map)
import Data.Char (toLower, isAlpha)
import qualified Data.Map as Map
import Data.Maybe
-- | Given a list of tokens, returns an analysis of the poem pieces
getWords :: [String] -> Dictionary -> [[Maybe Word]]
getWords strs dict = map (map (getWord dict)) (map words strs) where
testGetWords :: Test
testGetWords = "Test getWords" ~: TestList [
"fox" ~: getWords ["fox"] testDictionary ~?= [[justFox]],
"fox with breaks" ~: getWords ["fox", "\n", "fox", "fox"] testDictionary ~?=
[[justFox], [justFox, justFox]]
] where
justFox = Just testWordFox
testDictionary :: Dictionary
testDictionary = Map.fromList [
("fox", testWordFox),
("pox", testWordPox)
]
-- | Removes all Nothing's from the list and collapses entries
justWords :: [[Maybe Word]] -> [[Word]]
justWords w = filter (not . null) (map onlyWords w) where
onlyWords :: [Maybe Word] -> [Word]
onlyWords maybeWords = map fromJust (filter isJust maybeWords)
testJustWords :: Test
testJustWords = "Test justWords" ~: TestList [
justWords [[Just testWordFox, Nothing], [Nothing, Nothing]] ~?=
[[testWordFox]]
]
-- | Returns statistics about a word given the word
getWord :: Dictionary -> String -> Maybe Word
getWord dict str = Map.lookup str' dict where
str' = filter isAlpha $ map toLower str
testGetWord :: Test
testGetWord = "Test getWord" ~: TestList [
"just" ~: getWord testDictionary "fox" ~?= Just testWordFox,
"nothing" ~: getWord testDictionary "nothing" ~?= Nothing
]
testWords :: [Word]
testWords = [testWordFox, testWordPox]
analyzeTest :: IO ()
analyzeTest = do
runTestTT (TestList [
testGetWord,
testJustWords,
testGetWords
])
return ()