文字分類

善用 Hugging Face

Jacob H. Marquez

Lead Data Engineer

文字分類:情緒分析

$$

  • 依情緒傾向為文字貼標

$$

情緒分析

$$

  • 應用: 評論分析、社群情緒追蹤

情緒圖示

善用 Hugging Face

情緒分析:程式範例

from transformers import pipeline

my_pipeline = pipeline( "text-classification", model="distilbert-base-uncased-finetuned-sst-2-english" )
print(my_pipeline("Wi-Fi is slower than a snail today!"))
[{'label': 'NEGATIVE', 'score': 0.99}]
善用 Hugging Face

文字分類:文法正確性

$$

文法檢查

$$

  • 評估文字文法是否正確

$$

文法正確範例

$$

  • 應用: 文法檢查、語言學習工具
善用 Hugging Face

文法正確性:程式範例

from transformers import pipeline


# Create a pipeline for grammar checking grammar_checker = pipeline( task="text-classification", model="abdulmatinomotoso/English_Grammar_Checker" )
# Check grammar of the input text print(grammar_checker("He eat pizza every day."))
[{'label': 'LABEL_0', 'score': 0.99}]
善用 Hugging Face

文字分類:QNLI

$$ 問答

$$

$$

  • 檢查前提是否回答了問題

  • 應用: 問答系統、事實查核

QNLI 範例

善用 Hugging Face

QNLI:程式範例

from transformers import pipeline


classifier = pipeline( task="text-classification", model="cross-encoder/qnli-electra-base" )
classifier("Where is Seattle located?, Seattle is located in Washington state.")
[{'label': 'LABEL_0', 'score': 0.997}]
善用 Hugging Face

文字分類:動態類別指派

$$

  • 依內容動態指派類別

類別指派範例

  • 應用: 內容審核、推薦系統

$$

類別指派

善用 Hugging Face

動態類別指派:程式範例

classifier = pipeline(
  task="zero-shot-classification", 
  model="facebook/bart-large-mnli")


text = "Hey, DataCamp; we would like to feature your courses in our newsletter!" categories = ["marketing", "sales", "support"]
output = classifier(text, categories)
print(f"Top Label: {output['labels'][0]} with score: {output['scores'][0]}")
Top Label: support with score: 0.8183
善用 Hugging Face

文字分類的挑戰

歧義

善用 Hugging Face

文字分類的挑戰

反諷

善用 Hugging Face

文字分類的挑戰

多語

善用 Hugging Face

一起來練習吧!

善用 Hugging Face

Preparing Video For Download...