import re
class PigLatinConverter:
"""
A robust Pig Latin converter with proper handling of:
- Vowel and consonant-starting words
- Consonant clusters (including 'qu')
- Capitalization preservation
- Punctuation handling
"""
VOWELS = set('aeiouAEIOU')
def __init__(self):
pass
def is_vowel(self, char: str) -> bool:
"""Check if a character is a vowel."""
return char in self.VOWELS
def find_consonant_cluster(self, word: str) -> str:
"""
Find the consonant cluster at the start of a word.
Handles special case of 'qu' as a single unit.
"""
cluster = ""
for i, char in enumerate(word):
if self.is_vowel(char):
break
cluster += char
# Handle 'qu' case - treat as consonant unit
if char.lower() == 'u' and i > 0 and word[i-1].lower() == 'q':
pass # 'u' after 'q' stays in cluster
elif char.lower() == 'q' and i < len(word) - 1 and word[i+1].lower() == 'u':
pass # Will capture 'u' in next iteration
# Special handling for 'qu'
if cluster.lower().endswith('q') and len(word) > len(cluster):
if word[len(cluster)].lower() == 'u':
cluster += word[len(cluster)]
return cluster
def convert_word(self, word: str) -> str:
"""
Convert a single word to Pig Latin.
Preserves capitalization and punctuation.
"""
if not word:
return word
# Separate leading/trailing punctuation
leading_punct = ""
trailing_punct = ""
clean_word = word
# Extract leading punctuation
match = re.match(r'^([^a-zA-Z]*)([a-zA-Z].*)', clean_word)
if match:
leading_punct = match.group(1)
clean_word = match.group(2)
# Extract trailing punctuation
match = re.match(r'(.*?[a-zA-Z])([^a-zA-Z]*)$', clean_word)
if match:
clean_word = match.group(1)
trailing_punct = match.group(2)
if not clean_word:
return word
# Track capitalization
was_capitalized = clean_word[0].isupper()
was_all_caps = clean_word.isupper()
word_lower = clean_word.lower()
# Convert the word
if self.is_vowel(word_lower[0]):
# Vowel-starting word: add "way"
converted = word_lower + "way"
else:
# Consonant-starting word: move cluster to end, add "ay"
cluster = self.find_consonant_cluster(word_lower)
if cluster:
converted = word_lower[len(cluster):] + cluster + "ay"
else:
converted = word_lower + "way"
# Restore capitalization
if was_all_caps:
converted = converted.upper()
elif was_capitalized:
converted = converted.capitalize()
return leading_punct + converted + trailing_punct
def convert_sentence(self, sentence: str) -> str:
"""Convert an entire sentence to Pig Latin."""
# Split into words while preserving structure
words = sentence.split()
converted_words = [self.convert_word(word) for word in words]
return " ".join(converted_words)
def main():
"""Main function demonstrating the Pig Latin converter."""
# Create converter instance
converter = PigLatinConverter()
# Input sentence
sentence = "I am a computer programmer"
print("=" * 60)
print("PIG LATIN CONVERTER")
print("=" * 60)
print(f"\nOriginal Sentence: {sentence}")
print("-" * 60)
# Show word-by-word conversion
print("\nWord-by-Word Analysis:")
print("-" * 40)
words = sentence.split()
for word in words:
converted = converter.convert_word(word)
print(f" {word:15} → {converted}")
# Full conversion
result = converter.convert_sentence(sentence)
print("-" * 60)
print(f"\nFinal Output: {result}")
print("=" * 60)
# Additional test cases
print("\n\nAdditional Test Cases:")
print("=" * 60)
test_cases = [
"Hello World",
"The quick brown fox",
"I love programming",
"Apple and orange",
"Queen of England",
"String manipulation"
]
for test in test_cases:
print(f" {test:30} → {converter.convert_sentence(test)}")
if __name__ == "__main__":
main()