Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
('I am tired of this stuff.', 'neg'),
("I can't deal with this", 'neg'),
('He is my sworn enemy!', 'neg'),
('My boss is horrible.', 'neg')
]
test = [
('The beer was good.', 'pos'),
('I do not enjoy my job', 'neg'),
("I ain't feeling dandy today.", 'neg'),
("I feel amazing!", 'pos'),
('Gary is a friend of mine.', 'pos'),
("I can't believe I'm doing this.", 'neg')
]
classifier = NaiveBayesClassifier(train)
class WordListTest(TestCase):
def setUp(self):
self.words = 'Beautiful is better than ugly'.split()
self.mixed = ['dog', 'dogs', 'blob', 'Blobs', 'text']
def test_len(self):
wl = tb.WordList(['Beautiful', 'is', 'better'])
assert_equal(len(wl), 3)
def test_slicing(self):
wl = tb.WordList(self.words)
first = wl[0]
assert_true(isinstance(first, tb.Word))
assert_equal(first, 'Beautiful')
def test_init_with_tsv_file(self):
with open(TSV_FILE) as fp:
cl = NaiveBayesClassifier(fp)
assert_equal(cl.classify("I feel happy this morning"), 'pos')
training_sentence = cl.train_set[0][0]
assert_true(isinstance(training_sentence, unicode))
class MockRedisFormat(formats.BaseFormat):
def __init__(self, client, port):
self.client = client
self.port = port
@classmethod
def detect(cls, stream):
return True
def to_iterable(self):
return redis_train
formats.register('redis', MockRedisFormat)
mock_redis = mock.Mock()
cl = NaiveBayesClassifier(mock_redis, format='redis', port=1234)
assert_equal(cl.train_set, redis_train)
def __init__(self):
"""
Constructor
"""
super(TextBlobModel, self).__init__()
# Classifier
self._cl = NaiveBayesClassifier(train_set=[])
# end __init__
train = data.iloc[:x,:].reset_index(drop = True)
test = data.iloc[x:,:].reset_index(drop = True)
from textblob.classifiers import NaiveBayesClassifier as NBC
training_corpus = []
for k in range(len(train)):
training_corpus.append((train.content[k], train.sentiment[k]))
test_corpus = []
for l in range(len(test)):
test_corpus.append((test.content[l], test.sentiment[l]))
model = NBC(training_corpus)
print(model.accuracy(test_corpus))
predictions = []
for m in range(len(test)):
predictions.append(model.classify(test.content[m]))
predictions_df = pd.DataFrame({'Content':test.content, 'Emotion_predicted':predictions, 'Emotion_actual':test.sentiment})
predictions_df.to_csv('naive_emotion_recognizer.csv', index = False)
elapsed_time = time.time() - start_time
print ("processing time:", elapsed_time, "seconds")
'what are you doing at the moment',
'what are you working on',
'what you making')
experience_utterances = [(x, 'experience') for x in experience_utterances]
environment_utterances = [(x, 'enivornment') for x in environment_utterances]
working_on_utterances = [(x, 'working') for x in working_on_utterances]
# FIXME: find better way to flatten lists together
training_set = []
training_set.extend(experience_utterances)
training_set.extend(environment_utterances)
training_set.extend(working_on_utterances)
classifier = NaiveBayesClassifier(training_set)
print(classifier.show_informative_features(), classifier.labels())
bogus_utterances = (
'if you going to use nltk u may want to check this out spacy .io',
'sup people? I see the weather\'s getting better over there, Ben.',
'i had the same problem your having so thats my i made my own.',
'try http, instead of https'
)
# TODO: Figure out how to make this stronger
dual_utterance = ('how long have you been coding and what IDE do you use',)
test_utterances = ('what are you making',
'hey that nyancat is cool, how do you get that?')
for t in test_utterances:
from textblob.classifiers import NaiveBayesClassifier
train = [
('I love this sandwich.', 'pos'),
('This is an amazing shop!', 'pos'),
('We feel very good about these beers.', 'pos'),
('That is my best sword.', 'pos'),
('This is an awesome post', 'pos'),
('I do not like this cafe', 'neg'),
('I am tired of this bed.', 'neg'),
("I can't deal with this", 'neg'),
('She is my sworn enemy!', 'neg'),
('I never had a caring mom.', 'neg')
]
cl = NaiveBayesClassifier(train)
print(cl.classify("I just love breakfast"))
print(cl.classify("Yesterday was Sunday"))
print(cl.classify("Why can't he pay my bills"))
print(cl.classify("They want to kill the president of Bantu"))
pol_labels = pol_df['labels'].copy()
pol_labels[pol_mask] = 'pos'
pol_labels[~pol_mask] = 'neg'
pol_df['etc'] = pol_labels
pol_df
nb_training = set()
for i, row in pol_df.iterrows():
nb_training.add((row[0], row[2]))
for i, row in adj_df.iterrows():
nb_training.add((row[0], row[2]))
nb_training
nbc = NaiveBayesClassifier(nb_training)
prob_dist = nbc.prob_classify('trump hates racism')
prob_dist.max()
prob_dist.prob('neg')
nb_name = 'naivebayesclassifier.pkl'
with open(nb_name, 'wb') as f:
pickle.dump(nbc, f)
lin_reg_training = {}
for i, row in adj_df.iterrows():
lin_reg_training[row[0]] = round(row[1]/10.0, 3)
for i, row in pol_df.iterrows():
lin_reg_training[row[0]] = round(row[1]/10.0, 3)
with open('sentiment_lexicon.pkl', 'wb') as f: