# Tokenizing Domain Specific Text

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftokenizing-domain-specific-text%2F1978%3E%5C
**Category:** 🤗Tokenizers\
**Created:** [November 11, 2020, 5:29am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftokenizing-domain-specific-text%2F1978 "2020-11-11T05:29:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jodiak](https://avatars.discourse-cdn.com/v4/letter/j/8797f3/32.png) [@jodiak](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fjodiak)%5C
**Post date:** [November 11, 2020, 5:29am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftokenizing-domain-specific-text%2F1978%2F1 "2020-11-11T05:29:49Z")

</div>

Hello everyone, I’ve been referencing [this](https://arxiv.org/pdf/2002.00212.pdf) paper on training transformer based models using metadata enhanced MIDI and was thinking about implementing this using the huggingface transformers and tokenizer libraries as an introduction to these libraries beyond the basic language modeling examples. As I’ve been researching and referencing [this](https://huggingface.co/blog/how-to-train) tutorial I’ve ran into issues with tokenization and was wondering when training a tokenizer, how can I set up “word level” semantics? Technically each “word” in this case will be the data within a string like so ‘Event(name=Position, time=360, value=4/16, text=360)’ rather than just words and characters delimited on spaces like its doing now as listed below  
` #version: 0.2 - Trained by huggingface/tokenizers`  
`m e`  
` a l`  
` u e`  
` al ue`  
` i me`  
` n ame`  
` v alue`  
` Ġ value`  
` Ġt ex`  
` Ġt ime`  
` Ev en`  
` Ġtex t`  
` Even t`  
` ) ,`  
` Ġ Event`  
` N o`

Apologies on if these questions are noobish I’m grokking a lot of this as I go along. Any help is greatly appreciated.

---

<div class="post-metadata">

**Author:** ![thomwolf](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/thomwolf/32/2198_2.png) [@thomwolf](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fthomwolf)%5C
**Post date:** [November 12, 2020, 12:48pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftokenizing-domain-specific-text%2F1978%2F2 "2020-11-12T12:48:50Z")

</div>

pinging @anthony and @Narsil here 🙂

---

<div class="post-metadata">

**Author:** ![Narsil](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/narsil/32/40029_2.png) [@Narsil](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FNarsil)%5C
**Post date:** [November 12, 2020, 1:18pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftokenizing-domain-specific-text%2F1978%2F3 "2020-11-12T13:18:38Z")

</div>

What is your original data like ?

The “Word level” semantics is usually dealt with the `Pretokenizer` logic (that basically splits up the data where it’s relevant). In your case, it would depend on your original data. There is more info in the docs:

> **[The tokenization pipeline — tokenizers documentation](https://huggingface.co/docs/tokenizers/rust/latest/pipeline.html)**

If you have a specific example of what data comes in, and what you expect as a return, it’s probably going to be easier to give you pointers.

Cheers,  
Nicolas

---

<div class="post-metadata">

**Author:** ![jodiak](https://avatars.discourse-cdn.com/v4/letter/j/8797f3/32.png) [@jodiak](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fjodiak)%5C
**Post date:** [November 17, 2020, 7:31am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftokenizing-domain-specific-text%2F1978%2F4 "2020-11-17T07:31:40Z")

</div>

Hello Narsil, my data looks something similar to this: `Note On_XX`, `Tempo Value_XX`, `Chord_Note:type`, `Position_X/X`, where XX can be a number from 0 to 127 and X can be a number from 1 to 16. I also have a dictionary with every possible values for the above text types numericalized. Will I have to write a custom tokenizer in order to accomplish this tokenization? Is there a way to use my existing numericalized dictionary in a tokenizer? Thank you for your help and the documentation I’ve begun going over it.

---

<div class="post-metadata">

**Author:** ![Narsil](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/narsil/32/40029_2.png) [@Narsil](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FNarsil)%5C
**Post date:** [November 17, 2020, 9:05am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftokenizing-domain-specific-text%2F1978%2F5 "2020-11-17T09:05:34Z")

</div>

Yes it seems you vocabulary is well defined and sufficiently small (127 \* 16 \* 16) to be in a “WordLevel” tokenizer.

> **[Components — tokenizers documentation](https://huggingface.co/docs/tokenizers/python/latest/components.html?highlight=wordlevel#models)**

You can even create your vocabulary manually which makes it easier to run.

```python
vocab = {}
i=0
for value in range(0, 128):
    for position in range(0, 16):
      word = "Note on {value},Position_{position}"
      vocab[word] = i
      i += 1

import tokenizers
from tokenizers import pre_tokenizers

tokenizer = Tokenizer(BPE(vocab=vocab))
tokenizer.pre_tokenizer = pre_tokenizers.Whitespace()

encoded = tokenizer.encode("Note On_12,Position_16/16 Note on 45,Position_8/8")
encoded.ids # [3, 5]

```

The vocabulary creation I wrote seems a bit off compared to what you described but it should be close enough.

I assumed the possible values are relatively small (\< 50 000) because of 128 notes + 16 tempos + 16 values which makes 32k possible values being conservative. If there’s actually an order of magnitude more, then this approach won’t work anymore.

Does that help ?

---

<div class="post-metadata">

**Author:** ![jodiak](https://avatars.discourse-cdn.com/v4/letter/j/8797f3/32.png) [@jodiak](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fjodiak)%5C
**Post date:** [November 20, 2020, 4:57am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftokenizing-domain-specific-text%2F1978%2F6 "2020-11-20T04:57:35Z")

</div>

Thank you this is incredibly helpful.
