srgs 0.0.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +60 -4
- data/lib/srgs/elements/example.rb +11 -0
- data/lib/srgs/elements/grammar.rb +21 -0
- data/lib/srgs/elements/item.rb +15 -0
- data/lib/srgs/elements/lexicon.rb +12 -0
- data/lib/srgs/elements/meta.rb +12 -0
- data/lib/srgs/elements/metadata.rb +11 -0
- data/lib/srgs/elements/one_of.rb +16 -0
- data/lib/srgs/elements/rule.rb +19 -0
- data/lib/srgs/elements/rule_ref.rb +12 -0
- data/lib/srgs/elements/tag.rb +11 -0
- data/lib/srgs/elements/token.rb +13 -0
- data/lib/srgs/grammar_builder.rb +114 -0
- data/lib/srgs/test.rb +49 -0
- data/lib/srgs/version.rb +1 -1
- data/lib/srgs.rb +14 -3
- metadata +14 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 390a6193c41c0b64138ec62e9398d204b92fd56f
|
4
|
+
data.tar.gz: 82d27e46aaafceae8313531e052c747dfc1e1825
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2815041a09db82312bb0284c31fef3ae0380d4a367333adaa5bf6c52c664c9dae83a23898decf84d65601a3edc32c0d706af4e5e2ab663070f62ecfc4d31cb62
|
7
|
+
data.tar.gz: e2ed6e86daef6652ba3c29485cbd353b9ef747dca3ec2dd1de30edab23e4391aa70a060af02321b9a574d55deb045d5df1702721778552b96724df3e3eeba906
|
data/README.md
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
# SRGS
|
2
2
|
|
3
|
-
|
3
|
+
A tool for building srgs documents
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
|
-
gem '
|
9
|
+
gem 'srgs'
|
10
10
|
|
11
11
|
And then execute:
|
12
12
|
|
@@ -14,11 +14,67 @@ And then execute:
|
|
14
14
|
|
15
15
|
Or install it yourself as:
|
16
16
|
|
17
|
-
$ gem install
|
17
|
+
$ gem install srgs
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
Here's an example file. It will be documented better later
|
22
|
+
|
23
|
+
[This](http://msdn.microsoft.com/en-us/library/hh361653(v=office.14).aspx) is a good
|
24
|
+
reference on how srgs grammars work.
|
25
|
+
|
26
|
+
In the future, I plan to create a dsl for this but for now this is how it works.
|
27
|
+
```ruby
|
28
|
+
require 'srgs'
|
29
|
+
|
30
|
+
grammar = Srgs::Grammar.new
|
31
|
+
|
32
|
+
sunriseset = Srgs::Rule.new('riseset')
|
33
|
+
sunriseset_choices = Srgs::OneOf.new << Srgs::Item.new('rise') << Srgs::Item.new('set')
|
34
|
+
sunriseset << sunriseset_choices
|
35
|
+
sunriseset.scope = 'private'
|
36
|
+
|
37
|
+
sunriseset_ref = Srgs::RuleRef.new(sunriseset)
|
38
|
+
|
39
|
+
days = Srgs::Rule.new('days')
|
40
|
+
days_choices = Srgs::OneOf.new << Srgs::Item.new('today') << Srgs::Item.new('tomorrow') << Srgs::Item.new('currently')
|
41
|
+
days << days_choices
|
42
|
+
days.scope = 'private'
|
43
|
+
|
44
|
+
days_ref = Srgs::RuleRef.new(days)
|
45
|
+
|
46
|
+
weather = Srgs::Rule.new('weather')
|
47
|
+
sun = Srgs::Rule.new('sun')
|
48
|
+
|
49
|
+
question1 = Srgs::Item.new("Mycroft what is the weather");
|
50
|
+
question2 = Srgs::Item.new("Mycroft When is sun");
|
51
|
+
|
52
|
+
weather << question1
|
53
|
+
weather << days_ref
|
54
|
+
weather << Srgs::Tag.new("out.day=rules.days;")
|
55
|
+
weather.scope = 'public'
|
56
|
+
|
57
|
+
sun << question2
|
58
|
+
sun << sunriseset_ref
|
59
|
+
sun << Srgs::Tag.new("out.rise_or_set=rules.riseset;")
|
60
|
+
sun << days_ref
|
61
|
+
sun << Srgs::Tag.new("out.day=rules.days;")
|
62
|
+
sun.scope = 'public'
|
63
|
+
|
64
|
+
top_level = Srgs::Rule.new("toplevel");
|
65
|
+
top_level_choices = Srgs::OneOf.new
|
66
|
+
item1 = Srgs::Item.new Srgs::RuleRef.new(weather)
|
67
|
+
item2 = Srgs::Item.new Srgs::RuleRef.new(sun)
|
68
|
+
top_level_choices << item1
|
69
|
+
top_level_choices << item2
|
70
|
+
top_level << top_level_choices
|
71
|
+
|
72
|
+
grammar << top_level << weather << sun << days << sunriseset
|
73
|
+
|
74
|
+
grammar.root = "toplevel"
|
75
|
+
|
76
|
+
puts Srgs.build(grammar)
|
77
|
+
```
|
22
78
|
|
23
79
|
## Contributing
|
24
80
|
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Srgs
|
2
|
+
class Grammar
|
3
|
+
|
4
|
+
attr_accessor :root, :lexicon, :rules, :metas
|
5
|
+
|
6
|
+
def initialize(root = '')
|
7
|
+
@root = root
|
8
|
+
@lexicon = nil
|
9
|
+
@metas = Array.new
|
10
|
+
@rules = Array.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def <<(element)
|
14
|
+
@rules << element if element.is_a? Rule
|
15
|
+
@lexicon = element if element.is_a? Lexicon
|
16
|
+
@metas << element if element.is_a? Meta
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Srgs
|
2
|
+
class Item
|
3
|
+
|
4
|
+
attr_accessor :element, :repeat, :repeat_prob, :weight, :tag
|
5
|
+
|
6
|
+
def initialize(element, tag=nil, repeat=nil, repeat_prob = nil, weight = nil)
|
7
|
+
@element = element
|
8
|
+
@tag = tag
|
9
|
+
@repeat = repeat
|
10
|
+
@repeat_prob = repeat_prob
|
11
|
+
@weight = weight
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Srgs
|
2
|
+
class Rule
|
3
|
+
|
4
|
+
attr_accessor :id, :scope, :dynamic, :elements
|
5
|
+
|
6
|
+
def initialize(id, scope = 'private', dynamic = nil)
|
7
|
+
@id = id
|
8
|
+
@elements = Array.new
|
9
|
+
@scope = scope
|
10
|
+
@dynamic = dynamic
|
11
|
+
end
|
12
|
+
|
13
|
+
def <<(element)
|
14
|
+
@elements << element
|
15
|
+
self
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
|
3
|
+
module Srgs
|
4
|
+
extend self
|
5
|
+
|
6
|
+
def build(grammar)
|
7
|
+
builder = Nokogiri::XML::Builder.new do |xml|
|
8
|
+
xml.grammar do
|
9
|
+
lexicon(grammar.lexicon, xml) unless grammar.lexicon.nil?
|
10
|
+
grammar.metas.each do |meta|
|
11
|
+
meta(meta, xml)
|
12
|
+
end
|
13
|
+
grammar.rules.each do |rule|
|
14
|
+
rule(rule, xml)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end.to_xml
|
18
|
+
end
|
19
|
+
|
20
|
+
def example(example, xml)
|
21
|
+
xml.example example.text
|
22
|
+
end
|
23
|
+
|
24
|
+
def item(item, xml)
|
25
|
+
att = {}
|
26
|
+
set(:repeat, att, item.repeat)
|
27
|
+
set(:'repeat-prob', att, item.repeat_prob)
|
28
|
+
set(:weight, att, item.weight)
|
29
|
+
if item.element.is_a? Token
|
30
|
+
xml.item(att) do
|
31
|
+
tag(item.tag, xml) unless item.tag.nil?
|
32
|
+
token(item.element, xml)
|
33
|
+
end
|
34
|
+
elsif item.element.is_a? RuleRef
|
35
|
+
xml.item(att) do
|
36
|
+
tag(item.tag, xml) unless item.tag.nil?
|
37
|
+
rule_ref(item.element, xml)
|
38
|
+
end
|
39
|
+
else
|
40
|
+
xml.item(item.element, att) do
|
41
|
+
tag(item.tag, xml) unless item.tag.nil?
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def lexicon(lexicon, xml)
|
47
|
+
att = {uri: lexicon.uri }
|
48
|
+
set(:type, att, lexicon.type)
|
49
|
+
xml.lexicon(att)
|
50
|
+
end
|
51
|
+
|
52
|
+
def meta(meta, xml)
|
53
|
+
att = { content: meta.content }
|
54
|
+
set(:'http-equiv', att, meta.http_equiv)
|
55
|
+
set(:name, att, meta.name)
|
56
|
+
xml.meta(att)
|
57
|
+
end
|
58
|
+
|
59
|
+
def metadata(metadata, xml)
|
60
|
+
#TODO
|
61
|
+
raise "Not yet implemented"
|
62
|
+
end
|
63
|
+
|
64
|
+
def one_of(one_of, xml)
|
65
|
+
xml.send('one-of') do
|
66
|
+
one_of.items.each do |item|
|
67
|
+
item(item, xml)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def rule(rule, xml)
|
73
|
+
att = {id: rule.id}
|
74
|
+
set(:scope, att, rule.scope)
|
75
|
+
set(:'sapi:dynamic', att, rule.dynamic)
|
76
|
+
xml.rule(att) do
|
77
|
+
rule.elements.each do |element|
|
78
|
+
if element.is_a? Item
|
79
|
+
item(element, xml)
|
80
|
+
elsif element.is_a? RuleRef
|
81
|
+
rule_ref(element, xml)
|
82
|
+
elsif element.is_a? OneOf
|
83
|
+
one_of(element, xml)
|
84
|
+
elsif element.is_a? Token
|
85
|
+
token(element, xml)
|
86
|
+
elsif element.is_a? Tag
|
87
|
+
tag(element, xml)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def rule_ref(rule_ref, xml)
|
94
|
+
att = { uri: rule_ref.uri }
|
95
|
+
set(:special, att, rule_ref.special)
|
96
|
+
xml.ruleref(att)
|
97
|
+
end
|
98
|
+
|
99
|
+
def tag(tag, xml)
|
100
|
+
xml.tag tag.text
|
101
|
+
end
|
102
|
+
|
103
|
+
def token(token, xml)
|
104
|
+
att = {}
|
105
|
+
att(:'sapi:display', att, token.display)
|
106
|
+
att(:'sapi:pron', att, token.pron)
|
107
|
+
xml.token(token.text, att)
|
108
|
+
end
|
109
|
+
|
110
|
+
def set(sym, att, value)
|
111
|
+
att[sym] = value unless value.nil?
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
data/lib/srgs/test.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'srgs'
|
2
|
+
|
3
|
+
grammar = Srgs::Grammar.new
|
4
|
+
|
5
|
+
sunriseset = Srgs::Rule.new('riseset')
|
6
|
+
sunriseset_choices = Srgs::OneOf.new << Srgs::Item.new('rise') << Srgs::Item.new('set')
|
7
|
+
sunriseset << sunriseset_choices
|
8
|
+
sunriseset.scope = 'private'
|
9
|
+
|
10
|
+
sunriseset_ref = Srgs::RuleRef.new(sunriseset)
|
11
|
+
|
12
|
+
days = Srgs::Rule.new('days')
|
13
|
+
days_choices = Srgs::OneOf.new << Srgs::Item.new('today') << Srgs::Item.new('tomorrow') << Srgs::Item.new('currently')
|
14
|
+
days << days_choices
|
15
|
+
days.scope = 'private'
|
16
|
+
|
17
|
+
days_ref = Srgs::RuleRef.new(days)
|
18
|
+
|
19
|
+
weather = Srgs::Rule.new('weather')
|
20
|
+
sun = Srgs::Rule.new('sun')
|
21
|
+
|
22
|
+
question1 = Srgs::Item.new("Mycroft what is the weather");
|
23
|
+
question2 = Srgs::Item.new("Mycroft When is sun");
|
24
|
+
|
25
|
+
weather << question1
|
26
|
+
weather << days_ref
|
27
|
+
weather << Srgs::Tag.new("out.day=rules.days;")
|
28
|
+
weather.scope = 'public'
|
29
|
+
|
30
|
+
sun << question2
|
31
|
+
sun << sunriseset_ref
|
32
|
+
sun << Srgs::Tag.new("out.rise_or_set=rules.riseset;")
|
33
|
+
sun << days_ref
|
34
|
+
sun << Srgs::Tag.new("out.day=rules.days;")
|
35
|
+
sun.scope = 'public'
|
36
|
+
|
37
|
+
top_level = Srgs::Rule.new("toplevel");
|
38
|
+
top_level_choices = Srgs::OneOf.new
|
39
|
+
item1 = Srgs::Item.new Srgs::RuleRef.new(weather)
|
40
|
+
item2 = Srgs::Item.new Srgs::RuleRef.new(sun)
|
41
|
+
top_level_choices << item1
|
42
|
+
top_level_choices << item2
|
43
|
+
top_level << top_level_choices
|
44
|
+
|
45
|
+
grammar << top_level << weather << sun << days << sunriseset
|
46
|
+
|
47
|
+
grammar.root = "toplevel"
|
48
|
+
|
49
|
+
puts Srgs.build(grammar)
|
data/lib/srgs/version.rb
CHANGED
data/lib/srgs.rb
CHANGED
@@ -1,5 +1,16 @@
|
|
1
|
-
require
|
1
|
+
require 'srgs/version'
|
2
|
+
require 'srgs/elements/grammar'
|
3
|
+
require 'srgs/elements/item'
|
4
|
+
require 'srgs/elements/example'
|
5
|
+
require 'srgs/elements/lexicon'
|
6
|
+
require 'srgs/elements/meta'
|
7
|
+
require 'srgs/elements/metadata'
|
8
|
+
require 'srgs/elements/one_of'
|
9
|
+
require 'srgs/elements/rule'
|
10
|
+
require 'srgs/elements/rule_ref'
|
11
|
+
require 'srgs/elements/tag'
|
12
|
+
require 'srgs/elements/token'
|
13
|
+
require 'srgs/grammar_builder'
|
2
14
|
|
3
15
|
module Srgs
|
4
|
-
|
5
|
-
end
|
16
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: srgs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kristenmills
|
@@ -65,6 +65,19 @@ files:
|
|
65
65
|
- README.md
|
66
66
|
- Rakefile
|
67
67
|
- lib/srgs.rb
|
68
|
+
- lib/srgs/elements/example.rb
|
69
|
+
- lib/srgs/elements/grammar.rb
|
70
|
+
- lib/srgs/elements/item.rb
|
71
|
+
- lib/srgs/elements/lexicon.rb
|
72
|
+
- lib/srgs/elements/meta.rb
|
73
|
+
- lib/srgs/elements/metadata.rb
|
74
|
+
- lib/srgs/elements/one_of.rb
|
75
|
+
- lib/srgs/elements/rule.rb
|
76
|
+
- lib/srgs/elements/rule_ref.rb
|
77
|
+
- lib/srgs/elements/tag.rb
|
78
|
+
- lib/srgs/elements/token.rb
|
79
|
+
- lib/srgs/grammar_builder.rb
|
80
|
+
- lib/srgs/test.rb
|
68
81
|
- lib/srgs/version.rb
|
69
82
|
- srgs.gemspec
|
70
83
|
homepage: https://github.com/kristenmills/srgs
|