Skip to content

Instantly share code, notes, and snippets.

@language-engineering
Last active October 12, 2015 06:38
Show Gist options
  • Save language-engineering/3986589 to your computer and use it in GitHub Desktop.
Save language-engineering/3986589 to your computer and use it in GitHub Desktop.
from sussex_nltk.corpus_readers import AmazonReviewCorpusReader
def get_training_testing(category, feature_extractor=None, split=0.7):
'''
Helper function. Splits data evenly across positive and negative, and then formats it
ready for naive bayes. You can also optionally pass in your custom feature extractor
(see next section), and a custom split ratio.
'''
arcr = AmazonReviewCorpusReader()
pos_train, pos_test = split_data(arcr.positive().category(category).documents(), split)
neg_train, neg_test = split_data(arcr.negative().category(category).documents(), split)
training_data = format_data(pos_train, "pos", feature_extractor) + format_data(neg_train, "neg", feature_extractor)
testing_data = format_data(pos_test, "pos", feature_extractor) + format_data(neg_test, "neg", feature_extractor)
return testing_data, training_data
dvd_test, dvd_training = get_training_testing("dvd")
book_test, book_training = get_training_testing("book")
kitchen_test, kitchen_training = get_training_testing("kitchen")
electronics_test, electronics_training = get_training_testing("electronics")
source_1 = electronics_training
target_1 = electronics_test
source_2 = dvd_training + book_training + kitchen_training
target_2 = electronics_test
source_3 = dvd_training + book_training + kitchen_training + electronics_training
target_3 = electronics_test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment