1
0
forked from GitHub/gf-core

Update runtime-api.html to match the new visual look of the GF website

This commit is contained in:
John J. Camilleri
2018-12-05 15:48:07 +01:00
parent 159b6ee331
commit b974c09951

View File

@@ -1,29 +1,26 @@
<html> <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head> <head>
<title>C Runtime API</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<style> <style>
body { background: #eee; padding-top: 200px; } pre {
background-color:#eee;
pre.python {background-color:#ffc; display: none} margin-top: 1em;
pre.haskell {background-color:#ffc; display: block} padding: 0.5em 1em;
pre.java {background-color:#ffc; display: none} }
pre.csharp {background-color:#ffc; display: none} pre.python {display: none}
pre.haskell {display: block}
pre.java {display: none}
pre.csharp {display: none}
span.python {display: none} span.python {display: none}
span.haskell {display: inline} span.haskell {display: inline}
span.java {display: none} span.java {display: none}
span.csharp {display: none} span.csharp {display: none}
.header {
position: fixed;
top: 0;
left: 0;
background: #ddd;
width: 100%;
padding: 5pt;
border-bottom: solid #bbb 2pt;
}
</style> </style>
<script lang="javascript"> <script lang="javascript">
function change_language(href) { function change_language(href) {
var name = href.split("#")[1]; var name = href.split("#")[1];
@@ -50,14 +47,28 @@
</script> </script>
</head> </head>
<body onload="change_language(window.location.href); window.addEventListener('hashchange', function(e){change_language(window.location.href);});"> <body onload="change_language(window.location.href); window.addEventListener('hashchange', function(e){change_language(window.location.href);});">
<span class="header"> <div class="container-fluid" style="max-width: 1200px">
<h1>Using the <span class="python">Python</span> <span class="haskell">Haskell</span> <span class="java">Java</span> <span class="csharp">C#</span> binding to the C runtime</h1> <div class="header sticky-top border-bottom py-3 bg-white">
<a href=".." title="Home">
Choose a language: <a href="#haskell">Haskell</a> <a href="#python">Python</a> <a href="#java">Java</a> <a href="#csharp">C#</a> <img src="../doc/Logos/gf1.svg" height="120px" class="float-md-right ml-3 mb-3 bg-white" alt="GF Logo">
</span> </a>
<h1>
Using the
<span class="python">Python</span>
<span class="haskell">Haskell</span>
<span class="java">Java</span>
<span class="csharp">C#</span>
binding to the C runtime
</h1>
<h4 class="text-muted">Krasimir Angelov, July 2015 - August 2017</h4>
Choose a language:
<a href="#haskell" class="mx-1">Haskell</a>
<a href="#python" class="mx-1">Python</a>
<a href="#java" class="mx-1">Java</a>
<a href="#csharp" class="mx-1">C#</a>
</div>
<main class="py-4">
<h4>Krasimir Angelov, July 2015 - August 2017</h4>
<h2>Loading the Grammar</h2> <h2>Loading the Grammar</h2>
Before you use the <span class="python">Python</span> binding you need to import the <span class="haskell">PGF2 module</span><span class="python">pgf module</span><span class="java">pgf package</span><span class="csharp">PGFSharp package</span>: Before you use the <span class="python">Python</span> binding you need to import the <span class="haskell">PGF2 module</span><span class="python">pgf module</span><span class="java">pgf package</span><span class="csharp">PGFSharp package</span>:
@@ -127,7 +138,7 @@ Concr eng = gr.Languages["AppEng"];
<h2>Parsing</h2> <h2>Parsing</h2>
All language specific services are available as All language specific services are available as
<span class="python">methods of the class <tt>pgf.Concr</tt></span><span class="haskell">functions that take as an argument an object of type <tt>Concr</tt></span><span class="java">methods of the class <tt>Concr</tt></span><span class="csharp">methods of the class <tt>Concr</tt></span>. <span class="python">methods of the class <tt>pgf.Concr</tt></span><span class="haskell">functions that take as an argument an object of type <tt>Concr</tt></span><span class="java">methods of the class <tt>Concr</tt></span><span class="csharp">methods of the class <tt>Concr</tt></span>.
For example to invoke the parser, you can call: For example to invoke the parser, you can call:
<pre class="python"> <pre class="python">
@@ -220,10 +231,10 @@ Console.WriteLine(ep.Item1);
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>
<p>Note that depending on the grammar it is absolutely possible that for <p>Note that depending on the grammar it is absolutely possible that for
a single sentence you might get infinitely many trees. a single sentence you might get infinitely many trees.
In other cases the number of trees might be finite but still enormous. In other cases the number of trees might be finite but still enormous.
The parser is specifically designed to be lazy, which means that The parser is specifically designed to be lazy, which means that
each tree is returned as soon as it is found before exhausting each tree is returned as soon as it is found before exhausting
the full search space. For grammars with a patological number of the full search space. For grammars with a patological number of
trees it is advisable to pick only the top <tt>N</tt> trees trees it is advisable to pick only the top <tt>N</tt> trees
@@ -246,16 +257,16 @@ parsing with a different start category can be done as follows:</p>
</pre> </pre>
</span> </span>
<span class="haskell"> <span class="haskell">
There is also the function <tt>parseWithHeuristics</tt> which There is also the function <tt>parseWithHeuristics</tt> which
takes two more paramaters which let you to have a better control takes two more paramaters which let you to have a better control
over the parser's behaviour: over the parser's behaviour:
<pre class="haskell"> <pre class="haskell">
Prelude PGF2> let res = parseWithHeuristics eng (startCat gr) heuristic_factor callbacks Prelude PGF2> let res = parseWithHeuristics eng (startCat gr) heuristic_factor callbacks
</pre> </pre>
</span> </span>
<span class="java"> <span class="java">
There is also the method <tt>parseWithHeuristics</tt> which There is also the method <tt>parseWithHeuristics</tt> which
takes two more paramaters which let you to have a better control takes two more paramaters which let you to have a better control
over the parser's behaviour: over the parser's behaviour:
<pre class="java"> <pre class="java">
Iterable&lt;ExprProb&gt; iterable = eng.parseWithHeuristics(gr.startCat(), heuristic_factor, callbacks); Iterable&lt;ExprProb&gt; iterable = eng.parseWithHeuristics(gr.startCat(), heuristic_factor, callbacks);
@@ -281,7 +292,7 @@ to factor 0.0. When we increase the factor then parsing becomes faster
but at the same time the sorting becomes imprecise. The worst but at the same time the sorting becomes imprecise. The worst
factor is 1.0. In any case the parser always returns the same set of factor is 1.0. In any case the parser always returns the same set of
trees but in different order. Our experience is that even a factor trees but in different order. Our experience is that even a factor
of about 0.6-0.8 with the translation grammar still orders of about 0.6-0.8 with the translation grammar still orders
the most probable tree on top of the list but further down the list, the most probable tree on top of the list but further down the list,
the trees become shuffled. the trees become shuffled.
</p> </p>
@@ -457,7 +468,7 @@ the object has the following public final variables:
</span> </span>
</p> </p>
The linearization works even if there are functions in the tree The linearization works even if there are functions in the tree
that doesn't have linearization definitions. In that case you that doesn't have linearization definitions. In that case you
will just see the name of the function in the generated string. 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
@@ -483,7 +494,7 @@ true
<p> <p>
An already constructed tree can be analyzed and transformed An already constructed tree can be analyzed and transformed
in the host application. For example you can deconstruct in the host application. For example you can deconstruct
a tree into a function name and a list of arguments: a tree into a function name and a list of arguments:
<pre class="python"> <pre class="python">
>>> e.unpack() >>> e.unpack()
@@ -523,8 +534,8 @@ literal. For example the result from:
<span class="haskell"> <span class="haskell">
The result from <tt>unApp</tt> is <tt>Just</tt> if the expression The result from <tt>unApp</tt> is <tt>Just</tt> if the expression
is an application and <tt>Nothing</tt> in all other cases. is an application and <tt>Nothing</tt> in all other cases.
Similarly, if the tree is a literal string then the return value Similarly, if the tree is a literal string then the return value
from <tt>unStr</tt> will be <tt>Just</tt> with the actual literal. from <tt>unStr</tt> will be <tt>Just</tt> with the actual literal.
For example the result from: For example the result from:
</span> </span>
<pre class="haskell"> <pre class="haskell">
@@ -534,8 +545,8 @@ Prelude PGF2> readExpr "\"literal\"" >>= unStr
<span class="java"> <span class="java">
The result from <tt>unApp</tt> is not <tt>null</tt> if the expression The result from <tt>unApp</tt> is not <tt>null</tt> if the expression
is an application, and <tt>null</tt> in all other cases. is an application, and <tt>null</tt> in all other cases.
Similarly, if the tree is a literal string then the return value Similarly, if the tree is a literal string then the return value
from <tt>unStr</tt> will not be <tt>null</tt> with the actual literal. from <tt>unStr</tt> will not be <tt>null</tt> with the actual literal.
For example the output from: For example the output from:
</span> </span>
<pre class="java"> <pre class="java">
@@ -545,15 +556,15 @@ System.out.println(elit.unStr());
<span class="csharp"> <span class="csharp">
The result from <tt>UnApp</tt> is not <tt>null</tt> if the expression The result from <tt>UnApp</tt> is not <tt>null</tt> if the expression
is an application, and <tt>null</tt> in all other cases. is an application, and <tt>null</tt> in all other cases.
Similarly, if the tree is a literal string then the return value Similarly, if the tree is a literal string then the return value
from <tt>UnStr</tt> will not be <tt>null</tt> with the actual literal. from <tt>UnStr</tt> will not be <tt>null</tt> with the actual literal.
For example the output from: For example the output from:
</span> </span>
<pre class="csharp"> <pre class="csharp">
Expr elit = Expr.ReadExpr("\"literal\""); Expr elit = Expr.ReadExpr("\"literal\"");
Console.WriteLine(elit.UnStr()); Console.WriteLine(elit.UnStr());
</pre> </pre>
is just the string "literal". is just the string "literal".
<span class="python">Situations like this can be detected <span class="python">Situations like this can be detected
in Python by checking the type of the result from <tt>unpack</tt>. in Python by checking the type of the result from <tt>unpack</tt>.
It is also possible to get an integer or a floating point number It is also possible to get an integer or a floating point number
@@ -569,7 +580,7 @@ There are also the methods <tt>UnAbs</tt>, <tt>UnInt</tt>, <tt>UnFloat</tt> and
</span> </span>
</p> </p>
Constructing new trees is also easy. You can either use Constructing new trees is also easy. You can either use
<tt>readExpr</tt> to read trees from strings, or you can <tt>readExpr</tt> to read trees from strings, or you can
construct new trees from existing pieces. This is possible by construct new trees from existing pieces. This is possible by
<span class="python"> <span class="python">
@@ -612,7 +623,7 @@ Console.WriteLine(e2);
<p>If the host application needs to do a lot of expression manipulations, <p>If the host application needs to do a lot of expression manipulations,
then it is helpful to use a higher-level API to the grammar, then it is helpful to use a higher-level API to the grammar,
also known as "embedded grammars" in GF. The advantage is that also known as "embedded grammars" in GF. The advantage is that
you can construct and analyze expressions in a more compact way.</p> you can construct and analyze expressions in a more compact way.</p>
<span class="python"> <span class="python">
<p>In Python you first have to <tt>embed</tt> the grammar by calling: <p>In Python you first have to <tt>embed</tt> the grammar by calling:
@@ -721,7 +732,7 @@ call the method <tt>default</tt>. The following is an example:
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)
@@ -1007,7 +1018,7 @@ Traceback (most recent call last):
pgf.PGFError: The concrete syntax is not loaded pgf.PGFError: The concrete syntax is not loaded
</pre> </pre>
Before using the concrete syntax, you need to explicitly load it: Before using the concrete syntax, you need to explicitly load it:
<pre class="python"> <pre class="python">
>>> eng.load("AppEng.pgf_c") >>> eng.load("AppEng.pgf_c")
>>> print(eng.lookupMorpho("letter")) >>> print(eng.lookupMorpho("letter"))
@@ -1060,7 +1071,7 @@ Traceback (most recent call last):
pgf.PGFError: The concrete syntax is not loaded pgf.PGFError: The concrete syntax is not loaded
</pre> </pre>
Before using the concrete syntax, you need to explicitly load it: Before using the concrete syntax, you need to explicitly load it:
<pre class="java"> <pre class="java">
eng.load("AppEng.pgf_c") eng.load("AppEng.pgf_c")
for (MorphoAnalysis an : eng.lookupMorpho("letter")) { for (MorphoAnalysis an : eng.lookupMorpho("letter")) {
@@ -1289,6 +1300,7 @@ graph {
} }
</pre> </pre>
</main>
</div>
</body> </body>
</html> </html>