Python Forum
AttributeError: 'Response' object has no attribute 'replace'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AttributeError: 'Response' object has no attribute 'replace'
#1
#! python3
# using the inauguration speech of William Henry Harrison analyzed in the previous example, we can write the following code that generates arbitrarily # long Markov chains (with the chain length set to 100) based on the #structure of its text

import requests
from random import randint


def wordListSum(wordList):
    sum = 0
    for word, value in wordList.items():
        sum += value
    return sum


def retrieveRandomWord(wordList):
    randIndex = randint(1, wordListSum(wordList))
    for word, value in wordList.items():
        randIndex -= value
        if randIndex <= 0:
            return word


def buildWordDict(text):
    # Remove newlines and quotes
    text = text.replace("\n", " ")
    text = text.replace('"', "")
    # Make sure punctuation marks are treated as their own "words"
    # so that they will be included in the Markov chain
    punctuation = [",", ".", ";", ":"]
    for symbol in punctuation:
        text = text.replace(symbol, " " + symbol + " ")
    words = text.split(" ")
    # Filter out empty words
    words = [word for word in words if word != ""]
    wordDict = {}
    for i in range(1, len(words)):
        if words[i - 1] not in wordDict:
            # Create a new dictionary for this word
            wordDict[words[i - 1]] = {}
        if words[i] not in wordDict[words[i - 1]]:
            wordDict[words[i - 1]][words[i]] = 0
        wordDict[words[i - 1]][words[i]] += 1
    return wordDict


text = requests.get("http://pythonscraping.com/files/inaugurationSpeech.txt")
if text.status_code == 200:
    content = text.text
wordDict = buildWordDict(text)


# Generate a Markov chain of length 100
length = 100
chain = ""
currentWord = "I"
for i in range(0, length):
    chain += currentWord + " "
    currentWord = retrieveRandomWord(wordDict[currentWord])

print(chain)
Error:
Traceback (most recent call last): File "C:\Python36\kodovi\markov.py", line 49, in <module> wordDict = buildWordDict(text) File "C:\Python36\kodovi\markov.py", line 25, in buildWordDict text = text.replace("\n", " ") AttributeError: 'Response' object has no attribute 'replace' >>>
There is a replace method in the documentation, does this message means that there is not attribute new line?
And what is wrong with the line 49?
Reply


Messages In This Thread
AttributeError: 'Response' object has no attribute 'replace' - by Truman - Mar-17-2019, 12:07 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to fix "'dict_values' object has no attribute 'inject_wsgi'" in session_transacti devid 0 1,177 Aug-13-2023, 07:52 AM
Last Post: devid
  AttributeError: 'ellipsis' object has no attribute 'register_blueprint' Mechanicalpixelz 2 2,404 Dec-29-2021, 01:30 AM
Last Post: Mechanicalpixelz
  AttributeError: 'NoneType' object in a parser - stops it apollo 4 4,025 May-28-2021, 02:13 PM
Last Post: Daring_T
  AttributeError: ResultSet object has no attribute 'get_text' KatMac 1 4,386 May-07-2021, 05:32 PM
Last Post: snippsat
  Python 3.9 : BeautifulSoup: 'NoneType' object has no attribute 'text' fudgemasterultra 1 8,909 Mar-03-2021, 09:40 AM
Last Post: Larz60+
  'NavigableString' object has no attribute 'h2' RandomCoder 5 5,397 May-20-2020, 09:01 AM
Last Post: Larz60+
  AttributeError: 'str' object has no attribute 'xpath' nazmulfinance 4 10,476 Nov-11-2019, 05:15 PM
Last Post: nazmulfinance
  AttributeError: 'str' object has no attribute 'xpath' nazmulfinance 0 3,059 Nov-10-2019, 09:13 PM
Last Post: nazmulfinance
  form.populate_obj problem "object has no attribute translate" pascale 0 3,653 Jun-12-2019, 07:30 PM
Last Post: pascale
  Error object has no attribute text hcyeap 3 13,911 May-21-2019, 07:12 AM
Last Post: buran

Forum Jump:

User Panel Messages

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