Advertisement
Guest User

Untitled

a guest
Mar 28th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1.  
  2.  
  3. def translate(phrase):
  4. r = ""
  5. while len(phrase)>0:
  6. if phrase.find(" ")!= -1:
  7. firstWord = phrase[:phrase.find(" ")]
  8. #cuts off the firstWord
  9. phrase = phrase[phrase.find(" ")+1:]
  10. r += pigLatinWord(firstWord) + " "
  11. else:
  12. r += pigLatinWord(phrase)
  13. phrase = ""
  14. return r
  15.  
  16. def isVowel(char):
  17. return char in "AEIOUaeiou"
  18. def isConsonant(char):
  19. return not isVowel(char)
  20. def isPunc(char):
  21. return not str.isalpha(char)
  22. def iscaseY(char):
  23. return (char.find('y') != -1 or char.find('Y') != -1)
  24. def isHyphen(char): # is there a hypen in the word?
  25. return (char.find('-') != -1 )
  26. def isQu(char): # is 'qu' or 'Qu' the first 2 letters?
  27. return char == "qu" or char == "Qu"
  28.  
  29. def hypenchar(word): #seperates the words into two words
  30. first = word[:word.find('-')]
  31. second = word[word.find('-')+1:]
  32. return pigLatinWord(first) + "-" + pigLatinWord(second) #pigLatins with '-'
  33.  
  34. def yvowel(word):
  35. addToEnd=""
  36. front=""
  37. while word[0]!= "y" and word[0] != "Y"and not isVowel(word[0]):
  38. addToEnd+= word[0]
  39. word= word[1:]
  40. return word+addToEnd+ "ay"
  41.  
  42. def pigLatinWord(word):
  43. r = ""
  44. addToEnd = ""
  45. addToStart = ""
  46.  
  47. if isPunc(word[0]):
  48. p = 0
  49. while p<len(word) and isPunc(word[p]):
  50. p+=1
  51. addToStart = word[:p]
  52. word = word[p:]
  53. #print "addToStart:", addToStart
  54. #print "word:", word
  55.  
  56. #if iscaseY(word) and len(word) > 2:
  57. #return yvowel(word)
  58. #if iscaseY(word) and len(word) ==2:
  59. #return word[-1] + word[0] + "ay"
  60.  
  61. if isPunc(word[-1]):
  62. j = len(word)-1
  63. while j>=0 and isPunc(word[j]):
  64. j-=1
  65. addToEnd = word[j+1:]
  66. word = word[:j+1]
  67. #print "addToEnd:", addToEnd
  68. #print "word:", word
  69.  
  70. if isHyphen(word): #if there is a hyphen...
  71. r = hypenchar(word) #...do the hyphen related function
  72. #print "testword:", word
  73.  
  74. if isVowel(word[0]):
  75. r = word + "yay"
  76. else: #isConsonant
  77. i = 0
  78. while i<len(word) and isConsonant(word[i]):
  79. i+=1
  80. if isQu(word[:2]): #if 'qu' or 'Qu' is the prefix...
  81. i = 2 #... make first 2 letters move in following function
  82. r = word[i:]+word[:i]+"ay"
  83.  
  84. if word[0].isupper():
  85. r = r.capitalize()
  86. return addToStart + r + addToEnd
  87.  
  88.  
  89. print pigLatinWord("my")
  90. print pigLatinWord("fly")
  91.  
  92. print "-------Word-------"
  93. print "[Original]:", "Strong"
  94. print "[Pig Latin]:", pigLatinWord("Strong")
  95. print "[Original]:", "Oil"
  96. print "[Pig Latin]:", pigLatinWord("Oil")
  97. print "[Original]:", "Butter-fly"
  98. print "[Pig Latin]:", pigLatinWord("Butter-fly")
  99. print "[Original]:", "Stray"
  100. print "[Pig Latin]:", pigLatinWord("Stray")
  101. print "[Original]:", "Yelled"
  102. print "[Pig Latin]:", pigLatinWord("Yelled")
  103.  
  104. print "------Sentence-------"
  105. print "[Original]:", "\"A green salad,\" said Arthur emphatically."
  106. print "[Pig Latin]:", translate("\"A green salad,\" said Arthur emphatically.")
  107. print "[Original]:", "He yelled: \"What?\""
  108. print "[Pig Latin]:", translate("He yelled: \"What?\"")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement