Skip to content

Instantly share code, notes, and snippets.

@josht-jpg
Last active September 5, 2020 01:34
Show Gist options
  • Save josht-jpg/0584803a6d5f053b94fe99099d6047e3 to your computer and use it in GitHub Desktop.
Save josht-jpg/0584803a6d5f053b94fe99099d6047e3 to your computer and use it in GitHub Desktop.
Predicting sentence sentiment
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