From 1fd6af72a569ac70c104016b930bacfe59dd3e3b Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Fri, 18 Oct 2019 17:59:17 -0500 Subject: [PATCH 01/23] Add nltk 3.4.5 --- requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 71721d3..9250f1d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -python-docx==0.8.10 \ No newline at end of file +python-docx==0.8.10 +nltk==3.4.5 \ No newline at end of file From 87fb3b1edef9eed4ddaab049aee5420409fd5aeb Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Fri, 18 Oct 2019 19:33:34 -0500 Subject: [PATCH 02/23] Initial commit --- src/ch08/__init__.py | 1 + src/ch08/p1_count_syllables.py | 96 +++++++++++++++++++++++++++++ src/ch08/p1files/missing_words.json | 1 + tests/test_chapter08.py | 25 ++++++++ 4 files changed, 123 insertions(+) create mode 100644 src/ch08/__init__.py create mode 100644 src/ch08/p1_count_syllables.py create mode 100644 src/ch08/p1files/missing_words.json create mode 100644 tests/test_chapter08.py diff --git a/src/ch08/__init__.py b/src/ch08/__init__.py new file mode 100644 index 0000000..6bb9e9d --- /dev/null +++ b/src/ch08/__init__.py @@ -0,0 +1 @@ +"""Chapter 8.""" diff --git a/src/ch08/p1_count_syllables.py b/src/ch08/p1_count_syllables.py new file mode 100644 index 0000000..952a911 --- /dev/null +++ b/src/ch08/p1_count_syllables.py @@ -0,0 +1,96 @@ +"""Test count_syllables with a word dictionary file. + +Randomly select words from a word dictionary file and pass them through +:func:`count_syllables` to find their syllable counts. Output each word with +their respective syllable count. + +""" +import json +import os +from random import choice +from string import punctuation + +from nltk.corpus import cmudict + +from src.ch02 import DICTIONARY_FILE_PATH +from src.ch02.p1_cleanup_dictionary import cleanup_dict + +# Convert CMUdict into a dictionary. +CMUDICT = cmudict.dict() + +with open(os.path.join(os.path.dirname(__file__), + 'p1files/missing_words.json')) as in_file: + # Load local dictionary of words with syllable counts. + # Words as strings are keys and integers are values. + MISSING_WORDS = json.load(in_file) + + +def format_words(words: str) -> list: + """Format words for processing. + + Remove hyphens, convert to lowercase, and strip both punctuation and + possessives from word or phrase. + + Args: + words (str): Word or phrase to format for processing. + + Returns: + List of strings containing processed words. + + """ + words = words.replace('-', ' ') + word_list = words.lower().split() + for i, word in enumerate(word_list): + word = word.strip(punctuation) + if any([word.endswith("'s"), word.endswith("’s")]): + word_list[i] = word[:-2] + else: + word_list[i] = word + return word_list + + +def count_syllables(words: list) -> int: + """Use CMUdict to count syllables in English word. + + Calculate sum of syllable counts for each word in **words**. Checks + syllable counts in CMUdict's phoneme list, if word is not found in + CMUdict, also checks local dictionary with syllable counts. + + Args: + words (list): List of strings to sum number of syllables. + + Returns: + Integer representing number of syllables in **words**. + + Note: + Defaults to first element in CMUdict phoneme list. So, multiple + syllable counts are ignored. + + """ + syllables = 0 + for word in words: + if word in MISSING_WORDS: + syllables += MISSING_WORDS[word] + else: + for phonemes in CMUDICT[word][0]: + for phoneme in phonemes: + if phoneme[-1].isdigit(): + syllables += 1 + return syllables + + +def main(): + """Demonstrate count_syllables with a word dictionary file.""" + word_list = cleanup_dict(DICTIONARY_FILE_PATH) + for _ in range(15): + word = choice(word_list) + try: + syllables = count_syllables(format_words(word)) + except KeyError: + # Skip words in neither dictionary. + continue + print(f'{word} {syllables}') + + +if __name__ == '__main__': + main() diff --git a/src/ch08/p1files/missing_words.json b/src/ch08/p1files/missing_words.json new file mode 100644 index 0000000..99100cc --- /dev/null +++ b/src/ch08/p1files/missing_words.json @@ -0,0 +1 @@ +{"househusband": 3, "ibices": 3, "smooching": 2, "handpicking": 3, "tuxes": 2} \ No newline at end of file diff --git a/tests/test_chapter08.py b/tests/test_chapter08.py new file mode 100644 index 0000000..f3c9293 --- /dev/null +++ b/tests/test_chapter08.py @@ -0,0 +1,25 @@ +"""Test Chapter 8.""" +import unittest.mock + +import src.ch08.p1_count_syllables as count_syllables + + +class TestCountSyllables(unittest.TestCase): + """Test Count Syllables.""" + + def test_format_words(self): + """Test format_words.""" + # Test convert to lowercase. + for word in ['YOU', 'You', 'yOu', 'yoU', 'yOU', 'YOu', 'YoU', 'you']: + self.assertEqual(count_syllables.format_words(word), ['you']) + # Test remove hyphens. + self.assertEqual(count_syllables.format_words('nit-pick'), ['nit', 'pick']) + # Test remove punctuation. + self.assertEqual(count_syllables.format_words('nit-pick!'), ['nit', 'pick']) + # Test remove possessives. + for word in ['test’s', 'test\'s']: + self.assertEqual(count_syllables.format_words(word), ['test']) + + +if __name__ == '__main__': + unittest.main() From 74866273ba75065ecd939926dfc438c86f7aa661 Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Fri, 18 Oct 2019 19:43:53 -0500 Subject: [PATCH 03/23] Add test_count_syllables to TestCountSyllables --- tests/test_chapter08.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_chapter08.py b/tests/test_chapter08.py index f3c9293..5cbefd7 100644 --- a/tests/test_chapter08.py +++ b/tests/test_chapter08.py @@ -20,6 +20,13 @@ def test_format_words(self): for word in ['test’s', 'test\'s']: self.assertEqual(count_syllables.format_words(word), ['test']) + def test_count_syllables(self): + """Test count_syllables.""" + # Test word not in CMUdict. + self.assertEqual(count_syllables.count_syllables(['tuxes']), 2) + # Test word in CMUdict. + self.assertEqual(count_syllables.count_syllables(['test']), 1) + if __name__ == '__main__': unittest.main() From 3876c4fd7cfb6fc88a4958ddd329e7521109bbd6 Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Fri, 18 Oct 2019 19:59:15 -0500 Subject: [PATCH 04/23] Initial commit --- tests/data/ch08/dictionary.txt | 78 ++++++++++++++++++++++++ tests/data/ch08/main/count_syllables.txt | 14 +++++ 2 files changed, 92 insertions(+) create mode 100644 tests/data/ch08/dictionary.txt create mode 100644 tests/data/ch08/main/count_syllables.txt diff --git a/tests/data/ch08/dictionary.txt b/tests/data/ch08/dictionary.txt new file mode 100644 index 0000000..4e77037 --- /dev/null +++ b/tests/data/ch08/dictionary.txt @@ -0,0 +1,78 @@ +a +aardvark +abracadabra +b +bee +boson +c +cat +catatonic +d +dog +dirge +e +echo +ebeneezer +f +fox +finicky +g +gecko +gopher +h +hemoglobin +hermit +i +imp +indigo +j +jack-o-lantern +journey +k +kangaroo +kilometer +l +lemon +lime +m +mesolithic +moonlight +n +none +night +o +opaque +opulent +p +penny +pepper +q +quasar +quark +r +riddle +rubber +s +slight +swift +t +tonberry +tomato +u +ultraviolet +umbra +v +venus +vertiginous +w +whip +whirl +x +xena +xenon +y +yacht +yggdrasil +z +zen +zero \ No newline at end of file diff --git a/tests/data/ch08/main/count_syllables.txt b/tests/data/ch08/main/count_syllables.txt new file mode 100644 index 0000000..e9e74df --- /dev/null +++ b/tests/data/ch08/main/count_syllables.txt @@ -0,0 +1,14 @@ +dog 1 +hermit 2 +jack-o-lantern 4 +journey 2 +bee 1 +abracadabra 5 +penny 2 +hemoglobin 4 +opaque 2 +xenon 2 +xena 2 +opaque 2 +opaque 2 +cat 1 From 941fe04536401ff05c4654cecc979214e53c2726 Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Fri, 18 Oct 2019 19:59:40 -0500 Subject: [PATCH 05/23] Add test_main and setUpClass to TestCountSyllables --- tests/test_chapter08.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/test_chapter08.py b/tests/test_chapter08.py index 5cbefd7..e7259ea 100644 --- a/tests/test_chapter08.py +++ b/tests/test_chapter08.py @@ -1,5 +1,8 @@ """Test Chapter 8.""" import unittest.mock +import os +from random import Random +from io import StringIO import src.ch08.p1_count_syllables as count_syllables @@ -7,6 +10,11 @@ class TestCountSyllables(unittest.TestCase): """Test Count Syllables.""" + @classmethod + def setUpClass(cls): + """Configure attributes for use in this class only.""" + cls.random = Random() + def test_format_words(self): """Test format_words.""" # Test convert to lowercase. @@ -27,6 +35,22 @@ def test_count_syllables(self): # Test word in CMUdict. self.assertEqual(count_syllables.count_syllables(['test']), 1) + @unittest.mock.patch('src.ch08.p1_count_syllables.DICTIONARY_FILE_PATH', 'tests/data/ch08/dictionary.txt') + @unittest.mock.patch('sys.stdout', new_callable=StringIO) + @unittest.mock.patch('src.ch08.p1_count_syllables.choice') + def test_main(self, mock_choice, mock_stdout): + """Test demo main function.""" + self.random.seed(222) + mock_choice._mock_side_effect = self.random.choice + + count_syllables.main() + + # Test sys.stdout output. + with open(os.path.normpath('tests/data/ch08/main/count_syllables.txt'), + 'r') as file: + file_data = ''.join(file.readlines()) + self.assertEqual(mock_stdout.getvalue(), file_data) + if __name__ == '__main__': unittest.main() From 4cc5d4e6177bbe4854e88bca94a4b75d3eb212d0 Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Fri, 18 Oct 2019 20:00:54 -0500 Subject: [PATCH 06/23] Initial commit --- docs/source/src.ch08.rst | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 docs/source/src.ch08.rst diff --git a/docs/source/src.ch08.rst b/docs/source/src.ch08.rst new file mode 100644 index 0000000..cfbdca5 --- /dev/null +++ b/docs/source/src.ch08.rst @@ -0,0 +1,22 @@ +src.ch08 package +================ + +Submodules +---------- + +src.ch08.p1\_count\_syllables module +------------------------------------ + +.. automodule:: src.ch08.p1_count_syllables + :members: + :undoc-members: + :show-inheritance: + + +Module contents +--------------- + +.. automodule:: src.ch08 + :members: + :undoc-members: + :show-inheritance: From a1edcc5224b51837be8f430ba5a1b8bd35625afb Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Fri, 18 Oct 2019 20:01:04 -0500 Subject: [PATCH 07/23] Add src.ch08 --- docs/source/src.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/source/src.rst b/docs/source/src.rst index c27558a..63a119c 100644 --- a/docs/source/src.rst +++ b/docs/source/src.rst @@ -13,6 +13,7 @@ Subpackages src.ch05 src.ch06 src.ch07 + src.ch08 Module contents --------------- From 3dc409a887808dc48a11678ad21215128604de77 Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Fri, 18 Oct 2019 20:03:10 -0500 Subject: [PATCH 08/23] Fix spacing for skipsdist --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index e5fe121..a7984e2 100644 --- a/tox.ini +++ b/tox.ini @@ -6,7 +6,7 @@ [tox] envlist = py36, py37, lint, pydocstyle, sphinx skip_missing_interpreters = True -skipsdist=True +skipsdist = True [testenv] deps = From 56576bba5829102b0f6727118b3a96045107b2de Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Fri, 18 Oct 2019 20:11:49 -0500 Subject: [PATCH 09/23] Add nltk to intersphinx_mapping --- docs/source/conf.py | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/source/conf.py b/docs/source/conf.py index 1c1a756..3529a38 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -55,6 +55,7 @@ intersphinx_mapping = { 'python': ('https://docs.python.org/3/', None), 'docx': ('https://python-docx.readthedocs.io/en/latest/', None), + 'nltk': ('http://www.nltk.org/', None), } # Default options for autodoc directives. From 2a3ac2abd860d0f10b99c898c48376629eb47315 Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Fri, 18 Oct 2019 20:18:41 -0500 Subject: [PATCH 10/23] Add sphinx reference to nltk in count_syllables docstring --- src/ch08/p1_count_syllables.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ch08/p1_count_syllables.py b/src/ch08/p1_count_syllables.py index 952a911..3339376 100644 --- a/src/ch08/p1_count_syllables.py +++ b/src/ch08/p1_count_syllables.py @@ -53,8 +53,9 @@ def count_syllables(words: list) -> int: """Use CMUdict to count syllables in English word. Calculate sum of syllable counts for each word in **words**. Checks - syllable counts in CMUdict's phoneme list, if word is not found in - CMUdict, also checks local dictionary with syllable counts. + syllable counts in the :py:mod:`nltk.corpus` CMUdict phoneme list, if word + is not found in CMUdict, also checks local dictionary with syllable + counts. Args: words (list): List of strings to sum number of syllables. From e65fac894e72d6d82e206eee098604328fddaf05 Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Sat, 19 Oct 2019 13:02:49 -0500 Subject: [PATCH 11/23] Add another test to test_format_words --- tests/test_chapter08.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_chapter08.py b/tests/test_chapter08.py index e7259ea..5361caa 100644 --- a/tests/test_chapter08.py +++ b/tests/test_chapter08.py @@ -27,6 +27,8 @@ def test_format_words(self): # Test remove possessives. for word in ['test’s', 'test\'s']: self.assertEqual(count_syllables.format_words(word), ['test']) + # Test phrase. + self.assertEqual(count_syllables.format_words('TEST nit-pick'), ['test', 'nit', 'pick']) def test_count_syllables(self): """Test count_syllables.""" From e0a9d50a34a64efc229f5ee13565c35c3b6034f8 Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Sat, 19 Oct 2019 14:20:11 -0500 Subject: [PATCH 12/23] Add nltk.corpus.cmudict to install section --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index efb22c4..8f7d120 100644 --- a/.travis.yml +++ b/.travis.yml @@ -49,6 +49,7 @@ matrix: # Dependencies needed to run tox tests. install: - pip install tox + - python -m nltk.downloader cmudict # Run each test in matrix script: - tox -e $TOXENV \ No newline at end of file From 321281e1efbc1784c645da38f3ae541647b066e1 Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Sat, 19 Oct 2019 14:27:24 -0500 Subject: [PATCH 13/23] Add nltk to pip install --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 8f7d120..cc41c5b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -48,7 +48,7 @@ matrix: env: TOXENV=coveralls # Dependencies needed to run tox tests. install: - - pip install tox + - pip install tox nltk - python -m nltk.downloader cmudict # Run each test in matrix script: From 851917562dadb41bcffd3a7cb2f198b47c9c262a Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Sat, 19 Oct 2019 15:26:23 -0500 Subject: [PATCH 14/23] Revert "Add nltk to pip install" This reverts commit 321281e1 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index cc41c5b..8f7d120 100644 --- a/.travis.yml +++ b/.travis.yml @@ -48,7 +48,7 @@ matrix: env: TOXENV=coveralls # Dependencies needed to run tox tests. install: - - pip install tox nltk + - pip install tox - python -m nltk.downloader cmudict # Run each test in matrix script: From ae8fde9d26821adc95cad542859c6e22bf63f8bd Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Sat, 19 Oct 2019 15:26:33 -0500 Subject: [PATCH 15/23] Revert "Add nltk.corpus.cmudict to install section" This reverts commit e0a9d50a --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 8f7d120..efb22c4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -49,7 +49,6 @@ matrix: # Dependencies needed to run tox tests. install: - pip install tox - - python -m nltk.downloader cmudict # Run each test in matrix script: - tox -e $TOXENV \ No newline at end of file From 34b6b614bfa1c9d52af233d5ffba52ca6d09ffd8 Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Sat, 19 Oct 2019 15:29:06 -0500 Subject: [PATCH 16/23] Add cmudict download to module --- src/ch08/p1_count_syllables.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ch08/p1_count_syllables.py b/src/ch08/p1_count_syllables.py index 3339376..a96388c 100644 --- a/src/ch08/p1_count_syllables.py +++ b/src/ch08/p1_count_syllables.py @@ -10,11 +10,14 @@ from random import choice from string import punctuation +import nltk from nltk.corpus import cmudict from src.ch02 import DICTIONARY_FILE_PATH from src.ch02.p1_cleanup_dictionary import cleanup_dict +nltk.download('cmudict') + # Convert CMUdict into a dictionary. CMUDICT = cmudict.dict() From 6f37d7ca2fb9249384e52bb608d5e047e4525aca Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Sat, 19 Oct 2019 16:32:57 -0500 Subject: [PATCH 17/23] Add path check to cmudict download --- src/ch08/p1_count_syllables.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ch08/p1_count_syllables.py b/src/ch08/p1_count_syllables.py index a96388c..bce3bfd 100644 --- a/src/ch08/p1_count_syllables.py +++ b/src/ch08/p1_count_syllables.py @@ -16,7 +16,10 @@ from src.ch02 import DICTIONARY_FILE_PATH from src.ch02.p1_cleanup_dictionary import cleanup_dict -nltk.download('cmudict') +if not os.path.exists(os.path.expanduser('~/nltk_data/corpora/cmudict/cmudict')): + # FIXME: This is nearly impossible to test. + # Patching os affects every use of os in the module. + nltk.download('cmudict') # Convert CMUdict into a dictionary. CMUDICT = cmudict.dict() From bd9230315e4088f298b5229dd3e5b7f5c8ff0bf6 Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Sat, 19 Oct 2019 16:44:03 -0500 Subject: [PATCH 18/23] Fix pylint line-too-long and locally disable fixme --- src/ch08/p1_count_syllables.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ch08/p1_count_syllables.py b/src/ch08/p1_count_syllables.py index bce3bfd..2e68a25 100644 --- a/src/ch08/p1_count_syllables.py +++ b/src/ch08/p1_count_syllables.py @@ -16,7 +16,9 @@ from src.ch02 import DICTIONARY_FILE_PATH from src.ch02.p1_cleanup_dictionary import cleanup_dict -if not os.path.exists(os.path.expanduser('~/nltk_data/corpora/cmudict/cmudict')): +if not os.path.exists( + os.path.expanduser('~/nltk_data/corpora/cmudict/cmudict')): + # pylint: disable=fixme # FIXME: This is nearly impossible to test. # Patching os affects every use of os in the module. nltk.download('cmudict') From 52c96a00f47fdf67a70781dbfed3cdf2196189f7 Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Sat, 19 Oct 2019 16:56:39 -0500 Subject: [PATCH 19/23] Add nltk corpora directory to cache --- .travis.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index efb22c4..0bd9cdc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,10 @@ # https://travis-ci.com/JoseALermaIII dist: bionic # required for Python >= 3.7 language: python -cache: pip # Don't delete pip install +cache: + pip: true # Don't delete pip install + directories: + - $HOME/nltk_data/corpora/ # Branch safelist branches: only: From fc3663ad5ca5e25da5bda5445b910815c9d4286b Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Mon, 21 Oct 2019 21:17:57 -0500 Subject: [PATCH 20/23] Add attributes section to module docstring --- src/ch08/p1_count_syllables.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/ch08/p1_count_syllables.py b/src/ch08/p1_count_syllables.py index 2e68a25..95a444d 100644 --- a/src/ch08/p1_count_syllables.py +++ b/src/ch08/p1_count_syllables.py @@ -4,6 +4,13 @@ :func:`count_syllables` to find their syllable counts. Output each word with their respective syllable count. +Attributes: + CMUDICT (dict): Dictionary of CMUdict's phonemes with the word as a key + and its phonemes as a list of lists. + MISSING_WORDS (dict): Dictionary with syllable counts of words + missing from CMUdict's phoneme list where the word is the key and + its syllable count as an integer value. + """ import json import os From d2c3f56d187886399732f871b4eb8b0c227add30 Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Fri, 25 Oct 2019 16:32:41 -0500 Subject: [PATCH 21/23] Refactor main to use random sample --- src/ch08/p1_count_syllables.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ch08/p1_count_syllables.py b/src/ch08/p1_count_syllables.py index 95a444d..974f7dd 100644 --- a/src/ch08/p1_count_syllables.py +++ b/src/ch08/p1_count_syllables.py @@ -14,7 +14,7 @@ """ import json import os -from random import choice +from random import sample from string import punctuation import nltk @@ -98,8 +98,8 @@ def count_syllables(words: list) -> int: def main(): """Demonstrate count_syllables with a word dictionary file.""" word_list = cleanup_dict(DICTIONARY_FILE_PATH) - for _ in range(15): - word = choice(word_list) + sample_list = sample(word_list, 15) + for word in sample_list: try: syllables = count_syllables(format_words(word)) except KeyError: From b99cc363743101c6226dfef5cbf77dc62aa3b153 Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Fri, 25 Oct 2019 16:33:22 -0500 Subject: [PATCH 22/23] Refactor main to display words in neither dictionary --- src/ch08/p1_count_syllables.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ch08/p1_count_syllables.py b/src/ch08/p1_count_syllables.py index 974f7dd..90a2a00 100644 --- a/src/ch08/p1_count_syllables.py +++ b/src/ch08/p1_count_syllables.py @@ -104,6 +104,7 @@ def main(): syllables = count_syllables(format_words(word)) except KeyError: # Skip words in neither dictionary. + print(f'Not found: {word}') continue print(f'{word} {syllables}') From 4a73bd421826f65e083a46249342bedc855c1632 Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Fri, 25 Oct 2019 16:34:11 -0500 Subject: [PATCH 23/23] Refactor tests to reflect changes --- tests/data/ch08/main/count_syllables.txt | 9 +++++---- tests/test_chapter08.py | 6 +++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/tests/data/ch08/main/count_syllables.txt b/tests/data/ch08/main/count_syllables.txt index e9e74df..036a7b9 100644 --- a/tests/data/ch08/main/count_syllables.txt +++ b/tests/data/ch08/main/count_syllables.txt @@ -1,3 +1,4 @@ +Not found: yggdrasil dog 1 hermit 2 jack-o-lantern 4 @@ -7,8 +8,8 @@ abracadabra 5 penny 2 hemoglobin 4 opaque 2 -xenon 2 -xena 2 -opaque 2 -opaque 2 +venus 2 +umbra 2 cat 1 +whirl 1 +zen 1 diff --git a/tests/test_chapter08.py b/tests/test_chapter08.py index 5361caa..e8d1a3f 100644 --- a/tests/test_chapter08.py +++ b/tests/test_chapter08.py @@ -39,11 +39,11 @@ def test_count_syllables(self): @unittest.mock.patch('src.ch08.p1_count_syllables.DICTIONARY_FILE_PATH', 'tests/data/ch08/dictionary.txt') @unittest.mock.patch('sys.stdout', new_callable=StringIO) - @unittest.mock.patch('src.ch08.p1_count_syllables.choice') - def test_main(self, mock_choice, mock_stdout): + @unittest.mock.patch('src.ch08.p1_count_syllables.sample') + def test_main(self, mock_sample, mock_stdout): """Test demo main function.""" self.random.seed(222) - mock_choice._mock_side_effect = self.random.choice + mock_sample.side_effect = self.random.sample count_syllables.main()