Question

In: Computer Science

17. Write a function in Python which removes a set of common words from a long...

17. Write a function in Python which removes a set of common words from a long piece of text. Be sure to strip out any punctuation. The common words to be removed are:
​​a, an, as, at, by, for, in, is, it, of, that, this, to, was, will, the
These are typical words which are considered to have low semantic value.
Process each paragraph provided below individually. Your end result for each paragraph should be a string or list containing the processed paragraph with the common words and punctuation removed. It is not required to put the text samples into a file (you can simply copy and paste into a string variable inside your Python script).
For the text samples to process, use the following (taken from the official Python tutorial):

If you do much work on computers, eventually you find that there’s some task you’d like to automate.

For example, you may wish to perform a search-and-replace over a large number of text files, or rename and rearrange a bunch of photo files in a complicated way. Perhaps you’d like to write a small custom database, or a specialized GUI application, or a simple game.

If you’re a professional software developer, you may have to work with several C/C++/Java libraries but find the usual write/compile/test/re-compile cycle is too slow. Perhaps you’re writing a test suite for such a library and find writing the testing code a tedious task. Or maybe you’ve written a program that could use an extension language, and you don’t want to design and implement a whole new language for your application.

You could write a Unix shell script or Windows batch files for some of these tasks, but shell scripts are best at moving around files and changing text data, not well-suited for GUI applications or games. You could write a C/C++/Java program, but it can take a lot of development time to get even a first-draft program. Python is simpler to use, available on Windows, Mac OS X, and Unix operating systems, and will help you get the job done more quickly.
Python is simple to use, but it is a real programming language, offering much more structure and support for large programs than shell scripts or batch files can offer. On the other hand, Python also offers much more error checking than C, and, being a very-high-level language, it has high-level data types built in, such as flexible arrays and dictionaries. Because of its more general data types Python is applicable to a much larger problem domain than Awk or even Perl, yet many things are at least as easy in Python as in those languages.

Python allows you to split your program into modules that can be reused in other Python programs. It comes with a large collection of standard modules that you can use as the basis of your programs — or as examples to start learning to program in Python. Some of these modules provide things like file I/O, system calls, sockets, and even interfaces to graphical user interface toolkits like Tk.
Python is an interpreted language, which can save you considerable time during program development because no compilation and linking is necessary. The interpreter can be used interactively, which makes it easy to experiment with features of the language, to write throw-away programs, or to test functions during bottom-up program development. It is also a handy desk calculator.

Solutions

Expert Solution

Below is a screen shot of the python program to check indentation. Comments are given on every line explaining the code.

Below is the output of the program:

Below is the code to copy:
#CODE STARTS HERE----------------
def remove(text_string):
   #list of punctuations to remove
   punc = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
   punc_removed = "" #Used to store the string without punctuations
   for letter in text_string: #Loop every character in the string
      if letter not in punc: #If characters is not in punc, add it to new string
         punc_removed += letter

   #List of words to be removed
   stop_words = ["a", "an", "as", "at", "by", "for", "in", "is",
              "it", "of", "that", 'this', "to", "was", "will", "the"]
   word_removed = "" #Used to store the string after removing words
   for word in punc_removed.split(): #Loop through every word
      if word not in stop_words:
         word_removed += " "+word #Add words separated by space
   print(word_removed) #Print result

#Sample text
text = "If you do much work on computers, eventually you find that there’s some task you’d like to automate." \
      "For example, you may wish to perform a search-and-replace over a large number of text files, or rename " \
      "and rearrange a bunch of photo files in a complicated way. Perhaps you’d like to write a small custom " \
      "database, or a specialized GUI application, or a simple game." \
      "If you’re a professional software developer, you may have to work with several C/C++/Java libraries " \
      "but find the usual write/compile/test/re-compile cycle is too slow. Perhaps you’re writing a test suite " \
      "for such a library and find writing the testing code a tedious task. Or maybe you’ve written a program that " \
      "could use an extension language, and you don’t want to design and implement a whole new language for your application." \
      "You could write a Unix shell script or Windows batch files for some of these tasks, but shell scripts are " \
      "best at moving around files and changing text data, not well-suited for GUI applications or games. You " \
      "could write a C/C++/Java program, but it can take a lot of development time to get even a first-draft " \
      "program. Python is simpler to use, available on Windows, Mac OS X, and Unix operating systems, " \
      "and will help you get the job done more quickly.Python is simple to use, but it is a real programming language," \
      " offering much more structure and support for large programs than shell scripts or batch files can offer. " \
      "On the other hand, Python also offers much more error checking than C, and, being a very-high-level language," \
      " it has high-level data types built in, such as flexible arrays and dictionaries. Because of its more general" \
      " data types Python is applicable to a much larger problem domain than Awk or even Perl, yet many things are at" \
      " least as easy in Python as in those languages.Python allows you to split your program into modules that can be " \
      "reused in other Python programs. It comes with a large collection of standard modules that you can use as the" \
      " basis of your programs — or as examples to start learning to program in Python. Some of these modules provide " \
      "things like file I/O, system calls, sockets, and even interfaces to graphical user interface toolkits like Tk." \
      "Python is an interpreted language, which can save you considerable time during program development because no" \
      " compilation and linking is necessary. The interpreter can be used interactively, which makes it easy to experiment" \
      " with features of the language, to write throw-away programs, or to test functions during bottom-up program development." \
      " It is also a handy desk calculator."
remove(text) #function call
#CODE ENDS HERE------------------

Related Solutions

Write a Python program which takes a set of positive numbers from the input and returns...
Write a Python program which takes a set of positive numbers from the input and returns the sum of the prime numbers in the given set. The sequence will be ended with a negative number.
USE PYTHON : # Problem Set 04: - Write a function to seek for all even...
USE PYTHON : # Problem Set 04: - Write a function to seek for all even numbers and odd numbers in the middle of two number A and B. Print even and odd numbers in 1 and 2020 (including both these two numbers) # Problem Set 05: - A website requests an user to input his account password. - Write a program to examize the validity of the password. - The valid password must consists of: - At least 1...
Write a python program function to check the frequency of the words in text files. Make...
Write a python program function to check the frequency of the words in text files. Make sure to remove any punctuation and convert all words to lower case. If my text file is like this: Hello, This is Python Program? thAt chEcks% THE freQuency of the words! When is printed it should look like this: hello 1 this 1 is 1 python 1 program 1 that 1 checks 1 the 2 frequency 1 of 1 words 1
python Write a function pack_to_5(words) that takes a list of string objects as a parameter and...
python Write a function pack_to_5(words) that takes a list of string objects as a parameter and returns a new list containing each string in the title-case version. Any strings that have less than 5 characters needs to be expanded with the appropriate number of space characters to make them exactly 5 characters long. For example, consider the following list: words = ['Right', 'SAID', 'jO'] The new list would be: ['Right', 'Said ', 'Jo '] Since the second element only contains...
Write a Python function ???????? that takes in a nonnegative semiprime number ? which is the...
Write a Python function ???????? that takes in a nonnegative semiprime number ? which is the product of two prime numbers ? and ? and returns the tuple ( ?, ? ) where ?≤? . Example: ????????(22)=(2,11) Example: ????????(3605282209)=(59447,60647) This problem has a time-out limit of 1 second and a memory limit of 1MB. The number ? in all test-cases will satisfy 4≤?≤800000000000000 For example: Test Result print(factorMe(22)) (2, 11) print(factorMe(3605282209)) (59447, 60647)
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
Write a program in python that reads the elements of a set from the keyboard, stores...
Write a program in python that reads the elements of a set from the keyboard, stores them in a set, and then determines its powerset. Specifically, the program should repeatedly ask the user: Enter one more element ? [Y/N] If the user answers Y then an new element is read from the keyboard: Enter the new element in the set: This cycle continues until the user answers N to the first question. At that point the program shall compute the...
Write a Python program which uses a function to calculate the perimeter of a rectangle. a...
Write a Python program which uses a function to calculate the perimeter of a rectangle. a function named volume to calculate the volume of a cylinder volume = 3.14 x radius x radius x height .b function named volume to calculate the volume of a cuboid volume = Length x width x ht Write a Python Program to calculate the sum of all odd numbers for 2 to 20 using a for loop. 4. Write statements that assign random integers...
(Python) Write a program which accomplishes the following tasks: set a variable to the result of...
(Python) Write a program which accomplishes the following tasks: set a variable to the result of mathematical expression including +, -, * and / and of both Integer and Float values (or variables) set a variable to the result of a combination of string values (or variables) set a variable to the result of a combination of string, Integer and Float values (you may need to use the type casting functions) Using the following variables: a = 1.3 b =...
write a c++ member function that removes the first instance of a specific element in a...
write a c++ member function that removes the first instance of a specific element in a linked list and then return the size of the list after the removal whether it was successful or not.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT