# Creating patterns library from scratch

**URL:** <https://support.prodi.gy/t/creating-patterns-library-from-scratch/4583>\
**Category:** Uncategorized\
**Tags:** usage\
**Created:** [August 17, 2021, 7:00pm UTC](https://support.prodi.gy/t/creating-patterns-library-from-scratch/4583 "2021-08-17T19:00:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![toxtoth](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@toxtoth](https://support.prodi.gy/u/toxtoth)\
**Post date:** [August 17, 2021, 7:00pm UTC](https://support.prodi.gy/t/creating-patterns-library-from-scratch/4583/1 "2021-08-17T19:00:28Z")

</div>

Hi all, i'm wanting to create a patterns library manually from scratch but i don't think there is an existing recipe for this exact use case; unless i'm missed it??

I know i can use a pre-existing patterns before starting an ner.manual annotation session to pre-select text, but i want to:

- load sample text and start prodigy session
- select tokens and label them
- save those tokens to the patterns library _as I am progressing through the session_
- have any previously saved patterns pre-selected _in the same annotation session_

i.e. the annotator is shown pre-selected patterns that they have already annotated in that session, so they know they have already created a pattern and don't need to annotate that text again

any thoughts?

---

<div class="post-metadata">

**Author:** ![ines](https://sea2.discourse-cdn.com/flex020/user_avatar/support.prodi.gy/ines/32/3_2.png) [@ines](https://support.prodi.gy/u/ines)\
**Post date:** [August 18, 2021, 12:39am UTC](https://support.prodi.gy/t/creating-patterns-library-from-scratch/4583/2 "2021-08-18T00:39:57Z")

</div>

Hi! There's no built-in workflow for this, but you should be able to implement something like it by adapting a recipe like [`ner.manual`](https://github.com/explosion/prodigy-recipes/blob/master/ner/ner_manual.py) and adding an `update` callback that updates your matcher from spans annotated in the data.

See this thread for a pretty similar approach (and some considerations for how to handle the batching):

> [@Prodigy Custom Model; Model in the Loop (matcher)](https://support.prodi.gy/t/prodigy-custom-model-model-in-the-loop-matcher/4534/2):
>
> Hi! This is definitely a cool idea +1 And yes, for that to work you should implement your own function that adds annotated spans to the matcher as patterns. The PatternMatcher currently only sets the pattern-based annotations and is updated with the accept/reject information to update the scores assigned to the patterns (so you can use it in an active learning context where you filter based on certain/uncertain predictions). It doesn't add annotated spans to the patterns, since this isn't alw…

Instead of going via the `PatternMatcher`, you might want to use the `Matcher` or `PhraseMatcher` directly, which removes one layer of abstraction. When setting the `"spans"` on the incoming examples, just make sure you're filtering overlaps (e.g. with spaCy's [`filter_spans`](https://spacy.io/api/top-level/#util.filter_spans) utility) so you don't end up with overlapping matches. Alternatively, you could also use the new [`spans_manual` UI](https://prodi.gy/docs/api-interfaces#spans_manual), which lets you create and show overlapping spans. This could be pretty useful, actually, because it'll let you view potential conflicts and ambiguous patterns.

In your case, you'd just have to decide whether you want your patterns to be exact string matches (`text[span["start"]:span["end"]]`), or if you want to tokenize them so you can have a list of tokens like `[{"lower": "foo"}, {"lower": "bar"}]`. That kinda depends on your use case.

In your recipe, you could also add an `on_exit` callback that exports all patterns in the matcher to a file once you exit the annotation session, and some logic on load that sets up the matcher from the current dataset (for when you want to resume a session). You could also do the export this as a separate command that takes the name of the dataset and exports a patterns file based on the annotations.

---

<div class="post-metadata">

**Author:** ![toxtoth](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@toxtoth](https://support.prodi.gy/u/toxtoth)\
**Post date:** [August 18, 2021, 12:39pm UTC](https://support.prodi.gy/t/creating-patterns-library-from-scratch/4583/3 "2021-08-18T12:39:55Z")

</div>

Thank you Ines for your very fast and detailed response! I will give it a go and thank you for your help
