You are right, this is confusing and it's not in the documentation. Since you are new in Object Oriented Programming, I'll try to explain it as simple as I can.
In OOP, the main difference from a non-OOP language, is the existence of classes. Now you may think of a class, as you think of a struct (i.e. a group of variables that serve a common goal), but that can also have functions, called methods, that can be called at this class. Just like with a struct, you can create a variable of the type specified by this class, and this variable is called an instance or an object of this class (that's where OOP gets its name). To create an object of this class, you must call its constructor. The constructor returns an object of this class. Let's see an example of a class in Python.
class Human():
def __init__(self, my_name, my_age):
self.name = my_name
self.age = my_age
def introduce(self):
print(
"Hello, world! My name is {} and I'm {} years old!"
.format(self.name, self.age)
)
alice = Human("Alice", 23)
alice.introduce()
bob = Human("Bob", 24)
bob.introduce()
# here we don't keep the created object
Human("Catie", 25).introduce()
Here we have a class called Human, which has a constructor (called __init__()
in Python), and a method called introduce()
which prints some message. From the constructor, we can also see that the class Human has two private variables called name
and age
.
When run, the above code produces:
Hello, world! My name is Alice and I'm 23 years old!
Hello, world! My name is Bob and I'm 24 years old!
Hello, world! My name is Catie and I'm 25 years old!
Now to your original question. In the module nltk, we have some submodules. We have the module nltk
and the submodules nltk.tokenize
and the nltk.tokenize.casual
, along with many more.
Now in all three of the aforementioned modules, there is a constructor for a class called TweetTokenizer
. You can create an object of this class by calling its constructor.
Take a look at the third Human
created in our example, Cathie. You see that we don't keep the object returned by the constructor to a variable, we just call introduce()
and then the object is discarded.
You can do the same with the TweetTokenizer
constructor. In case you don't want to hold the object returned by the constructor, you can just create an object and immediately use it, and then discard it. So you can do all the following in Python:
nltk.TweetTokenizer().tokenize('hello world')
nltk.tokenize.TweetTokenizer().tokenize('hello world')
nltk.tokenize.casual.TweetTokenizer().tokenize('hello world')
And the all produce the same result:
['hello', 'world']
Since in your code, you will use the tokenizer many times, not just once, it's more appropriate (and more efficient) to hold the constructed object in a variable, and then call that tokenize()
method on it, as many times as you want, like so:
tknzr = nltk.tokenize.casual.TweetTokenizer()
tknzr.tokenize('hello world')
tknzr.tokenize('this is fun')
which produce the following:
['hello', 'world']
['this', 'is', 'fun']
You can use whichever of the three constructors you want, but I would suggest you use the nltk.tokenize.casual.TweetTokenizer()
, because it's the one mentioned in the documentation.
I hope I didn't confuse you more. If you have any questions, leave a comment bellow.