The purpose of this program is to read in a file, change all the words into individual tokens, and place these tokens into an array. The program then removes all punctuation and changes all letters to lowercase. Then the program should count how many times each command line argument occurs in the array, and print the result. My program is able to successfully create an array of depunctuated, lowercase tokens. My problem now is how to loop through the array and count the occurrences of a particular word, and how I should call these functions in the main function. My depunctuate function works as written
This is my program:
import sys
from scanner import *
def main():
print("the name of the program is",sys.argv[0])
for i in range(1,len(sys.argv),1):
print(" argument",i,"is", sys.argv[i])
tokens = readTokens("text.txt")
cleanTokens = depunctuateTokens(tokens)
words = [token.lower() for token in cleanTokens]
count = find(words)
print(words)
print(count)
def readTokens(s):
arr=[]
s=Scanner("text.txt")
token=s.readtoken()
while (token != ""):
arr.append(token)
token=s.readtoken()
s.close()
return arr
def depunctuateTokens(arr):
result=[]
for i in range(0,len(arr),1):
string=arr[i]
cleaned=""
punctuation="""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
for i in range(0,len(string),1):
if string[i] not in punctuation:
cleaned += string[i]
result.append(cleaned)
return result
def find(tokens,words):
return occurences(tokens,words)>0
def occurences(tokens,words):
count = 0
for i in range(0,len(words),1):
if (words[i] == tokens):
count += 1
return count
main()