From a5300ad062b82154f3f9533e143ea35515e6c39e Mon Sep 17 00:00:00 2001 From: aarne Date: Tue, 11 Nov 2008 10:28:32 +0000 Subject: tutorial complete with server and js --- doc/gf-tutorial.html | 206 +++++++++++++++++++++++++-------------------------- 1 file changed, 101 insertions(+), 105 deletions(-) (limited to 'doc/gf-tutorial.html') diff --git a/doc/gf-tutorial.html b/doc/gf-tutorial.html index fcf5683d4..18e353a23 100644 --- a/doc/gf-tutorial.html +++ b/doc/gf-tutorial.html @@ -238,19 +238,23 @@ Version 3.1.2, November 2008
  • Producing GFCC for the translator
  • A translator loop
  • A question-answer system -
  • Exporting GF datatypes to Haskell -
  • Example of exporting GF datatypes +
  • Abstract syntax of the query system +
  • Exporting GF datatypes to Haskell
  • The question-answer function
  • Converting between Haskell and GF trees
  • Putting it all together: the transfer definition
  • Putting it all together: the Main module
  • Putting it all together: the Makefile -
  • Translets: embedded translators in Java -
  • Dialogue systems in Java -
  • Language models for speech recognition +
  • Web server applications +
  • JavaScript applications +
  • Language models for speech recognition + @@ -5232,10 +5236,10 @@ point literals as arguments. Goals:

    @@ -5245,13 +5249,13 @@ Goals:

    Functionalities of an embedded grammar format

    GF grammars can be used as parts of programs written in other programming -languages. Haskell and Java. +languages, to be called host languages. This facility is based on several components:

    @@ -5264,23 +5268,13 @@ This facility is based on several components: The portable format is called PGF, "Portable Grammar Format".

    -A file can be produced in GF by the command -

    -
    -    > print_grammar | write_file FILE.pgf
    -
    -

    -There is also a batch compiler, executable from the operative system shell: +This format is produced by the GF batch compiler gfc, +executable from the operative system shell:

         % gfc --make SOURCE.gf
     

    -This applies to GF version 3 and upwards. Older GF used a format suffixed -.gfcm. -At the moment of writing, also the Java interpreter still uses the GFCM format. -

    -

    PGF is the recommended format in which final grammar products are distributed, because they are stripped from superfluous information and can be started and applied @@ -5456,50 +5450,60 @@ To reply in the same language as the question:

    +

    Abstract syntax of the query system

    +

    +Input: abstract syntax judgements +

    +
    +  abstract Query = {
    +  
    +    flags startcat=Question ;
    +  
    +    cat 
    +      Answer ; Question ; Object ;
    +  
    +    fun 
    +      Even   : Object -> Question ;
    +      Odd    : Object -> Question ;
    +      Prime  : Object -> Question ;
    +      Number : Int -> Object ;
    +  
    +      Yes : Answer ;
    +      No  : Answer ;
    +  }
    +
    +

    +

    + +

    +

    Exporting GF datatypes to Haskell

    To make it easy to define a transfer function, we export the abstract syntax to a system of Haskell datatypes:

    -    % gfc --output-format=haskell Food.gfcc
    +    % gfc --output-format=haskell Query.pgf
     

    It is also possible to produce the Haskell file together with GFCC, by

    -    % gfc --make --output-format=haskell FoodEng.gf FoodIta.gf
    +    % gfc --make --output-format=haskell QueryEng.gf
     

    -The result is a file named Food.hs, containing a -module named Food. +The result is a file named Query.hs, containing a +module named Query.

    - -

    Example of exporting GF datatypes

    -

    -Input: abstract syntax judgements -

    -
    -    cat 
    -      Answer ; Question ; Object ;
    -  
    -    fun 
    -      Even   : Object -> Question ;
    -      Odd    : Object -> Question ;
    -      Prime  : Object -> Question ;
    -      Number : Int -> Object ;
    -  
    -      Yes : Answer ;
    -      No  : Answer ;
    -

    Output: Haskell definitions

    -  newtype GInt = GInt Integer
    +  module Query where
    +  import PGF
       
       data GAnswer =
          GYes 
    @@ -5511,6 +5515,8 @@ Output: Haskell definitions
          GPrime GObject 
        | GOdd GObject 
        | GEven GObject 
    +  
    +  newtype GInt = GInt Integer
     

    All type and constructor names are prefixed with a G to prevent clashes. @@ -5571,8 +5577,8 @@ For the programmer, it is enougo to know:

    @@ -5584,7 +5590,7 @@ For the programmer, it is enougo to know: module TransferDef where import PGF (Tree) - import Math -- generated from GF + import Query -- generated from GF transfer :: Tree -> Tree transfer = gf . answer . fg @@ -5625,7 +5631,7 @@ Here is the complete code in the Haskell file TransferLoop.hs. main :: IO () main = do - gr <- file2grammar "Math.pgf" + gr <- readPGF "Query.pgf" loop (translate transfer gr) loop :: (String -> String) -> IO () @@ -5636,7 +5642,7 @@ Here is the complete code in the Haskell file TransferLoop.hs. loop trans translate :: (Tree -> Tree) -> PGF -> String -> String - translate tr gr = case parseAllLang gr (startCat gr) s of + translate tr gr s = case parseAllLang gr (startCat gr) s of (lg,t:_):_ -> linearize gr lg (tr t) _ -> "NO PARSE" @@ -5651,7 +5657,7 @@ To automate the production of the system, we write a Makefile as fo

       all:
    -          gfc --make -haskell MathEng.gf MathFre.gf
    +          gfc --make --output-format=haskell QueryEng
               ghc --make -o ./math TransferLoop.hs
               strip math
     
    @@ -5683,91 +5689,81 @@ Just to summarize, the source of the application consists of the following files

    -

    Translets: embedded translators in Java

    +

    Web server applications

    -NOTICE. Only for GF 2.9 and older at the moment. +PGF files can be used in web servers, for which there is a Haskell library included +in src/server/. How to build a server for tasks like translators is explained +in the README file in that directory.

    -A Java system needs many more files than a Haskell system. -To get started, fetch the package gfc2java from +One of the servers that can be readily built with the library (without any +programming required) is fridge poetry magnets. It is an application that +uses an incremental parser to suggest grammatically correct next words. Here +is an example of its application to the Foods grammars.

    -www.cs.chalmers.se/~bringert/darcs/gfc2java/ +

    -by using the Darcs version control system as described in this page. +

    + +

    JavaScript applications

    -The gfc2java package contains a script build-translet, which -can be applied -to any .gfcm file to create a translet, a small translation GUI. +JavaScript is a programming language that has interpreters built in in most +web browsers. It is therefore usable for client side web programs, which can even +be run without access to the internet. The following figure shows a JavaScript +program compiled from GF grammars as run on an iPhone.

    -For the Food -grammars of Lesson 2, we first create a file food.gfcm by +

    -
    -    % echo "pm | wf food.gfcm" | gf FoodEng.gf FoodIta.gf
    -

    -and then run +

    -
    -    % build_translet food.gfcm
    -
    + +

    Compiling to JavaScript

    -The resulting file translate-food.jar can be run with +JavaScript is one of the output formats of the GF batch compiler. Thus the following +command generates a JavaScript file from two Food grammars.

    -    % java -jar translate-food.jar
    +    % gfc --make --output-format=js FoodEng.gf FoodIta.gf
     

    -The translet looks like this: -

    -

    - +The name of the generated file is Food.js, derived from the top-most abstract +syntax name. This file contains the multilingual grammar as a JavaScript object.

    - -

    Dialogue systems in Java

    -

    -NOTICE. Only for GF 2.9 and older at the moment. -

    -

    -A question-answer system is a special case of a dialogue system, -where the user and -the computer communicate by writing or, even more properly, by speech. -The gf-java -homepage provides an example of a most simple dialogue system imaginable, -where two -the conversation has just two rules: -

    - - + +

    Using the JavaScript grammar

    -The conversation can be made in both English and Swedish; the user's initiative -decides which language the system replies in. Thus the structure is very similar -to the math program here. +To perform parsing and linearization, the run-time library +gflib.js is used. It is included in GF/lib/javascript/, together with +some other JavaScript and HTML files; these files can be used +as templates for building applications.

    -The GF and Java sources of the program can be -found in +An example of usage is +translator.html, +which is in fact initialized with +a pointer to the Food grammar, so that it provides translation between the English +and Italian grammars:

    -[www.cs.chalmers.se/~bringert/darcs/simpledemo http://www.cs.chalmers.se/~bringert/darcs/simpledemo] +

    -again accessible with the Darcs version control system. +The grammar must have the name grammar.js. The abstract syntax and start +category names in translator.html must match the ones in the grammar. +With these changes, the translator works for any multilingual GF grammar.

    - +

    Language models for speech recognition

    The standard way of using GF in speech recognition is by building @@ -5814,7 +5810,7 @@ Example: GSL generated from FoodsEng.gf.

    - +

    More speech recognition grammar formats

    Other formats available via the --output-format flag include: -- cgit v1.2.3