diff --git a/doc/food-js.png b/doc/food-js.png new file mode 100644 index 000000000..fe579b1a9 Binary files /dev/null and b/doc/food-js.png differ diff --git a/doc/food-magnet.png b/doc/food-magnet.png new file mode 100644 index 000000000..8b137875d Binary files /dev/null and b/doc/food-magnet.png differ 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,33 +5450,15 @@ To reply in the same language as the question:

    -

    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
    -
    -

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

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

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

    -

    - -

    - -

    Example of exporting GF datatypes

    +

    Abstract syntax of the query system

    Input: abstract syntax judgements

    +  abstract Query = {
    +  
    +    flags startcat=Question ;
    +  
         cat 
           Answer ; Question ; Object ;
       
    @@ -5494,12 +5470,40 @@ Input: abstract syntax judgements
       
           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 Query.pgf
    +
    +

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

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

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

    +

    + +

    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. -

    -

    -The gfc2java package contains a script build-translet, which -can be applied -to any .gfcm file to create a translet, a small translation GUI. -

    -

    -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
    -
    -

    -The resulting file translate-food.jar can be run with -

    -
    -    % java -jar translate-food.jar
    -
    -

    -The translet looks like this: -

    -

    - +

    -

    Dialogue systems in Java

    +

    JavaScript applications

    -NOTICE. Only for GF 2.9 and older at the moment. +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.

    -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: -

    - - -

    -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. -

    -

    -The GF and Java sources of the program can be -found in -

    -

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

    -

    -again accessible with the Darcs version control system. +

    +

    Compiling to JavaScript

    +

    +JavaScript is one of the output formats of the GF batch compiler. Thus the following +command generates a JavaScript file from two Food grammars. +

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

    +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. +

    +

    + +

    + +

    Using the JavaScript grammar

    +

    +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. +

    +

    +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: +

    +

    + +

    +

    +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: diff --git a/doc/gf-tutorial.txt b/doc/gf-tutorial.txt index c556cbe64..3cb22a3d4 100644 --- a/doc/gf-tutorial.txt +++ b/doc/gf-tutorial.txt @@ -4899,12 +4899,68 @@ Just to summarize, the source of the application consists of the following files #NEW -TODO: web server applications +==Web server applications== + +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`` ../src/server/README] file in that directory. + +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. + +[food-magnet.png] #NEW -TODO: JavaScript applications +==JavaScript applications== + +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. + +[iphone.jpg] + + +#NEW + +===Compiling to JavaScript=== + +JavaScript is one of the output formats of the GF batch compiler. Thus the following +command generates a JavaScript file from two ``Food`` grammars. +``` + % gfc --make --output-format=js FoodEng.gf FoodIta.gf +``` +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. + + +#NEW + +===Using the JavaScript grammar=== + +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. + +An example of usage is +[``translator.html`` ../lib/javascript/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: + +[food-js.png] + +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. + + + #NEW diff --git a/doc/iphone.jpg b/doc/iphone.jpg new file mode 100644 index 000000000..d9e138b88 Binary files /dev/null and b/doc/iphone.jpg differ diff --git a/examples/tutorial/semantics/Answer.hs b/examples/tutorial/old/semantics/Answer.hs similarity index 100% rename from examples/tutorial/semantics/Answer.hs rename to examples/tutorial/old/semantics/Answer.hs diff --git a/examples/tutorial/semantics/AnswerBase.hs b/examples/tutorial/old/semantics/AnswerBase.hs similarity index 100% rename from examples/tutorial/semantics/AnswerBase.hs rename to examples/tutorial/old/semantics/AnswerBase.hs diff --git a/examples/tutorial/semantics/Base.gf b/examples/tutorial/old/semantics/Base.gf similarity index 100% rename from examples/tutorial/semantics/Base.gf rename to examples/tutorial/old/semantics/Base.gf diff --git a/examples/tutorial/semantics/BaseEng.gf b/examples/tutorial/old/semantics/BaseEng.gf similarity index 100% rename from examples/tutorial/semantics/BaseEng.gf rename to examples/tutorial/old/semantics/BaseEng.gf diff --git a/examples/tutorial/semantics/BaseI.gf b/examples/tutorial/old/semantics/BaseI.gf similarity index 100% rename from examples/tutorial/semantics/BaseI.gf rename to examples/tutorial/old/semantics/BaseI.gf diff --git a/examples/tutorial/semantics/BaseIEng.gf b/examples/tutorial/old/semantics/BaseIEng.gf similarity index 100% rename from examples/tutorial/semantics/BaseIEng.gf rename to examples/tutorial/old/semantics/BaseIEng.gf diff --git a/examples/tutorial/semantics/BaseSwe.gf b/examples/tutorial/old/semantics/BaseSwe.gf similarity index 100% rename from examples/tutorial/semantics/BaseSwe.gf rename to examples/tutorial/old/semantics/BaseSwe.gf diff --git a/examples/tutorial/semantics/GSyntax.hs b/examples/tutorial/old/semantics/GSyntax.hs similarity index 100% rename from examples/tutorial/semantics/GSyntax.hs rename to examples/tutorial/old/semantics/GSyntax.hs diff --git a/examples/tutorial/semantics/LexBase.gf b/examples/tutorial/old/semantics/LexBase.gf similarity index 100% rename from examples/tutorial/semantics/LexBase.gf rename to examples/tutorial/old/semantics/LexBase.gf diff --git a/examples/tutorial/semantics/LexBaseEng.gf b/examples/tutorial/old/semantics/LexBaseEng.gf similarity index 100% rename from examples/tutorial/semantics/LexBaseEng.gf rename to examples/tutorial/old/semantics/LexBaseEng.gf diff --git a/examples/tutorial/semantics/LexBaseSwe.gf b/examples/tutorial/old/semantics/LexBaseSwe.gf similarity index 100% rename from examples/tutorial/semantics/LexBaseSwe.gf rename to examples/tutorial/old/semantics/LexBaseSwe.gf diff --git a/examples/tutorial/semantics/Logic.hs b/examples/tutorial/old/semantics/Logic.hs similarity index 100% rename from examples/tutorial/semantics/Logic.hs rename to examples/tutorial/old/semantics/Logic.hs diff --git a/examples/tutorial/semantics/SemBase.hs b/examples/tutorial/old/semantics/SemBase.hs similarity index 100% rename from examples/tutorial/semantics/SemBase.hs rename to examples/tutorial/old/semantics/SemBase.hs diff --git a/examples/tutorial/semantics/Top.hs b/examples/tutorial/old/semantics/Top.hs similarity index 100% rename from examples/tutorial/semantics/Top.hs rename to examples/tutorial/old/semantics/Top.hs