Python Forum
Creating new column with a input string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating new column with a input string
#1
Hi,

I am new to python and strugling with a code, i know its been clearly thrown the error still i am failing to get it might anyone can help

from textblob import TextBlob

sentence = input("Enter sentence:")

def sentiment_analysis(inputSentence):
 def getSubjectivity(text):
   return TextBlob(text).sentiment.subjectivity
  
 #Create a function to get the polarity
 def getPolarity(text):
   return TextBlob(text).sentiment.polarity
  
 #Create two new columns ‘Subjectivity’ & ‘Polarity’
 inputSentence ['TextBlob_Subjectivity'] =  inputSentence['inputSentence'].apply(getSubjectivity)
 inputSentence ['TextBlob_Polarity'] = inputSentence['inputSentence'].apply(getPolarity)
 
 def getAnalysis(score):
  if score < 0:
    return 'Negative'
  elif score == 0:
    return 'Neutral'
  else:
    return 'Positive'
 inputSentence ['TextBlob_Analysis'] = inputSentence['TextBlob_Polarity'].apply(getAnalysis)
 return inputSentence

sentiment_analysis(sentence)
giving me following error:

Output:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-21-5220518db4bc> in <module> ----> 1 sentiment_analysis(sentence) <ipython-input-20-0d8612b5763c> in sentiment_analysis(inputSentence) 8 9 #Create two new columns ‘Subjectivity’ & ‘Polarity’ ---> 10 inputSentence ['TextBlob_Subjectivity'] = inputSentence['inputSentence'].apply(getSubjectivity) 11 inputSentence ['TextBlob_Polarity'] = inputSentence['inputSentence'].apply(getPolarity) 12 TypeError: string indices must be integers
I want if user inputs a text, it should be populating with polarity and subjectivity along with text so i can do some analysis in it.

I went online which i can overcome using Vader, still i am eager to know how to accomplish my logic.

Thanks in advance
Reply
#2
The code you are showing and the code that produced the error message are not the same code. For example, the error is shown on line 10, but that line is line 14 in the source you show. So there is no way to know what is going on. Make sure that the code and the output you get correspond.

As I read the code, you are using TextBlob-style operations on a string. This makes no sense. If you wanted to
inputSentence ['TextBlob_Subjectivity'] =  inputSentence['inputSentence'].apply(getSubjectivity)
then inputSentence must be a TextBlob, which it is not. The error message is correct. You are trying to subscript a string using an invalid subscript.
Reply
#3
(Apr-14-2021, 07:38 AM)supuflounder Wrote: The code you are showing and the code that produced the error message are not the same code. For example, the error is shown on line 10, but that line is line 14 in the source you show. So there is no way to know what is going on. Make sure that the code and the output you get correspond.

As I read the code, you are using TextBlob-style operations on a string. This makes no sense. If you wanted to
inputSentence ['TextBlob_Subjectivity'] =  inputSentence['inputSentence'].apply(getSubjectivity)
then inputSentence must be a TextBlob, which it is not. The error message is correct. You are trying to subscript a string using an invalid subscript.

Yes, this code was written part by part in jupyter notebook, i had rearranged for it to post. for the record, Sentiment analysis definition is a separate code, which is throwing error when calling it at run time "sentiment_analysis(sentence)".
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Photo Creating column in python based on the other colums value count Bartek635 2 1,114 Apr-15-2021, 03:47 PM
Last Post: Bartek635
  Python win32api keybd_event: How do I input a string of characters? JaneTan 3 1,206 Oct-19-2020, 04:16 AM
Last Post: deanhystad
  Creating local variables from a string peckjonk 2 1,055 Feb-15-2020, 06:07 PM
Last Post: ndc85430
  How to print the 2nd column of a searched string in a csv ptey07 2 1,030 Jan-11-2020, 11:25 AM
Last Post: ptey07
  input string split Eric7Giants 3 1,466 Nov-13-2019, 07:19 PM
Last Post: Gribouillis
  with input remove a string from the list konsular 3 1,284 Oct-12-2019, 09:25 AM
Last Post: konsular
  Creating csv header from user-input list dvanommen 2 1,221 Aug-26-2019, 08:51 PM
Last Post: dvanommen
  simple string & input problem kungshamji 5 1,702 Jun-23-2019, 03:54 PM
Last Post: kungshamji
  Creating a new DataFrame from another DataFrame column Ilangos 1 1,154 Jun-05-2019, 09:47 AM
Last Post: Ilangos
  How to use a string method on user input Exsul 2 1,387 Mar-17-2019, 08:12 PM
Last Post: Exsul

Forum Jump:

User Panel Messages

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