-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(lib): Add a loadTrigraph function
- Loading branch information
1 parent
3bc485b
commit 5879ded
Showing
7 changed files
with
113 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
{-# LANGUAGE OverloadedLists #-} | ||
|
||
module Data.Gibberish.Trigraph | ||
( Language (..), | ||
TrigraphConfig (..), | ||
genTrigraph, | ||
loadTrigraph, | ||
) where | ||
|
||
import Paths_gibberish (getDataFileName) | ||
|
||
import Control.Exception (throwIO) | ||
import Control.Monad (unless) | ||
import Data.Aeson qualified as Aeson | ||
import Data.Gibberish.Types | ||
import Data.Map.Strict (Map ()) | ||
import Data.Map.Strict qualified as Map | ||
import Data.Maybe (fromJust) | ||
import Data.Text (Text ()) | ||
import Data.Text qualified as Text | ||
import System.Directory (doesFileExist) | ||
import System.FilePath ((</>)) | ||
|
||
data Language | ||
= English | ||
| CustomTrigraph TrigraphConfig | ||
deriving stock (Eq, Show) | ||
|
||
newtype TrigraphConfig = TrigraphConfig | ||
{unTrigraphConfig :: FilePath} | ||
deriving stock (Eq, Show) | ||
|
||
-- | Generate trigraphs from a list of words | ||
genTrigraph :: [Text] -> Trigraph | ||
genTrigraph = Trigraph . foldr foldWord Map.empty | ||
where | ||
foldWord = Map.unionWith combine . mkTrigraph | ||
combine (Frequencies f1) (Frequencies f2) = Frequencies $ Map.unionWith (+) f1 f2 | ||
|
||
-- | Generate a trigraph from a single word | ||
mkTrigraph :: Text -> Map Digram Frequencies | ||
mkTrigraph word = foldr insert' Map.empty $ scanTrigrams word | ||
where | ||
insert' (Trigram a b c) = | ||
Map.insertWith combineFrequencies (Digram a b) (mkFrequencies c) | ||
combineFrequencies (Frequencies m1) (Frequencies m2) = | ||
Frequencies (Map.unionWith (+) m1 m2) | ||
mkFrequencies c = Frequencies $ Map.singleton (Unigram c) 1 | ||
|
||
scanTrigrams :: Text -> [Trigram] | ||
scanTrigrams word = case Text.take 3 word of | ||
[a, b, c] -> Trigram a b c : scanTrigrams (Text.tail word) | ||
_ -> [] | ||
|
||
loadTrigraph :: Language -> IO Trigraph | ||
loadTrigraph English = loadBuiltinTrigraph "wamerican.json" | ||
loadTrigraph (CustomTrigraph cfg) = loadTrigraphFromFile (unTrigraphConfig cfg) | ||
|
||
loadBuiltinTrigraph :: FilePath -> IO Trigraph | ||
loadBuiltinTrigraph file' = loadTrigraphFromFile =<< getBuiltinFilePath file' | ||
where | ||
getBuiltinFilePath basename = getDataFileName ("data" </> "trigraphs" </> basename) | ||
|
||
loadTrigraphFromFile :: FilePath -> IO Trigraph | ||
loadTrigraphFromFile file' = do | ||
exists <- doesFileExist file' | ||
unless exists $ | ||
throwIO (TrigraphNotFound file') | ||
|
||
fromJust <$> Aeson.decodeFileStrict file' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters