Commit b88c926d authored by jfschaefer's avatar jfschaefer

added fragment 0

parent 67ca214c
.ipynb_checkpoints
This diff is collapsed.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Fragment 0.1\n",
"\n",
"In this fragment, we will extend the fragment 0 to get a first idea of how GF can handle more complex grammatical phenomena.\n",
"Concretely, we will extend our fragment to also support plurals, which means that the endings of verbs now depend on the plurality of the noun phrase.\n",
"Example sentences:\n",
"* John runs\n",
"* John and Mary run\n",
"* John and Mary are happy\n",
"\n",
"Furthermore, we will show to modularize our grammar (i.e. split it into more files).\n",
"In particular, we want to put the lexicon into a separate file."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Abstract changed, previous concretes discarded.\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"abstract Fragment0_1Grammar = {\n",
" cat\n",
" S; NP; VP; Conj;\n",
" fun\n",
" makeS : NP -> VP -> S;\n",
" combineS : Conj -> S -> S -> S;\n",
" -- our new rule: combine two NPs with a conjunction\n",
" combineNP : Conj -> NP -> NP -> NP;\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Abstract changed, previous concretes discarded.\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"abstract Fragment0_1Lexicon = Fragment0_1Grammar ** {\n",
" -- we extend Fragment0_1Grammar with more function constants\n",
" fun\n",
" john : NP;\n",
" mary : NP;\n",
" run : VP;\n",
" behappy : VP;\n",
" and : Conj;\n",
" or : Conj;\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A Trickier Concrete Syntax\n",
"\n",
"The grammar rules become a bit more complicated now. In particular, we need to:\n",
"* keep track of the plurality of noun phrases\n",
"* pick the verb form according the the plurality of the noun phrase"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Abstract changed, previous concretes discarded.\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"concrete Fragment0_1GrammarEng of Fragment0_1Grammar = {\n",
" -- First, we introduce a parameter type `Plurality`.\n",
" -- It can have two values: `Sg` and `Pl`.\n",
" param\n",
" Plurality = Sg | Pl;\n",
" \n",
" lincat\n",
" -- sentences and conjunction remain strings\n",
" S = Str;\n",
" Conj = Str;\n",
" -- A noun phrase need to contain two things now:\n",
" -- The string `s` and its plurality `p`.\n",
" -- For this we use a record type:\n",
" NP = { s : Str ; p : Plurality };\n",
" -- A verb phrase has one string for every plurality.\n",
" -- We can implement this with a table type.\n",
" -- In this table, the string for each plurality can be looked up.\n",
" VP = Plurality => Str;\n",
" \n",
" lin\n",
" -- `makeS` combines a noun phrase `n` and a verb phrase `v` into a string.\n",
" -- So we take the string of `n` and concatenate it with the correct verb phrase form.\n",
" -- We look up the right form in the table using the `!` operator.\n",
" makeS n v = n.s ++ v ! n.p;\n",
" combineS c s1 s2 = s1 ++ c ++ s2; -- no changes here\n",
" -- Two noun phrases are combined into a new noun phrase.\n",
" -- Therefore, we need to create a record type that has a string field\n",
" -- and a plurality field.\n",
" -- We set the plurality always to Pl.\n",
" combineNP c n1 n2 = {s = n1.s ++ c ++ n2.s; p = Pl};\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Operations\n",
"In the lexicon, we now need to use table and record types for verb and noun phrases respectively.\n",
"You can do this the following way:\n",
"\n",
"```\n",
"concrete Fragment0_1LexiconEng of Fragment0_1Lexicon = Fragment0_1GrammarEng ** {\n",
" lin\n",
" john = {s = \"John\"; p = Sg};\n",
" mary = {s = \"Mary\"; p = Sg};\n",
" run = table { Sg => \"runs\"; Pl => \"run\" };\n",
" behappy = table { Sg => \"is happy\"; Pl => \"are happy\" };\n",
"}\n",
"```\n",
"\n",
"This involves a lot of repetitive typing (which programmers hate).\n",
"You can avoid this (and make your grammar more flexible) by using operations:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Abstract changed, previous concretes discarded.\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"concrete Fragment0_1LexiconEng of Fragment0_1Lexicon = Fragment0_1GrammarEng ** {\n",
" oper\n",
" -- We will use `mkNP` to create (singular) noun phrases.\n",
" -- The operation has the type `Str -> NP`.\n",
" -- So we can call `mkNP \"John\"` to create the corresponding noun phrase.\n",
" -- The backslash is used for lambda notation (i.e. `str` stands for the argument)\n",
" mkNP : Str -> NP = \\str -> lin NP {s = str; p = Sg};\n",
" -- For verb phrases, we will need two strings.\n",
" mkVP : Str -> Str -> Plurality=>Str = \\s1 -> \\s2 -> table {Sg => s1; Pl => s2};\n",
" lin\n",
" john = mkNP \"John\";\n",
" mary = mkNP \"Mary\";\n",
" run = mkVP \"runs\" \"run\";\n",
" behappy = mkVP \"is happy\" \"are happy\";\n",
" and = \"and\";\n",
" or = \"or\";\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we can use our grammar for parsing and linearizing sentences.\n",
"One more tip for people who dislike typing more than necessary: Most (all?) GF commands have a short form.\n",
"The short form for `parse` is `p` and the short form for `linearize` is `l`."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"makeS (combineNP and john mary) behappy\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"p \"John and Mary are happy\""
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"The parser failed at token 4: \"is\"\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"p \"John and Mary is happy\""
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"makeS john behappy\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"p \"John is happy\""
]
}
],
"metadata": {
"kernelspec": {
"display_name": "GF",
"language": "gf",
"name": "gf"
},
"language_info": {
"file_extension": ".gf",
"mimetype": "text/gf",
"name": "gf"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
This diff is collapsed.
This diff is collapsed.
# Notebooks of the LBS Lecture 2019/2020
Jupyter comes in two versions: Jupyter notebooks and Jupyter lab.
Jupyter lab is the newer version (and also, confusingly, supports notebooks).
By default, Jupyter only supports Python.
However, more kernels can be installed to support other languages.
In this lecture, we are interested in two different kernels:
* The **GF kernel**
* The **GLF kernel**, which is an extension of the GF kernel to also support MMT
The GF kernel works quite well under linux/MacOS.
We are still improving the GLF kernel and it is more difficult to set up.
Therefore, it might make sense to first install GF kernel and work with it.
The GF kernel works better with Jupyter notebook than with Jupyter lab
(but the newer GLF kernel should work well with Jupyter lab).
You can find the GF kernel (with installation instructions) at [https://github.com/KWARC/gf_kernel](https://github.com/KWARC/gf_kernel). Note that the GF kernel requires GF to be installed (see [https://www.grammaticalframework.org/download/index.html](https://www.grammaticalframework.org/download/index.html)).
If you prefer to use an editor and the command line (or prefer to use Windows which currently isn't supported), you can just install GF directly [https://www.grammaticalframework.org/download/index.html](https://www.grammaticalframework.org/download/index.html).
There shouldn't be any need for you to install Jupyter notebook and the GF kernel at the moment.
## Notebooks
#### `Fragment0.ipynb`
A basic introduction to the Grammatical Framework (GF).
It covers translation of very simple sentences without any interesting grammatical rules.
#### `Fragment0.1.ipynb`
A first introduction into GF's concrete types and operations.
We also show how a grammar can be split into several files.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment