python - 将 json 文件拆分成句子

标签 python json loops

我有一个通过语音转文本生成的 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/

相关文章:

python - 如何有效地 cythonize "vectorize"函数(numpy 库)- python

python - 父级中的相对 QWidget 位置 - 不正确的 Y

python - 访问 `sklearn` 管道中的转换器功能

java - 解析简单的 Json 响应

java - Gson 用键解析数组的数组

java - 如何通过远程方法发送json-rpc http post请求并在java中传递加密参数

python - Scrapy 和图像抓取的问题

javascript - 根据 props 上的匹配集向元素数组添加属性 - es6

javascript - 画线javascript的循环逻辑

loops - MATLAB 中的复制求和运算符