How can callbacks be implemented in Python?

20 views
Skip to first unread message

Giuliano Lancioni

unread,
Jun 11, 2023, 9:35:45 AM6/11/23
to Grammatical Framework
I have a question about simple callbacks implementation in Python. Unfortunately the examples in src/runtime/python/examples come without any grammar (I guess it was some version of wide-coverage, but those grammars don't compile anymore) and all my attempts to adapt code from there failed.

Let's say I have a simple modification of Face grammar. Abstract:

abstract Face = {

  flags startcat = Message ;

  cat
    Message ; Person ; Object ; Number ;
  fun
    Have : Person -> Number -> Object -> Message ;  -- p has n o's
    Like : Person -> Object -> Message ;            -- p likes o
    You : Person ;
    Friend, Invitation : Object ;
    UnknownPerson : String -> Person ;
    One, Two, Hundred : Number ;
  }
 
English concrete:

concrete FaceEng of Face = open SyntaxEng, ParadigmsEng in {
  lincat
    Message = Cl ;
    Person = NP ;
    Object = CN ;
    Number = Numeral ;
  lin
    Have p n o = mkCl p have_V2 (mkNP n o) ;
    Like p o = mkCl p like_V2 (mkNP this_Quant o) ;
    You = mkNP youSg_Pron ;
    Friend = mkCN friend_N ;
    Invitation = mkCN invitation_N ;
    One = n1_Numeral ;
    Two = n2_Numeral ;
    Hundred = n100_Numeral ;
  oper
    like_V2 = mkV2 "like" ;
    invitation_N = mkN "invitation" ;
    friend_N = mkN "friend" ;
  }


Basically, in this version any string might be a Person, provided the callback function returns it correctly.
According to Krasimir's thesis (pp. 45-48), the grammar should be compiled with -literal=Person switch, which I did. 
At this point, a callback function should return UnknownPerson "anything" (e.g., by converting the string into an NP by symb in the RGL), which should be accepted by the parser. How should a working Python callback function be written for such a basic example?

Thank you for any hint!

Giuliano

Prasanth Kolachina

unread,
Jun 11, 2023, 10:54:24 PM6/11/23
to Grammatical Framework
Hi Giuliano,

From one of the few people who used to program in Python with GF, here is a pointer: https://github.com/pkolachi/gftranslator-benchmarking/blob/master/scripts/translation_pipeline_v3.py
You can look for functions named "callback" in that file. These are implemented as function closures. 

This repo was meant to evaluate GF translation abilities when I was in grad school, so Krasimir helped me with this code. Hope it helps you.

Giuliano Lancioni

unread,
Jun 12, 2023, 2:44:26 AM6/12/23
to Grammatical Framework
Dear Prasanth,

thank you very much for your answer! Unfortunately, I guess I can't go much forward with that, because it is basically a revised version of the script that can be found within the GF source code itself, where you find both translation_pipeline.py and gfutils.py and a readme about using them without any working sample grammars. When I tried to use them, all attempts failed.

The ideal would be to have an example of calling the translation_pipeline_v3.py script with a small GF grammar like the one I listed in my post. Haven't you any small grammar sample that does actually work with your script?

Thank you so much again!

Giuliano

Reply all
Reply to author
Forward
0 new messages