I am trying to get what a person says in Arabic and then print that to the terminal.
When I try to speak to the mic, any Arabic word I say gets printed as ????
with the number of question marks depending on the number of letters of what I said. I added the print(get_display(arabic_reshaper.reshape("مرحبا")))
to see if I can print Arabic characters in general and this is what shows in my terminal:
Listening...
Error: 'charmap' codec can't encode characters in position 0-4: character maps to <undefined>
Say that again...
I have already set my settings of the text editor to be of UTF-8. This is my code:
import speech_recognition as sr
import arabic_reshaper
import sys
from bidi.algorithm import get_display
def command():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 0.6
audio = r.listen(source)
try:
print(get_display(arabic_reshaper.reshape("مرحبا")))
ask = r.recognize_google(audio, language='ar-SA')
reshaped_ask = arabic_reshaper.reshape(ask)
bidi_text = get_display(reshaped_ask)
try:
if sys.stdout.encoding.lower() == 'utf-8':
print(bidi_text)
else:
print(bidi_text.encode(sys.stdout.encoding, errors='replace').decode(sys.stdout.encoding))
except UnicodeEncodeError:
print(bidi_text.encode(sys.stdout.encoding, errors='replace').decode(sys.stdout.encoding))
except Exception as e:
print("Error:", str(e))
print("Say that again...")
return ""
return ask
command()
When I try to run the following code:
reshaped_text = arabic_reshaper.reshape("مرحبا")
bidi_text = get_display(reshaped_text)
print(bidi_text)
I get مرحبا
printed from left to right and not right to left.
2
Answers
It doesn’t work because your terminal don’t support it. We can print those characters but they appear disjointly. To work with Arabic letters, I will suggest you to use python-eel or python-electron. These packages allow you to use html, css and js for front end of a GUI application so you can display the Arabic letters.
Vscode uses the system terminal as an integrated terminal, so the terminal character display may be related to the system environment language, vscode display language, the set terminal language, terminal code page and many other things. You may have to tweak each one until you find the right configuration to display the characters you want.
You can also install the Code Runner extension, and then select
Run Code
to execute the script, so the result will be displayed in the OUTPUT panel.You can also configure the
console
in launch.json to beexternalTerminal
, and then useRun --> Run Without Debugging
, so that the result will be displayed on the external terminal.