Home > Software engineering >  How does the nlp object work in spacy library?
How does the nlp object work in spacy library?

Time:02-06

From what I understand so far, it is an instance of the 'Language' class in spacy, and can process text and perform a bunch of operations on it.

import spacy

nlp = spacy.blank("en")

# Process the text
doc = nlp(
    "In 1990, more than 60% of people in East Asia were in extreme poverty. "
    "Now less than 4% are."
)

print(doc[0])
//prints "In"

The question that bothers me is that how does an object accept an argument(a string in this case) like a class does? What is the process?

I tried the following code to check if an object can receive an argument..

class ABC:
    def __init__(self,a=1):
        self.a = a
        
    def printa(self):
        print(self.a)

abc = ABC()
abc(2)
abc.printa()

It gives me an error: TypeError: 'ABC' object is not callable

spacy seems to be doing the same thing and it works..How?

CodePudding user response:

You can allow an object to be called like a function by providing a __call__ method:

class ABC:
    def __init__(self,a=1):
        self.a = a

    def printa(self):
        print(self.a)

    def __call__(self, *args):
        print(self.a, *args)


abc = ABC()
abc(2)
abc.printa()

Output:

1 2
1

Implementing the __call__ method makes the object callable.

As for the actual type of nlp:

>>> type(nlp)
<class 'spacy.lang.en.English'>

And it does indeed have __call__:

>>> hasattr(nlp, '__call__')
True
  • Related