You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 80d7e43
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: source/exercises/trigrams/trigrams.rst
+32-21Lines changed: 32 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -136,14 +136,15 @@ I’ll fire the signal and the fun will commence...
136
136
Developing Your Solution
137
137
========================
138
138
139
-
This assignment has two parts: the key one is the trigrams exercise itself, but you also need to do some text processing to get a full book in shape for processing.
139
+
This assignment has two parts: the key one is the trigrams exercise itself, but you also need to do some text processing to get a full book in shape for the building the trigrams.
140
140
141
141
I suggest you write the trigrams part first; it's more interesting :-)
142
142
143
+
143
144
Test Driven Development
144
145
-----------------------
145
146
146
-
You've recently learned about unit testing and Test Driven Development (TDD). That's put that to work on this exercise. Remember that the key to TDD is that you first decide on what you need a piece of your code to do, then you write a test to check that it does that, and only then do you write the actual code itself.
147
+
You've recently learned about unit testing and Test Driven Development (TDD). Let's put that to work on this exercise. Remember that the key to TDD is that you first decide on what you need a piece of your code to do, then you write a test to check that it does that, and only then do you write the actual code itself.
147
148
148
149
Because you're new to this, we're going to give you some tests to get you started. You'll find them in: :download:`test_trigrams.py <./test_trigrams.py>`, which should be with this assignment.
149
150
@@ -173,20 +174,23 @@ To run the tests, use the ``pytest`` test runner: set your working directory the
173
174
!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!
174
175
======================== 1 error in 0.13s =========================
175
176
176
-
You should have gotten something like that error: it is indicating that the "trigrams" module does not exist -- which makes sense, as you haven't written in yet. So the first step is the create your code file: name it ``trigrams.py`` and put it in the same directory as the ``test_trigrams.py`` file. It can be empty for now. Now try running pytest again, and it should get farther: you'll have a lot of test failures, but the test should actually run. You should get something like:
177
+
You should have gotten something like that error: it is indicating that the "trigrams" module does not exist -- which makes sense, as you haven't written it yet. So the first step is to create your code file: name it ``trigrams.py`` and put it in the same directory as the ``test_trigrams.py`` file. It can be empty for now. Now try running pytest again, and it should get farther: you'll have a lot of test failures, but the test should actually run. You should get something like:
177
178
178
179
.. code-block:: bash
179
180
180
-
...
181
-
> tris = trigrams.build_trigram(IWISH)
182
-
E AttributeError: module 'trigrams' has no attribute 'build_trigram'
183
-
184
-
test_trigrams.py:29: AttributeError
181
+
test_trigrams.py:130: AttributeError
185
182
====================== short test summary info =======================
@@ -319,7 +323,7 @@ So contrary to the usual practice, an index can be helpful here:
319
323
320
324
For each pair in the text, you need to add it to the dict. But:
321
325
322
-
- ``words[i:i + 2]`` is a list with two words in it. Can that be used as a key in a dict? (Try it.) If not, how can you make a valid key out of it?
326
+
- ``words[i:i + 2]`` is a list with two words in it. Can that be used as a key in a dict? (try it.) If not, how can you make a valid key out of it?
323
327
324
328
- As you loop through the text, you will collect pairs of words. Each time, a given pair may already be in the dict.
325
329
@@ -382,7 +386,7 @@ and one that tests if the following word lists are correct:
382
386
383
387
Note that if the first test fails, almost certainly the second will too (the second test explicitly looks for all the same keys). That's OK. It's still good to keep them separate, because the first test could pass while the second one fails -- it's nice to know you've made progress!
384
388
385
-
If it seems like we have hard-coded a lot of detail in-- you are right. But this is quite deliberate. And it is why we chose such a simple set of words to start out with. If you want read a bit more about this approach, this blogger puts it nicely:
389
+
If it seems like we have hard-coded a lot of detail into the tests -- you are right. But this is quite deliberate. And it is why we chose such a simple set of words to start out with. If you want toread a bit more about this approach, this blogger puts it nicely:
If both tests pass, you should now have code that will return a dict like we noted above::
@@ -400,7 +404,7 @@ Using the Trigrams dict
400
404
401
405
This is the fun part. Once you have a mapping of word pairs to following words, you can build up some new "fake" text. Re-read the previous sections again to remind yourself of the procedure. Here are a couple of additional hints and questions to consider:
402
406
403
-
- The ``random`` module <https://docs.python.org/3/library/random.html#module-random> is your friend here:
407
+
- The ``random`` module: https://docs.python.org/3/library/random.html#module-random is your friend here:
404
408
405
409
.. code-block:: python
406
410
@@ -412,14 +416,14 @@ This is the fun part. Once you have a mapping of word pairs to following words,
412
416
# pick a random item from a sequence
413
417
random.choice(a_list)
414
418
415
-
This is all pretty tricky to test -- after all, you are selecting random words -- you can't know what the result should be! There are two tactics you can take here.
419
+
This is all pretty tricky to test -- after all, you are selecting random words -- you can't know what the result should be! There are two tactics you can take to test code with ``random`` calls in it.
416
420
417
421
Tactic one is to break you code down into pieces that you *can*test -- everything BUT the random choices.
418
422
419
423
Tactic two is to set the random seed before each test, to assure the same result.
420
424
The built in``random`` module `provides a way to set the seed <https://docs.python.org/3/library/random.html#random.seed>`_: the ``random.seed()`` function.
421
425
422
-
.. note:: Computers don't really make truly random numbers. What they do is compute a sequence of numbers that are statistically very much like random numbers, known as `"pseudo random numbers" <https://en.wikipedia.org/wiki/Pseudorandom_number_generator>`_. If you start with the same initial value, known as the "seed", then you will get the same sequence of numbers. `Random seed <https://en.wikipedia.org/wiki/Random_seed>`_
426
+
.. note:: Computers don't really make truly random numbers. What they do is compute a sequence of numbers that are statistically very much like random numbers, known as `"pseudo random numbers" <https://en.wikipedia.org/wiki/Pseudorandom_number_generator>`_. If you start with the same initial value, known as the "seed", then you will get the same sequence of numbers (`random seed <https://en.wikipedia.org/wiki/Random_seed>`_).
423
427
424
428
The provided tests use both of these tactics.
425
429
@@ -442,7 +446,7 @@ The provided tests use both of these tactics.
442
446
443
447
So you'll need to define a function: ``pick_random_pair()`` that takes your trigram dict as input, and returns a random key.
444
448
445
-
Note that the particular result in the test is using a particular algorithm -- if you use a different one, you might get a different pair -- but you should get the same one every time, so you can make the test check for that.
449
+
Note that the particular result in the test is using a particular algorithm -- if you use a different one, you might get a different pair -- but since the seed is set, you should get the same one every time the test is run, so you can make the test check for the one your code returns.
446
450
447
451
Once you've got the first starting pair, you'll need to make your text,
448
452
so you'll need a data structure to build it up in. You probably want to build it up in a list, appending one word at a time. You can join it together at the end with ``"".join(the_list_of_words)``, which will make a string, separating the words with a space.
@@ -532,6 +536,8 @@ You now have the pieces you need to make some new text. Let's write a function t
532
536
# check that there is not a space between the period and the last word.
533
537
assert not sentence[-2].isspace()
534
538
539
+
Notice that this test did not set the random seed. Rather, it checked for various properties of the results, without checking for specific words. This is a helpful tactic -- have your tests check for what is important about the results, not necessarily the specific results.
540
+
535
541
You can now use the previous functions to make a ``make_sentence()`` function that passes these tests.
536
542
537
543
@@ -551,9 +557,7 @@ Hint: in a Project Gutenberg e-book, there is a line of text that starts with::
551
557
552
558
*** START OF THIS PROJECT GUTENBERG EBOOK
553
559
554
-
In the loop, you can process a single line of text to break it into words:
555
-
556
-
- calling ``.split()``
560
+
In the loop, you can process a single line of text to break it into words by calling ``.split()``.
557
561
558
562
Optional steps to cleaning up the text:
559
563
@@ -569,6 +573,9 @@ Be sure to use TDD as you develop the "clean up" code: write a test for one feat
569
573
570
574
Lather, rinse and repeat.
571
575
576
+
There are a number of tests for cleaning up the code commented out in the test file. Feel free to use these as a starting point.
577
+
578
+
572
579
**Hints:**
573
580
574
581
The ``string`` methods are your friend here.
@@ -606,4 +613,8 @@ For instance, your ``__main__`` block might look something like:
0 commit comments