Pig Latin Converter

How the Pig Latin Converter Works

Our Pig Latin converter comes in two versions: a web-based version using PyScript and a standalone Python script. Here's an overview of how each version processes the text:

Web Version (PyScript)

1. The user enters a word or sentence into the input field.
2. Once the "Convert" button is clicked, PyScript reads the input.
3. The program checks if the input is valid.
4. If the word starts with a vowel, "way" is appended to the end.
5. If the word starts with a consonant, the consonant(s) are moved to the end, and "ay" is added.
6. The result is displayed on the page, and the user can download the physical Python script.

Physical Python Script

1. Start the program.
2. Prompt user for input choice: "Read from a file or input directly? (file/terminal)"
3. If file:
  Read sentences from pig_latin_input_output.txt.
  Process each sentence and write results back to the file.
  Display completion message.
4. If terminal:
  Loop until exit:
    Prompt user: "Enter a word or sentence (or type 'exit' to quit):"
    If exit: Display exit message and break loop.
    If valid input: Convert to Pig Latin and display result.
    If empty input: Display "Please enter a valid word or sentence."
5. If invalid choice: Display "Invalid choice. Please type 'file' or 'terminal'."
6. End.

# Function to convert a single word to Pig Latin, handling punctuation and capitalization def pig_latin(word): vowels = 'aeiouAEIOU' punctuation = '' result = '' # Check for punctuation at the end of the word if not word[-1].isalpha(): punctuation = word[-1] word = word[:-1] # Check if word starts with a vowel if word and word[0] in vowels: result = word + 'way' else: # Move consonants to the end until the first vowel for i, letter in enumerate(word): if letter in vowels: result = word[i:] + word[:i] + 'ay' break if not result: # If no vowels, just add 'ay' to the end result = word + 'ay' # Capitalize if the original word was capitalized if word[0].isupper(): result = result.capitalize() return result + punctuation # Function to convert a sentence to Pig Latin def convert_sentence_to_pig_latin(sentence): words = sentence.split() pig_latin_words = [pig_latin(word) for word in words] return ' '.join(pig_latin_words) # Function to handle form input and update result def convertToPigLatin(event=None): sentence = Element("sentenceInput").element.value if sentence: result = convert_sentence_to_pig_latin(sentence) Element("result").element.innerHTML = f"{result}" else: Element("result").element.innerHTML = "Please enter a valid sentence." # Attach the function to the button without the need for proxy Element("convertButton").element.onclick = convertToPigLatin