Dictionary over PhraseMatcher causes slowdowns
Your Environment
Location /home/damiano/lavoro/python/testing/spacy
Python version 3.5.2
Models en, it
spaCy version 2.0.0
Platform Linux-4.4.0-98-generic-x86_64-with-Ubuntu-16.04-xenial
Hello! I have a dictionary with ~5000 words that i must match inside my document, i do:
patterns = [nlp(pattern) for pattern in open("myfile.txt").read().splitlines()]
matcher = PhraseMatcher(nlp.vocab)
matcher.add('citizenship', None, *patterns)
Here comes the problem, because if we have added custom components the nlp(pattern) will call them repeatedly (5000 in my case!). It is a huge amount of unnecessary work.
I have tried with a simple workaround doing:
nlp2 = spacy.load('it') # New model without custom components
patterns = [nlp2(pattern) for pattern in open("myfile.txt").read().splitlines()]
matcher = PhraseMatcher(nlp2.vocab)
matcher.add('citizenship', None, *patterns)
The problem with the code above is that it does not match anything when i do matches = matcher(doc)
How can we solve this problem?
Code:
import spacy
from spacy.matcher import PhraseMatcher
class FirstRecognizer(object):
name = 'first'
def __init__(self, nlp):
print("First component init!")
def __call__(self, doc):
print(">>> First component call <<<")
return doc
class SecondRecognizer(object):
name = 'second'
def __init__(self, nlp):
print("Second component init!")
patterns = [nlp(pattern) for pattern in ["one", "two", "three", "four"]]
self.matcher = PhraseMatcher(nlp.vocab)
self.matcher.add('second_match', None, *patterns)
def __call__(self, doc):
print(">>> Second component call <<<")
matches = self.matcher(doc)
print("Total matches: " + str(len(matches)))
return doc
nlp = spacy.load('en')
nlp.add_pipe(FirstRecognizer(nlp))
nlp.add_pipe(SecondRecognizer(nlp))
doc = nlp("This is one example.")
# output
First component init!
Second component init!
>>> First component call <<<
>>> First component call <<<
>>> First component call <<<
>>> First component call <<<
>>> First component call <<<
>>> Second component call <<<
Total matches: 1
Code with workaround (no matches)
import spacy
from spacy.matcher import PhraseMatcher
class FirstRecognizer(object):
name = 'first'
def __init__(self, nlp):
print("First component init!")
def __call__(self, doc):
print(">>> First component call <<<")
return doc
class SecondRecognizer(object):
name = 'second'
def __init__(self, nlp):
print("Second component init!")
nlp2 = spacy.load('en')
patterns = [nlp2(pattern) for pattern in ["one", "two", "three", "four"]]
self.matcher = PhraseMatcher(nlp2.vocab)
self.matcher.add('second_match', None, *patterns)
def __call__(self, doc):
print(">>> Second component call <<<")
matches = self.matcher(doc)
print("Total matches: " + str(len(matches)))
return doc
nlp = spacy.load('en')
nlp.add_pipe(FirstRecognizer(nlp))
nlp.add_pipe(SecondRecognizer(nlp))
doc = nlp("This is one example.")
# output
First component init!
Second component init!
>>> First component call <<<
>>> Second component call <<<
Total matches: 0
Try to look at nlp.disable_pipes:
with nlp.disable_pipes(*custom_components):
patterns = [nlp(pattern) for pattern in open("myfile.txt").read().splitlines()]
matcher = PhraseMatcher(nlp.vocab)
matcher.add('citizenship', None, *patterns)
@GreenRiverRUS pardon for the late reply. It works perfectly.
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.