-
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.
Trigram: a sequence of 3 letters Trigraph: a mapping of digrams to potential trigram completions
- Loading branch information
1 parent
c2982e6
commit 7309f03
Showing
9 changed files
with
55 additions
and
37 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
File renamed without changes.
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
20 changes: 11 additions & 9 deletions
20
src/Data/Gibberish/GenTrigrams.hs → src/Data/Gibberish/GenTrigraph.hs
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 |
---|---|---|
@@ -1,28 +1,30 @@ | ||
{-# LANGUAGE OverloadedLists #-} | ||
|
||
module Data.Gibberish.GenTrigrams (mapTrigrams) where | ||
module Data.Gibberish.GenTrigraph (genTrigraph) where | ||
|
||
import Data.Gibberish.Types | ||
import Data.Map.Strict (Map ()) | ||
import Data.Map.Strict qualified as Map | ||
import Data.Text (Text ()) | ||
import Data.Text qualified as Text | ||
|
||
mapTrigrams :: [Text] -> Map Digram Frequencies | ||
mapTrigrams [] = Map.empty | ||
mapTrigrams (x : xs) = Map.unionWith combine (mkTrigrams x) (mapTrigrams xs) | ||
-- | 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 | ||
|
||
mkTrigrams :: Text -> Map Digram Frequencies | ||
mkTrigrams word = foldr insert' Map.empty $ scanTrigrams word | ||
-- | Generate a trigraph from a single word | ||
mkTrigraph :: Text -> Map Digram Frequencies | ||
mkTrigraph word = foldr insert' Map.empty $ scanTrigrams word | ||
where | ||
insert' (a, b, c) = | ||
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 -> [(Char, Char, Char)] | ||
scanTrigrams :: Text -> [Trigram] | ||
scanTrigrams word = case Text.take 3 word of | ||
[a, b, c] -> (a, b, c) : scanTrigrams (Text.tail word) | ||
[a, b, c] -> Trigram a b c : scanTrigrams (Text.tail word) | ||
_ -> [] |
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
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