Debian Codegraph @

plyer/facades/stt.py

raw symbols SCIP
1 '''
2 Speech to Text
3 ==============
4
5 .. versionadded:: 1.4.0
6
7 Speech Recognition facade.
8
9 In order to check that your device supports voice recognition use method
10 `exist`.
11
12 Variable `language` indicates which language will be used to match words from
13 voice.
14
15 Use `start` to start voice recognition immediately and `stop` to stop.
16
17 .. note::
18 Needed permissions for Android: `RECORD_AUDIO` (and `INTERNET` if you want
19 online voice recognition API to be used)
20
21 .. note::
22 On Android platform, after execute `start` method you can hear BEEP!
23 Mute sound in order to disable it.
24
25 .. note::
26 For Android implementation to work there has to be an application with
27 `android.speech.RecognitionService` implementation present in the system.
28 Mostly it's `com.google.android.googlequicksearchbox` or "Google"
29 application (the search bar with the launcher widget).
30
31 Offline Speech Recognition on Android
32 -------------------------------------
33
34 Requires any application that provides an
35 `android.speech.RecognitionService` implementation to the other apps. One of
36 such applications is on a lot of devices preinstalled Google (quick search
37 box).
38
39 The API prefers offline recognition, but should be able to switch to online
40 alternative in case you don't have a language package installed (`INTERNET`
41 permission necessary).
42
43 You can enable offline speech recognition this way (Android 8.1):
44
45 * open the `Settings` app
46 * choose `Language & Input` / `Language & Keyboard` (Samsung might include it
47 in the `General` category)
48 * choose `On-Screen keyboard` or `Voice search`
49 * choose `Google Keyboard`
50 * choose `Offline Speech recognition`
51 * download language package if you don't have one already
52
53 Simple Examples
54 ---------------
55
56 To start listening::
57
58 >>> from plyer import stt
59 >>> stt.start()
60
61 To retrieve partial results while listening::
62
63 >>> assert stt.listening
64 >>> print(stt.partial_results)
65
66 To stop listening::
67
68 >>> stt.stop()
69
70 To retrieve results after the listening stopped::
71
72 >>> print(stt.results)
73 '''
74
75
76 class STT:
77 '''
78 Speech to text facade.
79 '''
80
81 _language = 'en-US'
82 '''
83 Default language in which platform will try to recognize voice.
84 In order to change language pick one from list by using
85 `supported_languages` method.
86 '''
87
88 _supported_languages = [
89 'en-US',
90 'pl-PL'
91 ]
92
93 results = []
94 '''
95 List of sentences found while listening. It may consist of many similar
96 and possible sentences that was recognition program.
97 '''
98
99 errors = []
100 '''
101 List of errors found while listening.
102 '''
103
104 partial_results = []
105 '''
106 List of results found while the listener is still being active.
107 '''
108
109 prefer_offline = True
110 '''
111 Preference whether to use offline language package necessary for
112 each platform dependant implementation or online API.
113 '''
114
115 listening = False
116 '''
117 Current state of listening.
118 '''
119
120 @property
121 def supported_languages(self):
122 '''
123 Return list of supported languages used in recognition.
124 '''
125
126 return self._supported_languages
127
128 @property
129 def language(self):
130 '''
131 Return current language.
132 '''
133
134 return self._language
135
136 @language.setter
137 def language(self, lang):
138 '''
139 Set current language.
140
141 Value can not be set if it's not supported. See `supported_languages`
142 to get what language you can set.
143
144 .. note::
145 We obviously can't check each language, therefore if you find
146 that a specific language is available to you and the only limitation
147 is our check for the internally defined `supported_languages`, feel
148 free to open a pull request for adding your language to the list.
149 '''
150
151 if lang in self.supported_languages:
152 self._language = lang
153
154 # public methods
155 def start(self):
156 '''
157 Start listening.
158 '''
159
160 self.results = []
161 self.partial_results = []
162 self._start()
163 self.listening = True
164
165 def stop(self):
166 '''
167 Stop listening.
168 '''
169
170 self._stop()
171 self.listening = False
172
173 def exist(self):
174 '''
175 Returns a boolean for speech recognition availability.
176 '''
177
178 return self._exist()
179
180 # private methods
181 def _start(self):
182 raise NotImplementedError
183
184 def _stop(self):
185 raise NotImplementedError
186
187 def _exist(self):
188 raise NotImplementedError