Skip to content

Commit

Permalink
Added Braille translator project
Browse files Browse the repository at this point in the history
  • Loading branch information
torismorgan committed Oct 2, 2024
1 parent 51e2c64 commit 2a9a4c0
Showing 1 changed file with 43 additions and 42 deletions.
85 changes: 43 additions & 42 deletions python/translator.py
Original file line number Diff line number Diff line change
@@ -1,65 +1,66 @@
# Step 1: Create a dictionary (lookup table) to map English letters, numbers, and punctuation to their Braille equivalents.
# Braille dictionary for letters, numbers, and punctuation
braille_dict = {
'a': '0.....', 'b': '0.0...', 'c': '00....', 'd': '00.0..', 'e': '0..0..',
'f': '000...', 'g': '0000..', 'h': '0.00..', 'i': '.00...', 'j': '.000..',
'k': '0...0.', 'l': '0.0.0.', 'm': '00..0.', 'n': '00.00.', 'o': '0..00.',
'p': '000.0.', 'q': '00000.', 'r': '0.000.', 's': '.00.0.', 't': '.0000.',
'u': '0...00', 'v': '0.0.00', 'w': '.000.0', 'x': '00..00', 'y': '00.000',
'z': '0..000', '1': '0.....', '2': '0.0...', '3': '00....', '4': '00.0..',
'5': '0..0..', '6': '000...', '7': '0000..', '8': '0.00..', '9': '.00...',
'0': '.000..', ' ': '......', '.': '..00.0', ',': '..0...', '?': '..0.00',
'!': '..000.', "'": '....0.', '-': '....00', ':': '..00..', ';': '..0.0.'
'z': '0..000',
'1': '0.....', '2': '0.0...', '3': '00....', '4': '00.0..', '5': '0..0..',
'6': '000...', '7': '0000..', '8': '0.00..', '9': '.00...', '0': '.000..',
' ': '......', '.': '..00.0', ',': '..0...', '?': '..0.00', '!': '..000.',
"'": '....0.', '-': '....00', ':': '..00..', ';': '..0.0.', '#': '.000..'
}

# Step 2: Create the inverse dictionary (to go from Braille to English)
# We are reversing the braille_dict here, so Braille symbols become keys and English letters become values.
# Reverse dictionary for Braille-to-English translation
inverse_braille_dict = {v: k for k, v in braille_dict.items()}

# Step 3: Function to convert English text to Braille
def translate_to_braille(text):
"""This function takes English text and converts it to Braille."""
result = [] # Create an empty list to store the Braille output
result = []
for char in text:
if char.isupper(): # If the letter is uppercase, handle it by first adding a capitalization indicator.
result.append('.....0') # "Capital follows" Braille symbol
result.append(braille_dict[char.lower()]) # Then convert the lowercase version of the letter to Braille
if char.isupper():
result.append('.....0')
result.append(braille_dict[char.lower()])
else:
result.append(braille_dict[char]) # Convert the lowercase letter directly
return ' '.join(result) # Join the Braille symbols with spaces and return them as a string
result.append(braille_dict[char])
return ' '.join(result)

# Step 4: Function to convert Braille to English
def translate_to_english(braille):
"""This function takes Braille and converts it back to English text."""
result = [] # Create an empty list to store the English output
capitalize_next = False # This flag will tell us if the next letter should be capitalized
for symbol in braille.split(' '): # Split the input by spaces to get each Braille symbol
if symbol == '.....0': # Check if it's the capitalization indicator
capitalize_next = True # If it is, set the flag to True
result = []
capitalize_next = False
number_mode = False

for symbol in braille.split(' '):
if symbol == '.000..':
number_mode = True
elif symbol == '.....0':
capitalize_next = True
else:
char = inverse_braille_dict.get(symbol, '') # Get the corresponding English letter for the Braille symbol
if capitalize_next: # If the flag is True, capitalize the letter
char = char.upper() # Capitalize the letter
capitalize_next = False # Reset the flag
result.append(char) # Add the letter to the result list
return ''.join(result) # Join the English letters into a string and return it
if number_mode:
char = inverse_braille_dict.get(symbol, '')
if char.isdigit():
result.append(char)
number_mode = False
else:
char = inverse_braille_dict.get(symbol, '')
if capitalize_next:
char = char.upper()
capitalize_next = False
result.append(char)

# Step 5: Main function to handle input and decide which translation to perform
def main():
"""Main function that handles user input and determines whether to translate to Braille or English."""
import sys # Import the sys module to access command-line arguments
if len(sys.argv) != 2: # Check if the user provided exactly one input (the text or Braille to translate)
print("Usage: python translator.py <text or braille>") # If not, print instructions
return # Exit the function if the input is incorrect
return ''.join(result)

input_text = sys.argv[1] # Get the user input (from the command line)
def main():
import sys
if len(sys.argv) != 2:
print("Usage: python translator.py <text or braille>")
return

# Step 6: Determine if the input is Braille or English
# If it contains '0' or '.', we assume it's Braille. Otherwise, it's English.
input_text = sys.argv[1]
if '0' in input_text or '.' in input_text:
print(translate_to_english(input_text)) # If it's Braille, translate it to English and print the result
print(translate_to_english(input_text))
else:
print(translate_to_braille(input_text)) # If it's English, translate it to Braille and print the result
print(translate_to_braille(input_text))

# Step 7: Run the main function when the script is executed
if __name__ == '__main__':
main() # Call the main function when the program is run
main()

0 comments on commit 2a9a4c0

Please sign in to comment.