-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
220 additions
and
56 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 |
---|---|---|
|
@@ -3,4 +3,4 @@ argcomplete | |
phonenumbers | ||
pandas | ||
sklearn | ||
typing_extensions | ||
typing_extensions |
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,2 @@ | ||
spacy-nightly[transformers]>=3.0.0rc1; python_version >= '3.6' | ||
|
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
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,52 @@ | ||
from typing import Generator, Iterable, Optional, Sequence | ||
|
||
import spacy | ||
from wasabi import msg | ||
|
||
from .base import Detector | ||
from ..filth import NamedEntityFilth, Filth, NameFilth, OrganizationFilth | ||
from ..utils import CanonicalStringSet | ||
|
||
|
||
class NamedEntityDetector(Detector): | ||
"""Use spacy's named entity recognition to clean named entities. | ||
List specific entities to include passing ``named_entities``, e.g. | ||
(PERSON) | ||
""" | ||
filth_cls_map = { | ||
'PERSON': NameFilth, | ||
'ORG': OrganizationFilth | ||
} | ||
name = 'named_entity' | ||
|
||
disallowed_nouns = CanonicalStringSet(["skype"]) | ||
|
||
def __init__(self, named_entities: Iterable[str] = {'PERSON'}, | ||
model: str = "en_core_web_trf", **kwargs): | ||
# Spacy NER are all upper cased | ||
self.named_entities = {entity.upper() for entity in named_entities} | ||
if model not in spacy.info()['pipelines']: | ||
msg.warn("Downloading spacy model {}".format(model)) | ||
spacy.cli.download(model) | ||
|
||
self.nlp = spacy.load(model) | ||
# Only enable necessary pipes | ||
self.nlp.select_pipes(enable=["transformer", "tagger", "parser", "ner"]) | ||
super(NamedEntityDetector, self).__init__(**kwargs) | ||
|
||
def iter_filth_documents(self, doc_names: Sequence[Optional[str]], | ||
doc_list: Sequence[str]) -> Generator[Filth, None, None]: | ||
for doc_name, doc in zip(doc_names, self.nlp.pipe(doc_list)): | ||
for ent in doc.ents: | ||
if ent.label_ in self.named_entities: | ||
# If there is no standard 'filth', returns a NamedEntity filth | ||
filth_cls = self.filth_cls_map.get(ent.label_, NamedEntityFilth) | ||
yield filth_cls(beg=ent.start_char, | ||
end=ent.end_char, | ||
text=ent.text, | ||
document_name=(str(doc_name) if doc_name else None), # None if no doc_name provided | ||
detector_name=self.name, | ||
label=ent.label_) | ||
|
||
def iter_filth(self, text: str, document_name: Optional[str] = None) -> Generator[Filth, None, None]: | ||
yield from self.iter_filth_documents([document_name], [text]) |
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,14 @@ | ||
from .base import Filth | ||
|
||
|
||
class NamedEntityFilth(Filth): | ||
""" | ||
Default filth type, for named entities (e.g. the ones in https://nightly.spacy.io/models/en#en_core_web_lg-labels), | ||
except the ones represented in any other filth. | ||
""" | ||
type = 'named_entity' | ||
|
||
def __init__(self, *args, label: str, **kwargs): | ||
super(NamedEntityFilth, self).__init__(*args, **kwargs) | ||
self.label = label.lower() | ||
self.replacement_string = "{}_{}".format(self.type, self.label) |
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
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,72 @@ | ||
import sys | ||
import unittest | ||
|
||
from scrubadub.detectors import NamedEntityDetector | ||
from scrubadub.filth import NameFilth, OrganizationFilth, NamedEntityFilth | ||
import scrubadub | ||
|
||
from base import BaseTestCase | ||
|
||
|
||
class NamedEntityTestCase(unittest.TestCase, BaseTestCase): | ||
""" | ||
Tests whether the detector is performing correctly from a function point of view. | ||
For accuracy tests use .benchmark_accuracy instead | ||
""" | ||
|
||
def setUp(self): | ||
self.detector = NamedEntityDetector() | ||
unittest.TestCase.skipTest( | ||
(sys.version_info.major, sys.version_info.minor) < (3, 6), | ||
"Named entity detector not supported for python<3.6" | ||
) | ||
|
||
def _assert_filth_type_and_pos(self, doc_list, beg_end_list, filth_class): | ||
doc_names = [str(x) for x in range(len(doc_list))] | ||
|
||
filth_list = list(self.detector.iter_filth_documents(doc_names, doc_list)) | ||
|
||
for filth, beg_end in zip(filth_list, beg_end_list): | ||
self.assertIsInstance(filth, filth_class) | ||
self.assertEqual((filth.beg, filth.end), beg_end) | ||
|
||
def test_names(self): | ||
doc_list = ["John is a cat", | ||
"When was Maria born?", | ||
"john is a cat", | ||
"when was maria born"] | ||
beg_end_list = [(0, 4), | ||
(9, 14), | ||
(0, 4), | ||
(9, 14)] | ||
|
||
self._assert_filth_type_and_pos(doc_list, beg_end_list, NameFilth) | ||
|
||
def test_organisations(self): | ||
doc_list = ["She started working for Apple this year", | ||
"But used to work for Google"] | ||
beg_end_list = [(24, 30), | ||
(21, 27)] | ||
|
||
self._assert_filth_type_and_pos(doc_list, beg_end_list, OrganizationFilth) | ||
|
||
def test_other_entity(self): | ||
self.detector.named_entities = {"GPE"} | ||
doc_list = ["London is a city in England"] | ||
beg_end_list = [(0, 6), | ||
(20, 27)] | ||
|
||
self._assert_filth_type_and_pos(doc_list, beg_end_list, NamedEntityFilth) | ||
|
||
def test_wrong_model(self): | ||
"""Test that it raises an error if user inputs invalid spacy model""" | ||
with self.assertRaises(SystemExit): | ||
NamedEntityDetector(model='not_a_valid_spacy_model') | ||
|
||
def test_iter_filth(self): | ||
doc = "John is a cat" | ||
|
||
output_iter_docs = list(self.detector.iter_filth_documents(doc_list=[doc], doc_names=["0"])) | ||
output_iter = list(self.detector.iter_filth(text=doc, document_name="0")) | ||
|
||
self.assertListEqual(output_iter, output_iter_docs) |