Well the question is simple I want to get the text from a livespeech() object after recognition. Can anyone help me do that? please.
Asked
Active
Viewed 513 times
2 Answers
0
Here's an example from the docs for you which is pretty self explanatory, just use a for
loop to iterate over the results returned by LiveSpeech()
;
import os
from pocketsphinx import LiveSpeech, get_model_path
model_path = get_model_path()
speech = LiveSpeech(
verbose=False,
sampling_rate=16000,
buffer_size=2048,
no_search=False,
full_utt=False,
hmm=os.path.join(model_path, 'en-us'),
lm=os.path.join(model_path, 'en-us.lm.bin'),
dic=os.path.join(model_path, 'cmudict-en-us.dict')
)
for phrase in speech:
print(phrase)
Here's a link to the Github page for reference. Hope it helps.

Nordle
- 2,915
- 3
- 16
- 34
-
okay let me continue the question from here I want to convert the phrase into a string but it gives me an error of typecast. And also thank you for the response really appreciate :) – Learner Dec 05 '18 at 12:50
-
No problem at all. Can you change the `print(phrase)` to `print(type(phrase))` - what's the output? Or paste the full stack error, that would help too. – Nordle Dec 05 '18 at 12:56
-
The Output is:
– Learner Dec 05 '18 at 13:02 -
That's why then, you'll need to access the attribute of the class instance in order to get the text. However you'd need to search the docs for that as I'm not sure under what attribute it is saved.. – Nordle Dec 05 '18 at 13:08
-
Yeah that's where I'm actually stuck I can't find the documentation about the Text attribute. anyways many thanks for your assistance although if you know someone who knows about it please do ask, it will be really helpful. Thanks again – Learner Dec 05 '18 at 14:33
0
Found the solution for anyone wondering, you can get the string from a LiveSpeech object with the method __str__
.
For example phrase.__str__()

Enzo
- 1
- 2
-
1Please don't call dunders manually like that. Just use [`str(phrase)`](https://docs.python.org/3/reference/datamodel.html?highlight=__str__#object.__str__). – ChrisGPT was on strike Aug 07 '22 at 21:55