-
Notifications
You must be signed in to change notification settings - Fork 2
Translation
Mehmet Dolgun edited this page Oct 20, 2020
·
6 revisions
Consider the previous simple grammar, modified for translation (see GLRParser/grm/simple_trans.grm):
S -> i VP : VP -m
S -> NP VP : NP VP
VP -> VP in NP : NP -de VP
VP -> VP with NP : NP -la VP
NP -> the man : adam
NP -> the telescope : teleskop
NP -> the house : ev
NP -> NP-1 in NP-2 : NP-2 -deki NP-1
NP -> NP-1 with NP-2 : NP-2 -lu NP-1
VP -> saw NP : NP -ı gördü
Given the above grammar and input string:
i saw the man in the house with the telescope
using following code to parse and translate (see example/demo_trans.py:
parser = Parser() # initialize parser object
parser.parse_grammar("..\GLRParser\grm\simple_trans.grm") # load grammar from a file
sent = "i saw the man in the house with the telescope" # sentence to parse
parser.compile() # constructs parsing tables
parser.parse(sent) # parse the sentence
tree = parser.make_tree() # generates parse forest
ttree = parser.trans_tree(tree) # translate the parse forest
print(ttree.pformat(False)) # pretty-print the translated parse forest
for trans in ttree.enum(): # enumerate and print all alternative translations in the parse forest
print(trans.replace(" -","")) # concat suffixesWe will get 5 alternative translations:
teleskopla evde adamı gördüm
teleskopla evdeki adamı gördüm
teleskoplu evde adamı gördüm
teleskoplu evdeki adamı gördüm
teleskoplu evdeki adamı gördüm
corresponding parse forrest is:
S(
VP(
NP(teleskop)
-la
VP(
NP(ev)
-de
VP(
NP(adam)
-ı
gördü
)
|
NP(
NP(ev)
-deki
NP(adam)
)
-ı
gördü
)
|
NP(
NP(teleskop)
-lu
NP(ev)
)
-de
VP(
NP(adam)
-ı
gördü
)
|
NP(
NP(teleskop)
-lu
NP(
NP(ev)
-deki
NP(adam)
)
|
NP(
NP(teleskop)
-lu
NP(ev)
)
-deki
NP(adam)
)
-ı
gördü
)
-m
)