Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.40 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #! -*- encoding: utf8 -*-
  3.  
  4. import re
  5.  
  6. """
  7. 1.- Pig Latin
  8.  
  9. Nombre Alumno: Marc Gavilán Gil
  10.  
  11. Nombre Alumno: Javier Martinez Bernia
  12. """
  13.  
  14. import sys
  15.  
  16.  
  17. def piglatin_word(word):
  18.     """
  19.    Esta función recibe una palabra en inglés y la traduce a Pig Latin
  20.  
  21.    :param word: la palabra que se debe pasar a Pig Latin
  22.    :return: la palabra traducida
  23.    """
  24.     # vocales = {'a','e','i','o','u','y'}
  25.     flagPrimeraMayuscula = word[0].isupper()
  26.     flagTodoMayuscula = word.isupper()
  27.    
  28.     # Comienza por letra
  29.     if word[0].isalpha():
  30.         # Comienza por vocal
  31.         # se agrega “yay” al final de la palabra
  32.         if word[0] in 'aeiouyAEIOUY':
  33.             # todo mayuscula
  34.             if flagTodoMayuscula:
  35.                 word = word + 'YAY'
  36.             else:
  37.                 word = word + 'yay'
  38.  
  39.         # Comienza por consonante
  40.         #   se mueven todas las consonantes antes de la
  41.         #   primera vocal al final y se agrega la sílaba “ay”
  42.         else:
  43.             pos = 0
  44.  
  45.             # Buscamos la pos de la primera vocal
  46.             for symbol in word:
  47.                 if symbol in 'aeiouyAEIOUY':
  48.                     break
  49.                 pos += 1
  50.            
  51.             # Movemos consonantes al final
  52.             newWord = word[pos:] + word[:pos]
  53.  
  54.             newWord = newWord.lower()
  55.  
  56.             if flagPrimeraMayuscula:
  57.                 newWord = newWord[0].upper() + newWord[1:]
  58.            
  59.             if flagTodoMayuscula:
  60.                 newWord = newWord.upper() + 'AY'
  61.             else:
  62.                 newWord = newWord + 'ay'
  63.  
  64.             word = newWord
  65.  
  66.     return word
  67.  
  68.  
  69. def piglatin_sentence(sentence):
  70.     """
  71.    Esta función recibe una frase en inglés i la traduce a Pig Latin
  72.  
  73.    :param sentence: la frase que se debe pasar a Pig Latin
  74.    :return: la frase traducida
  75.    """
  76.    
  77.     er = re.compile("(\w+)(\W*)")
  78.  
  79.     word_list = []
  80.     for word, puntuation in er.findall(sentence):
  81.         word_list.append(piglatin_word(word) + puntuation)
  82.     new_sentence = ''.join(word_list)
  83.  
  84.     return  new_sentence
  85.  
  86.  
  87. if __name__ == "__main__":
  88.     if len(sys.argv) > 1:
  89.         print(piglatin_sentence(sys.argv[1]))
  90.     else:
  91.         while True:
  92.             s = input('ENGLISH: ')
  93.             if len(s) > 0:
  94.                 print(piglatin_sentence(s))
  95.             else:
  96.                 break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement