ner_lenta_law = []
for ner_item in ner_lenta:
for index, span in enumerate(ner_item['spans']):
if span['type'] == 'NORP' and len(span['key'].split(' ')) > 1:
span_modified = {
'start': span['start'],
'stop': len(span['key'].split(' ')[0]),
'key': span['key'].split(' ')[0],
'type': span['type'],
}
ner_lenta_law.append(ner_item)
ner_lenta[index] = ner_lenta[index]['spans'][:index] + [span_modified] + ner_lenta[index]['spans'][index+1:]
The modified code above does the same thing as the original code below, but with fewer lines and less redundancy:
ner_lenta_law = []
for ner_item in ner_lenta:
for index, span in enumerate(ner_item['spans']):
if span['type'] == 'NORP' and len(span['key'].split(' ')) > 1:
ner_lenta_law.append(ner_item)
ner_item['spans'][index] = {
'start': span['start'],
'stop': len(span['key'].split(' ')[0]),
'key': span['key'].split(' ')[0],
'type': span['type'],
}
The modified code above is simpler and more concise. By assigning a new `span_modified` dictionary to replace the old `span` dictionary, we avoid having to create a new list to store the modified list, and instead modify the existing list in place. This reduces the overall number of lines and makes the code easier to read and understand.