Skip to content

Instantly share code, notes, and snippets.

@josht-jpg
Last active September 3, 2020 18:14
Show Gist options
  • Save josht-jpg/6b7da8b18f139aa7ff54696ee865424e to your computer and use it in GitHub Desktop.
Save josht-jpg/6b7da8b18f139aa7ff54696ee865424e to your computer and use it in GitHub Desktop.
NRC analysis
NRC = pd.read_csv("NRC.csv", names=["word", "sentiment", "classifaction"])
NRC_sentiments = ['anger', 'anticipation', 'disgust', 'fear', 'joy', 'negative',
'positive', 'sadness', 'suprise', 'trust']
def nrc_classify(word):
return NRC[NRC['word'] == word].loc[:, 'classifaction'].tolist()
def nrc_clean(book_nrc):
book_nrc['classifications'] = book_nrc['word2'].apply(nrc_classify)
book_nrc = book_nrc[book_nrc['classifications'].str.len() > 0]
classification_df = pd.DataFrame.from_dict(dict(book_nrc['classifications'])).transpose()
classification_df.columns = NRC_sentiments
book_nrc = book_nrc.join(classification_df)
book_nrc = book_nrc.drop(['classifications'], axis = 1)
return book_nrc
def nrc_context(book_bigrams):
for e in NRC_sentiments:
book_bigrams[e] = book_bigrams.apply(
lambda x: x[e] * -1 if x['word1'] in negations else x[e],
axis = 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment