EmoTFIDF 1.0.11__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- EmoTFIDF-1.0.11.dist-info/LICENSE +21 -0
- EmoTFIDF-1.0.11.dist-info/METADATA +88 -0
- EmoTFIDF-1.0.11.dist-info/RECORD +6 -0
- EmoTFIDF-1.0.11.dist-info/WHEEL +5 -0
- EmoTFIDF-1.0.11.dist-info/top_level.txt +1 -0
- emotfidf.py +111 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Mohamed Mostafa
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: EmoTFIDF
|
|
3
|
+
Version: 1.0.11
|
|
4
|
+
Summary: A library to extract emotions using two methods, 1- Using lexicon based, counting frequency of emotion2- Integrating TFIDF to add a contextNote that lexicon license is for research purposes only.
|
|
5
|
+
Home-page: https://github.com/mmsa/emotfidf
|
|
6
|
+
Author: mmsa12
|
|
7
|
+
Author-email: mmsa12@gmail.com
|
|
8
|
+
Classifier: Programming Language :: Python :: 3.6
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=2.7
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: nltk
|
|
15
|
+
|
|
16
|
+
EmoTFIDF is an emotion detection library (Lexicon approach) based in the National Research Council Canada (NRC) and this package is for research purposes only. Source: [lexicons for research] (http://sentiment.nrc.ca/lexicons-for-research/)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
#This library provides two types of emotions:
|
|
20
|
+
|
|
21
|
+
1- Lexicon based emotions which counting the frequency of the emotion based on the lexicon
|
|
22
|
+
2- Integrating TFIDF to add a context to the emotions.
|
|
23
|
+
|
|
24
|
+
#Installation
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
pip install EmoTFIDF
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
#List of emotions
|
|
32
|
+
|
|
33
|
+
-fear
|
|
34
|
+
-anger
|
|
35
|
+
-anticipation
|
|
36
|
+
-trust
|
|
37
|
+
-surprise
|
|
38
|
+
-positive
|
|
39
|
+
-negative
|
|
40
|
+
-sadness
|
|
41
|
+
-disgust
|
|
42
|
+
-joy
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
#Example of usage
|
|
46
|
+
##Get emotions from a sentence
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
from emotfidf import EmoTFIDF
|
|
50
|
+
|
|
51
|
+
comment = "I had a GREAT week, thanks to YOU! I am very happy today."
|
|
52
|
+
|
|
53
|
+
emTFIDF = EmoTFIDF()
|
|
54
|
+
|
|
55
|
+
emTFIDF.set_text(comment)
|
|
56
|
+
print(emTFIDF.em_frequencies)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
##Get emotions factorising TFIDF, you will need to add a context
|
|
61
|
+
|
|
62
|
+
Below is an example in pandas assuming you have a list of tweets/text and you would want to get emotions
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
emTFIDF = EmoTFIDF()
|
|
67
|
+
def getEmotionsTFIDF(s,emTFIDF):
|
|
68
|
+
emTFIDF.set_text(s)
|
|
69
|
+
emTFIDF.get_emotfidf()
|
|
70
|
+
return emTFIDF.em_tfidf
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
emTFIDF.computeTFIDF(df['text'])
|
|
74
|
+
df['emotions'] = new_df.apply(lambda x: getEmotionsTFIDF(x['text'], emTFIDF), axis=1)#em_tfidf
|
|
75
|
+
df2 = df['emotions'].apply(pd.Series)
|
|
76
|
+
final_df = pd.concat([df,df2],axis=1)
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
##Update 1.0.7
|
|
80
|
+
|
|
81
|
+
Thanks to [artofchores](https://www.reddit.com/user/artofchores/), from Reddit for his feedback.
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
Added a set_lexicon_path option if you would like to use your own lexicon
|
|
85
|
+
Remember to keep the same structure as the original emotions lexicon which located [here](https://raw.githubusercontent.com/mmsa/EmoTFIDF/main/emotions_lex.json)
|
|
86
|
+
```
|
|
87
|
+
emTFIDF.set_lexicon_path("other_lexicon.json")
|
|
88
|
+
```
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
emotfidf.py,sha256=w4duVbpS1hPQhrVLgFhRCAXkbI-fbYaw26Ms46_4sVM,4659
|
|
2
|
+
EmoTFIDF-1.0.11.dist-info/LICENSE,sha256=NtgiPe6QVePc6rvKYX6Fk4M22KlErbBVvmu6PJNeTyI,1091
|
|
3
|
+
EmoTFIDF-1.0.11.dist-info/METADATA,sha256=ZIfEWduFnJdNGyMEhihCiMuxxqi335R83MMU0HJGGdQ,2374
|
|
4
|
+
EmoTFIDF-1.0.11.dist-info/WHEEL,sha256=pkctZYzUS4AYVn6dJ-7367OJZivF2e8RA9b_ZBjif18,92
|
|
5
|
+
EmoTFIDF-1.0.11.dist-info/top_level.txt,sha256=7NgyBOtgMKjsVk-wrA8JqY03Zqctvq9kgXOTKv_Yn3A,9
|
|
6
|
+
EmoTFIDF-1.0.11.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
emotfidf
|
emotfidf.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
"""
|
|
3
|
+
@author: mmsa12
|
|
4
|
+
"""
|
|
5
|
+
import pandas as pd
|
|
6
|
+
import sklearn
|
|
7
|
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
|
8
|
+
from collections import Counter
|
|
9
|
+
import string, nltk
|
|
10
|
+
from nltk.corpus import stopwords
|
|
11
|
+
import urllib.request, json
|
|
12
|
+
nltk.download('wordnet')
|
|
13
|
+
nltk.download('punkt')
|
|
14
|
+
nltk.download('stopwords')
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def process_message(message):
|
|
18
|
+
words = nltk.word_tokenize(message.lower())
|
|
19
|
+
words = [w for w in words if len(w) > 3] # remove small words
|
|
20
|
+
# stop words
|
|
21
|
+
sw = stopwords.words('english')
|
|
22
|
+
words = [word for word in words if word.isnumeric() == False]
|
|
23
|
+
words = [word for word in words if word not in sw]
|
|
24
|
+
# remove punctuations
|
|
25
|
+
words = [word for word in words if word not in string.punctuation]
|
|
26
|
+
return ' '.join(words)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def get_emotions(self):
|
|
30
|
+
# get emotions
|
|
31
|
+
em_list = []
|
|
32
|
+
em_dict = dict()
|
|
33
|
+
em_frequencies = Counter()
|
|
34
|
+
lexicon_keys = self.lexicon.keys()
|
|
35
|
+
for word in self.words:
|
|
36
|
+
if word in lexicon_keys:
|
|
37
|
+
em_list.extend(self.lexicon[word])
|
|
38
|
+
em_dict.update({word: self.lexicon[word]})
|
|
39
|
+
for word in em_list:
|
|
40
|
+
em_frequencies[word] += 1
|
|
41
|
+
sum_values = sum(em_frequencies.values())
|
|
42
|
+
em_percent = {'fear': 0.0, 'anger': 0.0, 'anticipation': 0.0, 'trust': 0.0, 'surprise': 0.0, 'positive': 0.0,
|
|
43
|
+
'negative': 0.0, 'sadness': 0.0, 'disgust': 0.0, 'joy': 0.0}
|
|
44
|
+
for key in em_frequencies.keys():
|
|
45
|
+
em_percent.update({key: float(em_frequencies[key]) / float(sum_values)})
|
|
46
|
+
self.em_list = em_list
|
|
47
|
+
self.em_dict = em_dict
|
|
48
|
+
self.emotion_scores = dict(em_frequencies)
|
|
49
|
+
self.em_frequencies = em_percent
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class EmoTFIDF:
|
|
53
|
+
"""Lexicon source is (C) 2016 National Research Council Canada (NRC) and library is for research purposes only. Source: http://sentiment.nrc.ca/lexicons-for-research/"""
|
|
54
|
+
|
|
55
|
+
with urllib.request.urlopen("https://raw.githubusercontent.com/mmsa/EmoTFIDF/main/emotions_lex.json") as url:
|
|
56
|
+
lexicon = json.loads(url.read().decode())
|
|
57
|
+
|
|
58
|
+
def set_text(self, text):
|
|
59
|
+
self.text = process_message(text)
|
|
60
|
+
self.words = list(nltk.word_tokenize(self.text))
|
|
61
|
+
self.sentences = list(nltk.sent_tokenize(self.text))
|
|
62
|
+
get_emotions(self)
|
|
63
|
+
|
|
64
|
+
def set_lexicon_path(self, path):
|
|
65
|
+
self.path = path
|
|
66
|
+
if path != ' ' and path != 0:
|
|
67
|
+
with open(path) as jsonfile:
|
|
68
|
+
self.lexicon = json.load(jsonfile)
|
|
69
|
+
else:
|
|
70
|
+
with urllib.request.urlopen(
|
|
71
|
+
"https://raw.githubusercontent.com/mmsa/EmoTFIDF/main/emotions_lex.json") as url:
|
|
72
|
+
self.lexicon = json.loads(url.read().decode())
|
|
73
|
+
|
|
74
|
+
def computeTFIDF(self, docs):
|
|
75
|
+
vectorizer = TfidfVectorizer(max_features=200, stop_words=stopwords.words('english'),
|
|
76
|
+
token_pattern=r'(?u)\b[A-Za-z]+\b')
|
|
77
|
+
vectors = vectorizer.fit_transform(docs)
|
|
78
|
+
if sklearn.__version__ <= 0.24:
|
|
79
|
+
feature_names = vectorizer.get_feature_names()
|
|
80
|
+
elif sklearn.__version__ >= 1.0:
|
|
81
|
+
feature_names = vectorizer.get_feature_names_out()
|
|
82
|
+
dense = vectors.todense()
|
|
83
|
+
denselist = dense.tolist()
|
|
84
|
+
df = pd.DataFrame(denselist, columns=feature_names)
|
|
85
|
+
self.tfid = df
|
|
86
|
+
self.vectorizer = vectorizer
|
|
87
|
+
self.feature_names = feature_names
|
|
88
|
+
|
|
89
|
+
def get_ifidf_for_words(self):
|
|
90
|
+
tfidf_matrix = self.vectorizer.transform([self.text]).todense()
|
|
91
|
+
feature_index = tfidf_matrix[0, :].nonzero()[1]
|
|
92
|
+
tfidf_scores = zip([self.feature_names[i] for i in feature_index], [tfidf_matrix[0, x] for x in feature_index])
|
|
93
|
+
self.ifidf_for_words = dict(tfidf_scores)
|
|
94
|
+
|
|
95
|
+
def get_emotfidf(self):
|
|
96
|
+
self.get_ifidf_for_words()
|
|
97
|
+
em_percent = {'fear': 0.0, 'anger': 0.0, 'anticipation': 0.0, 'trust': 0.0, 'surprise': 0.0, 'positive': 0.0,
|
|
98
|
+
'negative': 0.0, 'sadness': 0.0, 'disgust': 0.0, 'joy': 0.0}
|
|
99
|
+
em_frequencies = Counter()
|
|
100
|
+
for word in self.em_list:
|
|
101
|
+
em_frequencies[word] += 1
|
|
102
|
+
for e in self.em_dict.keys():
|
|
103
|
+
if e in self.ifidf_for_words:
|
|
104
|
+
tfidf_weight = self.ifidf_for_words[e]
|
|
105
|
+
for a in self.em_dict[e]:
|
|
106
|
+
new_fre = round(em_frequencies[a] / tfidf_weight, 2)
|
|
107
|
+
em_frequencies[a] = new_fre
|
|
108
|
+
sum_values = sum(em_frequencies.values())
|
|
109
|
+
for key in em_frequencies.keys():
|
|
110
|
+
em_percent.update({key: round(float(em_frequencies[key]) / float(sum_values), 3)})
|
|
111
|
+
self.em_tfidf = em_percent
|