Skip to content

Commit

Permalink
update_factory_attributes can update either requires or assigns
Browse files Browse the repository at this point in the history
  • Loading branch information
apmoore1 committed Mar 16, 2022
1 parent 0c4b2f2 commit b017816
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
12 changes: 9 additions & 3 deletions docs/docs/api/spacy_api/utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,25 @@ through and output an UserWarning message that it has had to force this through.

```python
def update_factory_attributes(
meta_information_to_update: str,
factory_name: str,
new_attribute_name: str,
old_attribute_name: str
) -> None
```

Updates the [spaCy Language required attributes meta information](https://spacy.io/api/language#factorymeta)
for the given component, find through it's factory name,
by replacing the `old_attribute_name` with the `new_attribute_name`.
Updates the
[spaCy Language meta information](https://spacy.io/api/language#factorymeta)
for either `assigns` or `requires` for the given component, find through
it's factory name, by replacing the `old_attribute_name` with the
`new_attribute_name`.

<h4 id="update_factory_attributes.parameters">Parameters<a className="headerlink" href="#update_factory_attributes.parameters" title="Permanent link">&para;</a></h4>


- __meta\_information\_to\_update__ : `str` <br/>
Either `assigns` or `requires`, raises a ValueError if it is any other
value.
- __factory\_name__ : `str` <br/>
The name of the component factory, e.g. `pymusas_rule_based_tagger`
- __new\_attribute\_name__ : `str` <br/>
Expand Down
26 changes: 20 additions & 6 deletions pymusas/spacy_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,22 @@ def set_custom_token_extension(extension_name: str) -> None:
Token.set_extension(extension_name, default=None)


def update_factory_attributes(factory_name: str, new_attribute_name: str,
def update_factory_attributes(meta_information_to_update: str,
factory_name: str,
new_attribute_name: str,
old_attribute_name: str) -> None:
'''
Updates the [spaCy Language required attributes meta information](https://spacy.io/api/language#factorymeta)
for the given component, find through it's factory name,
by replacing the `old_attribute_name` with the `new_attribute_name`.
Updates the
[spaCy Language meta information](https://spacy.io/api/language#factorymeta)
for either `assigns` or `requires` for the given component, find through
it's factory name, by replacing the `old_attribute_name` with the
`new_attribute_name`.
# Parameters
meta_information_to_update : `str`
Either `assigns` or `requires`, raises a ValueError if it is any other
value.
factory_name : `str`
The name of the component factory, e.g. `pymusas_rule_based_tagger`
new_attribute_name : `str`
Expand All @@ -58,10 +65,17 @@ def update_factory_attributes(factory_name: str, new_attribute_name: str,
The name of the old attribute that is to be replaced with
the `new_attribute_name`. An example, `token.tag`.
'''
value_error = ('`meta_information_to_update` has to be either `assigns` '
f'or `requires` and not {meta_information_to_update}')
if meta_information_to_update not in set(['assigns', 'requires']):
raise ValueError(value_error)

factory_meta = Language.get_factory_meta(factory_name)
required_attributes = copy.deepcopy(factory_meta.requires)
required_attributes = copy.deepcopy(getattr(factory_meta,
meta_information_to_update))
updated_attributes = [attribute for attribute in required_attributes
if attribute != old_attribute_name]
updated_attributes.append(f'{new_attribute_name}')

factory_meta.requires = validate_attrs(updated_attributes)
setattr(factory_meta, meta_information_to_update,
validate_attrs(updated_attributes))
27 changes: 20 additions & 7 deletions tests/spacy_api/test_spacy_api_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,23 @@ def test_set_custom_token_extension() -> None:
set_custom_token_extension('tags')


def test_update_factory_attributes(create_test_component: str) -> None:
factory_meta_data = Language.get_factory_meta(create_test_component)
assert not factory_meta_data.requires
factory_meta_data.requires = ['token._.pos']

update_factory_attributes(create_test_component, 'token._.tag', 'token._.pos')
assert factory_meta_data.requires == ['token._.tag']
@pytest.mark.parametrize("meta_information_to_update",
["assigns", "requires", "error"])
def test_update_factory_attributes(meta_information_to_update: str,
create_test_component: str) -> None:

if meta_information_to_update == 'error':
with pytest.raises(ValueError):
update_factory_attributes(meta_information_to_update,
create_test_component,
'token._.tag', 'token._.pos')
else:
factory_meta_data = Language.get_factory_meta(create_test_component)
assert not getattr(factory_meta_data, meta_information_to_update)
setattr(factory_meta_data, meta_information_to_update, ['token._.pos'])

update_factory_attributes(meta_information_to_update,
create_test_component,
'token._.tag', 'token._.pos')
assert getattr(factory_meta_data,
meta_information_to_update) == ['token._.tag']

0 comments on commit b017816

Please sign in to comment.