Use better logic for auto-generating component name

Instances don't have __name__, so we try __class__.__name__ as well,
before giving up and defaulting to repr(component).
This commit is contained in:
ines 2017-10-10 04:23:05 +02:00
parent b4fc6b203c
commit 67350fa496
1 changed files with 8 additions and 1 deletions

View File

@ -224,7 +224,14 @@ class Language(object):
>>> nlp.add_pipe(component, name='custom_name', last=True) >>> nlp.add_pipe(component, name='custom_name', last=True)
""" """
if name is None: if name is None:
name = getattr(component, 'name', component.__name__) if hasattr(component, 'name'):
name = component.name
elif hasattr(component, '__name__'):
name = component.__name__
elif hasattr(component, '__class__') and hasattr(component.__class__, '__name__'):
name = component.__class__.__name__
else:
name = repr(component)
if name in self.pipe_names: if name in self.pipe_names:
raise ValueError("'{}' already exists in pipeline.".format(name)) raise ValueError("'{}' already exists in pipeline.".format(name))
if sum([bool(before), bool(after), bool(first), bool(last)]) >= 2: if sum([bool(before), bool(after), bool(first), bool(last)]) >= 2: