Question

In: Computer Science

Python Project Description: Text Processing This assignment requires you to process a short text from an...

Python Project Description: Text Processing

This assignment requires you to process a short text from an input file, perform some analyses, manipulate the text, and save the results in an output file. For example, the input file bentley.txt contains a brief introduction of Bentley University:

Each line in this file is a paragraph that contains one or more sentences. Note that each line can be very long!

The program will provide the following user interaction on the screen:

· Ask the user to enter the name of the input file that contains the document.

· Ask the user to enter the width of the line in the output file.

· Ask the user to enter a pattern (e.g., ‘on’).

· Ask the user for enter a new pattern (e.g., ‘@@’) that is used to replace the old pattern.

· Ask the user to choose Left/Right/Center justification for the output.

· Display a message showing the name of the output file written.

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:
Contents of Bentley.txt:


Console output:


Contents of Bentley_output.txt:


Below is the code to copy:
#CODE STARTS HERE----------------
import io #Used to loop paragraphs by "\n" as delimiter
#input all details here
file_input = input("Enter the file name: ")
line_width = int(input("Enter line width: "))
pattern_in = input("Enter the pattern to change: ")
pattern_out = input("Enter the pattern to replace the old pattern: ")
justification = input("Enter justification (Left/Right/Center): ")

#Open file and read text.
f = open(file_input,"r")
paragraph = f.read().replace(pattern_in,pattern_out) #replace pattern
f.close() #close file

#Add "\n" if the line is longer than line_width
modified_para = ""
for x in io.StringIO(paragraph): #loop through every line in the paragraph
   if len(x)>line_width: #Check length
      li = []
      for i in range(0, len(x), line_width): #If line is greater than line_width
         li.append(x[i:i + line_width]) #Add '\n' after every line_width number of chars
      modified_para += '\n'.join(li)
   else:
      modified_para+= x

#Modify output file name and print it
file_output = file_input.split(".")[0]+"_output."+file_input.split(".")[-1]
print("\nThe output file is",file_output)

#Open output file
f_out = open(file_output,"a+")

#If left justify
if justification.strip().lower() == "left":
   for x in io.StringIO(modified_para):
      f_out.write("{:<{y}}\n".format(x.strip(),y = line_width)) #Use "{:<}".format to left justify

if justification.strip().lower() == "right":
   for x in io.StringIO(modified_para):
      f_out.write("{:>{y}}\n".format(x.strip(),y = line_width)) #Use "{:>}".format to right justify

if justification.strip().lower() == "center":
   for x in io.StringIO(modified_para):
      f_out.write("{:^{y}}\n".format(x.strip(),y = line_width)) #Use "{:^}".format to center justify
f_out.close() #close file
#CODE ENDS HERE------------------

Related Solutions

Allow the main process to generate a text file containing the text of this assignment. The...
Allow the main process to generate a text file containing the text of this assignment. The main process will then create two other processes and pass to them the name of the file and two codes (code1 and code2) using a pipe() system call. The codes are two different alphabetic letters such as “a” and “k”. The child processes should search the file for the character in the interval received, compute their frequencies and return them through a separate pipe...
In Python This assignment involves the use of text files, lists, and exception handling and is...
In Python This assignment involves the use of text files, lists, and exception handling and is a continuation of the baby file assignment. You should now have two files … one called boynames2014.txt and one called girlnames2014.txt - each containing the top 100 names for each gender from 2014. Write a program which allows the user to search your files for a boy or girl name and display where that name ranked in 2014. For example … >>>   Enter gender...
Put theory into practice by performing stopwords removal and text processing in Python using the popular...
Put theory into practice by performing stopwords removal and text processing in Python using the popular NLTK library. 1:Obtain number of lines from the above text and make each line to store in an element of array؟ This is my first program. . We are the students of Master’s in Data Science, Hello! How are you? We are here to learn the python script, this is it now.
Description of the Assignment: Write a Python program to (a) create a new empty stack. Then,...
Description of the Assignment: Write a Python program to (a) create a new empty stack. Then, (b) add items onto the stack. (c) Print the stack contents. (d) Remove an item off the stack, and (e) print the removed item. At the end, (f) print the new stack contents: Please use the following comments in your python script: # ************************************************* # COP3375 # Student's full name ( <<< your name goes here) # Week 8: Assignment 1 # ************************************************* #...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of...
Description This assignment is a modification of the last assignment. In this assignment, you will input...
Description This assignment is a modification of the last assignment. In this assignment, you will input from a file called in.txt and output to a file called out.txt. Otherwise, the assignment will behave the same way as the last assignment. For doing this assignment, the statement doing input and output will be changed. Otherwise, the code will mostly remain unchanged. Creating Text IO Files In Eclipse, using the file menu, create the input file in.txt in project folder (not in...
In this assignment, you are going to write a Python program to demonstrate the IPO (Input-Process-Output)...
In this assignment, you are going to write a Python program to demonstrate the IPO (Input-Process-Output) cycle that is the heart of many imperative programs used for processing large amount of data. Daisy is recently hired by a warehouse. One of her job is to keep track the items ordered by all the branches of the company. The company wants to automate the task using a computer program. Being a friend of Daisy, she knows you are a Computer Science...
For Python: In this assignment you are asked to write a Python program to determine the...
For Python: In this assignment you are asked to write a Python program to determine the Academic Standing of a studentbased on their CGPA. The program should do the following: Prompt the user to enter his name. Prompt the user to enter his major. Prompt the user to enter grades for 3 subjects (A, B, C, D, F). Calculate the CGPA of the student. To calculate CGPA use the formula: CGPA = (quality points * credit hours) / credit hours...
Part I. THE SPEECH PRODUCTION PROCESS Directions: Write a SHORT description of the process — step...
Part I. THE SPEECH PRODUCTION PROCESS Directions: Write a SHORT description of the process — step by step — of producing voice and speech. Include ALL of the terms below. larynx        approximation     pharynx            pitch        phonation sound waves   vocal folds         articulation        amplitude   volume frequency     resonance               vibration
Assignment Description For this assignment you will complete a literature review on a topic related to...
Assignment Description For this assignment you will complete a literature review on a topic related to public health or health administration (depending on your major). You can pick any topic within your field that you find interesting and would like to know more about! You will locate a minimum of 4 journal articles on the topic and 1 online source and write a 1 page literature review based on your topic, your question and the sources you’ve selected. The purpose...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT