spaCy icon indicating copy to clipboard operation
spaCy copied to clipboard

incorrect entity extraction for persons and GPE

Open jacob7182 opened this issue 8 years ago • 2 comments

Hi,

I am looking to extract names and locations using NER in spaCy, however I have found it tags lot of non persons as PERSON and non locations as GPE e.g Buenos Aires as PERSON M.Sc, Duffy, PK, Criteria as GPE Milligrams, AE, DOSE, PP, Failures and lots of two letter abbreviation as PERSON Many more such examples are there.

I am using the standard code import spacy nlp = spacy.load('en_core_web_sm') doc = nlp('sometext') for ent in doc.ents: print(ent.text, ent.start_char, ent.end_char, ent.label_)

Is there is there a way to remove incorrect tagged words? or can we specify nlp to avoid certain types of words (like stopwords/small case) from identifying as people/locations? e.g in case of identifying Person/Locations, it could avoid words starting with small case.

Environment

  • Operating System: WIndows 64
  • Python Version Used: 3.6
  • spaCy Version Used: 2.0.11
  • Environment Information: Anaconda 3

jacob7182 avatar May 25 '18 08:05 jacob7182

The model is statistical, so its predictions always depend on the data the model was trained on and how similar the texts you're processing are to what the model has seen before (also see #2330 for some background on this).

Some of your results do look a bit surprising, though. Were those entities part of full sentences? (As a quick check, you might also want to run python -m spacy validate to make sure your models are up to date and compatible.)

In spaCy v2.x, it's much easier to update the existing model with a small number of examples and improve its predictions. In your case, you already have examples it's failing on, so you can correct them and feed them back into the model. See the docs on training for more details.

Alternatively, maybe all you need is a rule-based approach – that's totally fine, too. You could, for example, add a simple component to the end of your pipeline (after the entity recognizer) that filters out entities based on your own rules and specific criteria and overwrites the doc.ents with the filtered list. See here for more details on pipeline components.

def filter_entities(doc):
    new_ents = []
    for ent in doc.ents:
        # entities are spans, so you can check whether the first token is lowercase etc.
        if not ent[0].is_lower: 
            new_ents.append(ent)
    doc.ents = new_ents
    return doc

Of course, you can include many more rules and conditions. You can then add the component to your pipeline like this:

nlp.add_pipe(filter_entities, after='ner')

ines avatar May 25 '18 10:05 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 Jun 24 '18 11:06 lock[bot]