I am working on a Pig Latin convertor that prompts the user for a file path and then outputs the converted version with a word/line count.
• A function to generate the pig Latin version of a single word
• A function to print line and word counts to standard output
• Correct pig Latin output with identical formatting as the original text file
• Correct line and word counts
Everything except the pig latin converter is working and I have no idea why its output is wrong. My teacher said it had to do with a missing .strip("n") but I tried adding that and it still outputs the incorrect words.
Example of what it's doing:
Hello there.
My name is Roxy.
How are you?
elloHay here.
Mytay amenay isway oxy.
HowRay areway ou?
yay
This is my code. If you can, please show me what is wrong with the converter part. Thank you so much!
#Step 1: User enters text file.
#Step 2: Pig Latin function rewrites file and saves as .txt.
#Step 3: Tracks how many lines and words it rewrites.
vowels = ("A", "a", "E", "e", "I", "i", "O", "o", "U", "u")
# Functions
def pig_word(string):
line = string.strip("n")
for word in string.split(" "):
first_letter = word[0]
if first_letter in vowels:
retu word + "way"
else:
retu word[1:] + first_letter + "ay"
def pig_sentence(sentence):
word_list = sentence.split(" ")
convert = " "
for word in word_list:
convert = convert + pig_word(word)
convert = convert + " "
retu convert
def line_counter(s):
line_count = 0
for _ in s.split("n"):
line_count += 1
retu line_count
def word_counter(line):
word_count = 0
list_of_words = line.split()
word_count += len(list_of_words)
retu word_count
# File path conversion
text = raw_input("Enter the path of a text file: ")
file_path = open(text, "r")
out_file = open("pig_output.txt", "w")
s = file_path.read()
pig = pig_sentence(s)
out_file.write(pig+" ")
out_file.write("n")
linecount = line_counter(s)
wordcount = word_counter(s)
file_path.close()
out_file.close()
# Results
print "Translation finished and written to pig_output.txt"
print "A total of {} lines were translated successfully.".format(linecount)
print "A total of {} words were translated successfully.".format(wordcount)
print ""
