Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. import random
  2. from nltk.corpus import movie_reviews
  3. from textblob.classifiers import NaiveBayesClassifier
  4. random.seed(1)
  5.  
  6. train = [
  7. ('I love this sandwich.', 'pos'),
  8. ('This is an amazing place!', 'pos'),
  9. ('I feel very good about these beers.', 'pos'),
  10. ('This is my best work.', 'pos'),
  11. ("What an awesome view", 'pos'),
  12. ('I do not like this restaurant', 'neg'),
  13. ('I am tired of this stuff.', 'neg'),
  14. ("I can't deal with this", 'neg'),
  15. ('He is my sworn enemy!', 'neg'),
  16. ('My boss is horrible.', 'neg')
  17. ]
  18. test = [
  19. ('The beer was good.', 'pos'),
  20. ('I do not enjoy my job', 'neg'),
  21. ("I ain't feeling dandy today.", 'neg'),
  22. ("I feel amazing!", 'pos'),
  23. ('Gary is a friend of mine.', 'pos'),
  24. ("I can't believe I'm doing this.", 'neg')
  25. ]
  26.  
  27. cl = NaiveBayesClassifier(train)
  28.  
  29. # Grab some movie review data
  30. reviews = [(list(movie_reviews.words(fileid)), category)
  31. for category in movie_reviews.categories()
  32. for fileid in movie_reviews.fileids(category)]
  33. random.shuffle(reviews)
  34. new_train, new_test = reviews[0:100], reviews[101:200]
  35.  
  36. # Update the classifier with the new training data
  37. cl.update(new_train)
  38.  
  39. # Compute accuracy
  40. accuracy = cl.accuracy(test + new_test)
  41. print("Accuracy: {0}".format(accuracy))
  42.  
  43. # Show 5 most informative features
  44. cl.show_informative_features(5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement