【问题标题】:I'm getting this error AttributeError: 'NoneType' object has no attribute 'word_index'我收到此错误 AttributeError: 'NoneType' object has no attribute 'word_index'
【发布时间】:2020-08-03 10:28:55
【问题描述】:

即使当我打印句子时,它也会打印由逗号分隔的字符串数 这是我的代码:-

from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
import json

sentence = []
urls = []
labels = []

data = [json.loads(line) for line in open('Sarcasm_Headlines_Dataset.json', 'r')]
for item in data:
    sentence.append(item["headline"])
    urls.append(item['article_link'])
    labels.append(item['is_sarcastic'])
    
print(sentence)
tokenizer = Tokenizer(oov_token="<00V>")
tokenizer = tokenizer.fit_on_texts(sentence)
word_index =tokenizer.word_index

【问题讨论】:

  • fit_on_texts 返回None,尝试调用tokenizer.fit_on_texts(sentence)而不将其返回值分配给tokenizer

标签: python nlp tensorflow2.0


【解决方案1】:

fit_on_texts 是就地操作,它会返回None

代替:

tokenizer = tokenizer.fit_on_texts(sentence)

使用:

tokenizer = Tokenizer(oov_token="<00V>")
tokenizer.fit_on_texts(sentence)
word_index =tokenizer.word_index

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    • 2021-05-28
    • 1970-01-01
    相关资源
    最近更新 更多