Custom recipe with helper args

Initially asked this question but moving to this forum .

I have a custom recipe for annotating relation extraction annotations. The rel.manual recipe has an argument to add entities to the incoming text by adding the argument --add-ents. However I cant figure out how to apply this to a custom recipe.

To call the recipe I would run

prodigy rel_ext con_rel -F scripts/cust_id.py

and it works in that I can start annotating, but there are no entities so I really cant annotate the relations.

My first thought was to try adding the argument for the rel.manual recipe to the command for a custom recipe.

prodigy rel_ext con_rel -F scripts/cust_id.py --add-ents

But this creates the following error.

usage: prodigy rel_ext [-h] dataset [view_id]
prodigy rel_ext: error: unrecognized arguments: --add-ents

Is there a place you can add that argument, or are there hooks in the custom recipe to specify the custom recipe uses that argument. What I ended up doing was to create a function in the in the recipe to do this

    stream = get_stream()
    stream = add_tokens(nlp, stream)
    stream = add_ents_to_stream(nlp, stream) 

But it seems like there should be a way to use the --add-ents argument which likely does something similar to the add_ents_to_stream function that gets defined in this recipe. It seems like it would be more complicated for arguments like --patterns to implement those as a defined function in the recipe.

hi @darrkj!

Thanks for your question and welcome to the Prodigy community :wave:

Try to add this to your recipe:

from typing import List, Optional
import spacy

def setup_spans(
    spans: List[Span], span_type: str, default_label: Optional[str] = None
) -> List[Span]:
    result = []
    for span in spans:
        all_labels = [*span_label, NP_LABEL] if span_label else [NP_LABEL]
        if span_type == "pattern":
            new_label, _ = parse_pattern_name(span.label_)
        else:
            new_label = span.label_ or default_label or span_type.upper()
        if span_label is not None and new_label not in all_labels:
            continue
        span._.type = span_type
        span._.label = new_label
        # Set token attributes so they can be targeted by disable patterns
        for token in span:
            token._.label = new_label
            token._.type = span_type
        result.append(span)
    return result

spans.extend(setup_spans(doc.ents, "ent"))

I haven't fully tested out so there may be a missing import but should give you the general idea of what --add-ents is doing.

If you need more context, you can see the underlying recipe where prodigy is installed. You can find that as the Location when you run python -m prodigy stats. Then look for the /recipes/rel.py file.

Hope this helps!