Question

In: Computer Science

Main Objective: Create a function collect_statistics to count the number of words and mean length of...

Main Objective: Create a function collect_statistics to count the number of words and mean length of words in each sentence

This is my code so far but I am struggling to join the two functions together. I would appreciate the help!

The sentence given as an example is : "Haven't you eaten 8 oranges today?" the mean length of this sentence is 4.5 for reference.

Each sentence needs to be split up if given multiple sentences, assuming each sentence ends with a period.

Thank you!

My code so far:

def word_length_list(text):

    text = text.replace('--',' ')

    for p in string.punctuation + "‘’”“":

        text = text.replace(p,'')

    text = text.lower()

    words = text.split()

    word_length = []

    for i in words:

        count = 0

        for j in i:

            count = count + 1

        word_length.append(count)

    return(word_length)

testing1 = word_length_list("Haven't you eaten 8 oranges today?")

def collect_statistics(pandp):

    

    for i in ["!","?"]:

        if i in pandp:

            pandp = pandp.replace(i,".")

    

    pandp = pandp.split(".")

    pandp = [sentence.split() for sentence in pandp]

    pandp = [len(sentence) for sentence in pandp]

    finalpandp = len(pandp)

    return(finalpandp)

collect_statistics(testing1)

Solutions

Expert Solution

The code with removed error is given below.

The problem was in calling funtion collect_statistics. There parameter passed was testing1 which is not a string, rather the list of frequencies returned by the word_len_list. And we also needed to remove a few incorrect loops.

def word_length_list(text):
    text = text.replace('--',' ')
    for p in "‘’”“":
        text = text.replace(p,'')
    text = text.lower()
    words = text.split()
    word_length = []
    for i in words:
        count = 0
        for j in i:
            count = count + 1
        word_length.append(count)
    return(word_length)
    
testing1 = word_length_list("Haven't you eaten 8 oranges today?")
print(testing1)

def collect_statistics(pandp):
    for i in ["!","?"]:
            pandp = pandp.replace(i,".")
            
    pandp = pandp.split(".")
    pandp = [sentence.split() for sentence in pandp]
    pandp = [len(sentence) for sentence in pandp]
    finalpandp = len(pandp)
    return(finalpandp)
    
res = collect_statistics("Haven't you eaten 8 oranges today?")
print(res)

Related Solutions

** USING MATLAB TO PROGRAM The main objective of this lab is to create a game...
** USING MATLAB TO PROGRAM The main objective of this lab is to create a game that involves betting on the sum of two dice. The player will start out with some initial total amount of money. During each round, the player can bet some money that the sum of the two dice will be equal to a certain number. If the player wins the bet, that player adds the amount of the bet to his or her current total....
In Java: Write a program that will count the number of characters, words, and lines in...
In Java: Write a program that will count the number of characters, words, and lines in a file. Words are separated by whitespace characters. The file name should be passed as a command-line argument, as shown below. c:\exercise>java Exercise12_13 Loan.java File loan.java has 1919 characters 210 words 71 lines c:\exercise> Class Name: Exercise12_13
Using C++ 1. Create a main function in a main.cpp file. The main function should look...
Using C++ 1. Create a main function in a main.cpp file. The main function should look as follows int main() {return 0;} 2. Create an array. 3. Ask user to enter numbers in size of your array. 4. Take the numbers and store them in your array. 5. Go through your array and add all the numbers. 6. Calculate the average of the numbers. 7. Display the numbers, sum and average.
R Programming: create a vector for 1 to 31 and count the number of even and...
R Programming: create a vector for 1 to 31 and count the number of even and odds using ifelse()
(a)Count the number of length n permutations conisting of a single cycle, when written in cycle...
(a)Count the number of length n permutations conisting of a single cycle, when written in cycle notation. (Explain your answer.) (b)How many permutations of length 2n have a cycle of length n + 1? (Explain your answer.)
Posting together because they are related. Python 1. Create a program to count the number of...
Posting together because they are related. Python 1. Create a program to count the number of occurrences of each letter of the alphabet in a text file, and print out the total for each letter (if greater than 0). Ignore numbers, punctuation, and special characters. Make sure to treat capital and lowercase letters as occurrences of the same letter. The text file (sample_text.txt) is attached to the assignment. Example: For the text "My dog's name is Winston." The results would...
A microscope with an objective of focal length
A microscope with an objective of focal length 1.8 mm is used to inspect the tiny features of a computer chip. It is desired to resolve two objects only 400 nm apart. What diameter objective is needed if the microscope is used in air with light of wavelength 550 nm?  
How to count the number of words that only show up once in a text file,...
How to count the number of words that only show up once in a text file, and replace those words with a character '(unique)' using Python? Without list is better.
Exercise 10.11.1: Counting strings over {a, b, c}. infoAbout Count the number of strings of length...
Exercise 10.11.1: Counting strings over {a, b, c}. infoAbout Count the number of strings of length 9 over the alphabet {a, b, c} subject to each of the following restrictions. (a) The first or the last character is a. (b) The string contains at least 8 consecutive a's. (c) The string contains at least 8 consecutive identical characters. (d) The first character is the same as the last character, or the last character is a, or the first character is...
I am trying to use the countif or countifs function to count the number of genres...
I am trying to use the countif or countifs function to count the number of genres used to describe a film and no matter what syntax I use, the result is 0. Can I use the countif(s) function to count columns? I want to count the number of movies that have 3 or more genre's associated with them. Here is a sample of the data I am working with: primarykey Movie Genre1 Genre2 Genre3 Genre4 Genre5 Genre6 Genre7 tt0499549 Avatar...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT