Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve conditional extractors #8

Open
lukavdplas opened this issue May 8, 2023 · 1 comment
Open

Improve conditional extractors #8

lukavdplas opened this issue May 8, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@lukavdplas
Copy link
Contributor

lukavdplas commented May 8, 2023

It's not uncommon for extractors to be conditional, i.e. they should return None unless some condition holds.

The extractor class supports a condition applicable which allows you to check a property in the metadata, but this is not very flexible. It only works on the document level, for one thing.

It would be nice to change this to a more general condition, where applicable instead takes an extractor.

For the old functionality, you can use the Metadata extractor:

#extract the foo tag if 'bar' is set to True in the metadata
XML('foo', applicable=lambda metadata: metadata['bar'])

# becomes:
XML('foo', applicable=Metadata('bar'))

However, other cases can now use the same syntax to be a lot more readable:

# extract the foo tag if a bar tag exists in the node & is truthy
Combined(
	XML('foo'),
	XML('bar'),
	transform=lambda values: value[0] if value[1] else None
)

# becomes:
XML('foo',
	applicable=XML('bar')
)
@lukavdplas
Copy link
Contributor Author

lukavdplas commented Jun 6, 2023

Note: the secondary_tag property on the XML extractor is also used to specify some kind of condition which could be captured by this as well, I think. -> this already covered by #18

@lukavdplas lukavdplas transferred this issue from CentreForDigitalHumanities/I-analyzer Feb 16, 2024
@lukavdplas lukavdplas added the enhancement New feature or request label Feb 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant