JSGF

Build Status Gem Version Yard Docs

For all of your Java Speech Grammar Format parsing needs.

Usage

Reading an existing JSGF file is as simple as...

require 'jsgf'

grammar = JSGF.read('example.gram')

Writing to a file is similarly simple.

grammar.write('my_grammar.gram')

You can also write to an IO stream.

File.open('my_grammar.gram', 'w') do |f|
    grammar.write(f)
end

Grammar DSL

The JSGF gem includes a simple DSL for generating new grammars. The syntax follows the the JSGF syntax, but with a few differences.

  • Rule names can be either Symbols or Strings (they're converted to Strings internally)
  • Rules can be referenced using symbols in addition to the angle-bracket syntax used by JSGF
  • Alternations are created using arrays
  • Rules are private by default, however the root rules are automatically made public
grammar = JSGF.grammar 'Turtle' do
    rule quit: 'quit'                       # 'quit' and 'move' are public because
    rule move: 'go :direction :distance'    #   they are the roots of the grammar tree

    rule direction: %w{forward backward}
    rule distance: %w{one two three four five six seven eight nine ten}
end

Atoms can be made optional using the JSGF square-bracket syntax...

grammar = JSGF.grammar 'PoliteTurtle' do
    rule move: '[please] go :direction :distance'
    ...
end

Installation

Add this line to your application's Gemfile:

gem 'jsgf'

And then execute:

$ bundle

Or install it yourself as:

$ gem install jsgf

License

Copyright 2015-2016 Brandon Fosdick [email protected] and released under the BSD license.