-- | This module provides utilities for extracting a desired entity from a broader one.
module Study.Framework.Lojban.Extractors
( extractTrivialBridiGeneratorFromVocabulary
, extractSimpleBridiGeneratorFromTranslationGenerator
, extractLojbanSentencesFromTranslationGenerator
, extractLojbanSentencesFromTranslation
) where

import Core
import Language.Lojban.Core
import Util (chooseItemUniformly)
import Language.Lojban.Canonicalization (extractSimpleBridi)
import Study.Framework.Lojban.TranslationUtils (narrowTranslationGenerator)
import qualified Data.Text as T

-- | Produces trivial bridi (i.e. bridi with no sumti) from the vocabulary.
extractTrivialBridiGeneratorFromVocabulary :: Vocabulary -> SimpleBridiGenerator
extractTrivialBridiGeneratorFromVocabulary :: Vocabulary -> SimpleBridiGenerator
extractTrivialBridiGeneratorFromVocabulary Vocabulary
vocabulary StdGen
r0 = (SimpleBridi
simpleBridi, StdGen
r1) where
    (Text
brivla, StdGen
r1) = StdGen -> [Text] -> (Text, StdGen)
forall a. StdGen -> [a] -> (a, StdGen)
chooseItemUniformly StdGen
r0 ([Text] -> (Text, StdGen)) -> [Text] -> (Text, StdGen)
forall a b. (a -> b) -> a -> b
$ Vocabulary -> [Text]
vocabularyBrivlaList Vocabulary
vocabulary
    simpleBridi :: SimpleBridi
simpleBridi = Bool -> Text -> [Text] -> [Text] -> SimpleBridi
SimpleBridi Bool
False Text
brivla [] []

-- | Extracts a SimpleBridi among the sentences included in the translation generator.
--
-- Warning: the translation generator must consist solely of parsable sentences, otherwise
-- this function will yield an error upon encountering a non-parsable sentence.
extractSimpleBridiGeneratorFromTranslationGenerator :: TranslationGenerator -> SimpleBridiGenerator
extractSimpleBridiGeneratorFromTranslationGenerator :: TranslationGenerator -> SimpleBridiGenerator
extractSimpleBridiGeneratorFromTranslationGenerator TranslationGenerator
translationGenerator StdGen
r0 = (SimpleBridi
simpleBridi, StdGen
r1) where
    narrowedTranslationGenerator :: TranslationGenerator
narrowedTranslationGenerator = TranslationGenerator -> TranslationGenerator
narrowTranslationGenerator TranslationGenerator
translationGenerator
    (Text
sentence, StdGen
r1) = TranslationGenerator -> TextGenerator
extractLojbanSentencesFromTranslationGenerator TranslationGenerator
narrowedTranslationGenerator StdGen
r0
    simpleBridi :: SimpleBridi
simpleBridi = case (Text -> Either String SimpleBridi
extractSimpleBridi Text
sentence) of
        Left String
msg -> String -> SimpleBridi
forall a. HasCallStack => String -> a
error (String -> SimpleBridi) -> String -> SimpleBridi
forall a b. (a -> b) -> a -> b
$ String
"extractSimpleBridiFromTranslationGenerator: unable to parse sentence\nsentence: \"" String -> String -> String
forall a. [a] -> [a] -> [a]
++ (Text -> String
T.unpack Text
sentence) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\"\nmessage: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
msg String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\""
        Right SimpleBridi
simpleBridi' -> SimpleBridi
simpleBridi'

-- | Extracts a Lojban sentence among those included in the translation generator.
extractLojbanSentencesFromTranslationGenerator :: TranslationGenerator -> TextGenerator
extractLojbanSentencesFromTranslationGenerator :: TranslationGenerator -> TextGenerator
extractLojbanSentencesFromTranslationGenerator TranslationGenerator
translationGenerator StdGen
r0 = (Text
sentence, StdGen
r2) where
    (Translation
translation, StdGen
r1) = TranslationGenerator
translationGenerator StdGen
r0
    (Text
sentence, StdGen
r2) = StdGen -> [Text] -> (Text, StdGen)
forall a. StdGen -> [a] -> (a, StdGen)
chooseItemUniformly StdGen
r1 ([Text] -> (Text, StdGen)) -> [Text] -> (Text, StdGen)
forall a b. (a -> b) -> a -> b
$ Translation -> [Text]
extractLojbanSentencesFromTranslation Translation
translation

-- | Extracts the list of Lojban sentences from the translation.
extractLojbanSentencesFromTranslation :: Translation -> [T.Text]
extractLojbanSentencesFromTranslation :: Translation -> [Text]
extractLojbanSentencesFromTranslation ([Text]
lojban_sentences, [Text]
translated_sentences) = [Text]
lojban_sentences