Permalink
Browse files

Handle interpretations with multiple instances

  • Loading branch information...
1 parent f409567 commit f73a6c8d9a169992f37a5d27327e5486cd28c8fa @benlangfeld benlangfeld committed Nov 22, 2012
Showing with 69 additions and 10 deletions.
  1. +19 −6 lib/ruby_speech/nlsml/document.rb
  2. +50 −4 spec/ruby_speech/nlsml_spec.rb
@@ -41,11 +41,23 @@ def input_hash_for_interpretation(interpretation)
end
def instance_hash_for_interpretation(interpretation)
- instance_element = interpretation.at_xpath 'xf:instance', 'xf' => XFORMS_NAMESPACE
- instance_element ||= interpretation.at_xpath 'ns:instance', 'ns' => NLSML_NAMESPACE
- instance_element ||= interpretation.at_xpath 'instance'
- return unless instance_element
- element_children_key_value instance_element
+ instances = instance_elements interpretation
+ return unless instances.any?
+ element_children_key_value instances.first
+ end
+
+ def instances_collection_for_interpretation(interpretation)
+ instances = instance_elements interpretation
+ instances.map do |instance|
+ element_children_key_value instance
+ end
+ end
+
+ def instance_elements(interpretation)
+ instance_elements = interpretation.xpath 'xf:instance', 'xf' => XFORMS_NAMESPACE
+ instance_elements += interpretation.xpath 'ns:instance', 'ns' => NLSML_NAMESPACE
+ instance_elements += interpretation.xpath 'instance'
+ instance_elements
end
def element_children_key_value(element)
@@ -70,7 +82,8 @@ def interpretation_hash_for_interpretation(interpretation)
{
confidence: interpretation['confidence'].to_f/100,
input: input_hash_for_interpretation(interpretation),
- instance: instance_hash_for_interpretation(interpretation)
+ instance: instance_hash_for_interpretation(interpretation),
+ instances: instances_collection_for_interpretation(interpretation)
}
end
@@ -93,7 +93,8 @@
{
confidence: 0.6,
input: { mode: :speech, content: 'I want to go to Pittsburgh' },
- instance: { airline: { to_city: 'Pittsburgh' } }
+ instance: { airline: { to_city: 'Pittsburgh' } },
+ instances: [{ airline: { to_city: 'Pittsburgh' } }]
}
end
@@ -103,7 +104,8 @@
{
confidence: 0.4,
input: { content: 'I want to go to Stockholm' },
- instance: { airline: { to_city: 'Stockholm' } }
+ instance: { airline: { to_city: 'Stockholm' } },
+ instances: [{ airline: { to_city: 'Stockholm' } }]
}
]
end
@@ -137,7 +139,8 @@
{
confidence: 0.6,
input: { mode: :speech, content: 'I want to go to Pittsburgh' },
- instance: nil
+ instance: nil,
+ instances: []
}
end
@@ -147,7 +150,8 @@
{
confidence: 0.4,
input: { content: 'I want to go to Stockholm' },
- instance: nil
+ instance: nil,
+ instances: []
}
]
end
@@ -202,6 +206,48 @@
its(:best_interpretation) { should == expected_best_interpretation }
end
+ context "with multiple instances for a single interpretation" do
+ let :example_document do
+ '''
+<result xmlns="http://www.w3c.org/2000/11/nlsml" xmlns:myApp="foo" xmlns:xf="http://www.w3.org/2000/xforms" grammar="http://flight">
+ <interpretation confidence="100">
+ <input mode="speech">I want to go to Boston</input>
+ <xf:model>
+ <xf:group name="airline">
+ <xf:string name="to_city"/>
+ </xf:group>
+ </xf:model>
+ <xf:instance>
+ <myApp:airline>
+ <myApp:to_city>Boston, MA</myApp:to_city>
+ </myApp:airline>
+ </xf:instance>
+ <xf:instance>
+ <myApp:airline>
+ <myApp:to_city>Boston, UK</myApp:to_city>
+ </myApp:airline>
+ </xf:instance>
+ </interpretation>
+</result>
+ '''
+ end
+
+ let(:expected_interpretation) do
+ {
+ confidence: 1.0,
+ input: { content: 'I want to go to Boston', mode: :speech },
+ instance: { airline: { to_city: 'Boston, MA' } },
+ instances: [
+ { airline: { to_city: 'Boston, MA' } },
+ { airline: { to_city: 'Boston, UK' } }
+ ]
+ }
+ end
+
+ its(:interpretations) { should == [expected_interpretation] }
+ its(:best_interpretation) { should == expected_interpretation }
+ end
+
context "with no namespaces (because some vendors think this is ok)" do
let :example_document do
'''

0 comments on commit f73a6c8

Please sign in to comment.