{-# LANGUAGE OverloadedStrings #-}

-- | This module provides utilities for loading vocabulary from files.
module Study.Framework.Lojban.VocabularyLoaders
( loadVocabularyFromYamlCode
) where

import Core
import qualified Data.Map as M
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
import qualified Data.Yaml as Y

-- | Loads vocabulary from yaml code.
loadVocabularyFromYamlCode :: T.Text -> Vocabulary
loadVocabularyFromYamlCode :: Text -> Vocabulary
loadVocabularyFromYamlCode Text
yamlCode = Map Text [Text] -> Vocabulary
loadVocabularyFromYamlData Map Text [Text]
yamlData where
    yamlData :: M.Map T.Text [T.Text]
    Right Map Text [Text]
yamlData = ByteString -> Either String (Map Text [Text])
forall a. FromJSON a => ByteString -> Either String a
Y.decodeEither (ByteString -> Either String (Map Text [Text]))
-> ByteString -> Either String (Map Text [Text])
forall a b. (a -> b) -> a -> b
$ Text -> ByteString
TE.encodeUtf8 Text
yamlCode

-- | Loads vocabulary from yaml data.
loadVocabularyFromYamlData :: M.Map T.Text [T.Text] -> Vocabulary
loadVocabularyFromYamlData :: Map Text [Text] -> Vocabulary
loadVocabularyFromYamlData Map Text [Text]
yamlData = [Text] -> [Text] -> [Text] -> Vocabulary
Vocabulary [Text]
brivlaList [Text]
cmavoList [Text]
cmevlaList where
    brivlaList :: [Text]
brivlaList = [Text] -> Text -> Map Text [Text] -> [Text]
forall k a. Ord k => a -> k -> Map k a -> a
M.findWithDefault [] Text
"brivla" Map Text [Text]
yamlData
    cmavoList :: [Text]
cmavoList = [Text] -> Text -> Map Text [Text] -> [Text]
forall k a. Ord k => a -> k -> Map k a -> a
M.findWithDefault [] Text
"cmavo" Map Text [Text]
yamlData
    cmevlaList :: [Text]
cmevlaList = [Text] -> Text -> Map Text [Text] -> [Text]
forall k a. Ord k => a -> k -> Map k a -> a
M.findWithDefault [] Text
"cmevla" Map Text [Text]
yamlData