Permalink
Browse files

Use binary to store spamdb

  • Loading branch information...
1 parent e515beb commit 62757258aa7211f21d51751dc11c67ed98d34a70 @chrisdone committed Dec 4, 2016
Showing with 12 additions and 18 deletions.
  1. +1 −0 lpaste.cabal
  2. +11 −18 src/Spam.hs
View
@@ -59,6 +59,7 @@ Executable lpaste
,bytestring-trie
,MonadCatchIO-transformers
,conduit
+ ,binary
Other-modules:
Control.Monad.IO
Data.Time.Relative
View
@@ -1,3 +1,4 @@
+{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeFamilies #-}
@@ -19,6 +20,7 @@ module Spam
,corpus)
where
+import Data.Binary
import Data.ByteString (ByteString)
import qualified Data.ByteString as S
import Data.Conduit
@@ -27,41 +29,32 @@ import Data.Maybe
import Data.Trie (Trie)
import qualified Data.Trie as Trie
import qualified Data.Trie.Convenience as Trie
-import Data.Word
+import GHC.Generics
import System.Directory
-- | Spam database.
data SpamDB = DB
{ dbBad :: !Corpus
, dbGood :: !Corpus
- }
-
-instance Show SpamDB where
- show (DB (Corpus x a) (Corpus y b)) =
- show (x, Trie.toList a, y, Trie.toList b)
-
-instance Read SpamDB where
- readsPrec _ s = [(v, "")]
- where
- v =
- let (x, a, y, b) = read s
- in DB (Corpus x (Trie.fromList a)) (Corpus y (Trie.fromList b))
+ } deriving (Generic)
+instance Binary SpamDB
-- | A corpus of pastes.
data Corpus = Corpus
{ corpusMessages :: !Double
, corpusHistogram :: !(Trie Double)
- } deriving (Show)
+ } deriving (Show, Generic)
+instance Binary Corpus
-- | Read a spam database from file.
readDB :: FilePath -> IO SpamDB
readDB fp = do
exists <- doesFileExist fp
if exists
then do
- content <- readFile fp
- case reads content of
- [(db, "")] -> return db
+ result <- decodeFileOrFail fp
+ case result of
+ Right db -> return db
_ -> do
putStrLn "Failed to read spam database. Defaulting to empty one ..."
return emptyDB
@@ -71,7 +64,7 @@ readDB fp = do
-- | Write the spam database to file.
writeDB :: FilePath -> SpamDB -> IO ()
-writeDB fp = writeFile fp . show
+writeDB fp = encodeFile fp
-- | Classify a paste from 0 to 1. >=0.5 being spam.
classify :: SpamDB -> [ByteString] -> Double

0 comments on commit 6275725

Please sign in to comment.