Download - The NuGram dynamic grammar language

Transcript
Page 1: The NuGram dynamic grammar language

The NuGram Dynamic Grammar Language

Supported by NuGram 2.2June 16th, 2010

Page 2: The NuGram dynamic grammar language

Background

Extension to W3C’s SRGS ABNF format

Supported by a complete development environment –NuGram IDE

Dynamic grammars are deployable on NuGram Server

Copyright © 2010 Nu Echo Inc.

Page 3: The NuGram dynamic grammar language

Expressions

Constants– null, false, true– Strings

Ex: “abc”, “John Smith”, “05/30/2010”– Numbers

Ex: 10, 325.64

Variables– Must be a legal Java identifier– Top-level variables are provided by the instantiation context

Copyright © 2010 Nu Echo Inc.

Page 4: The NuGram dynamic grammar language

Expressions

Properties– Syntax: obj.propertyName– Property name must be a valid Java identifier

Method calls– Syntax: obj.methodName(arguments…)– Arguments are comma-separate

Array elements– Syntax: obj[indexValue]

Copyright © 2010 Nu Echo Inc.

Page 5: The NuGram dynamic grammar language

Expressions

Comparison operators– == – !=

Logical operators– ! (negation)– && (conjunction)– || (disjunction)

Copyright © 2010 Nu Echo Inc.

Page 6: The NuGram dynamic grammar language

Dynamic Words

Syntax@word ( Expressions… )– Values of all expressions are concatenated

Example

Copyright © 2010 Nu Echo Inc.

Template @word (name.firstname)

JSON Context {"name" : {"firstname": "dominique"}}

Resulting expansion dominique

Page 7: The NuGram dynamic grammar language

Dynamic Rule References

Syntax@ref ( Expressions… )– Values of all expressions are concatenated– Resulting value must start with “#” for a local rule reference

Example

Copyright © 2010 Nu Echo Inc.

Template @ref (“name.grxml?id=“ name.id)

JSON Context {"name" : {"id": "1234"}}

Resulting expansion $<name.grxml?id=1234>

Page 8: The NuGram dynamic grammar language

Dynamic Tags

Syntax@tag ( Expressions… )– Values of all expressions are concatenated

Example

Copyright © 2010 Nu Echo Inc.

Template @tag (“id = '“ name.id “';”)

JSON Context {"name" : {"id": "1234"}}

Resulting expansion {id = '1234';}

Page 9: The NuGram dynamic grammar language

Iterations

Syntax@for ( var : Expression ) Expansions…@end– Repeats the expansions for each value in the collection

resulting from evaluating Expression.

Copyright © 2010 Nu Echo Inc.

Page 10: The NuGram dynamic grammar language

Iterations

Example

Copyright © 2010 Nu Echo Inc.

Template @for (name : names) @word (name)@end

JSON Context {"names" : ["yves", "dominique", "gilles"] }

Resulting expansion yvesdominiquegilles

Page 11: The NuGram dynamic grammar language

Dynamic Sequences

Syntax@seq Expansions… @end– All direct expansions become part of the sequence

Copyright © 2010 Nu Echo Inc.

Page 12: The NuGram dynamic grammar language

Dynamic sequences

Example

Copyright © 2010 Nu Echo Inc.

Template @seq @for (digit : digits.split(“,”)) @word digit @end @tag (“out.digits = '” digits.replaceAll(“,”, “”) “'”)@end

JSON Context {"digits" : "1,2,3"}

Resulting expansion ( 1 2 3 {out.digits = '123'} )

Page 13: The NuGram dynamic grammar language

Dynamic Choices

Syntax@alt Expansions… @end– All direct expansions become alternative choices

Copyright © 2010 Nu Echo Inc.

Page 14: The NuGram dynamic grammar language

Dynamic Choices

Example 1

Copyright © 2010 Nu Echo Inc.

Template @alt @for (name : names) @word (name) @end@end

JSON Context {"names" : ["yves", "dominique", "gilles"] }

Resulting expansion yves | dominique | gilles

Page 15: The NuGram dynamic grammar language

Example 2Beware: @alt and @for may produce unexpected results

Template @alt @for (entry : entries) @word (entry.name) @tag ("out.id = '" entry.id "'") @end@end

JSON Context {"entries" : [{"name": "yves", "id":"225"}, {"name": "dominique", "id":"231"}]}

Resulting expansion yves | {out.id = '225'} | dominique | {out.id = '231'}

Dynamic Choices

Copyright © 2010 Nu Echo Inc.

Page 16: The NuGram dynamic grammar language

Template @alt @for (entry : entries) ( @word (entry.name) @tag ("out.id = '" entry.id "'") ) @end@end

JSON Context {"entries" : [{"name": "yves", "id":"225"}, {"name": "dominique", "id":"231"}]}

Resulting expansion ( yves {out.id = '225'} )| ( dominique {out.id = '231'} )

Example 2 (cont'd)Solution: use grouping

Dynamic Choices

Copyright © 2010 Nu Echo Inc.

Page 17: The NuGram dynamic grammar language

Local Variable Definitions

Syntax@var Ident = Expression : Expansions… @end– Ident is visible for the Expansions– Multiple variables can be declared at once

Example@var firstname = name.firstname, lastname = name.lastname : ( [@word lastname] [@word firstname] @word lastname )@end

Copyright © 2010 Nu Echo Inc.

Page 18: The NuGram dynamic grammar language

Conditionals

Syntax@if ( Expression ) Expansions… @elseif ( Expression ) Expansions… @else Expansions… @end– A single branch of the conditional is executed– @elseif and @else branches are optional– There can be more than one @elseif, but a single @else

Copyright © 2010 Nu Echo Inc.

Page 19: The NuGram dynamic grammar language

Macros Definitions

Syntax@define Ident (Parameters): Expansions… @end– Parameters are comma-separated identifiers– Can only appear as a top-level form

Example

@define fullname (name) : [@word name.firstname] @word name.lastname@end

Copyright © 2010 Nu Echo Inc.

Page 20: The NuGram dynamic grammar language

Macros Invocations

Syntax@call Ident (Expressions…)– Arguments are comma-separated

Example

@for (name : names) @call fullname (name)@end

Copyright © 2010 Nu Echo Inc.

Page 21: The NuGram dynamic grammar language

Dynamic Rules

Syntax@rule Expression = Expansions… @end– All direct expansions become alternative choices– Especially useful inside a top-level @for

Example

@for (typeSet : addresses.getStreetTypes()) @rule typeSet.getUniqueId() = @call streetTypes(typeSet) @end@end

Copyright © 2010 Nu Echo Inc.

Page 22: The NuGram dynamic grammar language

Dynamic Headers

Syntaxmode @string Expression;root @string Expression;language @string Expression;lexicon @string Expression;meta “string” is @string Expression;tag-format @string Expression;

– Expression must evaluate to a string– Useful for engine-specific headers

Copyright © 2010 Nu Echo Inc.

Page 23: The NuGram dynamic grammar language

Conditional Headers

Syntax– Add a @when clause at the end of the header form

Example

lexicon <mylexicon.xml> @when engine == “osr”;

tag-format <swi-semantics/1.0> @when engine == “osr”;meta “com.nuecho.generation.omit-tags” is “true” @when engine != “osr”;

Copyright © 2010 Nu Echo Inc.