Browse Source

Remove empty label name

pull/1310/head
Hironsan 4 years ago
parent
commit
02d1a22034
3 changed files with 13 additions and 7 deletions
  1. 2
      app/api/views/upload/dataset.py
  2. 10
      app/api/views/upload/label.py
  3. 8
      app/api/views/upload/labels.py

2
app/api/views/upload/dataset.py

@ -33,7 +33,7 @@ class Record:
@property @property
def label(self): def label(self):
return [label.name for label in self._label if label.has_name()]
return [label.name for label in self._label if label.has_name() and label.name]
class Records: class Records:

10
app/api/views/upload/label.py

@ -1,5 +1,5 @@
import abc import abc
from typing import Any, Dict
from typing import Any, Dict, Union
from pydantic import BaseModel from pydantic import BaseModel
@ -28,7 +28,7 @@ class Label(BaseModel, abc.ABC):
class CategoryLabel(Label): class CategoryLabel(Label):
label: str
label: Union[str, int]
def has_name(self) -> bool: def has_name(self) -> bool:
return True return True
@ -44,12 +44,12 @@ class CategoryLabel(Label):
raise TypeError(f'{obj} is not str.') raise TypeError(f'{obj} is not str.')
def replace(self, mapping: Dict[str, int]) -> 'Label': def replace(self, mapping: Dict[str, int]) -> 'Label':
label = mapping.get(self.label, self.label)
label = mapping[self.label]
return CategoryLabel(label=label) return CategoryLabel(label=label)
class OffsetLabel(Label): class OffsetLabel(Label):
label: str
label: Union[str, int]
start_offset: int start_offset: int
end_offset: int end_offset: int
@ -72,7 +72,7 @@ class OffsetLabel(Label):
raise TypeError(f'{obj} is invalid type.') raise TypeError(f'{obj} is invalid type.')
def replace(self, mapping: Dict[str, int]) -> 'Label': def replace(self, mapping: Dict[str, int]) -> 'Label':
label = mapping.get(self.label, self.label)
label = mapping[self.label]
return OffsetLabel( return OffsetLabel(
label=label, label=label,
start_offset=self.start_offset, start_offset=self.start_offset,

8
app/api/views/upload/labels.py

@ -11,7 +11,13 @@ class Labels:
def replace_label(self, mapping: Optional[Dict[str, int]] = None): def replace_label(self, mapping: Optional[Dict[str, int]] = None):
if not mapping: if not mapping:
return self return self
labels = [label.replace(mapping) for label in self.labels]
labels = []
for label in self.labels:
try:
label = label.replace(mapping)
labels.append(label)
except KeyError:
pass
return Labels(labels) return Labels(labels)
def dict(self) -> List[dict]: def dict(self) -> List[dict]:

Loading…
Cancel
Save