Instructions
Requirements and Specifications
Source Code
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...
for word in linewords:
#for each sublist in the dictionary...
for sublist in confulist:
# if that word appears in a sublist,
if word in sublist:
# replace it with one of the other word(s) in
# that sublist
# ---> fill in multiple lines here
# Get the index of the current word
current_index = sublist.index(word)
new_index = -1
# Now, pick a different index
if current_index == 0:
new_index = 1
else:
new_index = 0
new_word = sublist[new_index]
line = line.replace(word, new_word)
""" NOTE:
The hidden problem I see is that some sublist can contain 3 words instead of two, so
how do we choose the opposite word in this case? A solution to this would be
using a random generator (from random import randint) so we chose the index
if the new word making sure it is different from the index of the current word (the one to
be replaced) in the sublist
Another solution would be taking the sublist, copying it to a new variable, removing the word
to be replace from the sublist and then we will end with a sublist with a size of 1 or 2. If the size is 1 then
just take that word. If the size is 2 then take a random integer between 0 and 1
"""
# 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()