The purpose of this program is to read in an array of tokens, remove the punctuation, turn all the letters lower case, and then print the resulting array. the readTokens and depunctuateTokens functions both work correctly. My problem is with the decapitalizeTokens function. When I run the program I receive this error:
the name of the program is words.py
['hello', 'hello1', 'hello2']
Traceback (most recent call last):
File "words.py", line 41, in <module>
main()
File "words.py", line 10, in main
words = decapitalizeTokens(cleanTokens)
File "words.py", line 35, in decapitalizeTokens
if (ord(ch) <= ord('Z')):
TypeError: ord() expected string of length 1, but list found
My question is what formal parameters I should put into the decapitalizeTokens function in order to return the array resulting from the depunctuateTokens function, but with all the letters lowercase.
This is my program:
import sys
from scanner import *
arr=[]
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(arr)
words = decapitalizeTokens(cleanTokens)
def readTokens(s):
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)
print(result)
return result
def decapitalizeTokens(result):
if (ord(result) <= ord('Z')):
return chr(ord(result) + ord('a') - (ord('A')))
else:
print(result)
return result
main()
arr, and then also returning it fromreadTokensbut storing that copy intokens, is doubly confusing. Get rid of the global; move thearr = []into the first line ofreadTokens, and just usetokensinstead ofarrinsidemain, and it will be a lot clearer.lower()andsub()so mean, they do not deserve your friendship?range(len(s))and then uses[i]within the loop. Just dofor char in s:, and usechar.range(0, foo, 1);range(foo)does the same thing.