From 65037bfe0122c5a08539228818d9e2f093ab0b48 Mon Sep 17 00:00:00 2001 From: Krasimir Angelov Date: Thu, 24 Aug 2017 22:33:40 +0200 Subject: [PATCH] more work on the runtime documentation --- doc/runtime-api.html | 202 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 181 insertions(+), 21 deletions(-) diff --git a/doc/runtime-api.html b/doc/runtime-api.html index 015c3d372..61c6350ed 100644 --- a/doc/runtime-api.html +++ b/doc/runtime-api.html @@ -1,20 +1,25 @@ - + + + - +

Using the Python Haskell Java C# binding to the C runtime

Krasimir Angelov, July 2015

- Choose a language: Haskell Python Java C# + Choose a language: Haskell Python Java C#

Loading the Grammar

@@ -445,12 +450,24 @@ word form with its possible analyses: for entry in eng.fullFormLexicon(): print(entry) +
+for (entry in eng.fullFormLexicon()) {
+    System.out.println(entry);
+}
+
The second one implements a simple lookup. The argument is a word form and the result is a list of analyses:
 print(eng.lookupMorpho("letter"))
 [('letter_1_N', 's Sg Nom', inf), ('letter_2_N', 's Sg Nom', inf)]
 
+
+for (MorphoAnalysis an : eng.lookupMorpho("letter")) {
+    System.out.println(an.getLemma()+", "+an.getField()+", "+an.getProb());
+}
+letter_1_N, s Sg Nom, inf
+letter_2_N, s Sg Nom, inf
+

Access the Abstract Syntax

@@ -460,21 +477,52 @@ you can get a list of abstract functions: >>> gr.functions .... +
+Prelude PGF2> functions gr
+....
+
+gr.getFunctions() +.... + or a list of categories:
 >>> gr.categories
 ....
 
+
+Prelude PGF2> categories gr
+....
+
+
+List<String> cats = gr.getCategories()
+....
+
You can also access all functions with the same result category:
 >>> gr.functionsByCat("Weekday")
 ['friday_Weekday', 'monday_Weekday', 'saturday_Weekday', 'sunday_Weekday', 'thursday_Weekday', 'tuesday_Weekday', 'wednesday_Weekday']
 
+
+Prelude PGF2> functionsByCat gr "Weekday"
+['friday_Weekday', 'monday_Weekday', 'saturday_Weekday', 'sunday_Weekday', 'thursday_Weekday', 'tuesday_Weekday', 'wednesday_Weekday']
+
+
+List<String> cats = gr.getFunctionsByCat("Weekday")
+....
+
The full type of a function can be retrieved as:
 >>> print(gr.functionType("DetCN"))
 Det -> CN -> NP
 
+
+Prelude PGF2> print (gr.functionType "DetCN")
+Det -> CN -> NP
+
+
+System.out.println(gr.getFunctionType("DetCN"))
+Det -> CN -> NP
+

Type Checking Abstract Trees

@@ -488,6 +536,20 @@ AdjCN (PositA red_A) (UseN theatre_N) >>> print(ty) CN +
+Prelude PGF2> let Right (e,ty) = inferExpr gr e
+Prelude PGF2> print e
+AdjCN (PositA red_A) (UseN theatre_N)
+Prelude PGF2> print ty
+CN
+
+
+TypedExpr te = gr.inferExpr(e)
+System.out.println(te.getExpr())
+AdjCN (PositA red_A) (UseN theatre_N)
+System.out.println(te.getType())
+CN
+
The result is a potentially updated expression and its type. In this case we always deal with simple types, which means that the new expression will be always equal to the original expression. However, this @@ -500,16 +562,35 @@ wouldn't be true when dependent types are added. >>> print(e) AdjCN (PositA red_A) (UseN theatre_N) -In case of type error you will get an exception: +
+Prelude PGF2> let Just ty = readType "CN"
+Prelude PGF2> let Just e = checkExpr gr e ty
+Prelude PGF2> print e
+AdjCN (PositA red_A) (UseN theatre_N)
+
+
+Expr e = gr.checkExpr(e,Type.readType("CN"))
+>>> System.out.println(e)
+AdjCN (PositA red_A) (UseN theatre_N)
+
+

In case of type error you will get an exception:

 >>> e = gr.checkExpr(e,pgf.readType("A"))
 pgf.TypeError: The expected type of the expression AdjCN (PositA red_A) (UseN theatre_N) is A but CN is infered
 
-

+
+Prelude PGF2> let Just ty = readType "A"
+Prelude PGF2> let Just e = checkExpr gr e ty
+pgf.TypeError: The expected type of the expression AdjCN (PositA red_A) (UseN theatre_N) is A but CN is infered
+
+
+Expr e = gr.checkExpr(e,Type.readType("A"))
+pgf.TypeError: The expected type of the expression AdjCN (PositA red_A) (UseN theatre_N) is A but CN is infered
+

Partial Grammar Loading

-By default the whole grammar is compiled into a single file +

By default the whole grammar is compiled into a single file which consists of an abstract syntax together will all concrete languages. For large grammars with many languages this might be inconvinient because loading becomes slower and the grammar takes @@ -519,6 +600,13 @@ This is done by using the option -split-pgf in the compiler:

 $ gf -make -split-pgf App12.pgf
 
+
+$ gf -make -split-pgf App12.pgf
+
+
+$ gf -make -split-pgf App12.pgf
+
+

Now you can load the grammar as usual but this time only the abstract syntax will be loaded. You can still use the languages @@ -528,10 +616,20 @@ concrete syntax objects: >>> gr = pgf.readPGF("App.pgf") >>> eng = gr.languages["AppEng"] +
+PGF gr = PGF.readPGF("App.pgf")
+Concr eng = gr.getLanguages().get("AppEng")
+
However, if you now try to use the concrete syntax then you will get an exception:
->>> gr.languages["AppEng"].lookupMorpho("letter")
+>>> eng.lookupMorpho("letter")
+Traceback (most recent call last):
+  File "", line 1, in 
+pgf.PGFError: The concrete syntax is not loaded
+
+
+eng.lookupMorpho("letter")
 Traceback (most recent call last):
   File "", line 1, in 
 pgf.PGFError: The concrete syntax is not loaded
@@ -543,18 +641,29 @@ Before using the concrete syntax, you need to explicitly load it:
 >>> print(eng.lookupMorpho("letter"))
 [('letter_1_N', 's Sg Nom', inf), ('letter_2_N', 's Sg Nom', inf)]
 
+
+eng.load("AppEng.pgf_c")
+for (MorphoAnalysis an : eng.lookupMorpho("letter")) {
+    System.out.println(an.getLemma()+", "+an.getField()+", "+an.getProb());
+}
+letter_1_N, s Sg Nom, inf
+letter_2_N, s Sg Nom, inf
+
When you don't need the language anymore then you can simply unload it:
 >>> eng.unload()
 
+
+eng.unload()
+

GraphViz

-GraphViz is used for visualizing abstract syntax trees and parse trees. +

GraphViz is used for visualizing abstract syntax trees and parse trees. 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:

 >>> print(gr.graphvizAbstractTree(e))
@@ -570,6 +679,20 @@ n3 -- n4 [style = "solid"]
 n0 -- n3 [style = "solid"]
 }
 
+
+Prelude PGF2> putStrLn (graphvizAbstractTree gr e)
+graph {
+n0[label = "AdjCN", style = "solid", shape = "plaintext"]
+n1[label = "PositA", style = "solid", shape = "plaintext"]
+n2[label = "red_A", style = "solid", shape = "plaintext"]
+n1 -- n2 [style = "solid"]
+n0 -- n1 [style = "solid"]
+n3[label = "UseN", style = "solid", shape = "plaintext"]
+n4[label = "theatre_N", style = "solid", shape = "plaintext"]
+n3 -- n4 [style = "solid"]
+n0 -- n3 [style = "solid"]
+}
+
 >>> print(eng.graphvizParseTree(e))
@@ -607,6 +730,43 @@ graph {
   n0 -- n100000
   n2 -- n100001
 }
+
+
+Prelude PGF2> putStrLn (graphvizParseTree eng e)
+graph {
+  node[shape=plaintext]
+
+  subgraph {rank=same;
+    n4[label="CN"]
+  }
+
+  subgraph {rank=same;
+    edge[style=invis]
+    n1[label="AP"]
+    n3[label="CN"]
+    n1 -- n3
+  }
+  n4 -- n1
+  n4 -- n3
+
+  subgraph {rank=same;
+    edge[style=invis]
+    n0[label="A"]
+    n2[label="N"]
+    n0 -- n2
+  }
+  n1 -- n0
+  n3 -- n2
+
+  subgraph {rank=same;
+    edge[style=invis]
+    n100000[label="red"]
+    n100001[label="theatre"]
+    n100000 -- n100001
+  }
+  n0 -- n100000
+  n2 -- n100001
+}