Skip to content

Commit

Permalink
GH-38: Improve label class.
Browse files Browse the repository at this point in the history
  • Loading branch information
tabergma committed Aug 14, 2018
1 parent 3e3194e commit c65c594
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
13 changes: 11 additions & 2 deletions flair/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ def load(cls, name: str):


class Label:
"""
This class represents a label of a sentence. Each label has a name and optional a confidence value. The confidence
value needs to be between 0.0 and 1.0. Default value for the confidence is 0.0.
"""
def __init__(self, name: str, confidence: float = 0.0):
self.name = name
self.confidence = confidence
Expand All @@ -103,16 +107,21 @@ def name(self):

@name.setter
def name(self, name):
self._name = name
if not name:
raise ValueError('Incorrect label name provided. Label name needs to be set.')
else:
self._name = name

@property
def confidence(self):
return self._name
return self._confidence

@confidence.setter
def confidence(self, confidence):
if 0.0 <= confidence <= 1.0:
self._confidence = confidence
else:
self._confidence = 0.0


class Token:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,20 @@ def test_tagged_corpus_make_vocab_dictionary():
assert('.' in vocab.get_items())


def test_label_set_confidence():
label = Label('class_1', 3.2)

assert (0.0 == label.confidence)
assert ('class_1' == label.name)

label.confidence = 0.2

assert (0.2 == label.confidence)

with pytest.raises(ValueError):
label.name = ''


def test_tagged_corpus_make_label_dictionary():
sentence_1 = Sentence('sentence 1', labels=[Label('class_1')])
sentence_2 = Sentence('sentence 2', labels=[Label('class_2')])
Expand Down

0 comments on commit c65c594

Please sign in to comment.