Last active
March 15, 2022 11:49
-
-
Save allanj/5ad206f7f4645c0269b68fb2065712f4 to your computer and use it in GitHub Desktop.
Convert the IOB2 tagging scheme to BIOES tagging scheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def iob_iobes(tags): | |
""" | |
IOB2 (BIO) -> IOBES | |
""" | |
new_tags = [] | |
for i, tag in enumerate(tags): | |
if tag == 'O': | |
new_tags.append(tag) | |
elif tag.split('-')[0] == 'B': | |
if i + 1 != len(tags) and \ | |
tags[i + 1].split('-')[0] == 'I': | |
new_tags.append(tag) | |
else: | |
new_tags.append(tag.replace('B-', 'S-')) | |
elif tag.split('-')[0] == 'I': | |
if i + 1 < len(tags) and \ | |
tags[i + 1].split('-')[0] == 'I': | |
new_tags.append(tag) | |
else: | |
new_tags.append(tag.replace('I-', 'E-')) | |
else: | |
raise Exception('Invalid IOB format!') | |
return new_tags |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment