Python Forum
AttributeError: module 'string' has no attribute 'uppercase'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AttributeError: module 'string' has no attribute 'uppercase'
#1
Quote:trying to implement a match word to dictionary while return the error no attribure 'uppercase' [/quote]

from __future__ import print_function
import string
import re
import unittest

# You want to build a word cloud, an infographic where the size of a
# word corresponds to how often it appears in the body of text.

# To do this, you'll need data. Write code that takes a long string and
# builds its word cloud data in a dictionary, where the keys are
# words and the values are the number of times the words occurred.

# Think about capitalized words. For example, look at these sentences:

#  'After beating the eggs, Dana read the next step:'
#  'Add milk and eggs, then add flour and sugar.'

# What do we want to do with "After", "Dana", and "add"? In this
# example, your final dictionary should include one "Add" or "add" with
# a value of 22. Make reasonable (not necessarily perfect) decisions
# about cases like "After" and "Dana".

# Assume the input will only contain words and standard punctuation.


# ignore all punctuation except sentence enders . ! and ?

# lowercase any word that starts a sentence IFF it is also in the
# corpus of words as a lowercase word.

# How?

# split corpus into sentences (strings ending with a . ? or !)
# strip sentences into words
# push all words into a case sensitive, word frequency counting, dict
# scan the dict
#   if a cap word is in the dict as cap and downcase then downcase
# return dict

# we do make two passes through the input stream, but if this were a
# real problem, I'd use a lexing and parsing library to implement a
# real world's problem's requirements.


def word_cloud(input):
    """map string of words into a dict of word frequencies"""

    sentence_enders = r"\.|!|\?"
    sentences = re.split(sentence_enders, input)

    freq = {}
    for sentence in sentences:
        words = re.split(r"[^a-zA-Z0-9-]+", sentence)
        for word in words:
            count = freq.get(word, 0)
            freq[word] = count + 1

    def is_cap(word):
        ch = word[0:1]
        return ch in string.uppercase

    for word, count in freq.items():
        if is_cap(word) and word.lower() in freq:
            count = freq[word]
            freq[word.lower()] += count
            del freq[word]

    return freq


class TestWordCloud(unittest.TestCase):

    def test_examples(self):
        """test the given example"""
        test = 'After beating the eggs, Dana read the next step:' + \
            'Add milk and eggs, then add flour and sugar-free diet coke.'
        soln = {
            'After': 1,
            'Dana': 1,
            'add': 2,
            'and': 2,
            'beating': 1,
            'coke': 1,
            'diet': 1,
            'eggs': 2,
            'flour': 1,
            'milk': 1,
            'next': 1,
            'read': 1,
            'step': 1,
            'sugar-free': 1,
            'the': 2,
            'then': 1,
        }

        cloud = word_cloud(test)
        self.assertDictEqual(soln, cloud)

    def test_more_examples(self):
        "test some additional examples"

        tests = [
            ["We came, we saw, we conquered...then we ate Bill's "
             "(Mille-Feuille) cake."
             "The bill came to five dollars.",
             {
                 'Mille-Feuille': 1,
                 'The': 1,
                 'ate': 1,
                 'bill': 2,
                 'cake': 1,
                 'came': 2,
                 'conquered': 1,
                 'dollars': 1,
                 'five': 1,
                 's': 1,
                 'saw': 1,
                 'then': 1,
                 'to': 1,
                 'we': 4
             }
             ]
        ]

        for test, soln in tests:
            cloud = word_cloud(test)
            self.assertDictEqual(soln, cloud)


if __name__ == "__main__":
    unittest.main()
    suite = unittest.TestLoader().loadTestsFromTestCase(TestWordCloud)
    unittest.TextTestRunner(verbosity=2).run(suite)
[quote]Return this error

Error:
ERROR: test_examples (__main__.TestWordCloud) test the given example ---------------------------------------------------------------------- Traceback (most recent call last): File "D:/Python3.8.0/Python/Lib/WordFinder.py", line 105, in test_examples cloud = word_cloud(test) File "D:/Python3.8.0/Python/Lib/WordFinder.py", line 72, in word_cloud if is_cap(word) and word.lower() in freq: File "D:/Python3.8.0/Python/Lib/WordFinder.py", line 69, in is_cap return ch in string.uppercase AttributeError: module 'string' has no attribute 'uppercase' ====================================================================== ERROR: test_more_examples (__main__.TestWordCloud) test some additional examples ---------------------------------------------------------------------- Traceback (most recent call last): File "D:/Python3.8.0/Python/Lib/WordFinder.py", line 135, in test_more_examples cloud = word_cloud(test) File "D:/Python3.8.0/Python/Lib/WordFinder.py", line 72, in word_cloud if is_cap(word) and word.lower() in freq: File "D:/Python3.8.0/Python/Lib/WordFinder.py", line 69, in is_cap return ch in string.uppercase AttributeError: module 'string' has no attribute 'uppercase' ---------------------------------------------------------------------- Ran 2 tests in 0.000s FAILED (errors=2)
Reply


Messages In This Thread
AttributeError: module 'string' has no attribute 'uppercase' - by Anldra12 - Apr-22-2021, 04:52 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  trouble reading string/module from excel as a list popular_dog 0 512 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  getpass.getpass() results in AttributeError: module 'os' has no attribute 'O_NOCTTY' EarthAndMoon 4 1,007 Oct-03-2023, 02:00 PM
Last Post: deanhystad
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 2,286 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  Parallel processing - AttributeError: Can't get attribute 'sktimekmeans' Mohana1983 1 901 Jun-22-2023, 02:33 AM
Last Post: woooee
  Python: AttributeError: 'PageObject' object has no attribute 'extract_images' Melcu54 2 4,570 Jun-18-2023, 07:47 PM
Last Post: Melcu54
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,771 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,960 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 6,404 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  AttributeError: 'numpy.ndarray' object has no attribute 'load' hobbyist 8 7,522 Jul-06-2022, 10:55 AM
Last Post: deanhystad
  AttributeError: 'numpy.int32' object has no attribute 'split' rf_kartal 6 4,781 Jun-24-2022, 08:37 AM
Last Post: Anushka00

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020