mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-20 08:32:50 -06:00
make it possible to replace the probabilities while reading a new .pgf
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
|
||||
module PGF2 (-- * PGF
|
||||
PGF,readPGF,bootNGF,readNGF,newNGF,writePGF,showPGF,
|
||||
readPGFWithProbs, bootNGFWithProbs,
|
||||
|
||||
-- * Abstract syntax
|
||||
AbsName,abstractName,globalFlag,abstractFlag,
|
||||
@@ -109,11 +110,15 @@ import Text.PrettyPrint
|
||||
|
||||
-- | Reads a PGF file and keeps it in memory.
|
||||
readPGF :: FilePath -> IO PGF
|
||||
readPGF fpath =
|
||||
readPGF fpath = readPGFWithProbs fpath Nothing
|
||||
|
||||
readPGFWithProbs :: FilePath -> Maybe (Map.Map String Double) -> IO PGF
|
||||
readPGFWithProbs fpath mb_probs =
|
||||
withCString fpath $ \c_fpath ->
|
||||
alloca $ \p_revision ->
|
||||
withProbsCallback mb_probs $ \c_pcallback ->
|
||||
mask_ $ do
|
||||
c_db <- withPgfExn "readPGF" (pgf_read_pgf c_fpath p_revision)
|
||||
c_db <- withPgfExn "readPGF" (pgf_read_pgf c_fpath p_revision c_pcallback)
|
||||
c_revision <- peek p_revision
|
||||
fptr <- newForeignPtrEnv pgf_free_revision c_db c_revision
|
||||
langs <- getConcretes c_db fptr
|
||||
@@ -124,17 +129,37 @@ readPGF fpath =
|
||||
-- The NGF file is platform dependent and should not be copied
|
||||
-- between machines.
|
||||
bootNGF :: FilePath -> FilePath -> IO PGF
|
||||
bootNGF pgf_path ngf_path =
|
||||
bootNGF pgf_path ngf_path = bootNGFWithProbs pgf_path Nothing ngf_path
|
||||
|
||||
bootNGFWithProbs :: FilePath -> Maybe (Map.Map String Double) -> FilePath -> IO PGF
|
||||
bootNGFWithProbs pgf_path mb_probs ngf_path =
|
||||
withCString pgf_path $ \c_pgf_path ->
|
||||
withCString ngf_path $ \c_ngf_path ->
|
||||
alloca $ \p_revision ->
|
||||
withProbsCallback mb_probs $ \c_pcallback ->
|
||||
mask_ $ do
|
||||
c_db <- withPgfExn "bootNGF" (pgf_boot_ngf c_pgf_path c_ngf_path p_revision)
|
||||
c_db <- withPgfExn "bootNGF" (pgf_boot_ngf c_pgf_path c_ngf_path p_revision c_pcallback)
|
||||
c_revision <- peek p_revision
|
||||
fptr <- newForeignPtrEnv pgf_free_revision c_db c_revision
|
||||
langs <- getConcretes c_db fptr
|
||||
return (PGF c_db fptr langs)
|
||||
|
||||
withProbsCallback :: Maybe (Map.Map String Double) -> (Ptr PgfProbsCallback -> IO a) -> IO a
|
||||
withProbsCallback Nothing f = f nullPtr
|
||||
withProbsCallback (Just probs) f =
|
||||
allocaBytes (#size PgfProbsCallback) $ \callback ->
|
||||
bracket (wrapProbsCallback getProb) freeHaskellFunPtr $ \fptr -> do
|
||||
(#poke PgfProbsCallback, fn) callback fptr
|
||||
f callback
|
||||
where
|
||||
getProb _ c_name = do
|
||||
name <- peekText c_name
|
||||
case Map.lookup name probs of
|
||||
Nothing -> return nan
|
||||
Just p -> return p
|
||||
|
||||
nan = log (-1)
|
||||
|
||||
-- | Reads the grammar from an already booted NGF file.
|
||||
-- The function fails if the file does not exist.
|
||||
readNGF :: FilePath -> IO PGF
|
||||
|
||||
@@ -46,6 +46,7 @@ data PgfLinBuilderIface
|
||||
data PgfLinearizationOutputIface
|
||||
data PgfGraphvizOptions
|
||||
data PgfSequenceItor
|
||||
data PgfProbsCallback
|
||||
data PgfMorphoCallback
|
||||
data PgfCohortsCallback
|
||||
data PgfPhrasetableIds
|
||||
@@ -60,10 +61,14 @@ foreign import ccall unsafe "pgf_utf8_encode"
|
||||
pgf_utf8_encode :: Word32 -> Ptr CString -> IO ()
|
||||
|
||||
foreign import ccall "pgf_read_pgf"
|
||||
pgf_read_pgf :: CString -> Ptr (Ptr PGF) -> Ptr PgfExn -> IO (Ptr PgfDB)
|
||||
pgf_read_pgf :: CString -> Ptr (Ptr PGF) -> Ptr PgfProbsCallback -> Ptr PgfExn -> IO (Ptr PgfDB)
|
||||
|
||||
foreign import ccall "pgf_boot_ngf"
|
||||
pgf_boot_ngf :: CString -> CString -> Ptr (Ptr PGF) -> Ptr PgfExn -> IO (Ptr PgfDB)
|
||||
pgf_boot_ngf :: CString -> CString -> Ptr (Ptr PGF) -> Ptr PgfProbsCallback -> Ptr PgfExn -> IO (Ptr PgfDB)
|
||||
|
||||
type ProbsCallback = Ptr PgfProbsCallback -> Ptr PgfText -> IO Double
|
||||
|
||||
foreign import ccall "wrapper" wrapProbsCallback :: Wrapper ProbsCallback
|
||||
|
||||
foreign import ccall "pgf_read_ngf"
|
||||
pgf_read_ngf :: CString -> Ptr (Ptr PGF) -> Ptr PgfExn -> IO (Ptr PgfDB)
|
||||
|
||||
Reference in New Issue
Block a user