Skip to content

Commit

Permalink
Cope with parsing NLSML documents with no namespaces
Browse files Browse the repository at this point in the history
Many ASR vendors think this is acceptable. I'm too tired to educate them.
  • Loading branch information
benlangfeld committed Mar 2, 2013
1 parent 2392871 commit 1fbd7cf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/ruby_speech/nlsml/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ def ==(other)

def input_hash_for_interpretation(interpretation)
input_element = interpretation.at_xpath 'ns:input', 'ns' => NLSML_NAMESPACE
input_element ||= interpretation.at_xpath 'input'
{ content: input_element.content }.tap do |h|
h[:mode] = input_element['mode'].to_sym if input_element['mode']
end
end

def instance_hash_for_interpretation(interpretation)
instance_element = interpretation.at_xpath 'xf:instance', 'xf' => XFORMS_NAMESPACE
instance_element ||= interpretation.at_xpath 'instance'
return unless instance_element
element_children_key_value instance_element
end
Expand Down Expand Up @@ -76,7 +78,9 @@ def result
end

def interpretation_nodes
result.xpath('ns:interpretation', 'ns' => NLSML_NAMESPACE).sort_by { |int| -int[:confidence].to_i }
nodes = result.xpath 'ns:interpretation', 'ns' => NLSML_NAMESPACE
nodes += result.xpath 'interpretation'
nodes.sort_by { |int| -int[:confidence].to_i }
end
end
end
Expand Down
38 changes: 38 additions & 0 deletions spec/ruby_speech/nlsml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,43 @@
its(:interpretations) { should == expected_interpretations }
its(:best_interpretation) { should == expected_best_interpretation }
end

context "with no namespaces (because some vendors think this is ok" do
let :example_document do
'''
<result grammar="http://flight">
<interpretation confidence="60">
<input mode="speech">I want to go to Pittsburgh</input>
<model>
<group name="airline">
<string name="to_city"/>
</group>
</model>
<instance>
<airline>
<to_city>Pittsburgh</to_city>
</airline>
</instance>
</interpretation>
<interpretation confidence="40">
<input>I want to go to Stockholm</input>
<model>
<group name="airline">
<string name="to_city"/>
</group>
</model>
<instance>
<airline>
<to_city>Stockholm</to_city>
</airline>
</instance>
</interpretation>
</result>
'''
end

its(:interpretations) { should == expected_interpretations }
its(:best_interpretation) { should == expected_best_interpretation }
end
end
end

0 comments on commit 1fbd7cf

Please sign in to comment.