文本分类

Snowflake 生成式 AI 入门

James Cha-Earley

Senior Developer Advocate, Snowflake

文本分类的挑战

问题

$$

  • 评论可能包含任何信息

$$

  • 手动分类耗时,且无法扩展

解决方案

$$

  • 使用 Snowflake Cortex 自动化!

$$

  • classify_text()
Snowflake 生成式 AI 入门

定义类别

# Define categories
categories = ["overall_experience", "location", "staff", "food_beverages",
              "facilities"]
Snowflake 生成式 AI 入门

文本分类

from snowflake.cortex import classify_text

category = classify_text( str_input="The check-in was smooth and the staff were very friendly.", categories=categories )
print(category)
{  
  "label": "staff"  
}
Snowflake 生成式 AI 入门

将输出转换为字典

print(type(category))
<class 'str'>
import json

category_dict = json.loads(category)
print(type(category_dict))
<class 'dict'>
Snowflake 生成式 AI 入门

扩展工作流

  • 针对指定月份对评论进行分类的流水线
# Python code
month = 5
-- SQL query
SELECT *
FROM HOTELS.REVIEWS
WHERE EXTRACT(month FROM date) = '{{month}}'
Snowflake 生成式 AI 入门

应用 classify_text()

df = dataframe_1.to_pandas()

def classification(text): result = classify_text( str_input=text, categories=labels ) result_dict = json.loads(result) return result_dict["label"]
Snowflake 生成式 AI 入门

应用 classify_text()

# Apply classification to the DataFrame
df["category"] = reviews["DESCRIPTION"].apply(classification)

# Print the first row's predicted category print(df["category"].head(1))
0    overall_experience  
Name: category, dtype: object
Snowflake 生成式 AI 入门

情感分析

  • 只需关心评论情感

在 SQL 单元格中使用 AI_SENTIMENT

Snowflake 生成式 AI 入门

情感分析

AI_SENTIMENT - 提取情感

$$

  • 可用情感标签:positivenegativeneutralmixedunknown
Snowflake 生成式 AI 入门

让我们一起练习吧!

Snowflake 生成式 AI 入门

Preparing Video For Download...