-
-
Notifications
You must be signed in to change notification settings - Fork 129
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 #53 from chakki-works/enhancement/refactorClassifi…
…cationReport Enhancement/refactor classification report
- Loading branch information
Showing
3 changed files
with
104 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import abc | ||
|
||
|
||
class Reporter(abc.ABC): | ||
|
||
def __init__(self, *args, **kwargs): | ||
pass | ||
|
||
@abc.abstractmethod | ||
def report(self): | ||
raise NotImplementedError | ||
|
||
@abc.abstractmethod | ||
def write(self, row_name: str, precision: float, recall: float, f1: float, support: int): | ||
raise NotImplementedError | ||
|
||
|
||
class DictReporter(Reporter): | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__() | ||
self.report_dict = {} | ||
|
||
def report(self): | ||
return self.report_dict | ||
|
||
def write(self, row_name: str, precision: float, recall: float, f1: float, support: int): | ||
self.report_dict[row_name] = { | ||
'precision': precision, | ||
'recall': recall, | ||
'f1-score': f1, | ||
'support': support | ||
} | ||
|
||
def write_blank(self): | ||
pass | ||
|
||
|
||
class StringReporter(Reporter): | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__() | ||
self.buffer = [] | ||
self.row_fmt = '{:>{width}s} ' + ' {:>9.{digits}f}' * 3 + ' {:>9}' | ||
self.width = kwargs.get('width', 10) | ||
self.digits = kwargs.get('digits', 4) | ||
|
||
def report(self): | ||
report = self.write_header() | ||
report += '\n'.join(self.buffer) | ||
return report | ||
|
||
def write(self, row_name: str, precision: float, recall: float, f1: float, support: int): | ||
row = self.row_fmt.format( | ||
*[row_name, precision, recall, f1, support], | ||
width=self.width, | ||
digits=self.digits | ||
) | ||
self.buffer.append(row) | ||
|
||
def write_header(self): | ||
headers = ['precision', 'recall', 'f1-score', 'support'] | ||
head_fmt = '{:>{width}s} ' + ' {:>9}' * len(headers) | ||
report = head_fmt.format('', *headers, width=self.width) | ||
report += '\n\n' | ||
return report | ||
|
||
def write_blank(self): | ||
self.buffer.append('') |
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,26 @@ | ||
import pytest | ||
from seqeval.reporters import DictReporter | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'rows, expected', | ||
[ | ||
([], {}), | ||
( | ||
[['PERSON', 0.82, 0.79, 0.81, 24]], | ||
{ | ||
'PERSON': { | ||
'precision': 0.82, | ||
'recall': 0.79, | ||
'f1-score': 0.81, | ||
'support': 24 | ||
} | ||
} | ||
) | ||
] | ||
) | ||
def test_dict_reporter_output(rows, expected): | ||
reporter = DictReporter() | ||
for row in rows: | ||
reporter.write(*row) | ||
assert reporter.report() == expected |