# Mismatched tokenization

**URL:** <https://support.prodi.gy/t/mismatched-tokenization/5933>\
**Category:** Uncategorized\
**Created:** [September 13, 2022, 3:27pm UTC](https://support.prodi.gy/t/mismatched-tokenization/5933 "2022-09-13T15:27:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ttandel](https://avatars.discourse-cdn.com/v4/letter/t/85f322/32.png) [@ttandel](https://support.prodi.gy/u/ttandel)\
**Post date:** [September 13, 2022, 3:27pm UTC](https://support.prodi.gy/t/mismatched-tokenization/5933/1 "2022-09-13T15:27:01Z")

</div>

Hi Team,  
I am trying ner.manual recipe on dataset with pre-set spans. 90% of the time its working as expected but 10% records are throwing exception.

" **ValueError: Mismatched tokenization. Can't resolve span to token index 228. This can happen if your data contains pre-set spans. Make sure that the spans match spaCy's tokenization or add a 'tokens' property to your task**"

 ![image](https://us1.discourse-cdn.com/flex020/uploads/prodigy/original/2X/0/0a6cc002a777f537f4fd3fd6f25f3628f0b9edda.png)

input text is as below :  
{"text":"Contract Price:\r\nFor each Calculation Period, the price of the LAST NYM TRADING DAY of each NYMEX Natural Gas Futures contract from and\r\nincluding the February 2022 contract to and including the February 2022 contract PLUS USD .4 per MMBTU.","spans":[{"start":63,"end":83,"label":"prcIndex"},{"start":92,"end":117,"label":"prcIndex"},{"start":195,"end":208,"label":"prcWindow"},{"start":218,"end":222,"label":"prcDiffSign"},{"start":223,"end":226,"label":"prcCCY"},{"start":228,"end":229,"label":"prcDiffAmt"},{"start":234,"end":239,"label":"prcUOM"}]}

Could you please point me out where is the issue and what should I do to fix this ?

Thanks,

---

<div class="post-metadata">

**Author:** ![ryanwesslen](https://sea2.discourse-cdn.com/flex020/user_avatar/support.prodi.gy/ryanwesslen/32/2969_2.png) [@ryanwesslen](https://support.prodi.gy/u/ryanwesslen)\
**Post date:** [September 13, 2022, 4:16pm UTC](https://support.prodi.gy/t/mismatched-tokenization/5933/2 "2022-09-13T16:16:59Z")

</div>

hi @ttandel!

It seems the _character offsets_ in your annotations are referring to spans of texts that **do not map** to the _token boundaries_ produced by the tokenizer (`blank:en`) you're using in `ner.manual`.

What tokenizer did you use to get the pre-set spans?

Any chance you could redo the pre-set spans to use the same tokenizer as `ner.manual` (i.e., use the `blank:end`)?

This would be the easiest fix but I suspect it may not be possible. There are a lot (~39) posts with the keyword ["mismatched tokenization"](https://support.prodi.gy/search?q=mismatched%20tokenization) that can help.

I couldn't go through all of them, but this one gives you a snippet of code to identify which are the mismatched spans:

> [@Mismatched Tokenization on NER](https://support.prodi.gy/t/mismatched-tokenization-on-ner/4353/2):
>
> Hi! Are you able to find that particular example it fails on in your data? The basic problem here is this: the character offsets in your annotations are referring to spans of texts that do not map to the token boundaries produced by the tokenizer. This means that an NER model, which predicts token-based tags, couldn't be updated with this information and learn anything from it, because it will nevr actually get to see those tokens at runtime. For instance, imagine you have a text like "AB C", w…

Similarly, as this post discusses, the best course of action depends on what are the mismatches:

> [@Matching tokenisation on pre-existing annotated data](https://support.prodi.gy/t/matching-tokenisation-on-pre-existing-annotated-data/2710/2):
>
> Hi! The best approach depends on what the underlying mismatches are. If you need a better overview, you could write a small script that processes your texts with spaCy's default tokenizer and then calls [doc.char\_span](https://spacy.io/api/doc#char_span) with the start and end offsets. If that returns None, you have a mismatch. Maybe you'll find that it mostly comes down to things like punctuation (e.g. hyphens) that can be adjusted by slightly [tweaking spaCy's tokenization](https://spacy.io/usage/linguistic-features#native-tokenizer-additions) to better match your data. Or maybe it turns out that some…

See if you can use these posts to help and feel free to post back questions if you run into issues (or let us know if you're able to fix it!)

Also, a small ask, moving forward, please refrain from posting images of the code and/or output. It's easier to use the markdown code output as this will allow us to copy/paste (especially for code) and also make the code searchable instead of images. Thank you 🙂
