Python Forum
Score each word from list variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Score each word from list variable
#1
Hi, I have a list comprised of a tweet and score.
I'd like to split the tweet into words and assign the total score of the tweet to each word.
Any help is much appreciated, new to Python! Thanks


 
Tweet_list = [('You better believe you're beautiful', 3),('Happy Birthday to my friends',4)] 

def word_sentiment(): 
    list_words = [] 
    for tweet in tweet_list: 
        for word in tweet.split(" "): 
            list_words.append((word)) 
    return list_words 


Desired output:
Output:
[('You',3) ('better',3) (believe',3) ('you're',3) ('beautiful',3) ('Happy',4) ('Birthday',4) ('to',4) ('my',4) ('friends',4) ]
Reply
#2
def word_sentiment(tuple_list):
    list_words = []
    # Loop through each tuple (tp)
    for tp in tuple_list:
        for word in tp[0].split(" "):
            list_words.append((word, tp[1]))
    return list_words


tweet_list = [("You better believe you're beautiful", 3),("Happy Birthday to my friends",4)]
my_final_list = word_sentiment(tweet_list)
print(my_final_list)
Reply
#3
I observe ambiguity in "split the tweet into words and assign the total score of the tweet to each word".


My house, my rules. -> 'My' 'house,' 'my' 'rules.'
my house my rules -> 'my' 'house' 'my' 'rules'

Aren't 'my' and 'My' same words? Aren't 'rules.' and 'rules' also?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
(Jun-12-2019, 12:07 PM)gontajones Wrote:
def word_sentiment(tuple_list):
    list_words = []
    # Loop through each tuple (tp)
    for tp in tuple_list:
        for word in tp[0].split(" "):
            list_words.append((word, tp[1]))
    return list_words


tweet_list = [("You better believe you're beautiful", 3),("Happy Birthday to my friends",4)]
my_final_list = word_sentiment(tweet_list)
print(my_final_list)
Reply
#5
def word_sentiment(tuple_list):
    # Loop through each tuple (tp)
    return [[(word, tp[1]) for word in tp[0].split(" ")] for tp in tuple_list]
Reply
#6
Thanks gontajones!
I have another related to this...how would I remove a word from the tuple if it's in dictionary?

scores= { beautiful,3
friends= 4}

def word_sentiment(tuple_list):
    list_words = []
    # Loop through each tuple (tp)
    for tp in tuple_list:
        for word in tp[0].split(" "):
            list_words.append((word, tp[1]))
    return list_words
 
 
tweet_list = [("You better believe you're beautiful", 3),("Happy Birthday to my friends",4)]
my_final_list = word_sentiment(tweet_list)
print(my_final_list)
Desired Output:
Output:
('You',3) ('better',3) (believe',3) ('you're',3) ('Happy',4) ('Birthday',4) ('to',4) ('my',4)] #('beautiful',3) and ('friends',4) should not appear it the final output as they are included in the dictionary
Reply
#7
This time I'll let you try... Razz

You can use something like:
if word not in not_allowed_words:
    ...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  For Word, Count in List (Counts.Items()) new_coder_231013 6 2,576 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013
  Split string using variable found in a list japo85 2 1,296 Jul-11-2022, 08:52 AM
Last Post: japo85
  find some word in text list file and a bit change to them RolanRoll 3 1,523 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,488 Aug-12-2021, 04:25 PM
Last Post: palladium
  An IF statement with a List variable dedesssse 3 8,213 Jul-08-2021, 05:58 PM
Last Post: perfringo
  Create variable and list dynamically quest_ 12 4,398 Jan-26-2021, 07:14 PM
Last Post: quest_
  Trying to get the first letter of every word in a list DanielCook 2 2,146 Jan-05-2021, 05:06 PM
Last Post: deanhystad
Question Matching variable to a list index Gilush 17 5,855 Nov-30-2020, 01:06 AM
Last Post: Larz60+
  Print variable values from a list of variables xnightwingx 3 2,626 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  Trying to find first 2 letter word in a list of words Oldman45 7 3,725 Aug-11-2020, 08:59 AM
Last Post: Oldman45

Forum Jump:

User Panel Messages

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