Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.

Fix NER script: #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,6 @@ venv.bak/

# mypy
.mypy_cache/

# Models
/predictwhens
22 changes: 17 additions & 5 deletions models/ner.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@

logger = logging.getLogger(__name__)


ALL_MODELS = sum(
[list(conf.pretrained_config_archive_map.keys()) for conf in (BertConfig, RobertaConfig, DistilBertConfig)],
[])
# 'pretrained_config_archive_map' is deprecated and variable not actually in use
# ALL_MODELS = sum(
# [list(conf.pretrained_config_archive_map.keys()) for conf in (BertConfig, RobertaConfig, DistilBertConfig)],
# [])

MODEL_CLASSES = {
'bert': (BertConfig, BertForTokenClassification, BertTokenizer),
Expand Down Expand Up @@ -407,14 +407,23 @@ def predict(self, tasks, **kwargs):

result = []

# bool for when 'O'-label is not appended in 'result'
skipped = False

for label, group in groupby(zip(preds, starts, scores), key=lambda i: re.sub('^(B-|I-)', '', i[0])):
_, group_start, _ = list(group)[0]
if len(result) > 0:
if group_start == 0:
result.pop(-1)
else:
# when 'O' is skipped when appending 'result', 'end' of previous 'result' sould not be changed, until new label is appended
elif not skipped:
result[-1]['value']['end'] = group_start - 1
# remove incorrect predictions, where 'end is smaller than 'start'
if result[-1]['value']['end'] is not None and result[-1]['value']['end'] < result[-1]['value']['start']:
result.pop(-1)

if label != 'O':
skipped = False
result.append({
'from_name': from_name,
'to_name': to_name,
Expand All @@ -426,6 +435,9 @@ def predict(self, tasks, **kwargs):
'text': '...'
}
})
else:
skipped = True

if result and result[-1]['value']['end'] is None:
result[-1]['value']['end'] = len(string)
results.append({
Expand Down