为什么我的 python 代码给出类型错误,因为在将字典列表加载到 Tokenizer object 中时字典 object 不可调用?
Why does my python code gives the type error as the dict object is not callable when loading a list of dictionaries into a Tokenizer object?
我正在尝试使用 Jupiter notebook 使用来自 Kaggle 的讽刺数据集来编写讽刺检测模型。我已经将数据集下载到我的电脑上并将其修改为字典列表。该词典由三个键组成,即 article_link、is_sarcastic 和标题。
我下面的代码给出了以下错误:
TypeError Traceback(最后一次调用)
在
7 tokenizer.fit_on_texts(句子)
8个
----> 9 my_word_index=tokenizer.word_index()
10
11 打印(len(word_index))
TypeError: 'dict' object 不可调用
import os
import pandas
os.getcwd()
import json
os.chdir('C:/Users/IMALSHA/Desktop/AI content writing/Cousera Deep Neural Networks course/NLP lectures')
#loading data
with open('Sarcasm_Headlines_Dataset.json','r') as json_file:
data_set=json.load(json_file)
#defining lists
sentences=[]
labels=[]
urls=[]
for item in data_set:
sentences.append(item['headline'])
labels.append(item['is_sarcastic'])
urls.append(item['article_link'])
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
tokenizer=Tokenizer(oov_token="<oov>")
tokenizer.fit_on_texts(sentences)
word_index=tokenizer.word_index()
print(len(word_index))
print(word_index)
sequences=tokenizer.texts_to_sequences(sentences)
paded=pad_sequences(sequences)
print(paded[2])
问题如下:
word_index=tokenizer.word_index()
您可能想要将分词器的 word_index 存储到 word_index 变量中。相反,您调用 tokenizer.word_index 就好像它是一个 method/function,但它是一个字典。
因此,我认为您必须应用以下更正:
word_index=tokenizer.word_index
我正在尝试使用 Jupiter notebook 使用来自 Kaggle 的讽刺数据集来编写讽刺检测模型。我已经将数据集下载到我的电脑上并将其修改为字典列表。该词典由三个键组成,即 article_link、is_sarcastic 和标题。
我下面的代码给出了以下错误:
TypeError Traceback(最后一次调用) 在 7 tokenizer.fit_on_texts(句子) 8个 ----> 9 my_word_index=tokenizer.word_index() 10 11 打印(len(word_index))
TypeError: 'dict' object 不可调用
import os
import pandas
os.getcwd()
import json
os.chdir('C:/Users/IMALSHA/Desktop/AI content writing/Cousera Deep Neural Networks course/NLP lectures')
#loading data
with open('Sarcasm_Headlines_Dataset.json','r') as json_file:
data_set=json.load(json_file)
#defining lists
sentences=[]
labels=[]
urls=[]
for item in data_set:
sentences.append(item['headline'])
labels.append(item['is_sarcastic'])
urls.append(item['article_link'])
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
tokenizer=Tokenizer(oov_token="<oov>")
tokenizer.fit_on_texts(sentences)
word_index=tokenizer.word_index()
print(len(word_index))
print(word_index)
sequences=tokenizer.texts_to_sequences(sentences)
paded=pad_sequences(sequences)
print(paded[2])
问题如下:
word_index=tokenizer.word_index()
您可能想要将分词器的 word_index 存储到 word_index 变量中。相反,您调用 tokenizer.word_index 就好像它是一个 method/function,但它是一个字典。
因此,我认为您必须应用以下更正:
word_index=tokenizer.word_index