Reputation: 13
Self-study really requires me a lot of effort, and after wasting alot of time over this issue I decided to come here and ask for help. So, I'm using Google api for my project, which look like below:
def sample_analyze_sentiment(text_content):
"""
Analyzing Sentiment in a String
Args:
text_content The text content to analyze
"""
client = language_v1.LanguageServiceClient()
# text_content = 'I am so happy and joyful.'
# Available types: PLAIN_TEXT, HTML
type_ = language_v1.Document.Type.PLAIN_TEXT
# Optional. If not specified, the language is automatically detected.
# For list of supported languages:
# https://cloud.google.com/natural-language/docs/languages
language = "en"
document = {"content": text_content, "type_": type_, "language": language}
# Available values: NONE, UTF8, UTF16, UTF32
encoding_type = language_v1.EncodingType.UTF8
response = client.analyze_sentiment(request = {'document': document, 'encoding_type': encoding_type})
# Get overall sentiment of the input document
print(format(response.document_sentiment.score))
And what I want to do is iterating text data through this function to get the sentiment score of each rows.
df = pd.read_csv('Final.csv')
for items in df['Text']:
sample_analyze_sentiment(items)
Then mapping it into a new column of the dataframe (this is where I dont know how). My function return string of floats but it is definitely nonetype, so I think I cant do this. But, I still doubt cause it seem doable.
Help please.
Upvotes: 1
Views: 85
Reputation: 106
You could try to apply()
the function to the 'Text'
column in your dataframe. In order to do so, you have to return yur result (i.e. the sentiment) from your function:
def sample_analyze_sentiment(text_content):
"""
Analyzing Sentiment in a String
Args:
text_content The text content to analyze
"""
client = language_v1.LanguageServiceClient()
# text_content = 'I am so happy and joyful.'
# Available types: PLAIN_TEXT, HTML
type_ = language_v1.Document.Type.PLAIN_TEXT
# Optional. If not specified, the language is automatically detected.
# For list of supported languages:
# https://cloud.google.com/natural-language/docs/languages
language = "en"
document = {"content": text_content, "type_": type_, "language": language}
# Available values: NONE, UTF8, UTF16, UTF32
encoding_type = language_v1.EncodingType.UTF8
response = client.analyze_sentiment(request = {'document': document, 'encoding_type': encoding_type})
# Get overall sentiment of the input document
return format(response.document_sentiment.score)
Then it should be possible to create a new column 'sentiment'
in your dataframe with the following line:
df['sentiment'] = df['Text'].apply(sample_analyze_sentiment)
Upvotes: 1