我有一个通过语音转文本生成的 json 文件,它返回所有检测到的带标点符号的单词。现在我想用它造句。
我可以做一个 while 循环,直到检测到一个点,将所有单词附加到列表中并从中返回一个句子。但是这个 while 循环在第一个点处停止。我怎样才能让这个循环一直持续到 json 文件的末尾?
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 示例:
"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"
}
最佳答案
我认为解决方案很简单。你说“但是这个 while 循环在第一个点处停止。”这就是 while 所做的,它循环直到满足条件。所以只需将其替换为 if 结构即可。
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)
关于python - 将 json 文件拆分成句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56837025/