yghern / pwp-capstones

0 stars 0 forks source link

Account for final blank "sentences" #1

Open karl-project-review opened 6 years ago

karl-project-review commented 6 years ago

For reference, here is your get_average_sentence_length() function:

def get_average_sentence_length(text):
    text = text.replace('!', '.')
    text = text.replace('?', '.')
    sentences_in_text = text.split('.')
    word_count_list = []
    for sentence in sentences_in_text:
        word_count = 0
        for word in sentence.split():
            word_count += 1
        word_count_list.append(word_count)
    return sum(word_count_list)/len(sentences_in_text)

Note that when we split on the final period in each text sample, a blank "sentence" will be added to the end of sentences_in_text, and this "sentence" of length zero will throw off our calculations. We can account for this by modifying the 'sentences_in_text = text.split('.')' line to omit the final 'sentence' in the split using list notation, like so:

sentences_in_text = text.split('.')[:-1]
yghern commented 6 years ago

OK fixed, thanks for pointing that out