Skip to content

Repository files navigation

RubySpeech

RubySpeech is a library for constructing and parsing Text to Speech (TTS) and Automatic Speech Recognition (ASR) documents such as SSML, GRXML and NLSML. Such documents can be constructed to be processed by TTS and ASR engines, parsed as the result from such, or used in the implementation of such engines.

Installation

gem install ruby_speech

Library

SSML

RubySpeech provides a DSL for constructing SSML documents like so:

require'ruby_speech'speak=RubySpeech::SSML.drawdovoicegender: :male,name: 'fred'dostring"Hi, I'm Fred. The time is currently "say_asinterpret_as: 'date',format: 'dmy'do"01/02/1960"endendendspeak.to_s

becomes:

<speakxmlns="http://www.w3.org/2001/10/synthesis"version="1.0"xml:lang="en-US">
<voicegender="male"name="fred">
Hi, I'm Fred. The time is currently <say-asformat="dmy"interpret-as="date">01/02/1960</say-as>
</voice>
</speak>

Once your Speak is fully prepared and you're ready to send it off for processing, you must call to_doc on it to add the XML header:

<?xml version="1.0"?>
<speakxmlns="http://www.w3.org/2001/10/synthesis"version="1.0"xml:lang="en-US">
<voicegender="male"name="fred">
Hi, I'm Fred. The time is currently <say-asformat="dmy"interpret-as="date">01/02/1960</say-as>
</voice>
</speak>

You may also then need to call to_s.

GRXML

Construct a GRXML (SRGS) document like this:

require'ruby_speech'grammy=RubySpeech::GRXML.drawmode: :dtmf,root: 'pin'doruleid: 'digit'doone_ofdo('0'..'9').map{ |d| item{d}}endendruleid: 'pin',scope: 'public'doone_ofdoitemdoitemrepeat: '4'dorulerefuri: '#digit'end"#"enditemdo"* 9"endendendendgrammy.to_s

which becomes

<grammarxmlns="http://www.w3.org/2001/06/grammar"version="1.0"xml:lang="en-US"mode="dtmf"root="pin">
<ruleid="digit">
<one-of>
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
</one-of>
</rule>
<ruleid="pin"scope="public">
<one-of>
<item><itemrepeat="4"><rulerefuri="#digit"/></item>#</item>
<item>* 9</item>
</one-of>
</rule>
</grammar>

Grammar matching

It is possible to match some arbitrary input against a GRXML grammar, like so:

require'ruby_speech'
>> grammar=RubySpeech::GRXML.drawmode: :dtmf,root: 'pin'doruleid: 'digit'doone_ofdo('0'..'9').map{ |d| item{d}}endendruleid: 'pin',scope: 'public'doone_ofdoitemdoitemrepeat: '4'dorulerefuri: '#digit'end"#"enditemdo"* 9"endendendendmatcher=RubySpeech::GRXML::Matcher.newgrammar
>> matcher.match'*9'=>#<RubySpeech::GRXML::Match:0x00000100ae5d98@mode=:dtmf,@confidence=1,@utterance="*9",@interpretation="*9"
>
>> matcher.match'1234#'=>#<RubySpeech::GRXML::Match:0x00000100b7e020@mode=:dtmf,@confidence=1,@utterance="1234#",@interpretation="1234#"
>
>> matcher.match'5678#'=>#<RubySpeech::GRXML::Match:0x00000101218688@mode=:dtmf,@confidence=1,@utterance="5678#",@interpretation="5678#"
>
>> matcher.match'1111#'=>#<RubySpeech::GRXML::Match:0x000001012f69d8@mode=:dtmf,@confidence=1,@utterance="1111#",@interpretation="1111#"
>
>> matcher.match'111'=>#<RubySpeech::GRXML::NoMatch:0x00000101371660>

NLSML

Natural Language Semantics Markup Language is the format used by many Speech Recognition engines and natural language processors to add semantic information to human language. RubySpeech is capable of generating and parsing such documents.

It is possible to generate an NLSML document like so:

require'ruby_speech'nlsml=RubySpeech::NLSML.draw(grammar: 'http://flight','xmlns:myApp'=>'foo')dointerpretationconfidence: 0.6doinput"I want to go to Pittsburgh",mode: :speechmodeldogroupname: 'airline'dostringname: 'to_city'endendinstancedoself['myApp'].airlinedoto_city'Pittsburgh'endendendinterpretationconfidence: 0.4doinput"I want to go to Stockholm"modeldogroupname: 'airline'dostringname: 'to_city'endendinstancedoself['myApp'].airlinedoto_city"Stockholm"endendendendnlsml.to_s

becomes:

<?xml version="1.0"?>
<resultxmlns:myApp="foo"xmlns:xf="http://www.w3.org/2000/xforms"grammar="http://flight">
<interpretationconfidence="60">
<inputmode="speech">I want to go to Pittsburgh</input>
<xf:model>
<xf:groupname="airline">
<xf:stringname="to_city"/>
</xf:group>
</xf:model>
<xf:instance>
<myApp:airline>
<myApp:to_city>Pittsburgh</myApp:to_city>
</myApp:airline>
</xf:instance>
</interpretation>
<interpretationconfidence="40">
<input>I want to go to Stockholm</input>
<xf:model>
<xf:groupname="airline">
<xf:stringname="to_city"/>
</xf:group>
</xf:model>
<xf:instance>
<myApp:airline>
<myApp:to_city>Stockholm</myApp:to_city>
</myApp:airline>
</xf:instance>
</interpretation>
</result>

It's also possible to parse an NLSML document and extract useful information from it. Taking the above example, one may do:

document=RubySpeech.parsenlsml.to_sdocument.match?# => truedocument.interpretations# => [{confidence: 0.6,input: {mode: :speech,content: 'I want to go to Pittsburgh'},instance: {airline: {to_city: 'Pittsburgh'}}},{confidence: 0.4,input: {content: 'I want to go to Stockholm'},instance: {airline: {to_city: 'Stockholm'}}}]document.best_interpretation# => {confidence: 0.6,input: {mode: :speech,content: 'I want to go to Pittsburgh'},instance: {airline: {to_city: 'Pittsburgh'}}}

Check out the YARD documentation for more

Features:

SSML

  • Document construction
  • <voice/>
  • <prosody/>
  • <emphasis/>
  • <say-as/>
  • <break/>
  • <audio/>
  • <p/> and <s/>
  • <phoneme/>
  • <sub/>

Misc

  • <mark/>
  • <desc/>

GRXML

  • Document construction
  • <item/>
  • <one-of/>
  • <rule/>
  • <ruleref/>
  • <tag/>
  • <token/>

NLSML

  • Document construction
  • Simple data extraction from documents

TODO:

SSML

  • <lexicon/>
  • <meta/> and <metadata/>

GRXML

  • <meta/> and <metadata/>
  • <example/>
  • <lexicon/>

Links:

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or history.
    • If you want to have your own version, that is fine but bump version in a commit by itself so I can ignore when I pull
  • Send me a pull request. Bonus points for topic branches.

Copyright

Copyright (c) 2011 Ben Langfeld. MIT licence (see LICENSE for details).

About

A ruby library for TTS & ASR document preparation

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors