Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

USING PYTHON 13- write a function wordsPositions() with the following input and

ID: 3831356 • Letter: U

Question

USING PYTHON 13- write a function wordsPositions() with the following input and output. Input, s a string consisting of lower and upper cases letters and spaces. return: a dictionary in which each distinct word in s is a key and the corresponding value is a list of the position in s in which the word occurs. words are trated the same regardless of the capitazilation.

12-write a function named repeatWords(). The function repeatWords() takes two string parameters: the name of an input file and the name of an output file. The input file contains only lower case letters and white space.

The function repeatWords() should identify the word(s) that appear more than once in the file and write each such word to a line of the output file, followed by the number of times that the word appears. A repeated word should be written to only a single line of the output file, no matter how many times it appears in the input file. The order that the words are written to the output file does not matter.

Explanation / Answer

Given below is a python program wordPositions.py which prints a dictionary returned by a function named wordPositions( s ). This function accepts a string 's' which consists of only lower / upper case letters and spaces. There is key in the dictionary returned for each distinct word. The corresponding value for given key is a list of positions at which given word occurs within the input string.

File: wordPositions.py

import sys

def wordsPositions( s ):
"""Return a dictionary,
a) in which each distinct word in input string 's' is a key
b) corresponding value is a list of position in 's' in which given word occurs

Words are treated the same regardless of capitalization.
Input string 's' consists of letters (lower / upper case) and spaces only.
"""

# Create a list of lower case words from input string
words = s.lower().split()
dict = { }

# Update position list of each word in a dictionary

for idx, word in enumerate(words):
if dict.has_key(word):
posList = dict[word]
else:
posList = []

posList.append(idx + 1)
dict[word] = posList

# Return updated dictionary

return dict

if __name__ == "__main__":

if len(sys.argv) < 2:
print "Usage:", sys.argv[0], "<string>"
sys.exit(-1)

dict = wordsPositions(sys.argv[1])

print dict

Sample Execution Output:

$ python wordPositions.py "This is some line of string to test position of string of words"

{'string': [6, 11], 'this': [1], 'of': [5, 10, 12], 'is': [2], 'some': [3], 'to': [7], 'words': [13], 'test': [8], 'position': [9], 'line': [4]}
  
Given below is a python program repeatWords.py which calls a function repeatWords( inFile, outFile ). This function accepts input and output file names. The output file is updated with each word and corresponding appearance count in a separate line. The input file consists of only lower case letters and spaces.

File: repeatWords.py

import sys

def repeatWords( inFile, outFile ):
"""Create an output file,
- with a distinct word present in input file followed by the number of times the word appears

Input file consists of only lower case letters and spaces.
"""

# Open input file for reading

fIn = open(inFile, 'r')
dict = { }

# Process each line from input file

for line in fIn:

# Create a list of words in given line of text

words = line.split()

# Update appearance count of each word in dictionary

for word in words:
if dict.has_key(word):
count = dict[word]
else:
count = 0

dict[word] = count + 1

# Create output file with each word and corresponding appearance count on a new line

fOut = open(outFile, 'w')

for word, count in dict.items():
fOut.write(word + " " + str(count) + " ")

if __name__ == "__main__":

if len(sys.argv) < 3:
print "Usage:", sys.argv[0], "<inputFile> <outputFile>"
sys.exit(-1)

repeatWords(sys.argv[1], sys.argv[2])

Sample Input:

$ cat input.txt
this is one line of text to test words that appear again
this is another line of text

Sample Execution Output:
  
$ python repeatWords.py input.txt output.txt

$ cat output.txt
again 1
appear 1
that 1
this 2
of 2
is 2
one 1
to 1
another 1
words 1
text 2
test 1
line 2