有条件地将复杂JSON转换为python三元组

qncylg1j  于 2023-01-27  发布在  Python
关注(0)|答案(2)|浏览(329)

我有这个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)]等等,我该怎么做呢?

j7dteeu8

j7dteeu81#

像这样?

from pprint import pprint

data = {
    "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}
            ]
        ]
    }
}

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')]]}}
dtcbnfnu

dtcbnfnu2#

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

data = {
    "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}
            ]
        ]
    }
}

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')]

相关问题