mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-24 03:52:50 -06:00
the Literals API in Haskell no longer offers the sentence as an argument to the callbacks. It is just as easy to save the sentence in a closure and by doing that we save the repeated round about from C to Haskell strings
This commit is contained in:
@@ -267,7 +267,7 @@ parseWithHeuristics :: Concr -- ^ the language with which we parse
|
|||||||
-- A negative value tells the parser
|
-- A negative value tells the parser
|
||||||
-- to lookup up the default from
|
-- to lookup up the default from
|
||||||
-- the grammar flags
|
-- the grammar flags
|
||||||
-> [(Cat, Int -> String -> Int -> Maybe (Expr,Float,Int))]
|
-> [(Cat, Int -> Int -> Maybe (Expr,Float,Int))]
|
||||||
-- ^ a list of callbacks for literal categories.
|
-- ^ a list of callbacks for literal categories.
|
||||||
-- The arguments of the callback are:
|
-- The arguments of the callback are:
|
||||||
-- the index of the constituent for the literal category;
|
-- the index of the constituent for the literal category;
|
||||||
@@ -308,7 +308,7 @@ parseWithHeuristics lang cat sent heuristic callbacks =
|
|||||||
exprs <- fromPgfExprEnum enum parseFPl (lang,exprFPl)
|
exprs <- fromPgfExprEnum enum parseFPl (lang,exprFPl)
|
||||||
return (Right exprs)
|
return (Right exprs)
|
||||||
|
|
||||||
mkCallbacksMap :: Ptr PgfConcr -> [(String, Int -> String -> Int -> Maybe (Expr,Float,Int))] -> Ptr GuPool -> IO (Ptr PgfCallbacksMap)
|
mkCallbacksMap :: Ptr PgfConcr -> [(String, Int -> Int -> Maybe (Expr,Float,Int))] -> Ptr GuPool -> IO (Ptr PgfCallbacksMap)
|
||||||
mkCallbacksMap concr callbacks pool = do
|
mkCallbacksMap concr callbacks pool = do
|
||||||
callbacks_map <- pgf_new_callbacks_map concr pool
|
callbacks_map <- pgf_new_callbacks_map concr pool
|
||||||
forM_ callbacks $ \(cat,match) -> do
|
forM_ callbacks $ \(cat,match) -> do
|
||||||
@@ -318,10 +318,9 @@ mkCallbacksMap concr callbacks pool = do
|
|||||||
hspgf_callbacks_map_add_literal concr callbacks_map ccat match predict pool
|
hspgf_callbacks_map_add_literal concr callbacks_map ccat match predict pool
|
||||||
return callbacks_map
|
return callbacks_map
|
||||||
where
|
where
|
||||||
match_callback match _ clin_idx csentence poffset out_pool = do
|
match_callback match clin_idx poffset out_pool = do
|
||||||
sentence <- peekUtf8CString csentence
|
|
||||||
coffset <- peek poffset
|
coffset <- peek poffset
|
||||||
case match (fromIntegral clin_idx) sentence (fromIntegral coffset) of
|
case match (fromIntegral clin_idx) (fromIntegral coffset) of
|
||||||
Nothing -> return nullPtr
|
Nothing -> return nullPtr
|
||||||
Just (e,prob,offset') -> do poke poffset (fromIntegral offset')
|
Just (e,prob,offset') -> do poke poffset (fromIntegral offset')
|
||||||
|
|
||||||
@@ -342,7 +341,7 @@ mkCallbacksMap concr callbacks pool = do
|
|||||||
(#poke PgfExprProb, prob) ep prob
|
(#poke PgfExprProb, prob) ep prob
|
||||||
return ep
|
return ep
|
||||||
|
|
||||||
predict_callback _ _ _ _ = return nullPtr
|
predict_callback _ _ _ = return nullPtr
|
||||||
|
|
||||||
-- | The oracle is a triple of functions.
|
-- | The oracle is a triple of functions.
|
||||||
-- The first two take a category name and a linearization field name
|
-- The first two take a category name and a linearization field name
|
||||||
@@ -574,7 +573,7 @@ instance Exception PGFError
|
|||||||
-----------------------------------------------------------------------
|
-----------------------------------------------------------------------
|
||||||
|
|
||||||
type LiteralCallback =
|
type LiteralCallback =
|
||||||
PGF -> (ConcName,Concr) -> Int -> String -> Int -> Maybe (Expr,Float,Int)
|
PGF -> (ConcName,Concr) -> String -> Int -> Int -> Maybe (Expr,Float,Int)
|
||||||
|
|
||||||
-- | Callbacks for the App grammar
|
-- | Callbacks for the App grammar
|
||||||
literalCallbacks :: [(AbsName,[(Cat,LiteralCallback)])]
|
literalCallbacks :: [(AbsName,[(Cat,LiteralCallback)])]
|
||||||
@@ -583,7 +582,7 @@ literalCallbacks = [("App",[("PN",nerc),("Symb",chunk)])]
|
|||||||
-- | Named entity recognition for the App grammar
|
-- | Named entity recognition for the App grammar
|
||||||
-- (based on ../java/org/grammaticalframework/pgf/NercLiteralCallback.java)
|
-- (based on ../java/org/grammaticalframework/pgf/NercLiteralCallback.java)
|
||||||
nerc :: LiteralCallback
|
nerc :: LiteralCallback
|
||||||
nerc pgf (lang,concr) lin_idx sentence offset =
|
nerc pgf (lang,concr) sentence lin_idx offset =
|
||||||
case consume capitalized (drop offset sentence) of
|
case consume capitalized (drop offset sentence) of
|
||||||
(capwords@(_:_),rest) |
|
(capwords@(_:_),rest) |
|
||||||
not ("Eng" `isSuffixOf` lang && name `elem` ["I","I'm"]) ->
|
not ("Eng" `isSuffixOf` lang && name `elem` ["I","I'm"]) ->
|
||||||
@@ -618,7 +617,7 @@ nerc pgf (lang,concr) lin_idx sentence offset =
|
|||||||
-- | Callback to parse arbitrary words as chunks (from
|
-- | Callback to parse arbitrary words as chunks (from
|
||||||
-- ../java/org/grammaticalframework/pgf/UnknownLiteralCallback.java)
|
-- ../java/org/grammaticalframework/pgf/UnknownLiteralCallback.java)
|
||||||
chunk :: LiteralCallback
|
chunk :: LiteralCallback
|
||||||
chunk _ (_,concr) lin_idx sentence offset =
|
chunk _ (_,concr) sentence lin_idx offset =
|
||||||
case uncapitalized (drop offset sentence) of
|
case uncapitalized (drop offset sentence) of
|
||||||
Just (word0@(_:_),rest) | null (lookupMorpho concr word) ->
|
Just (word0@(_:_),rest) | null (lookupMorpho concr word) ->
|
||||||
Just (expr,0,offset+length word)
|
Just (expr,0,offset+length word)
|
||||||
|
|||||||
@@ -199,12 +199,12 @@ foreign import ccall "pgf/pgf.h pgf_align_words"
|
|||||||
foreign import ccall "pgf/pgf.h pgf_parse_with_heuristics"
|
foreign import ccall "pgf/pgf.h pgf_parse_with_heuristics"
|
||||||
pgf_parse_with_heuristics :: Ptr PgfConcr -> CString -> CString -> Double -> Ptr PgfCallbacksMap -> Ptr GuExn -> Ptr GuPool -> Ptr GuPool -> IO (Ptr GuEnum)
|
pgf_parse_with_heuristics :: Ptr PgfConcr -> CString -> CString -> Double -> Ptr PgfCallbacksMap -> Ptr GuExn -> Ptr GuPool -> Ptr GuPool -> IO (Ptr GuEnum)
|
||||||
|
|
||||||
type LiteralMatchCallback = Ptr () -> CInt -> CString -> Ptr CInt -> Ptr GuPool -> IO (Ptr PgfExprProb)
|
type LiteralMatchCallback = CInt -> Ptr CInt -> Ptr GuPool -> IO (Ptr PgfExprProb)
|
||||||
|
|
||||||
foreign import ccall "wrapper"
|
foreign import ccall "wrapper"
|
||||||
wrapLiteralMatchCallback :: LiteralMatchCallback -> IO (FunPtr LiteralMatchCallback)
|
wrapLiteralMatchCallback :: LiteralMatchCallback -> IO (FunPtr LiteralMatchCallback)
|
||||||
|
|
||||||
type LiteralPredictCallback = Ptr () -> CInt -> CString -> Ptr GuPool -> IO (Ptr PgfExprProb)
|
type LiteralPredictCallback = CInt -> CString -> Ptr GuPool -> IO (Ptr PgfExprProb)
|
||||||
|
|
||||||
foreign import ccall "wrapper"
|
foreign import ccall "wrapper"
|
||||||
wrapLiteralPredictCallback :: LiteralPredictCallback -> IO (FunPtr LiteralPredictCallback)
|
wrapLiteralPredictCallback :: LiteralPredictCallback -> IO (FunPtr LiteralPredictCallback)
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ translates pgf cfrom cto cat (mxt,mxv) s0 =
|
|||||||
|
|
||||||
cparse pgf concr cat input = parseWithHeuristics concr cat input (-1) callbacks where
|
cparse pgf concr cat input = parseWithHeuristics concr cat input (-1) callbacks where
|
||||||
callbacks = maybe [] cb $ lookup "App" literalCallbacks
|
callbacks = maybe [] cb $ lookup "App" literalCallbacks
|
||||||
cb fs = [(cat,f pgf ("TranslateEng",concr))|(cat,f)<-fs]
|
cb fs = [(cat,f pgf ("TranslateEng",concr) input)|(cat,f)<-fs]
|
||||||
|
|
||||||
lextext cnc = unwords . lexText' (\w -> case lookupMorpho cnc w of
|
lextext cnc = unwords . lexText' (\w -> case lookupMorpho cnc w of
|
||||||
_:_ -> w
|
_:_ -> w
|
||||||
|
|||||||
@@ -4,9 +4,7 @@
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
PgfLiteralCallback callback;
|
PgfLiteralCallback callback;
|
||||||
PgfExprProb* (*match)(PgfLiteralCallback* self,
|
PgfExprProb* (*match)(size_t lin_idx, size_t* poffset,
|
||||||
size_t lin_idx,
|
|
||||||
GuString sentence, size_t* poffset,
|
|
||||||
GuPool *out_pool);
|
GuPool *out_pool);
|
||||||
GuFinalizer fin;
|
GuFinalizer fin;
|
||||||
} HSPgfLiteralCallback;
|
} HSPgfLiteralCallback;
|
||||||
@@ -48,7 +46,7 @@ hspgf_match_callback(PgfLiteralCallback* self, PgfConcr* concr,
|
|||||||
size_t hs_offset =
|
size_t hs_offset =
|
||||||
hspgf_offset2hs(sentence, *poffset);
|
hspgf_offset2hs(sentence, *poffset);
|
||||||
PgfExprProb* ep =
|
PgfExprProb* ep =
|
||||||
callback->match(self, lin_idx, sentence, &hs_offset, out_pool);
|
callback->match(lin_idx, &hs_offset, out_pool);
|
||||||
*poffset = hspgf_hs2offset(sentence, hs_offset);
|
*poffset = hspgf_hs2offset(sentence, hs_offset);
|
||||||
|
|
||||||
return ep;
|
return ep;
|
||||||
|
|||||||
@@ -173,7 +173,7 @@ cpgfMain qsem command (t,(pgf,pc)) =
|
|||||||
--cparse = C.parse concr cat input
|
--cparse = C.parse concr cat input
|
||||||
cparse = C.parseWithHeuristics concr cat input (-1) callbacks
|
cparse = C.parseWithHeuristics concr cat input (-1) callbacks
|
||||||
callbacks = maybe [] cb $ lookup (C.abstractName pgf) C.literalCallbacks
|
callbacks = maybe [] cb $ lookup (C.abstractName pgf) C.literalCallbacks
|
||||||
cb fs = [(cat,f pgf (from,concr))|(cat,f)<-fs]
|
cb fs = [(cat,f pgf (from,concr) input)|(cat,f)<-fs]
|
||||||
{-
|
{-
|
||||||
-- Caching parse results:
|
-- Caching parse results:
|
||||||
parse' start mlimit ((from,concr),input) =
|
parse' start mlimit ((from,concr),input) =
|
||||||
|
|||||||
Reference in New Issue
Block a user