Attribute error...?

I’m getting a strange error on my repl. It’s when I run it and then enter something into it’s input, e.g. hello there.
This is the link to the Repl.
The error:

Traceback (most recent call last):
  File "main.py", line 122, in <module>
    chatbot.getpos(t=chatbot.gettokens(s=user))
  File "main.py", line 63, in gettokens
    self.tokens.append(current)
AttributeError: 'tuple' object has no attribute 'tokens'

I’m using a class with an __init__ function instance to make two variables global for it. So I used self keyword. I don’t know what’s wrong with it.

Any help is appreciated

1 Like

Instead of your __init__ looking like this:

def __init__(*self,tokens,tk_pos):
    tokens = []
    self.tokens = tokens
    tk_pos = []
    self.tk_pos = tk_pos

make it look like this:

def __init__(self, tokens: list, tk_pos: list): # guessing the args are list
    self.tokens = tokens
    self.tk_pos = tk_pos
1 Like

I changed it, but the error isn’t appearing there. It’s on line 61 where it reads:

self.tokens.append(current)

Where the error is

AttributeError: 'tuple' object has no attribute 'tokens'

I’m assuming the 'tuple' object is refering to self. But self.tokens should be valid (and the .append method) so I’m not sure what to do.

Not sure this s the error but the following code is in the wrong place i think

while True:
  user = input("§ ")
  chatbot.getpos(t=chatbot.gettokens(s=user))
  print(self.tokens)
  print(self.tk_pos)

Alot of his code was incorrect I’m going through and fixing it all.

Here is the fixed code:

hints = [
    "             [ SUBLIME CHATBOT ]             ",
    "┏━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┓",
    "┃    Limitations      ┃    Capabilities     ┃",
    "┣━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━┫",
    "┃Doesn't know anything┃Can generate some    ┃",
    "┃about the world      ┃relevant responses   ┃",
    "┃                     ┃                     ┃",
    "┃Understands simple   ┃Can be conversational┃",
    "┃sentences only       ┃                     ┃",
    "┃                     ┃                     ┃",
    "┃P̶r̶o̶b̶a̶b̶l̶y not sentient┃                     ┃",
    "┗━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━┛",
]

for l in hints:
    print(l)
print()


class pos:
    dt = ["the"]  # determiner
    pr = ["to"]  # proposition
    pn = ["you", "he", "she", "they"]  # pronoun
    ob = (open("nouns.txt", "r").read()).split("\n")  # nouns/objects
    vb = (open("verbs.txt", "r").read()).split("\n")  # verb
    cj = ["and", "but"]  # conjunction
    it = ["hello", "goodbye", "good", "bad"]  # interjection
    qu = ["what", "when", "how", "why", "if"]  # question word(s)
    ad = []  # adverbs
    aj = []  # adjectives

    def allpos():
        allofthem = []
        allofthem.append(pos.dt)
        allofthem.append(pos.pr)
        allofthem.append(pos.pn)
        allofthem.append(pos.ob)
        allofthem.append(pos.vb)
        allofthem.append(pos.cj)
        allofthem.append(pos.it)
        allofthem.append(pos.qu)
        allofthem.append(pos.ad)
        allofthem.append(pos.aj)
        return allofthem


class chatbot:
    def __init__(self, tokens: list, tk_pos: list):  # guessing the args are list
        self.tokens = tokens
        self.tk_pos = tk_pos

    def gettokens(self, s):
        i = 0
        current = ""
        while i < len(s):
            if s[i].isalpha() and i < len(s):
                current += s[i]
                i += 1
                while i < len(s) and s[i].isalpha():
                    current += s[i]
                    i += 1
                self.tokens.append(current)
                current = ""
            else:
                i += 1
        return self.tokens
        print("Tokens:", str(self.tokens))
        del self.tokens

    def getpos(self, t):
        j = 0
        while j < len(t):
            if t[j] in pos.dt:  # and i < len(t):
                self.tk_pos.append({"type": "dt", "value": t[j]})
                # print(self.tk_pos[j-1])
                if not t[j + 1] in pos.allpos():
                    # print(f'{t[j+1]} is a noun')
                    self.tk_pos.append({"type": "nn", "value": t[j + 1]})
                    j += 1
                j += 1
            elif t[j] in pos.pr:  # and i < len(t):
                self.tk_pos.append({"type": "pr", "value": t[j]})
                j += 1
            elif t[j] in pos.pn:  # and i < len(t):
                self.tk_pos.append({"type": "pn", "value": t[j]})
                j += 1
            elif t[j] in pos.vb:  # and i < len(t):
                self.tk_pos.append({"type": "vb", "value": t[j]})
                j += 1
            elif t[j] in pos.cj:  # and i < len(t):
                self.tk_pos.append({"type": "cj", "value": t[j]})
                j += 1
            elif t[j] in pos.qu:  # and i < len(t):
                self.tk_pos.append({"type": "qu", "value": t[j]})
                j += 1
            elif t[j] in pos.it:  # and i < len(t):
                self.tk_pos.append({"type": "it", "value": t[j]})
                j += 1
            elif t[j] in pos.ob:  # and i < len(t):
                self.tk_pos.append({"type": "ob", "value": t[j]})
                j += 1
            elif t[j] in pos.vb:  # and i < len(t):
                self.tk_pos.append({"type": "vb", "value": t[j]})
                j += 1
            else:
                if t[j].isdigit():
                    self.tk_pos.append({"type": "nu", "value": int(t[j])})
                else:
                    if not t[j] in pos.allpos():
                        self.tk_pos.append({"type": "ob", "value": t[j]})
            j += 1
        return self.tk_pos
        print("POS Tokens:", str(self.tk_pos))
        del self.tk_pos

    def predictres(self):
        pass


while True:
    user = input("§ ")
    chat = chatbot([], [])
    chat.getpos(t=chat.gettokens(s=user))
1 Like

Had a quick look (working hours here so little free time/mind) and i saw lot of weird things.

I only fixed the errors that were persisting.

It’s a WIP project, so it’s gonna look weird (lots of odd print statements and other stuff) until it’s finished.
Also I tend to do rather weird or unorthodox things in my code.

Did this code work @lightdefusion if so can you mark it as the solution.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.