Permalink
Browse files

Improve gword.py, fix the str.translate example

  • Loading branch information...
1 parent b778ec4 commit fc51793c7113c85e7a6b015c5546c3e696170267 @csev committed Dec 1, 2016
Showing with 16 additions and 24 deletions.
  1. +7 −16 book3/09-dictionaries.mkd
  2. +1 −1 code3/count3.py
  3. +3 −2 code3/gmane/gword.py
  4. +5 −5 unit3/testout/count3.txt
@@ -434,13 +434,12 @@ We can solve both these problems by using the string methods
`translate`. The `translate` is the most subtle of
the methods. Here is the documentation for `translate`:
-`string.translate(s, table[, deletechars])`
+`line.translate(str.maketrans(fromstr, tostr, deletestr))`
-*Delete all characters from s that are in deletechars (if present), and
-then translate the characters using table, which must be a 256-character
-string giving the translation for each character value, indexed by its
-ordinal. If table is None, then only the character deletion step is
-performed.*
+*Replace the characters in `fromstr` with the character in the same position in `tostr`
+and delete all characters that are in `deletestr`.
+The `fromstr` and `tostr` can be empty strings and the `deletestr`
+parameter can be omitted.*
We will not specify the `table` but we will use the
`deletechars` parameter to delete all of the punctuation. We
@@ -453,23 +452,15 @@ will even let Python tell us the list of characters that it considers
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
~~~~
+The parameters used by `translate` were different in Python 2.0.
+
We make the following modifications to our program:
\VerbatimInput{../code3/count2.py}
\begin{trinketfiles}
../code3/romeo-full.txt
\end{trinketfiles}
-We use `translate` to remove all punctuation and
-`lower` to force the line to lowercase. Otherwise the program
-is unchanged. Note that for Python 2.5 and earlier,
-`translate` does not accept `None` as the first
-parameter so use this code instead for the translate call:
-
-~~~~ {.python}
-print a.translate(string.maketrans(' ',' '), string.punctuation
-~~~~
-
Part of learning the "Art of Python" or "Thinking Pythonically" is
realizing that Python often has built-in capabilities for many common
data analysis problems. Over time, you will see enough example code and
View
@@ -2,7 +2,7 @@
fhand = open('romeo-full.txt')
counts = dict()
for line in fhand:
- line = line.translate(string.punctuation)
+ line = line.translate(str.maketrans('', '', string.punctuation))
line = line.lower()
words = line.split()
for word in words:
@@ -18,8 +18,8 @@
counts = dict()
for message_row in cur :
text = subjects[message_row[0]]
- text = text.translate(None, string.punctuation)
- text = text.translate(None, '1234567890')
+ text = text.translate(str.maketrans('','',string.punctuation))
+ text = text.translate(str.maketrans('','','1234567890'))
text = text.strip()
text = text.lower()
words = text.split()
@@ -54,3 +54,4 @@
fhand.write( "\n];\n")
print("Output written to gword.js")
+print("Open gword.htm in a browser to see the vizualization")
@@ -1,10 +1,10 @@
-119 +
-60 i
+61 i
42 and
+40 romeo
34 to
34 the
32 thou
-29 that
+32 juliet
+30 that
29 my
-28 juliet+
-27 romeo+
+24 thee

0 comments on commit fc51793

Please sign in to comment.