I am a novice in python and I treat to extract emotions from list and put each attribute to its corresponding series to train machine learning algorithms for example this one instance in list and I want put anger in new series with its value= 0.013736263736263736 and so on.
"{'anger':", '0.013736263736263736,', "'anticipation':", '0.0027472527472527475,', "'disgust':", '0.03296703296703297,', "'fear':", '0.0027472527472527475,', "'joy':", '0.0,', "'negative':", '0.06043956043956044,', "'positive':", '0.019230769230769232,', "'sadness':", '0.0027472527472527475,', "'surprise':", '0.008241758241758242,', "'trust':", '0.019230769230769232}'] thanks in advance
You can manipulate with dictionary and sentiment in many ways, let me show a few:
1. Add item to collection, sample
def calc_sentiment(word: str):
return 0.5
def Add_to_Collection(sentense: str):
sentiment={}
for word in str.split(sentense):
sentiment[word]=calc_sentiment(word)
return sentiment
test='My name is John Cache. It is my real name.'
print(Add_to_Collection(test))
you can see outputs:
{'My': 0.5, 'name': 0.5, 'is': 0.5, 'John': 0.5, 'Cache.': 0.5, 'It': 0.5, 'my': 0.5, 'real': 0.5, 'name.': 0.5}
2. Add item to collection with sum of sentiments, sample
def calc_sentiment(word: str):
return 0.5
def Add_to_Collection_Sum(sentense: str):
sentiment={}
for word in str.split(sentense):
sentiment[word] = sentiment[word] + calc_sentiment(word) if word in sentiment else calc_sentiment(word)
return sentiment
test='My name is John Cache. It is my real name.'
print(Add_to_Collection_Sum(test))
you can see outputs (see value 1.0 for 'is'):
{'My': 0.5, 'name': 0.5, 'is': 1.0, 'John': 0.5, 'Cache.': 0.5, 'It': 0.5, 'my': 0.5, 'real': 0.5, 'name.': 0.5}
3. Add item to collection with list of all sentiments, sample
def calc_sentiment(word: str):
return 0.5
def Add_to_Collection_AddToList(sentense: str):
sentiment={}
items=[]
for word in str.split(sentense):
if word in sentiment:
items=sentiment[word]
items.append(calc_sentiment(word))
else:
sentiment[word]=[calc_sentiment(word)]
return sentiment
test='My name is John Cache. It is my real name.'
print(Add_to_Collection_AddToList(test))
you can see outputs (see double values [0.5, 0.5] for 'is'):
{'My': [0.5], 'name': [0.5], 'is': [0.5, 0.5], 'John': [0.5], 'Cache.': [0.5], 'It': [0.5], 'my': [0.5], 'real': [0.5], 'name.': [0.5]}