forked from GitHub/gf-core
update the Python tutorial to be compatible with Python3
This commit is contained in:
@@ -41,7 +41,7 @@ which respresents the language.
|
|||||||
For example the following will extract the English language:
|
For example the following will extract the English language:
|
||||||
<pre class="code">
|
<pre class="code">
|
||||||
>>> eng = gr.languages["AppEng"]
|
>>> eng = gr.languages["AppEng"]
|
||||||
>>> print eng
|
>>> print(eng)
|
||||||
<pgf.Concr object at 0x7f7dfa4471d0>
|
<pgf.Concr object at 0x7f7dfa4471d0>
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
@@ -58,18 +58,22 @@ abstract trees. You can get the next tree by calling next:
|
|||||||
<pre class="code">
|
<pre class="code">
|
||||||
>>> p,e = i.next()
|
>>> p,e = i.next()
|
||||||
</pre>
|
</pre>
|
||||||
|
or by calling __next__ if you are using Python 3:
|
||||||
|
<pre class="code">
|
||||||
|
>>> p,e = i.__next__()
|
||||||
|
</pre>
|
||||||
The results are always pairs of probability and tree. The probabilities
|
The results are always pairs of probability and tree. The probabilities
|
||||||
are negated logarithmic probabilities and which means that the lowest
|
are negated logarithmic probabilities and which means that the lowest
|
||||||
number encodes the most probable result. The possible trees are
|
number encodes the most probable result. The possible trees are
|
||||||
returned in decreasing probability order (i.e. increasing negated logarithm).
|
returned in decreasing probability order (i.e. increasing negated logarithm).
|
||||||
The first tree should have the smallest <tt>p</tt>:
|
The first tree should have the smallest <tt>p</tt>:
|
||||||
<pre class="code">
|
<pre class="code">
|
||||||
>>> print p
|
>>> print(p)
|
||||||
35.9166526794
|
35.9166526794
|
||||||
</pre>
|
</pre>
|
||||||
and this is the corresponding abstract tree:
|
and this is the corresponding abstract tree:
|
||||||
<pre class="code">
|
<pre class="code">
|
||||||
>>> print e
|
>>> print(e)
|
||||||
PhrUtt NoPConj (UttS (UseCl (TTAnt TPres ASimul) PPos (PredVP (DetNP (DetQuant this_Quant NumSg)) (UseComp (CompNP (DetCN (DetQuant IndefArt NumSg) (AdjCN (PositA small_A) (UseN theatre_N)))))))) NoVoc
|
PhrUtt NoPConj (UttS (UseCl (TTAnt TPres ASimul) PPos (PredVP (DetNP (DetQuant this_Quant NumSg)) (UseComp (CompNP (DetCN (DetQuant IndefArt NumSg) (AdjCN (PositA small_A) (UseN theatre_N)))))))) NoVoc
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
@@ -116,7 +120,7 @@ a new expression like this:
|
|||||||
</pre>
|
</pre>
|
||||||
and then we can linearize it:
|
and then we can linearize it:
|
||||||
<pre class="code">
|
<pre class="code">
|
||||||
>>> print eng.linearize(e)
|
>>> print(eng.linearize(e))
|
||||||
red theatre
|
red theatre
|
||||||
</pre>
|
</pre>
|
||||||
This method produces only a single linearization. If you use variants
|
This method produces only a single linearization. If you use variants
|
||||||
@@ -124,7 +128,7 @@ in the grammar then you might want to see all possible linearizations.
|
|||||||
For that purpouse you should use linearizeAll:
|
For that purpouse you should use linearizeAll:
|
||||||
<pre class="code">
|
<pre class="code">
|
||||||
>>> for s in eng.linearizeAll(e):
|
>>> for s in eng.linearizeAll(e):
|
||||||
print s
|
print(s)
|
||||||
red theatre
|
red theatre
|
||||||
red theater
|
red theater
|
||||||
</pre>
|
</pre>
|
||||||
@@ -140,7 +144,7 @@ Finally, you could also get a linearization which is bracketed into
|
|||||||
a list of phrases:
|
a list of phrases:
|
||||||
<pre class="code">
|
<pre class="code">
|
||||||
>>> [b] = eng.bracketedLinearize(e)
|
>>> [b] = eng.bracketedLinearize(e)
|
||||||
>>> print b
|
>>> print(b)
|
||||||
(CN:4 (AP:1 (A:0 red)) (CN:3 (N:2 theatre)))
|
(CN:4 (AP:1 (A:0 red)) (CN:3 (N:2 theatre)))
|
||||||
</pre>
|
</pre>
|
||||||
Each bracket is actually an object of type pgf.Bracket. The property
|
Each bracket is actually an object of type pgf.Bracket. The property
|
||||||
@@ -158,7 +162,7 @@ will just see the name of the function in the generated string.
|
|||||||
It is sometimes helpful to be able to see whether a function
|
It is sometimes helpful to be able to see whether a function
|
||||||
is linearizable or not. This can be done in this way:
|
is linearizable or not. This can be done in this way:
|
||||||
<pre class="code">
|
<pre class="code">
|
||||||
>>> print eng.hasLinearization("apple_N")
|
>>> print(eng.hasLinearization("apple_N"))
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<h2>Analysing and Constructing Expressions</h2>
|
<h2>Analysing and Constructing Expressions</h2>
|
||||||
@@ -199,11 +203,11 @@ to call the method <tt>default</tt>. The following is an example:
|
|||||||
<pre class="code">
|
<pre class="code">
|
||||||
>>> class ExampleVisitor:
|
>>> class ExampleVisitor:
|
||||||
def on_DetCN(self,quant,cn):
|
def on_DetCN(self,quant,cn):
|
||||||
print "Found DetCN"
|
print("Found DetCN")
|
||||||
cn.visit(self)
|
cn.visit(self)
|
||||||
|
|
||||||
def on_AdjCN(self,adj,cn):
|
def on_AdjCN(self,adj,cn):
|
||||||
print "Found AdjCN"
|
print("Found AdjCN")
|
||||||
cn.visit(self)
|
cn.visit(self)
|
||||||
|
|
||||||
def default(self,e):
|
def default(self,e):
|
||||||
@@ -228,7 +232,7 @@ using the constructor for <tt>pgf.Expr</tt>:
|
|||||||
<pre class="code">
|
<pre class="code">
|
||||||
>>> quant = pgf.readExpr("DetQuant IndefArt NumSg")
|
>>> quant = pgf.readExpr("DetQuant IndefArt NumSg")
|
||||||
>>> e2 = pgf.Expr("DetCN", [quant, e])
|
>>> e2 = pgf.Expr("DetCN", [quant, e])
|
||||||
>>> print e2
|
>>> print(e2)
|
||||||
DetCN (DetQuant IndefArt NumSg) (AdjCN (PositA red_A) (UseN theatre_N))
|
DetCN (DetQuant IndefArt NumSg) (AdjCN (PositA red_A) (UseN theatre_N))
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
@@ -250,7 +254,7 @@ After that you can simply import the module:
|
|||||||
Now creating new trees is just a matter of calling ordinary Python
|
Now creating new trees is just a matter of calling ordinary Python
|
||||||
functions:
|
functions:
|
||||||
<pre class="code">
|
<pre class="code">
|
||||||
>>> print App.DetCN(quant,e)
|
>>> print(App.DetCN(quant,e))
|
||||||
DetCN (DetQuant IndefArt NumSg) (AdjCN (PositA red_A) (UseN house_N))
|
DetCN (DetQuant IndefArt NumSg) (AdjCN (PositA red_A) (UseN house_N))
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
@@ -262,12 +266,12 @@ The following code just iterates over the lexicon and prints each
|
|||||||
word form with its possible analyses:
|
word form with its possible analyses:
|
||||||
<pre class="code">
|
<pre class="code">
|
||||||
for entry in eng.fullFormLexicon():
|
for entry in eng.fullFormLexicon():
|
||||||
print entry
|
print(entry)
|
||||||
</pre>
|
</pre>
|
||||||
The second one implements a simple lookup. The argument is a word
|
The second one implements a simple lookup. The argument is a word
|
||||||
form and the result is a list of analyses:
|
form and the result is a list of analyses:
|
||||||
<pre class="code">
|
<pre class="code">
|
||||||
print eng.lookupMorpho("letter")
|
print(eng.lookupMorpho("letter"))
|
||||||
[('letter_1_N', 's Sg Nom', inf), ('letter_2_N', 's Sg Nom', inf)]
|
[('letter_1_N', 's Sg Nom', inf), ('letter_2_N', 's Sg Nom', inf)]
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
@@ -291,7 +295,7 @@ You can also access all functions with the same result category:
|
|||||||
</pre>
|
</pre>
|
||||||
The full type of a function can be retrieved as:
|
The full type of a function can be retrieved as:
|
||||||
<pre class="code">
|
<pre class="code">
|
||||||
>>> print gr.functionType("DetCN")
|
>>> print(gr.functionType("DetCN"))
|
||||||
Det -> CN -> NP
|
Det -> CN -> NP
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
@@ -302,9 +306,9 @@ for simple types. Dependent types are still not fully implemented
|
|||||||
in the current runtime. The inference is done with method <tt>inferExpr</tt>:
|
in the current runtime. The inference is done with method <tt>inferExpr</tt>:
|
||||||
<pre class="code">
|
<pre class="code">
|
||||||
>>> e,ty = gr.inferExpr(e)
|
>>> e,ty = gr.inferExpr(e)
|
||||||
>>> print e
|
>>> print(e)
|
||||||
AdjCN (PositA red_A) (UseN theatre_N)
|
AdjCN (PositA red_A) (UseN theatre_N)
|
||||||
>>> print ty
|
>>> print(ty)
|
||||||
CN
|
CN
|
||||||
</pre>
|
</pre>
|
||||||
The result is a potentially updated expression and its type. In this
|
The result is a potentially updated expression and its type. In this
|
||||||
@@ -316,7 +320,7 @@ wouldn't be true when dependent types are added.
|
|||||||
<p>Type checking is also trivial:
|
<p>Type checking is also trivial:
|
||||||
<pre class="code">
|
<pre class="code">
|
||||||
>>> e = gr.checkExpr(e,pgf.readType("CN"))
|
>>> e = gr.checkExpr(e,pgf.readType("CN"))
|
||||||
>>> print e
|
>>> print(e)
|
||||||
AdjCN (PositA red_A) (UseN theatre_N)
|
AdjCN (PositA red_A) (UseN theatre_N)
|
||||||
</pre>
|
</pre>
|
||||||
In case of type error you will get an exception:
|
In case of type error you will get an exception:
|
||||||
@@ -359,7 +363,7 @@ pgf.PGFError: The concrete syntax is not loaded
|
|||||||
Before using the concrete syntax, you need to explicitly load it:
|
Before using the concrete syntax, you need to explicitly load it:
|
||||||
<pre class="code">
|
<pre class="code">
|
||||||
>>> eng.load("AppEng.pgf_c")
|
>>> eng.load("AppEng.pgf_c")
|
||||||
>>> print eng.lookupMorpho("letter")
|
>>> print(eng.lookupMorpho("letter"))
|
||||||
[('letter_1_N', 's Sg Nom', inf), ('letter_2_N', 's Sg Nom', inf)]
|
[('letter_1_N', 's Sg Nom', inf), ('letter_2_N', 's Sg Nom', inf)]
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
@@ -376,7 +380,7 @@ In both cases the result is a GraphViz code that can be used for
|
|||||||
rendering the trees. See the examples bellow.
|
rendering the trees. See the examples bellow.
|
||||||
|
|
||||||
<pre class="code">
|
<pre class="code">
|
||||||
>>> print gr.graphvizAbstractTree(e)
|
>>> print(gr.graphvizAbstractTree(e))
|
||||||
graph {
|
graph {
|
||||||
n0[label = "AdjCN", style = "solid", shape = "plaintext"]
|
n0[label = "AdjCN", style = "solid", shape = "plaintext"]
|
||||||
n1[label = "PositA", style = "solid", shape = "plaintext"]
|
n1[label = "PositA", style = "solid", shape = "plaintext"]
|
||||||
@@ -391,7 +395,7 @@ n0 -- n3 [style = "solid"]
|
|||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<pre class="code">
|
<pre class="code">
|
||||||
>>> print eng.graphvizParseTree(e)
|
>>> print(eng.graphvizParseTree(e))
|
||||||
graph {
|
graph {
|
||||||
node[shape=plaintext]
|
node[shape=plaintext]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user