# How to get string offsets from custom NER pipeline?

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-get-string-offsets-from-custom-ner-pipeline%2F12212%3E%5C
**Category:** 🤗Transformers\
**Created:** [November 23, 2021, 2:56pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-get-string-offsets-from-custom-ner-pipeline%2F12212 "2021-11-23T14:56:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndreaSottana](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@AndreaSottana](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FAndreaSottana)%5C
**Post date:** [November 23, 2021, 2:56pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-get-string-offsets-from-custom-ner-pipeline%2F12212%2F1 "2021-11-23T14:56:56Z")

</div>

Hello,  
I have fine-tuned a custom NER model, and when doing inference, I would like a way to convert from token-level predictions back to the offsets of the original string.  
Take the example below

```auto
from transformers import pipeline
ner = pipeline("ner", model='dbmdz/bert-large-cased-finetuned-conll03-english')
print(ner("I went to Rome yesterday to watch Rome vs Bayer Munich"))

```

I get this output

```auto
[{'entity': 'I-LOC', 'score': 0.99968606, 'index': 4, 'word': 'Rome', 'start': 10, 'end': 14}, 
{'entity': 'I-ORG', 'score': 0.9314538, 'index': 8, 'word': 'Rome', 'start': 32, 'end': 36}, 
{'entity': 'I-ORG', 'score': 0.99915195, 'index': 10, 'word': 'Bay', 'start': 40, 'end': 43}, 
{'entity': 'I-ORG', 'score': 0.99730355, 'index': 11, 'word': '##er', 'start': 43, 'end': 45},
{'entity': 'I-ORG', 'score': 0.9979789, 'index': 12, 'word': 'Munich', 'start': 46, 'end': 52}]

```

As you can see the entities are split into tokens (e.g. “Bayer Munich” takes 3 tokens). What I’d like to get is something like this

```auto
[
  {
    "entity_group": "LOC",
    "score": 0.9996861219406128,
    "word": "Rome",
    "start": 10,
    "end": 14
  },
  {
    "entity_group": "ORG",
    "score": 0.9314539432525635,
    "word": "Rome",
    "start": 32,
    "end": 36
  },
  {
    "entity_group": "ORG",
    "score": 0.9981448650360107,
    "word": "Bayer Munich",
    "start": 40,
    "end": 52
  }
]

```

Is there a clean way to achieve this, or do I have to write a function to do this myself?  
Please give answers which are also applicable to custom models not available in the HuggingFace Hub, as ultimately my pipeline would be something like

```auto
ner = pipeline("ner", model='my_trained_model', tokenizer=AutoTokenizer.from_pretrained("...")

```

where `my_trained_model` is the path to the folder containing my model trained with the `transformers.Trainer`
