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
english_support.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, Aris Thallas, Manos Tsardoulias
18 # contact: akintsakis@issel.ee.auth.gr, aris.thallas@{iti.gr, gmail.com}, etsardou@iti.gr
19 
20 
21 import mmap
22 
23 from language_support import *
24 
25 ## @class EnglishSupport
26 # @brief Allows the creation of configuration files for English Sphinx speech recognition
28 
29  ## Performs initializations
30  def __init__(self):
31 
32  # Initialize LanguageSupport
33  super(EnglishSupport, self).__init__()
34 
35  ## The English dictionary file
36  #
37  # Located in path global_parameters.GlobalParams#_language_models_url +
38  # "/englishPack/cmudict-en-us.dict"
39  self._english_dict_file = None
40  ## The mmap of the English dictionary file contents
42 
43  # Open the generic english dictionary file
44  try:
45  # TODO: Split the rapp_sphinx4_java_libraries package into libraries and
46  # language models
47  english_dictionary = self._globalParams._language_models_url + \
48  "/englishPack/cmudict-en-us.dict"
49  self._english_dict_file = open(english_dictionary, 'r')
50  except IOError:
51  RappUtilities.rapp_print("English dictionary could not be opened!")
52  # Create a mapping of the file
53  self._english_dict_mapping = mmap.mmap(self._english_dict_file.fileno(), 0, \
54  access = mmap.ACCESS_READ)
55 
56  ## Compute the English word phonemes
57  #
58  # @param words [list::string] The set of English words
59  # @return enhanced_words [dictionary] The English word->phonemes mapping
60  def getWordPhonemes(self, words):
61  enhanced_words = {}
62  for word in words:
63  inner_words = []
64  inner_phonemes = []
65  if "-" not in word: # Check for conjoined english words
66  index = self._english_dict_mapping.find("\n" + word + " ")
67  if index == -1:
68  raise RappError("ERROR: Word " + word +\
69  " does not exist in the English Dictionary")
70  else:
71  self._english_dict_file.seek(index + 1)
72  line = self._english_dict_file.readline()
73  line = line[:-1]
74  split_line = line.split(" ")
75  inner_phonemes = split_line[1:]
76 
77  else:
78  inner_words = word.split("-")
79  for in_w in inner_words:
80  index = self._english_dict_mapping.find("\n" + in_w + " ")
81  if index == -1:
82  raise RappError("ERROR: Word " + in_w +\
83  " does not exist in the English Dictionary")
84  else:
85  self._english_dict_file.seek(index + 1) # +1 because of the extra \n
86  line = self._english_dict_file.readline()
87  line = line[:-1] # to erase the \n
88  split_line = line.split(" ")
89 
90  #enhanced_words[split_line[0]] = []
91  for i in range(1, len(split_line)):
92  inner_phonemes.append(split_line[i])
93  enhanced_words[word] = inner_phonemes
94 
95  return enhanced_words
96 
97  ## Computes the Limited English Configuration
98  #
99  # @param words [list::string] The set of words to be identified
100  # @param grammar [list::string] The Sphinx grammar parameter
101  # @param sentences [list::string] The Sphinx sentences parameter
102  #
103  # @return limited_sphinx_configuration [dictionary] The Limited English configuration
104  # @return engEngDict [dictionary] Introduced for code interface compliance
105  def getLimitedVocebularyConfiguration(self, words, grammar, sentences):
106 
107  engEngDict = {}
108  for word in words:
109  engEngDict.update( {word: word} )
110 
111  RappUtilities.rapp_print(words)
112 
113  try:
114  enhanced_words = self.getWordPhonemes( words )
115  except RappError as e:
116  raise RappError(e.value)
117 
118  try:
119  limited_sphinx_configuration= \
120  self._vocabulary.createConfigurationFiles(enhanced_words, grammar, sentences)
121  except RappError as e:
122  raise RappError(e.value)
123 
124  return limited_sphinx_configuration, engEngDict
def getWordPhonemes
Compute the English word phonemes.
_english_dict_mapping
The mmap of the English dictionary file contents.
def getLimitedVocebularyConfiguration
Computes the Limited English Configuration.
Allows the creation of configuration files for Sphinx speech recognition.
Allows the creation of configuration files for English Sphinx speech recognition. ...