RAPP Platform  v0.6.0
RAPP Platform is a collection of ROS nodes and back-end processes that aim to deliver ready-to-use generic services to robots
 All Classes Namespaces Files Functions Variables Macros
sphinx4_configuration_params.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- encode: utf-8 -*-
3 
4 #Copyright 2015 RAPP
5 
6 #Licensed under the Apache License, Version 2.0 (the "License");
7 #you may not use this file except in compliance with the License.
8 #You may obtain a copy of the License at
9 
10  #http://www.apache.org/licenses/LICENSE-2.0
11 
12 #Unless required by applicable law or agreed to in writing, software
13 #distributed under the License is distributed on an "AS IS" BASIS,
14 #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #See the License for the specific language governing permissions and
16 #limitations under the License.
17 
18 # Authors: Aris Thallas
19 # contact: aris.thallas@iti.gr
20 
21 import yaml
22 import hashlib
23 
24 from global_parameters import GlobalParams
25 
26 from rapp_utilities import RappUtilities
27 
28 ## @class SphinxConfigurationParams
29 # @brief Contains the parameters required for the Sphinx configuration
31 
32  ## @brief Initializes an empty configuration (constructor)
33  def __init__(self, configuration = None):
34 
35  ## Contains global Sphinx parameters
36  #
37  # (see global_parameters.GlobalParams)
38  self._globalParams = GlobalParams()
39 
40  ## The language of the request
41  self._language = 'el'
42  ## The words to be identified
43  self._words = []
44  ## Sphinx grammar attribute
45  self._grammar = []
46  ## Sphinx sentence attribute
47  self._sentences = []
48 
49  if configuration != None:
50  self._readConfigurationYaml( configuration )
51 
52  ## @brief Change attributes to those specified by the request
53  #
54  # @param params [rapp_platform_ros_communications::SpeechDetectionSphinx4Wrapper::SpeechRecognitionSphinx4TotalSrvRequest] The service request
55  def makeEqualToRequest(self, params):
56  self._language = params.language
57  self._words = params.words
58  self._grammar = params.grammar
59  self._sentences = params.sentences
60 
61  ## @brief Change attributes to those specified by the Class instance
62  #
63  # @param params [SphinxConfigurationParams] The class instance
64  def makeEqualToInstance(self, instance):
65  self._language = instance._language
66  self._words = instance._words
67  self._grammar = instance._grammar
68  self._sentences = instance._sentences
69 
70  ## @brief Read the configuration from configuration yaml
71  #
72  # @param confName [string] The name of the requested configuration
73  def _readConfigurationYaml(self, confName):
74  RappUtilities.rapp_print( "Reading preconfiguration: " + confName )
75  yamlStream = open( self._globalParams._sphinx_preconf, 'r' )
76  yamlFile = yaml.load(yamlStream)
77  if confName not in yamlFile['configurations']:
78  RappUtilities.rapp_print('Wrong configuration name provided. Leaving ' + \
79  'default values', 'ERROR')
80  return
81 
82  tempConf = SphinxConfigurationParams()
83 
84  # Read configuration language
85  yamlLang = yamlFile['configurations'][confName]['language']
86  if yamlLang != None:
87  tempConf._language = yamlLang.encode('utf-8')
88  else:
89  RappUtilities.rapp_print('Configuration language not provided. ' + \
90  'Leaving default values', 'ERROR')
91  return
92 
93  # Read configuration words
94  yamlWords = yamlFile['configurations'][confName]['words']
95  if yamlWords != None:
96  tempConf._words = [word.encode('utf-8') for word in yamlWords]
97  else:
98  RappUtilities.rapp_print('Configuration words not provided. ' + \
99  'Leaving default values', 'ERROR')
100  return
101 
102  # Read configuration sentences
103  yamlSent = yamlFile['configurations'][confName]['sentences']
104  if yamlSent != None:
105  tempConf._sentences = [sentence.encode('utf-8') for sentence in yamlSent]
106  else:
107  RappUtilities.rapp_print('Configuration sentences not provided. ' + \
108  'Leaving default values', 'ERROR')
109  return
110 
111  # Read configuration grammar
112  yamlGram = yamlFile['configurations'][confName]['grammar']
113  if yamlGram != None:
114  tempConf._grammar = [grammar.encode('utf-8') for grammar in yamlGram]
115  else:
116  tempConf._grammar = []
117 
118  # Copy temporary values to self
119  self.makeEqualToInstance( tempConf )
120 
121  RappUtilities.rapp_print( 'Language: ' + str(self._language) )
122  RappUtilities.rapp_print( 'Words: ' + str(self._words) )
123  for word in self._words:
124  RappUtilities.rapp_print( 'word: ' + str(word) )
125  RappUtilities.rapp_print( 'Grammar: ' + str(self._grammar))
126  RappUtilities.rapp_print( 'Sentences: ' + str(self._sentences) )
127 
128 
129 
130  ## @brief Checks if a SphinxConfigurationParams instance equals self
131  #
132  # @param params [SphinxConfigurationParams] The class instance
133  # @param status [bool] True if configurations match
134  def equalsRequest(self, params):
135  if ( self._language == params.language and \
136  self._words == params.words and \
137  self._grammar == params.grammar and \
138  self._sentences == params.sentences
139  ):
140  return True
141  else:
142  return False
143 
144  ## @brief Calculates the configuration's sha1 hash
145  #
146  # Hash is used to identify common request configurations for proper
147  # subprocess selection.
148  # (Requests with common requests do not require reconfiguration reducing
149  # computation time)
150  #
151  # @return hexdigest [string] The hash digest containing only hexadecimal digits
152  def getHash(self):
153  hash_object = hashlib.sha1()
154  hash_object.update( self._language )
155  for word in self._words:
156  hash_object.update( word )
157  for gram in self._grammar:
158  hash_object.update( gram )
159  for sent in self._sentences:
160  hash_object.update( sent )
161  return hash_object.hexdigest()
def equalsRequest
Checks if a SphinxConfigurationParams instance equals self.
def makeEqualToInstance
Change attributes to those specified by the Class instance.