Final Assignment Programming Class

Posted Under: Programming

Ask A Question
Viewed 20
Abstract: You've seen a lot of different things about languages and have used a few. While all languages are good at something, none are good at everything, so selection of a language to use for a problem should closely mirror how you expect to compute the result - most of the time. While there is no and can be no best language to solve all problems, there are fads in coding and development just like anything else, often to the detriment of the popular language. Its capabilities are inflated and it starts to grow 'wide' rather than 'tall. That is, people start figuring out ways to port their favorite language to problems it is not meant to solve and how to expand its range of application, rather than focus on what it does well. Python is unfortunately rather popular. Introduction: In the course of preparing assignments, I often need to generate information for you to examine, modify, or fix. One metaphor would be making a puzzle. It is delivered in its end state in pieces and the goal is for you to assemble it. This means I often spend a fair amount of time in text processing, scripting and automatically generating and randomizing various files, information and behaviors. I was using Python to generate some files for a Python assignment and ran in to the issue I spoke of in class - Python does not really push its morphisms. For instance, arrays are common in Java and C, but what if you could only have 1-dimensional arrays? While you don't often use 2, 3, 4 or 5 dimensions, they are there for you. What if you only had pointers to values and pointers could not point to other pointers? It would be very hard to get data in from the commandline in C, since argv requires a pointer to a pointer (i.e. a 2-D array) to work. While I do certainly like list comprehensions in Python, I wish they built taller and made the list comprehensions go a little farther. If you do simple things with comprehensions, they're fine, but if you complicate things a bit, they get somewhat clunky. Look at '': " import sys def unfoo(inname, outname): fin = open(inname, 'r') fout = open(outname, 'w') # returns the whole file as a string # fin.readlines( ) returns the whole file as a list of lines # for line in fin reads a line at a time for line in fin: frepl = line.replace('foo','') fout.write(frepl) fin.close( ) fout.close( ) def main ( ): unfoo(sys.argv[1], sys.argv[2]) if __name__ == '__main__': main() " .. that is a nice, clean, simple function. Read in a whole file and replace each occurrence of single word with a different single word and write it out. Easy. So, the actions above are: read in file as a list for each line in that file: replace all occurrences of 'foo' with a space write the line out Take that basic function and add a list of words to swap: read in list of word replacements as a list of lists (each sublist holds two or three words that will be swapped/replaced for each other) read in a file as a list for each line in that file: if a word in that line appears in a word replacement sublist, replace it with the opposite word (i.e. swap 1st word for 2nd and vice versa) write the line out That fairly direct augmentation runs beyond what Python can represent easily. Your assignment is to write it using the skeleton I outline below. Methodology: First take the code skeleton below. It is also attached as "". " import sys def confu(dictname, inname, outname): confulist = [] linewords = [] #open and read in the dictionary # the dictionary is a list of lists # each sublist is a tab-separated list of # two to three words file = open(dictname, 'r') for line in file.readlines(): confulist.append(line.strip().split("\t")) file.close() #open up the file of text whose words you # are going to replace 'inname' file = open(inname, 'r') #open up/create the file that will hold the # modified version of 'infile' outfile = open(outname, 'w') #for each line in 'infile' ... for line in file.readlines(): #get a list of all the words in it ... linewords = line.strip().split(" ") # for each word in the file... #--> fill in line here #for each sublist in the dictionary... # ---> fill in line here # if that word appears in a sublist, # replace it with one of the other word(s) in # that sublist # ---> fill in multiple lines here # write the modified line out to the 'outfile' outfile.write(line) file.close() outfile.close() def main ( ): confu(sys.argv[1], sys.argv[2], sys.argv[3]) if __name__ == '__main__': main() " The first parameter is the 'dictionary' of word replacements. I've attached a handy list of English homophones (words that sound the same, but are spelled differently and have different meanings) as "confusion_source.txt". You can make your own for testing or only use a subset of the one I supplied while you're working in order to shorten the output or make things simpler, but make sure your code works with the file I attached. The second parameter is the 'input' of words to be replaced. I've attached a basic input with a single line as "basic_infile.txt". The third parameter is the 'output' where lines with all replaced words are sent to. Be sure to be careful when you give Python that filename. It will overwrite anything you give it without prompting. I recommend you test this in a new directory and back up your code before you test it. You can run it on iLabs as: "python confusion_source.txt basic_infile.txt outfilename.txt" If it is coded correctly, the content of your outfile should be: "You no it's critical too some up all you're principle sites." I'd recommend first commenting out the write to the outfile and then replacing lines as you go and printing them to see if you can get the information that is detailed in the comments to print out. As simple as 'unfoo' is, reading in a list of replacement words and replacing them should not be this clunky. The issue is that the words are text and not a simple list. The other issue is matching. Since any word in a sublist can match, there isn't just one way to run through them. There is also a hidden problem! See if you can figure it out! Remember, it is supposed to simply replace words with other words, so capitalzation and punctuation should be preserved, as well as all spaces and line breaks. Results: Submit your modified "" on or before the due date. If you can figure it out, submit a description of the 'hidden problem' as a text comment on the Canvas assignment and include a "problem_input.txt" infile with your submission that demonstrates the issue(s). Both the description and the file are required for extra points.

No uploads for this question

Explanations and Answers 0

No answers posted

Post your Answer - free or at a fee

Login to your tutor account to post an answer

Posting a free answer earns you +20 points.


NB: Post a homework question for free and get answers - free or paid homework help.

Get answers to: Final Assignment Programming Class or similar questions only at Tutlance.

Related Questions