2

I have to create a python file that prompts the user for a file path to a text document and then convert it into pig Latin and do a line/word 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

I can't figure out why the pig latin is coming out wrong. My teacher said that I need another string.strip("\n") because it is making the words convert wrong but I have no idea where I am supposed to put that.

Also my line counter is broken. It counts but it always says 222 lines.

How can I make it just count the lines with words ?

#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:
            return word + "way"     
        else:
            return 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 + " "   
    return convert

def line_counter(s):
    line_count = 0
    for line in s:
        line_count += 1
    return line_count

def word_counter(line):
    word_count = 0
    list_of_words = line.split()
    word_count += len(list_of_words)
    return 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 "\n\n\n\nTranslation 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 "\n\n\n\n"
Community
  • 1
  • 1
Roxy
  • 29
  • 3

3 Answers3

0

your first problem is here:

def pig_word(string):
    line = string.strip("\n")  #!!!! line is NEVER USED !!!
    for word in string.split(" "): #you want *line*.split here

the second issue is caused by iterating over a string, it goes through every character instead of every line like a file does:

>>> for i in "abcd":
...     print(i)    

a
b
c
d

so in your line_counter instead of doing:

for line in s:
    line_count += 1

you just need to do:

for line in s.split("\n"):
    line_count += 1
Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
0
  1. The first reason why your not getting the output you want is because in your pig_word(string) function, you return the first word in the string when you put that return inside of your for loop. Also, your teacher was talking about taking all the lines into the function, and iterating over each line via str.split('\n'). \n represents the "new-line" character.

You can try something like this to correct that.

def pig_sentence(string):
    lines = []
    for line in string.split('\n'):
        new_string = ""
        for word in line.split(" "):
            first_letter = word[0]
            if first_letter in vowels:
                new_string += word + "way"
            else:
                new_string += word[1:] + first_letter + "ay"
            lines.append(new_string)
    return lines

The Changes Made

  • Initialized a new list lines that we can append to throughout the loops.

  • Iterate over each line in the passed in string.

  • For each line, create a new string new_string.

  • Use your code, but instead of returning we add it to new_string, then append new_string to our list of new lines, lines.

  • Note that this removes the need for two functions. Also note that I renamed pig_word to pig_sentence.


  1. The second error is in your function line_counter(s). You are iterating over each character rather than each line. Here add that str.split('\n') again to get the output you want by splitting the string into a list of lines then iterating over the list.

Here is the modified function:

def line_counter(s):
    line_count = 0
    for _ in s.split('\n'):
        line_count += 1
    return line_count

(Since there is nothing erroneous with your file i.o., I'm just going to use a string literal here for the testing.)


Test

paragraph = """\
Hello world
how are you
pig latin\
"""

lines = line_counter(paragraph)
words = sum([word_counter(line) for line in paragraph.split('\n')])
out = pig_sentence(paragraph)

print(lines, words, out)

The output is what we expect!

3 7 ['elloHay', 'elloHayorldway', 'owhay', 'owhayareway', 'owhayarewayouyay', 'igpay', 'igpayatinlay']
Jason
  • 2,278
  • 2
  • 17
  • 25
  • Thank you so much!! The counters now work but when I added the pig_sentence part to change out my two functions I receive a TypeError: cannot concatenate 'str' and 'list' objects. How do I fix that error? – Roxy Apr 16 '16 at 15:21
  • Which functions do you currently have? – Jason Apr 17 '16 at 03:16
0

You are removing only the space, you need to remove all punctuation as well as end of line characters. Replace

split(" ")

with

split()

Your sentence list is the equivalent of

sentence = 'Hello there.\nMy name is Roxy.\nHow are you?

If you print after split(" ") and split() you will see the difference and you will get the results that you expect.

Additionally, you will get incorrect results because you will have there translated to heretay. you need to loop around so that it comes out as erethay

That is move every consonent to the end before adding 'ay' so that the new word starts with a vowel.

sabbahillel
  • 4,357
  • 1
  • 19
  • 36