1
$\begingroup$

I am a bit new to python. I have a json dataset which I have to use in a lstm using keras. My implementation is about emotion analysis for a set of reviews using lstm. I have run the code below:

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)

where my variable 'a' is a sentence and I got my results as shown below:

{'fear': 0.19682443, 'disappointed': 0.19954187, 'joy': 0.20219588, 'happy': 0.20103683, 'sad': 0.20040102}

But I have to run a json dataset where I have to add this output for each review in the json file. Can someone please guide me on how to modify the above codes to have a good reults Please..?

Below is an example of my Json file:

{
     "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"
    }

Additional: I want my result to appear like this:

{
     "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}"
    }

Can you please help me out.

$\endgroup$
1
$\begingroup$

I'm not sure how you are processing your JSON file right now. But Pandas is really convinient way for doing so.
So for start you can read the file with pandas, following instructions here.

A dataframe will be creatd and then After you did all your processing, adding different columns and rows to your dataframe, you can use pandas.DataFrame.to_json for converting it back to JSON file.

So you will have a new column named "emotion" and you can fill in the values for it with the label_probsvariable that you are printing.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.