Search code examples
pythonjsonpython-3.xnlp

Render JSON value as a text besides a key Python


I have a document that is the output of a list of lists with dictionaries inside:

[{'entity_group': 'literal', 'score': 0.99999213, 'word': 'DNA', 'start': 0, 'end': 3}, {'entity_group': 'metaphoric', 'score': 0.9768174, 'word': 'loop', 'start': 4, 'end': 8}, {'entity_group': 'literal', 'score': 0.9039155, 'word': 'ing,', 'start': 8, 'end': 12}]

I want to group the "literal" in order to get the text only, so that it becomes

DNA {metaphoric:loop}ing

so leave the metaphoric as it is. I tried with this code below but it says string indices must be integers, and I'm not actually able to get the solution as I wish.

with open(r'MYFILE.txt', 'r') as res:
  texty = res.read()
  for group in texty[::-1]:
      ent = group["entity_group"]
      if ent != 'literal': 
      text2 = replace_at(ent, group['end'], group['end'], text)
print(text2)

Solution

  • Your desire output is unclear so I can only guess what I think you wanted until you update your question.

    import json
    
    with open ('MYFILE.txt') as f:
        texty = json.load (f)
    
    for group in texty:
        if 'literal' not in group ['entity_group']:
            print ('DNA {%s:%s}ing' % (group ['entity_group'], group ['word']))
    

    Output:

    DNA {metaphoric:loop}ing