split json file into sentences

Viewed 704

I have a json file generated via speech-to-text that returns all the detected words with punctuation. Now I want to create sentences from it.

I can make a while loop until there is a dot detected, append all the words to a list and return a sentence from it. But this while loop stops at the first dot. How can I make that loop continue till the end of the json file?

with open(json_file) as f:
    data = json.load(f)

for word in data['words']:
    while not data['words'][i]['name'] == '.':
        sentenceList.append(data['words'][i]['name'])
        i +=1
    sentence = ' '.join(word for word in sentenceList)
print (sentence)

json example:

"words": [
    {
      "duration": "0.18", 
      "confidence": "0.990", 
      "name": "Is", 
      "time": "0.80"
    }, 
    {
      "duration": "0.27", 
      "confidence": "1.000", 
      "name": "dit", 
      "time": "0.99"
    }, 
    {
      "duration": "0.24", 
      "confidence": "1.000", 
      "name": "met", 
      "time": "1.50"
    }, 
    {
      "duration": "0.54", 
      "confidence": "0.990", 
      "name": "vaart", 
      "time": "1.86"
    }, 
    {
      "duration": "0.33", 
      "confidence": "0.990", 
      "name": ".", 
      "time": "2.40"
    }, 
    {
      "duration": "0.06", 
      "confidence": "0.910", 
      "name": "We", 
      "time": "2.73"
    }, 
    {
      "duration": "0.21", 
      "confidence": "1.000", 
      "name": "hebben", 
      "time": "2.79"
    }, 
    {
      "duration": "0.09", 
      "confidence": "1.000", 
      "name": "het", 
      "time": "3.00"
    }, 
    {
      "duration": "0.42", 
      "confidence": "1.000", 
      "name": "vandaag", 
      "time": "3.09"
    }, 
    {
      "duration": "0.30", 
      "confidence": "1.000", 
      "name": "over", 
      "time": "3.51"
    }, 
    {
      "duration": "0.60", 
      "confidence": "1.000", 
      "name": "België", 
      "time": "3.81"
    }, 
    {
      "duration": "0.18", 
      "confidence": "1.000", 
      "name": ".", 
      "time": "4.50"
    }
3 Answers

I think the solution is straight forward. You say "But this while loop stops at the first dot." That's what a while does, it loops until the condition is met. So just replace it with an if structure.

with open(json_file) as f:
    data = json.load(f)

for word in data['words']:
    # Check if it's a word or a dot
    if not data['words'][i]['name'] == '.':
        # If word, add it to the array
        sentenceList.append(data['words'][i]['name'])
        i +=1
# All words are appended, now join.
sentence = ' '.join(word for word in sentenceList)
print(sentence)

In your case simple if statement would be enough to check the end of a sentence (since each words sequence in the input structure is ended with "name": "."):

sentenceList = []
for word in data['words']:
    if word['name'] == '.':
        sentence = ' '.join(word for word in sentenceList)
        sentenceList = []
        print(sentence)
    else:
        sentenceList.append(word['name'])

The output:

Is dit met vaart
We hebben het vandaag over België

Using itertools.groupby:

data = '''{"words": [
    {
      "duration": "0.18",
      "confidence": "0.990",
      "name": "Is",
      "time": "0.80"
    },
    {
      "duration": "0.27",
      "confidence": "1.000",
      "name": "dit",
      "time": "0.99"
    },
    {
      "duration": "0.24",
      "confidence": "1.000",
      "name": "met",
      "time": "1.50"
    },
    {
      "duration": "0.54",
      "confidence": "0.990",
      "name": "vaart",
      "time": "1.86"
    },
    {
      "duration": "0.33",
      "confidence": "0.990",
      "name": ".",
      "time": "2.40"
    },
    {
      "duration": "0.06",
      "confidence": "0.910",
      "name": "We",
      "time": "2.73"
    },
    {
      "duration": "0.21",
      "confidence": "1.000",
      "name": "hebben",
      "time": "2.79"
    },
    {
      "duration": "0.09",
      "confidence": "1.000",
      "name": "het",
      "time": "3.00"
    },
    {
      "duration": "0.42",
      "confidence": "1.000",
      "name": "vandaag",
      "time": "3.09"
    },
    {
      "duration": "0.30",
      "confidence": "1.000",
      "name": "over",
      "time": "3.51"
    },
    {
      "duration": "0.60",
      "confidence": "1.000",
      "name": "België",
      "time": "3.81"
    },
    {
      "duration": "0.18",
      "confidence": "1.000",
      "name": ".",
      "time": "4.50"
    }
]}'''

import json
from itertools import groupby
d = json.loads(data)
lst = [' '.join(i['name'] for i in g) + '.' for v, g in groupby(d['words'], lambda w: w['name'] != '.') if v]

print(lst)

Prints:

['Is dit met vaart.', 'We hebben het vandaag over België.']
Related