Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #! -*- encoding: utf8 -*-
- import re
- """
- 1.- Pig Latin
- Nombre Alumno: Marc Gavilán Gil
- Nombre Alumno: Javier Martinez Bernia
- """
- import sys
- def piglatin_word(word):
- """
- Esta función recibe una palabra en inglés y la traduce a Pig Latin
- :param word: la palabra que se debe pasar a Pig Latin
- :return: la palabra traducida
- """
- # vocales = {'a','e','i','o','u','y'}
- flagPrimeraMayuscula = word[0].isupper()
- flagTodoMayuscula = word.isupper()
- # Comienza por letra
- if word[0].isalpha():
- # Comienza por vocal
- # se agrega “yay” al final de la palabra
- if word[0] in 'aeiouyAEIOUY':
- # todo mayuscula
- if flagTodoMayuscula:
- word = word + 'YAY'
- else:
- word = word + 'yay'
- # Comienza por consonante
- # se mueven todas las consonantes antes de la
- # primera vocal al final y se agrega la sílaba “ay”
- else:
- pos = 0
- # Buscamos la pos de la primera vocal
- for symbol in word:
- if symbol in 'aeiouyAEIOUY':
- break
- pos += 1
- # Movemos consonantes al final
- newWord = word[pos:] + word[:pos]
- newWord = newWord.lower()
- if flagPrimeraMayuscula:
- newWord = newWord[0].upper() + newWord[1:]
- if flagTodoMayuscula:
- newWord = newWord.upper() + 'AY'
- else:
- newWord = newWord + 'ay'
- word = newWord
- return word
- def piglatin_sentence(sentence):
- """
- Esta función recibe una frase en inglés i la traduce a Pig Latin
- :param sentence: la frase que se debe pasar a Pig Latin
- :return: la frase traducida
- """
- er = re.compile("(\w+)(\W*)")
- word_list = []
- for word, puntuation in er.findall(sentence):
- word_list.append(piglatin_word(word) + puntuation)
- new_sentence = ''.join(word_list)
- return new_sentence
- if __name__ == "__main__":
- if len(sys.argv) > 1:
- print(piglatin_sentence(sys.argv[1]))
- else:
- while True:
- s = input('ENGLISH: ')
- if len(s) > 0:
- print(piglatin_sentence(s))
- else:
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement