I'm a little unsure of what's going on with the syntax when using the NLTK to tokenize strings.

In the tips for the pset this code is shown initially:


Then they mention this, removing the "casual" part. So I'm confused why that's not needed and also why this needs to be split up in to two lines of code like this:

tokenizer = nltk.tokenize.TweetTokenizer()
tokens = tokenizer.tokenize(tweet) 

I managed to get it working in one line of code as follows, however if I removed the parenthesis after TweetTokenizer; the program produced an error. So this code works:

tokes = nltk.tokenize.TweetTokenizer().tokenize(text)

But this doesn't:

tokes = nltk.tokenize.TweetTokenizer.tokenize(text)

Hoping someone can explain what's going on. I'm very new to OOP but my understanding was that it was all hierarchical so I'm confused why "tokenize" is used twice in all examples above.


1 Answer 1


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):
            "Hello, world! My name is {} and I'm {} years old!"
            .format(self.name, self.age)

alice = Human("Alice", 23)

bob = Human("Bob", 24)

# 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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .