{-# LANGUAGE OverloadedStrings #-}

-- | This module provides utilities for loading translations from files.
module Study.Framework.Lojban.TranslationLoaders
( loadTranslationsFromYamlCode
, loadTranslationGeneratorFromYamlCode
, loadTranslationsByExpressionFromYamlCode
) where

import Core
import Study.Framework.Lojban.TranslationUtils (expandTranslation)
import Util (generatorFromList)
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 translations from yaml code.
loadTranslationsFromYamlCode :: T.Text -> [Translation]
loadTranslationsFromYamlCode :: Text -> [Translation]
loadTranslationsFromYamlCode Text
yamlCode = (Map Text [Text] -> Translation)
-> [Map Text [Text]] -> [Translation]
forall a b. (a -> b) -> [a] -> [b]
map Map Text [Text] -> Translation
loadTranslationFromYamlData [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 translations from yaml data.
loadTranslationFromYamlData :: M.Map T.Text [T.Text] -> Translation
loadTranslationFromYamlData :: Map Text [Text] -> Translation
loadTranslationFromYamlData Map Text [Text]
yamlData = Translation -> Translation
expandTranslation (Translation -> Translation) -> Translation -> Translation
forall a b. (a -> b) -> a -> b
$ (Map Text [Text]
yamlData Map Text [Text] -> Text -> [Text]
forall k a. Ord k => Map k a -> k -> a
M.! Text
"lojban_sentences", Map Text [Text]
yamlData Map Text [Text] -> Text -> [Text]
forall k a. Ord k => Map k a -> k -> a
M.! Text
"translated_sentences")

-- | Loads 'TranslationGenerator' from yaml code.
loadTranslationGeneratorFromYamlCode :: T.Text -> TranslationGenerator
loadTranslationGeneratorFromYamlCode :: Text -> TranslationGenerator
loadTranslationGeneratorFromYamlCode = [Translation] -> TranslationGenerator
forall a. [a] -> StdGen -> (a, StdGen)
generatorFromList ([Translation] -> TranslationGenerator)
-> (Text -> [Translation]) -> Text -> TranslationGenerator
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Translation]
loadTranslationsFromYamlCode

-- | Loads 'TranslationsByExpression' from yaml code.
loadTranslationsByExpressionFromYamlCode :: T.Text -> TranslationsByExpression
loadTranslationsByExpressionFromYamlCode :: Text -> TranslationsByExpression
loadTranslationsByExpressionFromYamlCode Text
yamlCode = Map Text [Translation] -> TranslationsByExpression
forall k a. Map k a -> [(k, a)]
M.assocs (Map Text [Translation] -> TranslationsByExpression)
-> Map Text [Translation] -> TranslationsByExpression
forall a b. (a -> b) -> a -> b
$ ([Map Text [Text]] -> [Translation])
-> Map Text [Map Text [Text]] -> Map Text [Translation]
forall a b k. (a -> b) -> Map k a -> Map k b
M.map [Map Text [Text]] -> [Translation]
handleExpression Map Text [Map Text [Text]]
yamlData where
    yamlData :: M.Map T.Text [M.Map T.Text [T.Text]]
    Right Map Text [Map Text [Text]]
yamlData = ByteString -> Either String (Map Text [Map Text [Text]])
forall a. FromJSON a => ByteString -> Either String a
Y.decodeEither (ByteString -> Either String (Map Text [Map Text [Text]]))
-> ByteString -> Either String (Map Text [Map Text [Text]])
forall a b. (a -> b) -> a -> b
$ Text -> ByteString
TE.encodeUtf8 Text
yamlCode
    handleExpression :: [M.Map T.Text [T.Text]] -> [Translation]
    handleExpression :: [Map Text [Text]] -> [Translation]
handleExpression = (Map Text [Text] -> Translation)
-> [Map Text [Text]] -> [Translation]
forall a b. (a -> b) -> [a] -> [b]
map Map Text [Text] -> Translation
loadTranslationFromYamlData

-- | Saves 'TranslationsByExpression' to yaml code.
saveTranslationsByExpressionToYamlText :: TranslationsByExpression -> T.Text
saveTranslationsByExpressionToYamlText :: TranslationsByExpression -> Text
saveTranslationsByExpressionToYamlText TranslationsByExpression
translationsByExpression = ByteString -> Text
TE.decodeUtf8 (ByteString -> Text) -> ByteString -> Text
forall a b. (a -> b) -> a -> b
$ Map Text [Map Text [Text]] -> ByteString
forall a. ToJSON a => a -> ByteString
Y.encode Map Text [Map Text [Text]]
yamlData where
    yamlData :: M.Map T.Text [M.Map T.Text [T.Text]]
    yamlData :: Map Text [Map Text [Text]]
yamlData = ([Translation] -> [Map Text [Text]])
-> Map Text [Translation] -> Map Text [Map Text [Text]]
forall a b k. (a -> b) -> Map k a -> Map k b
M.map [Translation] -> [Map Text [Text]]
encodeExpression (Map Text [Translation] -> Map Text [Map Text [Text]])
-> Map Text [Translation] -> Map Text [Map Text [Text]]
forall a b. (a -> b) -> a -> b
$ TranslationsByExpression -> Map Text [Translation]
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList TranslationsByExpression
translationsByExpression
    encodeExpression :: [Translation] -> [M.Map T.Text [T.Text]]
    encodeExpression :: [Translation] -> [Map Text [Text]]
encodeExpression = (Translation -> Map Text [Text])
-> [Translation] -> [Map Text [Text]]
forall a b. (a -> b) -> [a] -> [b]
map Translation -> Map Text [Text]
encodeTranslation
    encodeTranslation :: Translation -> M.Map T.Text [T.Text]
    encodeTranslation :: Translation -> Map Text [Text]
encodeTranslation ([Text]
lojban_sentences, [Text]
english_sentences) = [(Text, [Text])] -> Map Text [Text]
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList ([(Text, [Text])] -> Map Text [Text])
-> [(Text, [Text])] -> Map Text [Text]
forall a b. (a -> b) -> a -> b
$ [(Text
"lojban_sentences", [Text]
lojban_sentences), (Text
"translated_sentences", [Text]
english_sentences)]