snowflake.snowpark.DataFrameAIFunctions.sentiment¶
- DataFrameAIFunctions.sentiment(input_column: Union[snowflake.snowpark.column.Column, str], categories: Optional[List[str]] = None, *, output_column: Optional[str] = None) snowflake.snowpark.DataFrame[source]¶
- Extract sentiment analysis from text content. - This method analyzes the sentiment of text in each row, providing overall sentiment and optionally sentiment for specific categories or aspects mentioned in the text. - Parameters:
- input_column – The column (Column object or column name as string) containing the text to analyze for sentiment. 
- categories – Optional list of up to 10 categories (also called entities or aspects) for which sentiment should be extracted. Each category may be a maximum of 30 characters long. For example, if extracting sentiment from restaurant reviews, you might specify - ['cost', 'quality', 'service', 'wait time']as categories. If not provided, only overall sentiment is returned.
- output_column – The name of the output column to be appended. If not provided, a column named - AI_SENTIMENT_OUTPUTis appended.
 
- Returns:
- A new DataFrame with an appended output column containing sentiment results. The output is a JSON object with a - categoriesfield containing an array of records. Each record includes:- name: The category name (- overallfor overall sentiment)
- sentiment: One of- unknown,- positive,- negative,- neutral, or- mixed
 
 - Examples: - >>> # Overall sentiment analysis >>> df = session.create_dataframe([ ... ["The movie had amazing visual effects but the plot was terrible."], ... ["The food was delicious but the service was slow."], ... ["Everything about this experience was perfect!"], ... ], schema=["review"]) >>> result_df = df.ai.sentiment( ... input_column="review", ... output_column="sentiment" ... ) >>> result_df.columns ['REVIEW', 'SENTIMENT'] >>> results = result_df.collect() >>> import json >>> overall_sentiment = json.loads(results[2]["SENTIMENT"])["categories"][0] >>> overall_sentiment["name"] 'overall' >>> overall_sentiment["sentiment"] 'positive' >>> # Sentiment analysis with specific categories >>> from snowflake.snowpark.functions import col >>> df = session.create_dataframe([ ... ["The hotel room was spacious and clean, but the wifi was terrible and the breakfast was mediocre."], ... ["Great location and friendly staff, though the parking was expensive."], ... ], schema=["review"]) >>> result_df = df.ai.sentiment( ... input_column=col("review"), ... categories=["room", "wifi", "breakfast", "location", "staff", "parking"], ... output_column="detailed_sentiment" ... ) >>> result_df.columns ['REVIEW', 'DETAILED_SENTIMENT'] >>> results = result_df.collect() >>> sentiments = json.loads(results[0]["DETAILED_SENTIMENT"])["categories"] >>> # Check that we have sentiments for overall plus the specified categories >>> len(sentiments) > 1 True >>> category_names = [s["name"] for s in sentiments] >>> "overall" in category_names True >>> "room" in category_names True - Note - AI_SENTIMENT can analyze sentiment in English, French, German, Hindi, Italian, Spanish, and Portuguese. You can specify categories in the language of the text or in English. - This function or method is experimental since 1.39.0.