Python Word Counter
** You will need to download Python 3.5 IDLE to input code
⢠Python: The Python interpreter (version 3), freely available from the Python Foundation.
#!/usr/bin/env python3
"""
Name: <lastname>, <firstname>
UID: <uid>
INST326, Homework 1
Date: <date>
"""
# The following sample text is provided for you to test your code. You can leave it as is.
sample = """
We set sail on this new sea because there is new knowledge
to be gained, and new rights to be won, and they must be won and used
for the progress of all people. For space science, like nuclear science
and all technology, has no conscience of its own. Whether it will become
a force for good or ill depends on man, and only if the United States
occupies a position of pre-eminence can we help decide whether this new
ocean will be a sea of peace or a new terrifying theater of war. I do not
say the we should or will go unprotected against the hostile misuse of
space any more than we go unprotected against the hostile use of land or
sea, but I do say that space can be explored and mastered without feeding
the fires of war, without repeating the mistakes that man has made in
extending his writ around this globe of ours.
There is no strife, no prejudice, no national conflict in outer space
as yet. Its hazards are hostile to us all. Its conquest deserves the
best of all mankind, and its opportunity for peaceful cooperation many
never come again. But why, some say, the moon? Why choose this as our
goal? And they may well ask why climb the highest mountain? Why, 35
years ago, fly the Atlantic? Why does Rice play Texas?
We choose to go to the moon. We choose to go to the moon in this decade
and do the other things, not because they are easy, but because they are
hard, because that goal will serve to organize and measure the best of
our energies and skills, because that challenge is one that we are willing
to accept, one we are unwilling to postpone, and one which we intend
to win, and the others, too.
"""
def create_list():
pass
def tally_words():
pass
def sort_by_frequency(counts):
pass
def main():
pass
# The purpose of this block will be explained in class. You should leave it as is.
if __name__ == "__main__":
main()
Get Help With a similar task to - Python Word Counter
Additional Instructions:
A common task in computer-aided textual analysis is to count the frequency of words in a given text. Your task is to create such a counter that meets the following requirements: 1. [1 point] Define a function "create_list()" that takes an arbitrary string as an input parameter (called "text"), and returns a list of the individual words in the input string. The words returned should be all lowercase, and should have trailing periods, commas, and question marks removed. Other punctuation, including any internal punctuation such as hyphens, should be retained. There should be no whitespace (no newlines, returns, tabs, spaces) in your list of words. There should also be no empty strings in the returned list. HINT: Look at the split built-in string method. Also, endswith() and string slicing can be used to remove trailing punctuation. Input: "To be, or not to be?" Output: ['to', 'be', 'or', 'not', 'to', 'be'] 2. [1 point] Define a function "tally_words()" that takes a list of strings as input, and returns a dictionary with a key for each unique string in the input list, and a value for each key that is an integer representing the number of occurrences of that string in the input list. HINT: You will need to implement some checking of whether each word is in the dictionary already. Input: ['to', 'be', 'or', 'not', 'to', 'be'] Output: {'to': 2, 'be': 2, 'or': 1, 'not': 1} 3. [1 point] Define a function "sort_by_frequency()" that takes a dictionary as input and returns a list of tuples with the first element is the value and the second element the key from the original dictionary. The resulting list of tuples should be sorted in descending order (so the higher counts come first). For the purpose of this assignment the sorting of tied words is not significant. HINT: Converting to a list of tuples in this form is intended to facilitate the sorting of the list. Look at the rules for how tuples sort naturally. Input: {'to': 2, 'be': 2, 'or': 1, 'not': 1} Output: [(2, 'to'), (2, 'be'), (1, 'or'), (1, 'not')] 4. [1 point] Define a function called "main()" in which you call create_list() on the provided sample text, tally_words() on the resulting list of words, sort_by_frequency on the resulting count dictionary, and finally which loops over the sorted list of tuples, printing each pair in the following form: (2) to (2) be (1) or (1) not 5. [1 point] Define test functions for each of the three processing functions that demonstrate that you have met the requirements for each function. You do not need a test function for the main() function. Your test functions should be named in the form "test_<function_name>". How to set up and run these tests will be explained in this week's module, Module 3. The file hw1_template.py can be used as a starting point for writing your solution. It provides a framework for setting up your code as well as sample text that can be used for testing.
Related Questions
Tutlance Experts offer help in a wide range of topics. Here are some of our top services:
- Math homework help
- Nursing homework help
- Statistics homework help
- Nursing coursework help
- Capstone project writing services
- Essay writers for hire
- Case study writing help
- Buy college papers online
- Buy college research papers
- College homework help
- Professional resume writing services
- Programming homework help
- Coursework writing help
- Term paper writing help
- Biology homework help
- Do my physics homework
- Dissertation data analysis help
- PhD Dissertation writing services
- Chemistry homework help
Post your project now for free and watch professional experts outbid each other in just a few minutes.