-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hs
59 lines (47 loc) · 2.12 KB
/
Main.hs
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
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Data.Text (Text)
import qualified Data.Text as T (unpack, pack, strip, drop, length)
import qualified Data.Map as M (Map, empty, insert, keys, lookup)
import Text.Regex
import Data.Char (isAlpha)
import Data.List (dropWhileEnd)
import Data.Maybe (Maybe, isJust, fromJust)
import Debug.Trace (trace)
import Web.Spock
import qualified Network.HTTP.Types.Status as HttpStatus
main :: IO ()
main = do
dictionaryContent <- readFile "dictionary.txt"
let dictionaryMap = parseDictionary dictionaryContent
runSpock 5000 $ spockT id $ do
get root $ do
setStatus HttpStatus.ok200
json dictionaryMap
get "keys" $ do
setStatus HttpStatus.ok200
json . M.keys $ dictionaryMap
get (var) $ \key -> do
setStatus HttpStatus.ok200
json . M.lookup key $ dictionaryMap
parseDictionary :: String -> M.Map Text Text
parseDictionary c = toMap . groupLines $ dictionaryLines
where dictionaryLines :: [String]
dictionaryLines = dropWhile (== "") .
filter (\l -> not (length l == 1 && (isAlpha . Prelude.head $ l))) .
takeWhile (/= "End of Project Gutenberg's The Devil's Dictionary, by Ambrose Bierce") .
dropWhile (/= "A") .
lines $ c
groupLines :: [String] -> [[String]]
groupLines [] = []
groupLines (x:xs) = (x : fst parts) : (groupLines . snd $ parts)
where parts = break (isJust . matchEntryStart) xs
toMap :: [[String]] -> M.Map Text Text
toMap = foldl (\m v -> M.insert (key v) (value v) m) M.empty
where key (x:xs) = T.pack . fromJust . matchEntryStart $ x
value x = T.strip . T.drop ((+1) . T.length . key $ x) . T.pack . unlines $ x
matchEntryStart :: String -> Maybe String
matchEntryStart l = case matchRegex entryStartRegex l of
Just (x:xs) -> Just x
Nothing -> Nothing
where entryStartRegex = mkRegex "(^[A-Z]+),"