How to run this code (with no basic on using python)

63 views
Skip to first unread message

Zac

unread,
Feb 15, 2013, 1:30:19 AM2/15/13
to nltk-...@googlegroups.com
import nltk.classify.util
from nltk.classify import NaiveBayesClassifier
from nltk.corpus import movie_reviews
 
def word_feats(words):
    return dict([(word, True) for word in words])
 
negids = movie_reviews.fileids('neg')
posids = movie_reviews.fileids('pos')
 
negfeats = [(word_feats(movie_reviews.words(fileids=[f])), 'neg') for f in negids]
posfeats = [(word_feats(movie_reviews.words(fileids=[f])), 'pos') for f in posids]
 
negcutoff = len(negfeats)*3/4
poscutoff = len(posfeats)*3/4
 
trainfeats = negfeats[:negcutoff] + posfeats[:poscutoff]
testfeats = negfeats[negcutoff:] + posfeats[poscutoff:]
print 'train on %d instances, test on %d instances' % (len(trainfeats), len(testfeats))
 
classifier = NaiveBayesClassifier.train(trainfeats)
print 'accuracy:', nltk.classify.util.accuracy(classifier, testfeats)
classifier.show_most_informative_features()

Zac

unread,
Feb 15, 2013, 1:31:57 AM2/15/13
to nltk-...@googlegroups.com

Alex Rudnick

unread,
Feb 15, 2013, 1:36:29 AM2/15/13
to nltk-...@googlegroups.com
Hey Zac,

There are some really good resources out there for learning how to use
Python! I recommend Dive Into Python 3 and perhaps How To Think Like A
Computer Scientist, both of which are free online.

http://getpython3.com/diveintopython3/
http://www.greenteapress.com/thinkpython/

You're going to get a whole lot more out of NLTK if you learn some
programming and some Python.
> --
> You received this message because you are subscribed to the Google Groups
> "nltk-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nltk-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
-- alexr
Reply all
Reply to author
Forward
0 new messages