arrays - Mapping a function over all the letters of a token in python -
The purpose of this program is to read in an array of tokens, remove punctuation, change all letters lower case, And then print out the resulting array, ReadTokens and depunctuateTokens both work correctly. My problem is with decapitalizeTokens function when I run the program, I get this error:
The name of the program is word.py ['hello', 'hello 1', 'hello 2'] Tracebacks (Lastest Call Final): File "words.py", line 41, & lt; Module & gt; Main () file "words.py", line 10, in the main words = decapitalizeTokens (cleantunes) file "words.py", line 35, decapitalizeTokens if (ord (ch) & lt; = ord ('Z')) : TypeError: required string of ord () length 1, but the
list has found that the formal parameters should be inserted into the decapitalizeTokens function so that the user returned the array generated from the Talkence function Go, but lowercase with all the letters
This is my program:
Male import import system * arr = [] def main (): I print range (1, lane (sys.argv), 1) for print ("program name", sys. Argv [0]): print (" Logic ", i," is ", sys.argv [i]) token = readTokens (" text.txt ") CleanTokens = depunctuateTokens (arr) word = decapitalizeTokens (cleanTokens) def readTokens (s): s = scanner (" text .txt ") token = s.readtoken () while (token! =" "): Arr.append (token string = ARR [i] clear =" "" = "" "=" "=" punctuation = " ""! "# $% & Amp; () * +, - / /;; & lt; = & gt ;? @ [\] ^ _` {|} ~ "" In the category I (0, Lane String), 1): if the string [i] is not in punctuation: clear + = str C [i] results Kappend (clear) Print (results) return result DAP DecapitalizeTokens (result): if (ord (results) & lt; = Return ('z'): Return f r (ord (result) + ord ('a') - (ord ('a')) Other: print (results) Return result main ()
decapitalizeTokens function works on a single character. You can see it in a list of strings. If you want to call it based on each letter of that string, then you need a loop in the list, and then there is a loop on every string anywhere.You can do this Are clear With token statement, for token in token like: word = '' for token in four: word + = decaptializeTokens (four) word + = word
Word = P> ... or by using comprehension: word = [''. (For four in tokens decapitalizeTokens (four)) for tokens in cleantookens]
However, I think that it makes more sense to move the loop to the decapitalizeTokens
function, both depending on its plural name, and On the fact that you Nearly the same end is the same designated dmuptuittocens
function If you create decapitalizeTokens
, in the same way you have created DipTextTracks
, then your existing call works well:
word = decapitalizeTokens (CleanToken)
In the form of a side note, the underlying method on the string is already what you want, so you can change this whole mess like this:
clearTokens for the token in the word [Token.lower ()]
... whichever is your effort a nasty bug will be fine. What, say, will decapitalizeTokens
with a digit or a space.
And, likewise, depunctuateTokens
can change the way to a call similarly. For example (for Python 2.x slightly different, but you can read the docs and understand it):
punctuation marks = ""! "# $% & Amp; '() * +, - / .; & lt; = & gt ;? [[]] {{}}" "Punctmap = {ord}: none for four in punctuation } CleanToken = [token.translate (punctmap)) token in pure token]
Comments
Post a Comment