-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from mideind/fix/ifd_output_map_oddities
Modified IFD pos mapping to comply with standard
- Loading branch information
Showing
12 changed files
with
124 additions
and
16 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from typing import List | ||
|
||
|
||
class BIOParser: | ||
def __init__(self, labels: List[str]): | ||
self.labels = labels | ||
self.idx = 0 | ||
self.last = None | ||
|
||
def _over(self) -> str: | ||
"""Ensures legal BIO tags, i.e. I-tags have same | ||
label as preceding B-tag and B-tags start new | ||
spans. | ||
""" | ||
self.idx += 1 | ||
cur_label = self.labels[self.idx - 1] | ||
|
||
if self.last is None: | ||
return cur_label | ||
|
||
if cur_label == "O": | ||
return cur_label | ||
|
||
if self.last == "O": | ||
# In case the label starts with I | ||
return "B" + cur_label[1:] | ||
|
||
_, last_cat = self.last.split("-") | ||
cur_head, _ = cur_label.split("-") | ||
if cur_head != "B": | ||
return f"{cur_head}-{last_cat}" | ||
return cur_label | ||
|
||
def over(self) -> str: | ||
label = self._over() | ||
self.last = label | ||
return label | ||
|
||
@classmethod | ||
def parse(cls, labels: List[str]) -> List[str]: | ||
parser = cls(labels) | ||
fixed_labels = [] | ||
while parser.idx != len(parser.labels): | ||
fixed_labels.append(parser.over()) | ||
return fixed_labels |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from greynirseq.nicenlp.utils.ner_parser import BIOParser | ||
|
||
# First value is the incorrect labels, the second the correct | ||
TEST_LABELS = [ | ||
("B-X B-Y I-Y O", "B-X B-Y I-Y O"), | ||
("O I-a I-a B-u O", "O B-a I-a B-u O"), | ||
("O I-a O I-a B-u O", "O B-a O B-a B-u O"), | ||
("B-p I-r O I-a B-p", "B-p I-p O B-a B-p"), | ||
( | ||
"O O B-person I-money O O I-time I-time O O B-location I-location", | ||
"O O B-person I-person O O B-time I-time O O B-location I-location", | ||
), | ||
] | ||
|
||
|
||
def test_bioparser(): | ||
for incorrect, correct in TEST_LABELS: | ||
assert BIOParser.parse(incorrect.split()) == correct.split() |