I did not use nltk package for unigram model, and I had nearly 100 lines. It will be a lot complicated for bigram or trigram. I heard that NLTK can calculate perplexity score.
import nltk
from nltk.model.ngram import NgramModel
from nltk.probability import LidstoneProbDist
f_in= open(r'C:\Python27\A.txt')
ln = f_in.read()
words = nltk.word_tokenize(ln)
my_bigrams = nltk.bigrams(words)
my_trigrams = nltk.trigrams(words)
estimator = lambda fdist, bins: LidstoneProbDist(fdist, 0.2)