Last active
September 5, 2020 01:34
-
-
Save josht-jpg/0584803a6d5f053b94fe99099d6047e3 to your computer and use it in GitHub Desktop.
Predicting sentence sentiment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def predict_sentences(book, stop_words): | |
#Break up book into sentences | |
book_sentences = pd.DataFrame(book.split("."), columns = ['sentence']) | |
#Clean sentences | |
book_sentences['sentence'] = book_sentences['sentence'].\ | |
apply(lambda x: clean_labelled(x, stop_words)) | |
book_sentences = book_sentences[book_sentences['sentence'].\ | |
str.len() > 0] | |
book_sentences['classification'] = book_sentences['sentence'].\ | |
apply(nrc_sentence) | |
#Adjust classifications | |
book_sentences_adjusted = labelled_adjust_class(book_sentences) | |
book_sentences_adjusted.index = np.arange(0, | |
book_sentences_adjusted.shape[0]) | |
#Predict sentences' sentiment | |
book_sentences_score = pd.concat([book_sentences_adjusted, pd.Series( \ | |
model.predict(book_sentences_adjusted.loc[:, 'anger':'trust']))], | |
axis = 1) | |
book_sentences_score = book_sentences_score.rename( \ | |
columns = {0 : 'sentence_score'}) | |
return book_sentences_score | |
notes_sentece_predictions = predict_sentences(notes, stop_words_context) | |
crime_sentece_predictions = predict_sentences(crime, stop_words_context) | |
idiot_sentece_predictions = predict_sentences(idiot, stop_words_context) | |
possessed_sentece_predictions = predict_sentences(possessed, stop_words_context) | |
brothers_sentece_predictions = predict_sentences(brothers, stop_words_context) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment