文字分類

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...