Question

In: Computer Science

I'm Getting an "unindented error" Please fix the bolded codes. Because I don't know whats going...

I'm Getting an "unindented error" Please fix the bolded codes. Because I don't know whats going on. Thank You.

# This program exercises lists.

# The following files must be in the same folder:
# abstractcollection.py
# abstractlist.py
# arraylist.py
# arrays.py
# linkedlist.py
# node.py
# input.txt - the input text file.

# Input: input.txt
# This file must be in the same folder.
# To keep things simple:
# This file contains no punctuation.
# This file contains only lowercase characters.
# Output: output.txt
# This file will be created in the same folder.
# All articles are removed.
# Certain prepositions are removed.
# Duplicate consecutive words are reduced to a single occurrence.
# Note: The words "first" and "last" are not reduced.
# Certain misspelled words are flagged.
# Occurrences of "first" are moved to the front of a line.
# Occurrences of "last" are moved to the end of a line.

from arraylist import ArrayList
from linkedlist import LinkedList

# Data:

articles = ArrayList(["a", "the"])
prepositions = LinkedList(["after", "before", "from", "in", "off", "on", "under", "out", "over", "to"])
misspellings = ["foriegn", "excede", "judgement", "occurrance", "preceed", "rythm", "thier", ]

inputFile = open("input.txt", "r")
outputFile = open("output.txt", "w")

FIRST = "first"
FLAG = "FLAG:"
LAST = "last"

# Processing:

# Part 1:
# Removes all items from the words list that are found in the removals list.
# Input:
# words - an ArrayList of words, no uppercase, no punctuation
# wordsIter - a list iterator for the words list
# removals - the list of words to remove
def removeItems(words, wordsIter, removals):
wordsIter.first()
  
while(words.hasNext()):

word = words.next()

if word in removals:

wordsIter.remove()

# Part 2:
# Removes extra occurrances of consecutive duplicate words from the words list.
# Note: It does not reduce the words "first" and "last".
# Input:
# words - an ArrayList of words, no uppercase, no punctuation
# wordsIter - a list iterator for the words list
def reduceDuplicates(words, wordsIter):
previousWord=""
wordsIter.first()
while(words.hasNext()):
word=words.next()
if(word=="first" or word=="last"):
previousWord=word
continue
if(word==previousWord):
wordsIter.remove()
previousWord=word

# Part 3:
# Flags certain misspelled words in the words list by inserting "FLAG:" before them.
# Input:
# words - an ArrayList of words, no uppercase, no punctuation
# wordsIter - a list iterator for the words list
# misspellings - the list of misspelled words to flag
def flagMisspelled(words, wordsIter, misspellings):
wordsIter.first()
while(wordsIter.hasNext()):
word=words.next()
if word in misspellings:
wordsIter.insert(FLAG)
wordsIter.next()

# Part 4:
# Move all occurrences of "first" to the front of the words list.
# Input:
# words - an ArrayList of words, no uppercase, no punctuation
# wordsIter - a list iterator for the words list
def moveFirstLit(words, wordsIter):
countFirst = 0
wordsIter.first()
while (wordsIter.hasNext()):
word = wordsIter.next()
if (word == FIRST):
wordsIter.remove()
countFirst += 1
for count in range(countFirst):
wordsIter.first()
if (wordsIter.hasNext()):
wordsIter.next()
wordsIter.insert(FIRST)

# Part 5:
# Move all occurrences of "last" to the end of the words list.
# Input:
# words - an ArrayList of words, no uppercase, no punctuation
# wordsIter - a list iterator for the words list
def moveLastLit(words, wordsIter):
countLast = 0
wordsIter.last()
while (wordsIter.hasNext()):
word = wordsIter.next()
if (word == LAST):
wordsIter.remove()
countLast += 1
for count in range(countLast):
wordsIter.last()
if (wordsIter.hasNext()):
wordsIter.next()
wordsIter.insert(LAST)

def writeOutputLine(words):
outputLine = " ".join(words)
outputLine = outputLine + "\n"
print(outputLine, end="")
outputFile.write(outputLine)

# Main processing loop:
for line in inputFile:
words = ArrayList(line.split())
wordsIter = words.listIterator()

# Make no changes to blank lines:
if (len(words) == 0):
writeOutputLine(words)
continue

# Make no changes to comment lines:
if (words[0] == "#"):
writeOutputLine(words)
continue

# Remove articles:
removeItems(words, wordsIter, articles)
  
# Remove prepositions:
removeItems(words, wordsIter, prepositions)

# Reduce duplicate consecutive words to a single occurrence:
reduceDuplicates(words, wordsIter)

# Insert "FLAG:" before certain misspelled words:
flagMisspelled(words, wordsIter, misspellings)
  
# Move all occurrences of "first" to the front of the line:
moveFirstLit(words, wordsIter)
  
# Move all occurrences of "last" to the end of the line:
moveLastLit(words, wordsIter)

# Write output line:
writeOutputLine(words)

# Wrap-up
inputFile.close()
outputFile.close()

Solutions

Expert Solution

# This program exercises lists.

# The following files must be in the same folder:
# abstractcollection.py
# abstractlist.py
# arraylist.py
# arrays.py
# linkedlist.py
# node.py
# input.txt - the input text file.

# Input: input.txt
# This file must be in the same folder.
# To keep things simple:
# This file contains no punctuation.
# This file contains only lowercase characters.
# Output: output.txt
# This file will be created in the same folder.
# All articles are removed.
# Certain prepositions are removed.
# Duplicate consecutive words are reduced to a single occurrence.
# Note: The words "first" and "last" are not reduced.
# Certain misspelled words are flagged.
# Occurrences of "first" are moved to the front of a line.
# Occurrences of "last" are moved to the end of a line.

from arraylist import ArrayList
from linkedlist import LinkedList

# Data:

articles = ArrayList(["a", "the"])
prepositions = LinkedList(["after", "before", "from", "in", "off", "on", "under", "out", "over", "to"])
misspellings = ["foriegn", "excede", "judgement", "occurrance", "preceed", "rythm", "thier", ]

inputFile = open("input.txt", "r")
outputFile = open("output.txt", "w")

FIRST = "first"
FLAG = "FLAG:"
LAST = "last"

# Processing:

# Part 1:
# Removes all items from the words list that are found in the removals list.
# Input:
# words - an ArrayList of words, no uppercase, no punctuation
# wordsIter - a list iterator for the words list
# removals - the list of words to remove
def removeItems(words, wordsIter, removals):
        wordsIter.first()       
        while(words.hasNext()):
                word = words.next()
                if word in removals:
                        wordsIter.remove()

# Part 2:
# Removes extra occurrances of consecutive duplicate words from the words list.
# Note: It does not reduce the words "first" and "last".
# Input:
# words - an ArrayList of words, no uppercase, no punctuation
# wordsIter - a list iterator for the words list
def reduceDuplicates(words, wordsIter):
        previousWord=""
        wordsIter.first()
        while(words.hasNext()):
                word=words.next()
                if(word=="first" or word=="last"):
                        previousWord=word
                        continue
                if(word==previousWord):
                        wordsIter.remove()
                previousWord=word

# Part 3:
# Flags certain misspelled words in the words list by inserting "FLAG:" before them.
# Input:
# words - an ArrayList of words, no uppercase, no punctuation
# wordsIter - a list iterator for the words list
# misspellings - the list of misspelled words to flag
def flagMisspelled(words, wordsIter, misspellings):
        wordsIter.first()
        while(wordsIter.hasNext()):
                word=words.next()
                if word in misspellings:
                        wordsIter.insert(FLAG)
                        wordsIter.next()

# Part 4:
# Move all occurrences of "first" to the front of the words list.
# Input:
# words - an ArrayList of words, no uppercase, no punctuation
# wordsIter - a list iterator for the words list
def moveFirstLit(words, wordsIter):
        countFirst = 0
        wordsIter.first()
        while (wordsIter.hasNext()):
                word = wordsIter.next()
                if (word == FIRST):
                        wordsIter.remove()
                        countFirst += 1
        for count in range(countFirst):
                wordsIter.first()
                if (wordsIter.hasNext()):
                        wordsIter.next()
                        wordsIter.insert(FIRST)

# Part 5:
# Move all occurrences of "last" to the end of the words list.
# Input:
# words - an ArrayList of words, no uppercase, no punctuation
# wordsIter - a list iterator for the words list
def moveLastLit(words, wordsIter):
        countLast = 0
        wordsIter.last()
        while (wordsIter.hasNext()):
                word = wordsIter.next()
                if (word == LAST):
                        wordsIter.remove()
                        countLast += 1
        for count in range(countLast):
                wordsIter.last()
                if (wordsIter.hasNext()):
                        wordsIter.next()
                        wordsIter.insert(LAST)

def writeOutputLine(words):
        outputLine = " ".join(words)
        outputLine = outputLine + "\n"
        print(outputLine, end="")
        outputFile.write(outputLine)

# Main processing loop:
for line in inputFile:
        words = ArrayList(line.split())
        wordsIter = words.listIterator()

        # Make no changes to blank lines:
        if (len(words) == 0):
                writeOutputLine(words)
                continue

        # Make no changes to comment lines:
        if (words[0] == "#"):
                writeOutputLine(words)
                continue

        # Remove articles:
        removeItems(words, wordsIter, articles)

        # Remove prepositions:
        removeItems(words, wordsIter, prepositions)

        # Reduce duplicate consecutive words to a single occurrence:
        reduceDuplicates(words, wordsIter)

        # Insert "FLAG:" before certain misspelled words:
        flagMisspelled(words, wordsIter, misspellings)

        # Move all occurrences of "first" to the front of the line:
        moveFirstLit(words, wordsIter)

        # Move all occurrences of "last" to the end of the line:
        moveLastLit(words, wordsIter)

        # Write output line:
        writeOutputLine(words)

# Wrap-up
inputFile.close()
outputFile.close()
**************************************************
If any issues, please ask in comments. you have not given arraylist and linkedlist files, so i can not run and test this code.

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

I'm getting an error message with this code and I don't know how to fix it...
I'm getting an error message with this code and I don't know how to fix it The ones highlighted give me error message both having to deal Scanner input string being converted to an int. I tried changing the input variable to inputText because the user will input a number and not a character or any words. So what can I do to deal with this import java.util.Scanner; public class Project4 { /** * @param args the command line arguments...
I don't know why I keep getting the following error: AttributeError: 'tuple' object has no attribute...
I don't know why I keep getting the following error: AttributeError: 'tuple' object has no attribute 'size' I am using python in Anaconda. import numpy as np def information_gain(x_array, y_array): parent_entropy = entropy(x_array) split_dict = split(y_array) for val in split_dict.values(): freq = val.size / x_array.size child_entropy = entropy([x_array[i] for i in val]) parent_entropy -= child_entropy* freq return parent_entropy x = np.array([0, 1, 0, 1, 0, 1]) y = np.array([0, 1, 0, 1, 1, 1]) print(round(information_gain(x, y), 4)) x = np.array([0,...
Please fix this python script, I keep getting a return outside function error and cannot get...
Please fix this python script, I keep getting a return outside function error and cannot get it to run: def caesar(plainText, shift):     cipherText = "" for char in plainText:     newChar = ord(char) + shift     if newChar > 128:       newChar = newChar % 128     finalChar = chr(newChar)     cipherText += finalChar return cipherText text = input("Enter a message: "); s = input("Enter the distance value: "); print (caesar(text, int(s)));
Hello I have this error in the code, I do not know how to fix it....
Hello I have this error in the code, I do not know how to fix it. It is written in C++ using a Eclipse IDE Error: libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string bus.h =========== #pragma once #include using namespace std; class Bus { private:    string BusId; // bus ID    string Manufacturer; // manufacturer of the bus    int BusCapacity; // bus capacity    int Mileage; // mileage of bus    char Status; // current status...
I am getting an error at linen 57 and can't figure out how to fix it....
I am getting an error at linen 57 and can't figure out how to fix it. // Java program to read a CSV file and display the min, max, and average of numbers in it. import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.Scanner; public class Main {     // method to determine and return the minimum number from the array     public static int minimum(int numbers[])     {         int minIdx = 0;         for(int i=1;i<numbers.length;i++)         {             if((minIdx...
I keep getting the error below in zbooks. How can I fix this. Thank You JAVA...
I keep getting the error below in zbooks. How can I fix this. Thank You JAVA zyLabsUnitTest.java:14: error: cannot find symbol s = MidtermProblems.difference(75, 75); ^ symbol: method difference(int,int) location: class MidtermProblems 1 error import java.lang.Math; public class MidtermProblems { public static String difference(int a, int b) { int diff = Math.abs(a - b); String ans = "";    if (diff == 0) { ans = "EQUAL";    } else if (diff > 10) { ans = "Big Difference "...
In java, I keep getting the error below and I can't figure out what i'm doing...
In java, I keep getting the error below and I can't figure out what i'm doing wrong. Any help would be appreciated. 207: error: not a statement allocationMatrix[i][j];
I'm getting an error with my code on my EvenDemo class. I am supposed to have...
I'm getting an error with my code on my EvenDemo class. I am supposed to have two classes, Event and Event Demo. Below is my code.  What is a better way for me to write this? //******************************************************** // Event Class code //******************************************************** package java1; import java.util.Scanner; public class Event {    public final static double lowerPricePerGuest = 32.00;    public final static double higherPricePerGuest = 35.00;    public final static int cutOffValue = 50;    public boolean largeEvent;    private String...
Hi, I don’t know why I keep getting this error and I’ve spent a while on...
Hi, I don’t know why I keep getting this error and I’ve spent a while on it now. I am coding in C++. Here is my .h ___ #ifndef CARD_H #define CARD_H #include <iostream> #include <string> using namespace std; class Card { private: int powerLevel; string element; public: Card(); Card(string, int); string getElement(); int getPowerLevel(); void displayCard(); }; #endif ___ Here is my .cpp ___ #include <iostream> #include "Card.h" #include <string> using namespace std; Card::Card() { element = ""; powerLevel...
I keep getting minor errors I can't figure out and I don't know how to convert...
I keep getting minor errors I can't figure out and I don't know how to convert decimal .10 to percentage 10% either.   With these functions defined now expand the program for a company who gives discounts on items bought in bulk. Create a main function and inside of it ask the user how many different items they are buying. For each item have the user input a price and quantity, validating them with the functions that you wrote. Use your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT