Search code examples
pythonlistnlpattributeerrornamed-entity-recognition

AttributeError: 'list' object has no attribute 'ents'


I am using this code and get the csv file as a list into doc [].

doc = []
with open(r'C:\Users\DELL\Desktop\Final project\Requirements1.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=';')
for riga in csv_reader:
    for campo in riga:
        print(campo)
        doc.append(nlp(campo))

But, When I do the named entity recognition for this using this code below,

for entity in doc.ents:
print(entity.text, entity.label)

I am getting this error.

AttributeError: 'list' object has no attribute 'ents'

What should I do about this? Please help me.enter image description here


Solution

  • Do this.

    docs = [] # NOTE THIS CHANGED
    with open(r'C:\Users\DELL\Desktop\Final project\Requirements1.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=';')
    for riga in csv_reader:
        for campo in riga:
            print(campo)
            docs.append(nlp(campo))
    
    # now to get the ner results...
    
    for doc in docs:
        for ent in doc.ents:
            print(ent.text, ent.label)