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
limited_vocabulary_creator.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 #Copyright 2015 RAPP
4 
5 #Licensed under the Apache License, Version 2.0 (the "License");
6 #you may not use this file except in compliance with the License.
7 #You may obtain a copy of the License at
8 
9  #http://www.apache.org/licenses/LICENSE-2.0
10 
11 #Unless required by applicable law or agreed to in writing, software
12 #distributed under the License is distributed on an "AS IS" BASIS,
13 #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 #See the License for the specific language governing permissions and
15 #limitations under the License.
16 
17 # Authors: Athanassios Kintsakis, Manos Tsardoulias
18 # contact: akintsakis@issel.ee.auth.gr, etsardou@iti.gr
19 
20 
21 import rospy
22 import sys
23 import tempfile
24 import atexit
25 import shutil
26 import os
27 
28 from global_parameters import GlobalParams
29 
30 from rapp_exceptions import RappError
31 from rapp_utilities import RappUtilities
32 
33 ## @class LimitedVocabularyCreator
34 # @brief Creates temporary configuration files for the input limited vocabulary
36 
37  ## Performs initializations
38  def __init__(self):
39  ## Contains global Sphinx parameters
40  #
41  # (see global_parameters.GlobalParams)
42  self._globalParams = GlobalParams()
43 
44  if not os.path.exists( self._globalParams._tmp_language_models_url ):
45  rospy.logwarn( "Language temporary directory does not exist. Path: " + \
46  self._globalParams._tmp_language_models_url )
47  os.makedirs(self._globalParams._tmp_language_models_url)
48 
49  ## The temporary directory containing the configurations
50  self._languages_package = tempfile.mkdtemp( prefix='tmp_language_pack_', \
51  dir = self._globalParams._tmp_language_models_url )
52 
53  # Delete temp file at termination
54  atexit.register(shutil.rmtree, self._languages_package)
55 
56 
57  ## The default configuration
59  'jar_path' : ".:" + self._globalParams._sphinx_jar_files_url + \
60  "/" + self._globalParams._sphinx_jar_file + ":" \
61  + self._globalParams._sphinx_package_url + "/src", \
62  'configuration_path' : self._globalParams._language_models_url + \
63  "/greekPack/default.config.xml", \
64  'acoustic_model' : self._globalParams._acoustic_models_url, \
65  'grammar_name' : '', \
66  'grammar_folder' : '', \
67  'dictionary' : '', \
68  'language_model' : '', \
69  'grammar_disabled' : True
70  }
71 
72 
73  ## Creates temporary configuration files for the input limited vocabulary
74  #
75  # The 'words' input argument is of the form:
76  # words = {
77  # 'word1_en_chars': [phonem1, phonem2,...],
78  # 'word2_en_chars': [phonem1, phonem2,...]
79  # ...
80  # }
81  #
82  # @param words [list::string] The set of words to be identified
83  # @param grammar [list::string] The Sphinx grammar parameter
84  # @param sentences [list::string] The Sphinx sentences parameter
85  #
86  # @return conf [dictionary] The final configuration
87  # @return status [string] Either the error (string) or True (bool)
88  def createConfigurationFiles(self, words, grammar, sentences):
89  tmp_configuration = self._sphinx_configuration
90 
91  RappUtilities.rapp_print( "Creating configuration files with parameters:" )
92  RappUtilities.rapp_print( "Words: " + str(words) )
93  RappUtilities.rapp_print( "Sentences: " + str(sentences) )
94  RappUtilities.rapp_print( "Grammar: " + str(grammar) )
95  # Create custom dictionary file
96  tmp_configuration['dictionary'] = os.path.join( self._languages_package, \
97  'custom.dict' )
98  custom_dict = open(tmp_configuration['dictionary'], 'w')
99  for word in words:
100  tmp_line = word
101  for phoneme in words[word]:
102  tmp_line += " " + phoneme.replace('-', '').replace('_', '')
103  custom_dict.write(tmp_line + '\n')
104  custom_dict.close()
105 
106  # Check grammar
107  if len(grammar) == 0:
108  tmp_configuration['grammar_disabled'] = True
109  else:
110  tmp_configuration['grammar_disabled'] = False
111  tmp_configuration['grammar_name'] = 'custom'
112  tmp_configuration['grammar_folder'] = self._languages_package
113  custom_grammar = open(os.path.join( tmp_configuration['grammar_folder'], \
114  tmp_configuration['grammar_name']) + '.gram', 'w')
115  custom_grammar.write('#JSGF V1.0;\n')
116  custom_grammar.write("grammar " + tmp_configuration['grammar_name'] + ';\n')
117  counter = 1
118  #custom_grammar.write("public <cmd1>=(")
119  for i in range(0, len(grammar)):
120  gram = grammar[i]
121  # check is all words in grammar exist in words
122  gram_words = gram.split(" ")
123  for gw in gram_words:
124  if gw not in words and gram not in words:
125  raise RappError('Word ' + gw +
126  ' is not in words but exists in grammar')
127 
128  custom_grammar.write("public <cmd" + str(counter) + ">=" + "\"" + gram + "\";\n")
129  #custom_grammar.write("\"" + gram + "\"")
130  #if i == len(grammar) - 1:
131  #custom_grammar.write(");")
132  #else:
133  #custom_grammar.write(" | ")
134  counter += 1
135  custom_grammar.close()
136 
137  # Fix sentences / language model
138  # Check sentences: All words must exist in sentences
139  # Continue with the sentences setup
140  tmp_configuration['language_model'] = os.path.join( self._languages_package, \
141  "sentences.lm.bin" )
142  custom_sentences = open(self._languages_package + '/sentences.txt', 'w')
143  if len(sentences) != 0:
144  for sent in sentences:
145  # Split sentence
146  sent_words = sent.split(" ")
147  for sw in sent_words:
148  if sw not in words and sent not in words:
149  raise RappError('Word ' + sw +
150  ' is not in words but exists in a sentence')
151 
152  custom_sentences.write("<s> " + sent + " </s>\n")
153  else:
154  for word in words:
155  custom_sentences.write("<s> " + word + " </s>\n")
156  custom_sentences.close()
157 
158  # Run script to fix the language model
159  RappUtilities.rapp_print( "Sphinx: Creating language model files\n" )
160  if self._globalParams._allow_sphinx_output == True:
161  bash_file = self._globalParams._language_models_url + "/greekPack/run.sh"
162  bash_command = "cp " + bash_file + " " + self._languages_package + \
163  " && cd " + self._languages_package + " && bash run.sh"
164  else:
165  bash_file = self._globalParams._language_models_url + \
166  "/greekPack/run_silent.sh"
167  bash_command = "cp " + bash_file + " " + self._languages_package + \
168  " && cd " + self._languages_package + " && bash run_silent.sh"
169 
170  os.system(bash_command)
171 
172  return tmp_configuration
Creates temporary configuration files for the input limited vocabulary.
def createConfigurationFiles
Creates temporary configuration files for the input limited vocabulary.