# main.py
# Contains dictionaries that map English letters to Braille.
letters = {
'a': chr(10241), 'b': chr(10243), 'c': chr(10249), 'd': chr(10265),
'e': chr(10257), 'f': chr(10251), 'g': chr(10267), 'h': chr(10259),
'i': chr(10250), 'j': chr(10266), 'k': chr(10245), 'l': chr(10247),
'm': chr(10253), 'n': chr(10269), 'o': chr(10261), 'p': chr(10255),
'q': chr(10271), 'r': chr(10263), 's': chr(10254), 't': chr(10270),
'u': chr(10277), 'v': chr(10279), 'w': chr(10298), 'x': chr(10285),
'y': chr(10301), 'z': chr(10293)
}
contractions = {
'but': chr(10243), 'can': chr(10249), 'do': chr(10265), 'every': chr(10257),
'from': chr(10251), 'go': chr(10267), 'have': chr(10259), 'just': chr(10266),
'knowledge': chr(10280), 'like': chr(10296), 'more': chr(10253), 'not': chr(10269),
'people': chr(10255), 'quite': chr(10271), 'rather': chr(10263), 'so': chr(10254),
'that': chr(10270), 'us': chr(10277), 'very': chr(10279), 'it': chr(10285),
'you': chr(10301), 'as': chr(10293), 'and': chr(10287), 'for': chr(10303),
'of': chr(10295), 'the': chr(10286), 'with': chr(10302), 'will': chr(10298),
'his': chr(10278), 'in': chr(10260), 'was': chr(10292), 'to': chr(10262)
}
punctuation = {
',': chr(10242), ';': chr(10246), ':': chr(10258), '.': chr(10290),
'!': chr(10262), '(': chr(10294), ')': chr(10294), '“': chr(10278),
'”': chr(10292), '?': chr(10278), '/': chr(10252), '#': chr(10300),
'\'': chr(10244), '…': chr(10290) + chr(10290) + chr(10290), '’': chr(10244),
'': chr(10276), '-': chr(10276), '‐': chr(10276), '‑': chr(10276),
'‒': chr(10276), '–': chr(10276), '—': chr(10276), '―': chr(10276)
}
numbers = {
'1': chr(10241), '2': chr(10243), '3': chr(10249), '4': chr(10265),
'5': chr(10257), '6': chr(10251), '7': chr(10267), '8': chr(10259),
'9': chr(10250), '0': chr(10266)
}
# Contains dictionaries that map Braille to English letters.
braille_to_alpha = {
chr(10241): 'a', chr(10243): 'b', chr(10249): 'c', chr(10265): 'd',
chr(10257): 'e', chr(10251): 'f', chr(10267): 'g', chr(10259): 'h',
chr(10250): 'i', chr(10266): 'j', chr(10245): 'k', chr(10247): 'l',
chr(10253): 'm', chr(10269): 'n', chr(10261): 'o', chr(10255): 'p',
chr(10271): 'q', chr(10263): 'r', chr(10254): 's', chr(10270): 't',
chr(10277): 'u', chr(10279): 'v', chr(10298): 'w', chr(10285): 'x',
chr(10301): 'y', chr(10293): 'z'
}
braille_contractions = {
chr(10243): 'but', chr(10249): 'can', chr(10265): 'do', chr(10257): 'every',
chr(10251): 'from', chr(10267): 'go', chr(10259): 'have', chr(10266): 'just',
chr(10280): 'knowledge', chr(10296): 'like', chr(10253): 'more', chr(10269): 'not',
chr(10255): 'people', chr(10271): 'quite', chr(10263): 'rather', chr(10254): 'so',
chr(10270): 'that', chr(10277): 'us', chr(10279): 'very', chr(10285): 'it',
chr(10301): 'you', chr(10293): 'as', chr(10287): 'and', chr(10303): 'for',
chr(10295): 'of', chr(10286): 'the', chr(10302): 'with', chr(10298): 'will',
chr(10278): 'his', chr(10260): 'in', chr(10292): 'was', chr(10262): 'to'
}
braille_punctuation = {
chr(10242): ',', chr(10246): ';', chr(10258): ':', chr(10290): '.',
chr(10262): '!', chr(10294): '()', chr(10278): '“', chr(10292): '”',
chr(10252): '/', chr(10300): '#', chr(10244): '\'', chr(10276): '-'
}
braille_numbers = {
chr(10241): '1', chr(10243): '2', chr(10249): '3', chr(10265): '4',
chr(10257): '5', chr(10251): '6', chr(10267): '7', chr(10259): '8',
chr(10250): '9', chr(10266): '0'
}
# Function to convert a character to Braille
def char_to_braille(c):
if c in contractions:
return contractions[c]
elif c in letters:
return letters[c]
elif c in punctuation:
return punctuation[c]
elif c in numbers:
return numbers[c]
return ''
# Function to translate text to Braille
def translate_to_braille(text):
return ''.join(char_to_braille(c) for c in text)
# Function to translate Braille to text
def braille_to_text(braille):
translated_text = ''
for char in braille:
if char in braille_contractions:
translated_text += braille_contractions[char] + ' '
elif char in braille_to_alpha:
translated_text += braille_to_alpha[char]
elif char in braille_punctuation:
translated_text += braille_punctuation[char]
elif char in braille_numbers:
translated_text += braille_numbers[char]
else:
translated_text += '?' # Use '?' for unknown characters
return translated_text.strip()
# Function to print the translation map in alphabetical order
def print_translation_map():
print("Translation Map:")
print("Letters to Braille:")
for letter in sorted(letters.keys()):
print(f"{letter} -> {letters[letter]}")
print("\nBraille to Letters:")
for braille in sorted(braille_to_alpha.keys()):
print(f"{braille} -> {braille_to_alpha[braille]}")
print("\nContractions:")
for contraction in sorted(contractions.keys()):
print(f"{contraction} -> {contractions[contraction]}")
# Function to display help information
def display_help():
print("Help Menu:")
print("1. Translate text to Braille: Enter plain text to convert it to Braille.")
print("2. Translate Braille to text: Enter Braille characters to convert them to plain text.")
print("3. Print translation map: Displays the mapping of letters and Braille characters.")
print("4. Exit: Exits the program.")
print("5. Help: Displays this help menu.")
# Function to validate text input
def is_valid_text(text):
# Check if the text contains only valid characters (letters, spaces, and punctuation)
valid_chars = set(letters.keys()) | set(punctuation.keys()) | {' '}
return all(c in valid_chars for c in text)
# Function to validate Braille input
def is_valid_braille(braille):
# Check if the Braille input contains only valid Braille characters
valid_braille_chars = set(braille_to_alpha.keys())
return all(char in valid_braille_chars for char in braille)
def main():
print("Braille Translator")
print("Type 'help' for assistance.")
while True:
print("\nOptions:")
print("1. Translate text to Braille")
print("2. Translate Braille to text")
print("3. Print translation map")
print("4. Exit")
print("5. Help")
choice = input("Enter your choice (1-5): ")
if choice == '1':
text = input("Enter text to translate to Braille: ").strip()
if not is_valid_text(text):
print("Invalid input. Please enter only letters, spaces, and punctuation.")
continue
braille_output = translate_to_braille(text)
print(f"Braille Translation: {braille_output}")
elif choice == '2':
braille = input("Enter Braille to translate to text: ").strip()
if not is_valid_braille(braille):
print("Invalid Braille input. Please enter only valid Braille characters.")
continue
translated_text = braille_to_text(braille)
print(f"Translated Text: {translated_text}")
elif choice == '3':
print_translation_map()
elif choice == '4':
print("Exiting...")
break
elif choice == '5':
display_help()
else:
print("Invalid choice. Please enter a number between 1 and 5.")
continue
# Ask the user if they want to perform another action
repeat_choice = input("Would you like to perform another action? (yes/no): ").strip().lower()
if repeat_choice not in ['yes', 'y']:
print("Exiting...")
break
if __name__ == "__main__":
main()Loading
esp32-s3-devkitc-1
esp32-s3-devkitc-1