AttributeError: объект 'spacy.tokens.doc.Doc' не имеет атрибута 'lower'

Я работаю над добавлением текстов в список, а затем меняю тексты на вложения слов, а затем занимаюсь машинным обучением. «Insts» в «статьях» собираются с помощью spacy, но затем я столкнулся с этой ошибкой, как показано ниже. Кто-нибудь может сказать мне, как исправить эту ошибку? Могу ли я изменить тип spacy.tokens.doc.Doc на str?

def main(annotations_file, max_insts=-1):
    articles = reader.read_corpus(annotations_file,max_insts=max_insts)
    texts=[]
    random.seed(5)
    random.shuffle(articles)
    # arti = list()
    sect = list()
    label_bef=list()
    label_dur=list()
    label_aft=list()

    for insts in articles:
        for inst in insts:
            texts.append(inst.possessor.doc._.article_title_doc)
            #sect.append(inst.possessor.doc._.section_title_doc)
            label_bef.append(inst.labels['BEF'])
            label_dur.append(inst.labels['DUR'])
            label_aft.append(inst.labels['AFT'])

    embeddings_index = {}
    with open('glove.6B.100d.txt') as f:
        for line in f:
            word, coefs = line.split(maxsplit=1)
            coefs = np.fromstring(coefs, 'f', sep=' ')
            embeddings_index[word] = coefs

    tokenizer = Tokenizer(num_words=MAX_NUM_WORDS)
    tokenizer.fit_on_texts(texts)
    word_index = tokenizer.word_index
Traceback (most recent call last):
   File "sample.py", line 117, in <module>
     main(args.ANNOTATIONS_FILE, args.max_articles)
   File "sample.py", line 51, in main
     tokenizer.fit_on_texts(texts)
   File "/home/huweilong/miniconda3/envs/nre/lib/python3.6/site-packages/keras_preprocessing/text.py", line 223, in fit_on_texts
self.split)
   File "/home/huweilong/miniconda3/envs/nre/lib/python3.6/site-packages/keras_preprocessing/text.py", line 43, in text_to_word_sequence
     text = text.lower()
   AttributeError: 'spacy.tokens.doc.Doc' object has no attribute 'lower'

person excelsior    schedule 10.03.2020    source источник
comment
Просто сделай doc.lower_   -  person Alaa M.    schedule 18.03.2021


Ответы (1)


Вы можете получить строковое представление документа spaCy, вызвав doc.text.

person Anastasiia Iurshina    schedule 25.03.2020