{-# LANGUAGE OverloadedStrings #-}
module Language.Lojban.Core
( SimpleBridi (..)
, SimpleBridiGenerator
, SimpleBridiDisplayer
, TextGenerator
, SentenceCanonicalizer
, Dictionary (..)
, Gismu (..)
, Cmavo (..)
, Brivla (..)
, dictFindBrivla
, dictFindBrivla'
, dictLookupValsiDefinition
, retrieveBrivlaPlaces
) where
import System.Random (StdGen)
import Data.Maybe (fromMaybe)
import qualified Data.Text as T
import qualified Data.Map as M
import qualified Data.Aeson as A
data SimpleBridi = SimpleBridi
{ SimpleBridi -> Bool
simpleBridiXu :: Bool
, SimpleBridi -> Text
simpleBridiSelbri :: T.Text
, SimpleBridi -> [Text]
simpleBridiSumti :: [T.Text]
, :: [T.Text]
} deriving (Int -> SimpleBridi -> ShowS
[SimpleBridi] -> ShowS
SimpleBridi -> [Char]
(Int -> SimpleBridi -> ShowS)
-> (SimpleBridi -> [Char])
-> ([SimpleBridi] -> ShowS)
-> Show SimpleBridi
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SimpleBridi -> ShowS
showsPrec :: Int -> SimpleBridi -> ShowS
$cshow :: SimpleBridi -> [Char]
show :: SimpleBridi -> [Char]
$cshowList :: [SimpleBridi] -> ShowS
showList :: [SimpleBridi] -> ShowS
Show, SimpleBridi -> SimpleBridi -> Bool
(SimpleBridi -> SimpleBridi -> Bool)
-> (SimpleBridi -> SimpleBridi -> Bool) -> Eq SimpleBridi
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SimpleBridi -> SimpleBridi -> Bool
== :: SimpleBridi -> SimpleBridi -> Bool
$c/= :: SimpleBridi -> SimpleBridi -> Bool
/= :: SimpleBridi -> SimpleBridi -> Bool
Eq)
type SimpleBridiGenerator = StdGen -> (SimpleBridi, StdGen)
type SimpleBridiDisplayer = StdGen -> SimpleBridi -> (T.Text, StdGen)
type TextGenerator = StdGen -> (T.Text, StdGen)
type SentenceCanonicalizer = T.Text -> Either String T.Text
data Dictionary = Dictionary
{ Dictionary -> Text
dictIdentifier :: T.Text
, Dictionary -> Map Text Gismu
dictGismu :: M.Map T.Text Gismu
, Dictionary -> Map Text Cmavo
dictCmavo :: M.Map T.Text Cmavo
, Dictionary -> Map Text Brivla
dictBrivla :: M.Map T.Text Brivla
, Dictionary -> Map Text Gismu
dictRafsi :: M.Map T.Text Gismu
, Dictionary -> Map Text Text
dictValsiDefinition :: M.Map T.Text T.Text
, Dictionary -> Map Text [Text]
dictBrivlaPlaces :: M.Map T.Text [T.Text]
} deriving (Int -> Dictionary -> ShowS
[Dictionary] -> ShowS
Dictionary -> [Char]
(Int -> Dictionary -> ShowS)
-> (Dictionary -> [Char])
-> ([Dictionary] -> ShowS)
-> Show Dictionary
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Dictionary -> ShowS
showsPrec :: Int -> Dictionary -> ShowS
$cshow :: Dictionary -> [Char]
show :: Dictionary -> [Char]
$cshowList :: [Dictionary] -> ShowS
showList :: [Dictionary] -> ShowS
Show)
data Gismu = Gismu
{ Gismu -> Text
gismuText :: T.Text
, Gismu -> [Text]
gismuRafsi :: [T.Text]
, Gismu -> [Text]
gismuEnglishPlaces :: [T.Text]
, Gismu -> [Text]
gismuEnglishKeywords :: [T.Text]
, Gismu -> Text
gismuEnglishDefinition :: T.Text
, Gismu -> Text
gismuEnglishNotes :: T.Text
, Gismu -> [Text]
gismuConfer :: [T.Text]
, Gismu -> Text
gismuTeachingCode :: T.Text
, Gismu -> Int
gismuOldFrequencyCount :: Int
, Gismu -> Int
gismuIRCFrequencyCount :: Int
} deriving (Int -> Gismu -> ShowS
[Gismu] -> ShowS
Gismu -> [Char]
(Int -> Gismu -> ShowS)
-> (Gismu -> [Char]) -> ([Gismu] -> ShowS) -> Show Gismu
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Gismu -> ShowS
showsPrec :: Int -> Gismu -> ShowS
$cshow :: Gismu -> [Char]
show :: Gismu -> [Char]
$cshowList :: [Gismu] -> ShowS
showList :: [Gismu] -> ShowS
Show)
data Cmavo = Cmavo
{ Cmavo -> Text
cmavoText :: T.Text
, Cmavo -> Text
cmavoEnglishClassification :: T.Text
, Cmavo -> Text
cmavoEnglishKeyword :: T.Text
, Cmavo -> Text
cmavoEnglishDefinition :: T.Text
, Cmavo -> Text
cmavoEnglishNotes :: T.Text
, Cmavo -> [Text]
cmavoEnglishConfer :: [T.Text]
, Cmavo -> Int
cmavoIRCFrequencyCount :: Int
} deriving (Int -> Cmavo -> ShowS
[Cmavo] -> ShowS
Cmavo -> [Char]
(Int -> Cmavo -> ShowS)
-> (Cmavo -> [Char]) -> ([Cmavo] -> ShowS) -> Show Cmavo
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Cmavo -> ShowS
showsPrec :: Int -> Cmavo -> ShowS
$cshow :: Cmavo -> [Char]
show :: Cmavo -> [Char]
$cshowList :: [Cmavo] -> ShowS
showList :: [Cmavo] -> ShowS
Show)
data Brivla = Brivla
{ Brivla -> Text
brivlaText :: T.Text
, Brivla -> Text
brivlaDefinition :: T.Text
, Brivla -> Int
brivlaIRCFrequencyCount :: Int
} deriving (Int -> Brivla -> ShowS
[Brivla] -> ShowS
Brivla -> [Char]
(Int -> Brivla -> ShowS)
-> (Brivla -> [Char]) -> ([Brivla] -> ShowS) -> Show Brivla
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Brivla -> ShowS
showsPrec :: Int -> Brivla -> ShowS
$cshow :: Brivla -> [Char]
show :: Brivla -> [Char]
$cshowList :: [Brivla] -> ShowS
showList :: [Brivla] -> ShowS
Show)
instance Eq Gismu where
Gismu
x == :: Gismu -> Gismu -> Bool
== Gismu
y = (Gismu -> Text
gismuText Gismu
x) Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== (Gismu -> Text
gismuText Gismu
y)
instance Eq Cmavo where
Cmavo
x == :: Cmavo -> Cmavo -> Bool
== Cmavo
y = (Cmavo -> Text
cmavoText Cmavo
x) Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== (Cmavo -> Text
cmavoText Cmavo
y)
dictFindBrivla :: Dictionary -> T.Text -> Maybe Brivla
dictFindBrivla :: Dictionary -> Text -> Maybe Brivla
dictFindBrivla Dictionary
dictionary Text
key = (Dictionary -> Map Text Brivla
dictBrivla Dictionary
dictionary) Map Text Brivla -> Text -> Maybe Brivla
forall k a. Ord k => Map k a -> k -> Maybe a
M.!? Text
key
dictFindBrivla' :: Dictionary -> T.Text -> Brivla
dictFindBrivla' :: Dictionary -> Text -> Brivla
dictFindBrivla' Dictionary
dictionary Text
key = Brivla -> Maybe Brivla -> Brivla
forall a. a -> Maybe a -> a
fromMaybe Brivla
forall {a}. a
errorResult (Dictionary -> Text -> Maybe Brivla
dictFindBrivla Dictionary
dictionary Text
key) where
errorResult :: a
errorResult = [Char] -> a
forall a. HasCallStack => [Char] -> a
error ([Char] -> a) -> [Char] -> a
forall a b. (a -> b) -> a -> b
$ [Char]
"Brivla not found in dictionary: " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ (Text -> [Char]
T.unpack Text
key)
dictLookupValsiDefinition :: Dictionary -> T.Text -> T.Text
dictLookupValsiDefinition :: Dictionary -> Text -> Text
dictLookupValsiDefinition Dictionary
dictionary Text
key = Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
forall {a}. a
errorResult ((Dictionary -> Map Text Text
dictValsiDefinition Dictionary
dictionary) Map Text Text -> Text -> Maybe Text
forall k a. Ord k => Map k a -> k -> Maybe a
M.!? Text
key) where
errorResult :: a
errorResult = [Char] -> a
forall a. HasCallStack => [Char] -> a
error ([Char] -> a) -> [Char] -> a
forall a b. (a -> b) -> a -> b
$ [Char]
"Valsi not found in dictionary: " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ (Text -> [Char]
T.unpack Text
key)
retrieveBrivlaPlaces :: Dictionary -> T.Text -> [T.Text]
retrieveBrivlaPlaces :: Dictionary -> Text -> [Text]
retrieveBrivlaPlaces Dictionary
dictionary Text
brivla =
let lastSelbriInBrivla :: Text
lastSelbriInBrivla = [Text] -> Text
forall a. HasCallStack => [a] -> a
last ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
T.splitOn Text
" " Text
brivla
places :: [Text]
places = [Text] -> Text -> Map Text [Text] -> [Text]
forall k a. Ord k => a -> k -> Map k a -> a
M.findWithDefault [] Text
lastSelbriInBrivla (Map Text [Text] -> [Text]) -> Map Text [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ Dictionary -> Map Text [Text]
dictBrivlaPlaces Dictionary
dictionary
in if [Text] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Text]
places
then [Char] -> [Text]
forall a. HasCallStack => [Char] -> a
error ([Char] -> [Text]) -> [Char] -> [Text]
forall a b. (a -> b) -> a -> b
$ [Char]
"Missing brivla places for '" [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ (Text -> [Char]
T.unpack Text
brivla) [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
"'"
else [Text]
places
instance A.ToJSON Dictionary where
toJSON :: Dictionary -> Value
toJSON Dictionary
dictionary = [Pair] -> Value
A.object
[ (Key
"valsiDefinition", Map Text Text -> Value
forall a. ToJSON a => a -> Value
A.toJSON (Map Text Text -> Value) -> Map Text Text -> Value
forall a b. (a -> b) -> a -> b
$ Dictionary -> Map Text Text
dictValsiDefinition Dictionary
dictionary)
]