spaCy icon indicating copy to clipboard operation
spaCy copied to clipboard

Custom entity recognition from gazette

Open xiapoy opened this issue 9 years ago • 4 comments

Hi,

I have a gazette of expressions as a list:

fruits = [
orange,
banana,
apple,
pineapple,
dragon fruit,
...]

Some contains more than 1 token.

I want to tag all these expressions as a new custom entity called 'FRUIT'.

I understood I'll need the spaCy matcher, but I didn't find a simple and easy way to do this in the docs yet.

My expected output would be:

import spacy
nlp = spacy.load('en')
doc = nlp(u'Today I went to the supermarket and bought 1 orange and 1 banana.')
for ent in doc.ents:
    print(ent.label_, ent.text)
    # FRUIT orange
    # FRUIT banana

Do you have any code suggestion on how to do this?

Thanks for building spaCy, such a great framework!

xiapoy avatar Sep 29 '17 08:09 xiapoy

Sorry, the matcher docs for v1.x are currently not as complete as they should be.

spaCy v2.0, currently in alpha comes with a few improvements to the Matcher, and more extensive docs and examples. Note that not all of the behaviour described here is available in v1.x, though. However, if you want to try the new version, you can do so via pip install spacy-nightly.

One option would be to create token match patterns based on your list, for example:

patterns = [
    [{'ORTH': 'orange'}],  # match the exact string "orange"
    [{'LOWER': 'dragon'}, {'LOWER': 'fruit'}]  # match two tokens whose lowercase texts mach "dragon" and "fruit"
]

Each dictionary represents one token, and you can use the available token attributes, e.g. ORTH, LOWER, but also IS_PUNCT, LIKE_NUM etc.

If your list is very large, you could also use the new PhraseMatcher (only available in spaCy v2.0!), which works just like the Matcher, but takes Doc objects instead of token patterns.

import spacy
from spacy.matcher import PhraseMatcher

nlp = spacy.load('en')                   # or whichever model you're using
docs = [nlp(fruit) for fruit in fruits]  # create a Doc for each fruit

matcher = PhraseMatcher(nlp.vocab)       # initialise the matcher with the shared vocab
matcher.add('FRUIT', on_match, *docs)    # add FRUIT pattern and add docs as match patterns

As the second argument of matcher.add(), you can pass in an on_match callback that's executed when the matcher finds a match, and gives you access to it. See this example for details on how to use it to add entities.

ines avatar Oct 04 '17 14:10 ines

Thanks @ines for taking the time to answer in details 🙂I'll try this out! spaCy is just great!

xiapoy avatar Oct 10 '17 19:10 xiapoy

@kimeaa Yay, thanks! Btw, we just published this blog post about spaCy v2.0 and it includes more examples of using the PhraseMatcher to assign entities based on a terminology list: https://explosion.ai/blog/spacy-v2-pipelines-extensions

ines avatar Oct 16 '17 18:10 ines

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar May 08 '18 14:05 lock[bot]