RazvanP
RazvanP

Reputation: 131

TypeError: argument of type 'spacy.tokens.token.Token' is not iterable

I need to check if the subject of the sentence exist in a list, but I have some problems with this error and I don't understand how can I fix it

def __init__(self, user_input):
    personal_words = ["I", "ME", "US"]
    er = ["YOU"]
    pos = pop(user_input)
    for token in pos:
        if token.dep == nsubj:
            subject = token
            print(subject)
            if any(item in subject for item in personal_words):
                personal()
            elif any(item in subject for item in er):
                era()
            else:
                n_personal()

TypeError: argument of type 'spacy.tokens.token.Token' is not iterable

Upvotes: 0

Views: 5045

Answers (2)

Ines Montani
Ines Montani

Reputation: 7105

This isn't a bug – the Token object is a container object for the data of a single token, not an iterable sequence. In your code, you're checking item in subject, which expects subject (the token) to be a sequence. If you want to check whether the token text matches a string, you actually want to be checking token.text == string.

Upvotes: 1

Nicola Bernini
Nicola Bernini

Reputation: 111

As it should be iterable I supposed the problem is related to some bug in the internals possibly version specific (by the way, please next time share a bit more about your environment i.e. what version of of the different frameworks you are using) After a search I have found this one which seems to be related

TypeError: 'spacy.tokens.token.Token' object is not iterable

Just copy-pasting the proposed solutions

@larry0123du I solved this by installing Spacy==1.9 (I was using Spacy==2.0 before)
if word is "spacy.tokens.token.Token",you can chage it to "word.text" in Spacy==2.018 @larry0123du @ @brandenchan

Upvotes: 0

Related Questions