python examples added

This commit is contained in:
aarneranta
2021-03-24 09:16:47 +01:00
parent 7afba566d5
commit b7c3877756
17 changed files with 2109 additions and 0 deletions

25
python/minitranslator.py Normal file
View File

@@ -0,0 +1,25 @@
# minimal translator that reads one line from stdio and translates from Eng to Swe with Minilang
# presupposes 'make minilang'
import pgf
# change these three to translate with other grammars and languages
absmodule = "MiniLang"
fromname = absmodule + "Eng"
toname = absmodule + "Swe"
def main():
# read in the grammar, set up to and from languages
grammar = pgf.readPGF(absmodule + ".pgf")
fromlang = grammar.languages[fromname]
tolang = grammar.languages[toname]
# read a line of input, parse in "from" language, linearize in "to" language
line = input("")
parseresult = fromlang.parse(line)
prob,tree = parseresult.__next__()
print(tolang.linearize(tree))
main()