spaCy icon indicating copy to clipboard operation
spaCy copied to clipboard

LOWER does not always work when presented with random casing

Open mjsonofharry opened this issue 3 years ago • 3 comments

How to reproduce the behavior

Create a matcher with the following pattern:

[{'LOWER': 'git'}]

Then match on this sentence:

GitHub is a platform and cloud-based service for software development and version control using gIT.

Observe that "gIT" is correctly matched. Then try matching on this sentence:

GitHub is a platform and cloud-based service for software development and version control using giT.

This time, observe that "giT" is not matched.

Here's a demo of the problem I made using the online rule-based matcher explorer.

Alternatively, here's a code snippet:

from spacy.matcher import Matcher
import spacy

spacy.cli.download('en_core_web_sm')
model = spacy.load('en_core_web_sm')

matcher = Matcher(model.vocab)
matcher.add(key=0, patterns=[[{'LOWER': 'git'}]])

sentence1 = "GitHub is a platform and cloud-based service for software development and version control using gIT."
sentence2 = "GitHub is a platform and cloud-based service for software development and version control using giT."

doc1 = model(sentence1)
matches1 = matcher(doc1, as_spans=True)
print(f"{sentence1} {matches1}")

doc2 = model(sentence2)
matches2 = matcher(doc2, as_spans=True)
print(f"{sentence2} {matches2}")

Punctuation seems to play a role, but I'm not sure how. If you remove the period from the end of the failing sentence, the matcher works.

Your Environment

  • spaCy version: 3.6.1
  • Platform: macOS-13.5-arm64-arm-64bit
  • Python version: 3.9.17
  • Pipelines: en_core_web_sm (3.6.0)

mjsonofharry avatar Aug 11 '23 19:08 mjsonofharry

Thanks for raising this issue, @mjsonofharry. We'll look into it and update here.

rmitsch avatar Aug 16 '23 12:08 rmitsch

So this is related to incorrect tokenization. The matcher works as intended, but in the second sentence, the dot is added to the word. To check, run this:

for token in doc1:
    print(token)
for token in doc2:
    print(token)

You'll notice that for doc2 the last token is "gIT.", which is why this token doesn't match the [{'LOWER': 'git'}] rule. To circumvent this, you might want to add a special tokenization rule.

rmitsch avatar Aug 23 '23 06:08 rmitsch

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.

github-actions[bot] avatar Sep 23 '23 00:09 github-actions[bot]