我对python有点陌生。我有一个 json 数据集,我必须在使用 keras 的 lstm 中使用它。我的实现是关于使用 lstm 对一组评论进行情感分析。我已经运行了下面的代码:
a = "ive probably mentioned this before but i really do feel proud of myself for actually keeping up with my new years resolution of monthly and weekly goals"
# Encode samples
tokenized_sample = a.split(" ")
encoded_samples = [[word2id[word] for word in tokenized_sample]]
# Padding
encoded_samples = keras.preprocessing.sequence.pad_sequences(encoded_samples, maxlen=max_words)
# Make predictions
label_probs, attentions = model_with_attentions.predict(encoded_samples)
label_probs = {id2label[_id]: prob for (label, _id), prob in zip(label2id.items(), label_probs[0])}
# Get word attentions using attenion vector
token_attention_dic = {}
max_score = 0.0
min_score = 0.0
for token, attention_score in zip(tokenized_sample, attentions[0][-len(tokenized_sample):]):
token_attention_dic[token] = math.sqrt(attention_score)
print(label_probs)
其中我的变量“a”是一个句子,我得到的结果如下所示:
{'fear': 0.19682443, 'disappointed': 0.19954187, 'joy': 0.20219588, 'happy': 0.20103683, 'sad': 0.20040102}
但是我必须运行一个 json 数据集,我必须在 json 文件中为每个评论添加这个输出。有人可以指导我如何修改上述代码以获得良好的结果请..?
以下是我的 Json 文件的示例:
{
"name": "3",
"Date": "September 25, 2017",
"comment": "Nice Room, good entertainment - well organised\nFood was wonderful at Brasserie Chic - Seafood Platter (for 2)\nThats’ where it ends…\nStaff was woeful, very badly mannered and almost rude\nTiling works in lobby from Saturday 3pm till 11:30pm without any notice or apologies\nPolishing of tiles at 6:45am Sunday morning, again with no notice, or apology given.\nPillows smell of sweat\nPipe noises every 5 minute for 10sec in walls!\nMuch to improve on, shame as the renovated hotel looks very nice, and the food great!\nDisappointed."
},
{
"name": "2",
"Date": "June 05, 2017",
"comment": "A wonferful experience. The hotel staff went out of their way to make the stay personal and special."
},
{
"name": "1",
"Date": "September 18, 2015",
"comment": "At the desk, they don’t give any information at all when you check-in (like at what time breakfast is served, you have to guess) or promoting the hotel; communication is very scarce. It’d have been better to do your own check-in. For a 5* hotel, English is very poor except when you call over the phone. We asked a question in English at the reception desk, one of them stepped back & the other one asked if we speak French. Overall ok, very nice hotel but room standard more of a 4 star hotel ,swimming pool funny. The men carrying the Luggage, and on car park duty were friendly & the food at the Yusu restaurant is good"
}
附加:我希望我的结果如下所示:
{
"name": "3",
"Date": "September 25, 2017",
"comment": "Nice Room, good entertainment - well organised\nFood was wonderful at Brasserie Chic - Seafood Platter (for 2)\nThats’ where it ends…\nStaff was woeful, very badly mannered and almost rude\nTiling works in lobby from Saturday 3pm till 11:30pm without any notice or apologies\nPolishing of tiles at 6:45am Sunday morning, again with no notice, or apology given.\nPillows smell of sweat\nPipe noises every 5 minute for 10sec in walls!\nMuch to improve on, shame as the renovated hotel looks very nice, and the food great!\nDisappointed."
"emotion": "{'fear': 0.19682443, 'disappointed': 0.19954187, 'joy': 0.20219588, 'happy': 0.20103683, 'sad': 0.20040102}"
},
{
"name": "2",
"Date": "June 05, 2017",
"comment": "A wonferful experience. The hotel staff went out of their way to make the stay personal and special."
"emotion": "{'fear': 0.19682443, 'disappointed': 0.19954187, 'joy': 0.20219588, 'happy': 0.20103683, 'sad': 0.20040102}"
},
{
"name": "1",
"Date": "September 18, 2015",
"comment": "At the desk, they don’t give any information at all when you check-in (like at what time breakfast is served, you have to guess) or promoting the hotel; communication is very scarce. It’d have been better to do your own check-in. For a 5* hotel, English is very poor except when you call over the phone. We asked a question in English at the reception desk, one of them stepped back & the other one asked if we speak French. Overall ok, very nice hotel but room standard more of a 4 star hotel ,swimming pool funny. The men carrying the Luggage, and on car park duty were friendly & the food at the Yusu restaurant is good"
"emotion": "{'fear': 0.19682443, 'disappointed': 0.19954187, 'joy': 0.20219588, 'happy': 0.20103683, 'sad': 0.20040102}"
}
你能帮帮我吗?