【问题标题】:Convert complex JSON to python tuple of three conditionally有条件地将复杂的JSON转换为三元组的python元组
【发布时间】:2023-01-24 21:09:19
【问题描述】:

我有这个 JSON

{
    "journal.pbio.0050304.xml": {
        "sentence": [
            [
                {"entity_group": "literal", "score": 0.9961686, "word": "The anterior\u2013posterior (A\u2013P) axis ", "start": 0, "end": 299}
            ],
            [
                {"entity_group": "literal", "score": 0.9932352, "word": "RA, Fgfs, and Wnts are all produced at the posterior of the embryo, and might therefore be expected to form posterior-", "start": 0, "end": 118},
                {"entity_group": "metaphoric", "score": 0.874372, "word": "to", "start": 118, "end": 120},
                {"entity_group": "literal", "score": 0.99049604, "word": "-anterior gradients (for Fgf8", "start": 120, "end": 149},
                {"entity_group": "metaphoric", "score": 0.9993481, "word": "this", "start": 150, "end": 154}
            ]
        ]
    },
    "journal.pbio.0050093.xml": {
        "sentence": [
            [
                {"entity_group": "literal", "score": 0.9961686, "word": "The anterior\u2013posterior (A\u2013P) axis ", "start": 0, "end": 299}
            ]
        ]
    }
}

我只想接受实体组,开始和结束并将它们转换为元组,如下所示: [(0, 299, 'literal'),(186, 194, 'literal'), ('metaphoric', 196, 199)],等等。我该怎么做?

【问题讨论】:

  • 你的元组结构似乎不一致。最初是 (start, end, entity_group),然后是 (entity_group, start, end)。这背后的逻辑是什么?

标签: python json python-3.x data-structures


【解决方案1】:

是这样的吗?

from pprint import pprint

data = {
    "journal.pbio.0050304.xml": {
        "sentence": [
            [
                {"entity_group": "literal", "score": 0.9961686, "word": "The anterioru2013posterior (Au2013P) axis ", "start": 0, "end": 299}
            ],
            [
                {"entity_group": "literal", "score": 0.9932352, "word": "RA, Fgfs, and Wnts are all produced at the posterior of the embryo, and might therefore be expected to form posterior-", "start": 0, "end": 118},
                {"entity_group": "metaphoric", "score": 0.874372, "word": "to", "start": 118, "end": 120},
                {"entity_group": "literal", "score": 0.99049604, "word": "-anterior gradients (for Fgf8", "start": 120, "end": 149},
                {"entity_group": "metaphoric", "score": 0.9993481, "word": "this", "start": 150, "end": 154}
            ]
        ]
    },
    "journal.pbio.0050093.xml": {
        "sentence": [
            [
                {"entity_group": "literal", "score": 0.9961686, "word": "The anterioru2013posterior (Au2013P) axis ", "start": 0, "end": 299}
            ]
        ]
    }
}


for file in data.values():
    for idx1, sentence in enumerate(file["sentence"]):
        new_sentence = [word for word in sentence]
        for idx2, word in enumerate(sentence):
            new_sentence[idx2] = (word["start"], word["end"], word["entity_group"])
        file["sentence"][idx1] = new_sentence

pprint(data)

结果:

{'journal.pbio.0050093.xml': {'sentence': [[(0, 299, 'literal')]]},
 'journal.pbio.0050304.xml': {'sentence': [[(0, 299, 'literal')],
                                           [(0, 118, 'literal'),
                                            (118, 120, 'metaphoric'),
                                            (120, 149, 'literal'),
                                            (150, 154, 'metaphoric')]]}}

【讨论】:

    【解决方案2】:

    您只需要先遍历字典值,然后遍历列表和子列表,如下所示:

    data = {
        "journal.pbio.0050304.xml": {
            "sentence": [
                [
                    {"entity_group": "literal", "score": 0.9961686, "word": "The anterioru2013posterior (Au2013P) axis ", "start": 0, "end": 299}
                ],
                [
                    {"entity_group": "literal", "score": 0.9932352, "word": "RA, Fgfs, and Wnts are all produced at the posterior of the embryo, and might therefore be expected to form posterior-", "start": 0, "end": 118},
                    {"entity_group": "metaphoric", "score": 0.874372, "word": "to", "start": 118, "end": 120},
                    {"entity_group": "literal", "score": 0.99049604, "word": "-anterior gradients (for Fgf8", "start": 120, "end": 149},
                    {"entity_group": "metaphoric", "score": 0.9993481, "word": "this", "start": 150, "end": 154}
                ]
            ]
        },
        "journal.pbio.0050093.xml": {
            "sentence": [
                [
                    {"entity_group": "literal", "score": 0.9961686, "word": "The anterioru2013posterior (Au2013P) axis ", "start": 0, "end": 299}
                ]
            ]
        }
    }
    
    output = []
    
    for v in data.values():
        for s in v.get('sentence', []):
            for d in s:
                output.append((d.get('start'), d.get('end'), d.get('entity_group')))
    
    print(output)
    

    输出:

    [(0, 299, 'literal'), (0, 118, 'literal'), (118, 120, 'metaphoric'), (120, 149, 'literal'), (150, 154, 'metaphoric'), (0, 299, 'literal')]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-30
      • 2022-01-05
      • 2019-10-27
      相关资源
      最近更新 更多