Matching label with sentence in json format

Question:

I have a huge list of dictionaries with data labeled as follows

{'id': 2,
 'text': '"The hotel has many restaurants to enjoy a meal. My husband and I went to the Japanese restaurant and we only found sushi. Considering that it is an international hotel, they should have a larger variety of options.',
 'label': [[0, 46, 'general services'], [47, 214, 'japanese food']]},

And I want to create the next output, split the sentence and put the label in front of it, like this.

The hotel has many restaurants to enjoy a meal , general services.
My husband and I went to the Japanese restaurant and we only found sushi. Considering that it is an international hotel, they should have a larger variety of options, japanese food

I used the code:

for x in data[2]['label']:
  print(data[2]['text'][x[0]:x[1]],x[2])

But I want to generalize this, for the whole data set. I really would appreciate your help.

Asked By: bavarianbear

||

Answers:

If you have list of dictionaries as you said in your question you can do:

data = [
    {
        "id": 2,
        "text": "The hotel has many restaurants to enjoy a meal. My husband and I went to the Japanese restaurant and we only found sushi. Considering that it is an international hotel, they should have a larger variety of options.",
        "label": [[0, 46, "general services"], [47, 214, "japanese food"]],
    }
]

for d in data:
    for (start, end, label) in d["label"]:
        print("{}, {}".format(d["text"][start:end], label))

Prints:

The hotel has many restaurants to enjoy a meal, general services
 My husband and I went to the Japanese restaurant and we only found sushi. Considering that it is an international hotel, they should have a larger variety of options., japanese food

EDIT: Alternative output:

out = []
for d in data:
    for (start, end, label) in d["label"]:
        out.append(
            {"idx": d["id"], "label": label, "sentence": d["text"][start:end]}
        )

print(out)

Prints:

[
    {
        "idx": 2,
        "label": "general services",
        "sentence": "The hotel has many restaurants to enjoy a meal",
    },
    {
        "idx": 2,
        "label": "japanese food",
        "sentence": " My husband and I went to the Japanese restaurant and we only found sushi. Considering that it is an international hotel, they should have a larger variety of options.",
    },
]
Answered By: Andrej Kesely