Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
DAN
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Container Registry
Operate
Terraform modules
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Automatic Text Recognition
DAN
Commits
12144cda
Commit
12144cda
authored
1 year ago
by
Eva Bardou
Browse files
Options
Downloads
Patches
Plain Diff
Convert NER prediction to BIO format
parent
65128721
No related branches found
No related tags found
1 merge request
!325
Convert NER prediction to BIO format
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
dan/bio.py
+57
-0
57 additions, 0 deletions
dan/bio.py
with
57 additions
and
0 deletions
dan/bio.py
0 → 100644
+
57
−
0
View file @
12144cda
from
typing
import
Dict
from
dan.utils
import
EntityType
def
convert
(
text
:
str
,
ner_tokens
:
Dict
[
str
,
EntityType
])
->
str
:
iob_string
:
str
=
""
# Full IOB formatted string
entity_types
:
list
[
str
]
=
[]
# Encountered entity types
inside
:
bool
=
False
# Whether we are inside an entity
for
word
in
text
.
split
():
if
not
word
:
continue
token_iterator
=
iter
(
ner_tokens
.
items
())
while
(
ner_token
:
=
next
(
token_iterator
,
None
))
is
not
None
:
name
,
entity_type
=
ner_token
if
word
[
0
]
==
entity_type
.
start
:
word
=
word
[
1
:]
entity_types
.
append
(
entity_type
.
start
)
if
entity_type
.
end
:
inside
=
False
continue
elif
entity_type
.
end
and
word
[
-
1
]
==
entity_type
.
end
:
# Make sure the token is the closing of the current entity
assert
entity_types
[
-
1
]
==
entity_type
.
start
word
=
word
[:
-
1
]
# Remove from queue
entity_types
.
pop
()
# if there is no more entity, you remove inside
# else we continue parent entity
inside
=
bool
(
entity_types
)
# Not a NER token
# If there is no current entity type
if
not
entity_types
:
iob_string
+=
f
"
\n
{
word
}
O
"
continue
# There is at least one entity type
if
inside
:
# We are inside an entity
iob_string
+=
f
"
\n
{
word
}
I-
{
name
}
"
else
:
# We are starting an entity
inside
=
True
iob_string
+=
f
"
\n
{
word
}
B-
{
name
}
"
print
(
iob_string
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment