module Data.DAWG.Static
(
DAWG (..)
, lookup
, numStates
, index
, byIndex
, hash
, unHash
, empty
, fromList
, fromListWith
, fromLang
, freeze
, Weight
, weigh
, assocs
, keys
, elems
) where
import Prelude hiding (lookup)
import Control.Applicative ((<$), (<$>), (<|>))
import Control.Arrow (first)
import Data.Binary (Binary)
import Data.Vector.Binary ()
import Data.Vector.Unboxed (Unbox)
import qualified Data.IntMap as M
import qualified Data.Vector as V
import Data.DAWG.Node hiding (Node)
import qualified Data.DAWG.Node as N
import qualified Data.DAWG.Node.Specialized as NS
import qualified Data.DAWG.VMap as VM
import qualified Data.DAWG.Internal as I
import qualified Data.DAWG as D
type Node a b = N.Node (Maybe a) (Edge b)
newtype DAWG a b c = DAWG { unDAWG :: V.Vector (Node b c) }
deriving (Show, Eq, Ord, Binary)
empty :: Unbox c => DAWG a b c
empty = DAWG $ V.fromList
[ Branch 1 VM.empty
, Leaf Nothing ]
numStates :: DAWG a b c -> Int
numStates = V.length . unDAWG
nodeBy :: ID -> DAWG a b c -> Node b c
nodeBy i d = unDAWG d V.! i
leafValue :: Node b c -> DAWG a b c -> Maybe b
leafValue n = value . nodeBy (eps n)
lookup :: (Unbox c, Enum a) => [a] -> DAWG a b c -> Maybe b
lookup xs' =
let xs = map fromEnum xs'
in lookup'I xs 0
lookup'I :: Unbox c => [Sym] -> ID -> DAWG a b c -> Maybe b
lookup'I [] i d = leafValue (nodeBy i d) d
lookup'I (x:xs) i d = case onSym x (nodeBy i d) of
Just e -> lookup'I xs (to e) d
Nothing -> Nothing
assocs :: (Enum a, Unbox c) => DAWG a b c -> [([a], b)]
assocs d = map (first (map toEnum)) (assocs'I 0 d)
assocs'I :: Unbox c => ID -> DAWG a b c -> [([Sym], b)]
assocs'I i d =
here ++ concatMap there (trans n)
where
n = nodeBy i d
here = case leafValue n d of
Just x -> [([], x)]
Nothing -> []
there (x, e) = map (first (x:)) (assocs'I (to e) d)
keys :: (Unbox c, Enum a) => DAWG a b c -> [[a]]
keys = map fst . assocs
elems :: Unbox c => DAWG a b c -> [b]
elems = map snd . assocs'I 0
fromList :: (Enum a, Ord b) => [([a], b)] -> DAWG a b ()
fromList = freeze . D.fromList
fromListWith :: (Enum a, Ord b) => (b -> b -> b) -> [([a], b)] -> DAWG a b ()
fromListWith f = freeze . D.fromListWith f
fromLang :: Enum a => [[a]] -> DAWG a () ()
fromLang = freeze . D.fromLang
type Weight = Int
weigh :: Unbox c => DAWG a b c -> DAWG a b Weight
weigh d = (DAWG . V.fromList)
[ branch n (apply ws (trans n))
| i <- [0 .. numStates d 1]
, let n = nodeBy i d
, let ws = accum (children n) ]
where
branch Branch{..} es = Branch eps es
branch Leaf{..} _ = Leaf value
nodeWeight = ((V.!) . V.fromList) (map detWeight [0 .. numStates d 1])
detWeight i = case nodeBy i d of
Leaf w -> maybe 0 (const 1) w
n -> sum . map nodeWeight $ allChildren n
accum = init . scanl (+) 0 . map nodeWeight
apply ws ts = VM.fromList
[ (x, annotate w e)
| (w, (x, e)) <- zip ws ts ]
allChildren n = eps n : children n
children = map to . edges
freeze :: D.DAWG a b -> DAWG a b ()
freeze d = DAWG . V.fromList $
map (N.toGeneric . NS.reIdent newID . oldBy)
(M.elems (inverse old2new))
where
old2new = M.fromList $ (D.root d, 0) : zip (nodeIDs d) [1..]
newID = (M.!) old2new
nodeIDs = filter (/= D.root d) . map fst . M.assocs . I.nodeMap . D.graph
oldBy i = I.nodeBy i (D.graph d)
inverse :: M.IntMap Int -> M.IntMap Int
inverse =
let swap (x, y) = (y, x)
in M.fromList . map swap . M.toList
index :: Enum a => [a] -> DAWG a b Weight -> Maybe Int
index xs = index'I (map fromEnum xs) 0
index'I :: [Sym] -> ID -> DAWG a b Weight -> Maybe Int
index'I [] i d = 0 <$ leafValue (nodeBy i d) d
index'I (x:xs) i d = do
let n = nodeBy i d
v = maybe 0 (const 1) (leafValue n d)
e <- onSym x n
w <- index'I xs (to e) d
return (v + w + label e)
hash :: Enum a => [a] -> DAWG a b Weight -> Maybe Int
hash = index
byIndex :: Enum a => Int -> DAWG a b Weight -> Maybe [a]
byIndex ix d = map toEnum <$> byIndex'I ix 0 d
byIndex'I :: Int -> ID -> DAWG a b Weight -> Maybe [Sym]
byIndex'I ix i d
| ix < 0 = Nothing
| otherwise = here <|> there
where
n = nodeBy i d
v = maybe 0 (const 1) (leafValue n d)
here
| ix == 0 = [] <$ leafValue (nodeBy i d) d
| otherwise = Nothing
there = do
(x, e) <- VM.findLastLE cmp (edgeMap n)
xs <- byIndex'I (ix v label e) (to e) d
return (x:xs)
cmp e = compare (label e) (ix v)
unHash :: Enum a => Int -> DAWG a b Weight -> Maybe [a]
unHash = byIndex