
Text Classification Using Python 209
from nltk.util import ngrams
pos_feat = [(word_feats(ngrams(reader.words(fileids=[f]), 2)),
’pos’)
for f in pos_ids]
Listing 6: Generation of the positive and negative feature dictionaries
that when the two words not and funny appear together we are 14.3 times more like to
be looking at a negative movie review than a positive one.
FIGURE 9.6: Performance of NLTK model using bigrams.
Now that bigrams features have been modelled, using trigrams is as simple as changing
the parameter passed to the ngrams function from 2 to 3 as shown in Listing 7.
pos_feat = [(word_feats(ngrams(reader.words(fileids=[f]), 3)),
’pos’)
for