Skip to content

Instantly share code, notes, and snippets.

@LowriWilliams
Created October 26, 2020 15:51
Show Gist options
  • Save LowriWilliams/b8d307bc242a4a097b02380da4361eb5 to your computer and use it in GitHub Desktop.
Save LowriWilliams/b8d307bc242a4a097b02380da4361eb5 to your computer and use it in GitHub Desktop.
aspect_sa/sentiwordnet
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [],
"source": [
"from nltk.corpus import wordnet\n",
"\n",
"def get_wordnet_pos(treebank_tag):\n",
"\n",
" if treebank_tag.startswith('J'):\n",
" return wordnet.ADJ\n",
" elif treebank_tag.startswith('V'):\n",
" return wordnet.VERB\n",
" elif treebank_tag.startswith('N'):\n",
" return wordnet.NOUN\n",
" elif treebank_tag.startswith('R'):\n",
" return wordnet.ADV\n",
" else:\n",
" pass\n",
"\n",
"positive_words = []\n",
"negative_words = []\n",
"\n",
"for i in df['Dominant_topic'].unique():\n",
" if i == 1:\n",
" tmp_1 = df.loc[df['Dominant_topic'] == i]\n",
" \n",
" for j in tmp_1['tokenise'].values.tolist():\n",
" for p in nltk.pos_tag(j):\n",
" get_pos_tag = get_wordnet_pos(p[1])\n",
" if type(get_pos_tag) == str:\n",
" try: \n",
" synset = swn.senti_synset(p[0] + '.' + get_pos_tag +'.01')\n",
"\n",
" if synset.obj_score() <= 0.49:\n",
" if synset.pos_score() > synset.neg_score() and p[0] in wn_affect:\n",
" positive_words.append(p[0])\n",
" elif synset.neg_score() > synset.pos_score() and p[0] in wn_affect:\n",
" negative_words.append(p[0]) \n",
" except:\n",
" pass"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment