# Accept hyphen(-) in patterns shape

**URL:** <https://support.prodi.gy/t/accept-hyphen-in-patterns-shape/875>\
**Category:** Uncategorized\
**Tags:** usage, ner, spacy\
**Created:** [October 12, 2018, 11:57am UTC](https://support.prodi.gy/t/accept-hyphen-in-patterns-shape/875 "2018-10-12T11:57:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![smanjil](https://avatars.discourse-cdn.com/v4/letter/s/48db29/32.png) [@smanjil](https://support.prodi.gy/u/smanjil)\
**Post date:** [October 12, 2018, 11:57am UTC](https://support.prodi.gy/t/accept-hyphen-in-patterns-shape/875/1 "2018-10-12T11:57:37Z")

</div>

Hi,

I was trying to annotate this texts:

```python
{"text":"Silicon Valley Investors Flexed Their Muscles in Uber C15.3 Fight"}
{"text":"Uber is a Creature of an Industry 2-123 Struggling to Grow Up 1-840x"}
{"text":"\u2018The Internet Is Broken\u2019: @ev Is P12.23 Trying to Salvage It"}

```

with the patterns defined as:

```python
{"label":"CODE","pattern":[{"SHAPE":"d-ddd"}], "example": ["7-780"]}
{"label":"CODE","pattern":[{"SHAPE":"Xdd.d"}], "example": ["C15.3"]}
{"label":"CODE","pattern":[{"SHAPE":"d-dddx"}], "example": ["1-840x"]}

```

This “P12.23” type of text is detected well, but the ones with hyphens in it like “1-840x” are not detected, but sometimes only 1 is detected as CODE and not others as a whole.

I would appreciate if anyone would suggest me on how to achieve this?

---

<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:** [October 12, 2018, 12:15pm UTC](https://support.prodi.gy/t/accept-hyphen-in-patterns-shape/875/2 "2018-10-12T12:15:12Z")

</div>

Hi! I think the main reason your patterns don't match is because the patterns are **token-based**. By default, the English tokenizer will split some of the strings into two tokens:

```python
from spacy.lang.en import English

nlp = English()
doc = nlp("7-780")
print([token.text for token in doc])
# ['7', '-', '780']

```

This means that your patterns also need to reflect this by defining **one dict per token**. For example:

```python
[{"SHAPE": "d"}, {"ORTH": "-"}, {"SHAPE": "ddd"}]

```

If you haven't seen it already, you might also find our interactive `Matcher` demo useful, which lets you test your patterns against a text to make sure they match:

> **[Rule-based Matcher Explorer · Demos · Explosion](https://explosion.ai/demos/matcher)**
>
> Test spaCy's rule-based Matcher by creating token patterns interactively and running them over your text. Explore how spaCy processes your text – and why your pattern matches, or doesn't.

---

<div class="post-metadata">

**Author:** ![smanjil](https://avatars.discourse-cdn.com/v4/letter/s/48db29/32.png) [@smanjil](https://support.prodi.gy/u/smanjil)\
**Post date:** [October 12, 2018, 12:22pm UTC](https://support.prodi.gy/t/accept-hyphen-in-patterns-shape/875/3 "2018-10-12T12:22:11Z")

</div>

Hi, Thank you for the quick help!

I came across the interactive pattern explorer and was able to generate this:

```python
{"label":"CODE", "pattern":[{"SHAPE": "d"}, {"ORTH": "-"}, {"SHAPE": "ddd"}], "example": ["7-780"]}
{"label":"CODE", "pattern":[{"SHAPE": "d"}, {"ORTH": "-"}, {"SHAPE": "dddx"}], "example": ["1-840x"]}

```

And, now, sometimes the pattern matches correctly and sometimes it just identifies 7 as a code from 7-780…

It’s strange, and sometimes although the code is there in the text it does not get it but rather highlights another word as a code. Why?

---

<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:** [October 12, 2018, 12:27pm UTC](https://support.prodi.gy/t/accept-hyphen-in-patterns-shape/875/4 "2018-10-12T12:27:53Z")

</div>

> [@smanjil](#):
>
> It’s strange, and sometimes although the code is there in the text it does not get it but rather highlights another word as a code. Why?

Are you using `ner.teach` with `--patterns`? Because if so, the other suggestions you're seeing are suggestions by the model. Once you've updated the model in the loop with examples of your `CODE` entity, it will try to also make suggestions. In the beginning, those can be random – but as you annotate and accept / reject the examples, the model should ideally adjust to the entity definition.

You might also want to try using a few more patterns with more examples (if possible for your category). After all, you're starting completely from scratch and the model doesn't know anything about your entity `CODE` yet.

Alternatively, if you really only want to label pattern matches in your text without a model in the loop, you could also just use the `ner.match` recipe.

---

<div class="post-metadata">

**Author:** ![smanjil](https://avatars.discourse-cdn.com/v4/letter/s/48db29/32.png) [@smanjil](https://support.prodi.gy/u/smanjil)\
**Post date:** [October 12, 2018, 12:29pm UTC](https://support.prodi.gy/t/accept-hyphen-in-patterns-shape/875/5 "2018-10-12T12:29:06Z")

</div>

Sounds good. Will certainly look into that and get back here. 🙂
