Python Forum
Attach sentiment to each word in a list of tuples
Thread Rating:
  • 3 Vote(s) - 2.33 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Attach sentiment to each word in a list of tuples
#4
(Apr-19-2017, 06:58 PM)zivoni Wrote: You need to split each sentence  into words and then append pairs (word, sentiment) to your list. It can be done by adding another for loop into your original for loop,
    for row in pos_sentences2:
        for word in row.split():
            temp.append((word, 'positive'))
but perhaps its easier do it with a list comprehension. And you dont need to implement basically same function for positive and negative sentiment, instead that you can use sentences and sentiment as parameters for your function.
def append_sentiment(sentences, sentiment):
    return [(word, sentiment) for sentence in sentences
                              for word in sentence.split()]
Output:
In [4]: sentences = ['omg its already 0', 'Juuust chillin'] In [5]: append_sentiment(sentences, 'positive') Out[5]: [('omg', 'positive'),  ('its', 'positive'),  ('already', 'positive'),  ('0', 'positive'),  ('Juuust', 'positive'),  ('chillin', 'positive')]

Hi, please, what should I do to achieve the same if I have it in pandas dataframe and have positive or negative numbers to mark the score?

Example:

id | text | score
12 | I like this | 2

Wanted result:

id | text | score
12 | ('I', 2), ('like', 2), ('this', 2) | 2
Reply


Messages In This Thread
RE: Attach sentiment to each word in a list of tuples - by ojirgl - Oct-13-2018, 02:02 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sentiment Analysis Classifier lode 0 1,786 Feb-04-2019, 05:00 AM
Last Post: lode

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020