summaryrefslogtreecommitdiff
path: root/src/compiler/GF/Index.hs
blob: eeb8697b344b12d4fa81d40d8fff32395fb94f03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
{--
This module provide a function for indexing a pgf. 

It reads the pgf and add a global flag, called "index", containing a string 
with concrete names and size in bytes separated by a column.
ex : "DisambPhrasebookEng:18778 PhrasebookBul:49971 PhrasebookCat:32738..."
--}
module GF.Index (addIndex) where

import PGF
import PGF.Data
--import PGF.Binary
import Data.Binary
import Data.ByteString.Lazy (length) -- readFile
import qualified Data.Map as Map
import Data.Map (toAscList)
import Data.List (intercalate)
--import qualified Data.ByteString.Lazy as BS

addIndex  :: PGF -> PGF
addIndex pgf = pgf {gflags = flags}  
  where flags = Map.insert (mkCId "index") (LStr $ showIndex index) (gflags pgf)
        index = getIndex pgf


showIndex :: [(String,Int)] -> String
showIndex = intercalate " " . map f
  where f (name,size) = name ++ ":" ++ show size

getsize :: Binary a => a -> Int
getsize x = let bs = encode x in fromIntegral $ Data.ByteString.Lazy.length bs

getIndex :: PGF -> [(String,Int)]
getIndex pgf = cncindex
  where cncindex = map f $ Data.Map.toAscList $ concretes pgf
        f (cncname,cnc) = (show cncname, getsize cnc)