要構建一個TextBlob文本分類器,首先需要準備訓練數據和測試數據。訓練數據是一組已經標記好分類的文本數據,用來訓練模型。測試數據是一組未標記的文本數據,用來測試訓練模型的準確性。
接下來,可以按照以下步驟來構建TextBlob文本分類器:
from textblob import TextBlob
from textblob.classifiers import NaiveBayesClassifier
train_data = [
("I love this product", "positive"),
("This product is terrible", "negative"),
("I would recommend this to my friends", "positive"),
("I am very disappointed with this product", "negative")
]
test_data = [
"I am happy with this purchase",
"I regret buying this product"
]
cl = NaiveBayesClassifier(train_data)
for text in test_data:
result = cl.classify(text)
print(f"Text: {text}, Classification: {result}")
通過以上步驟,就可以構建一個簡單的TextBlob文本分類器并對測試數據進行分類了。可以根據實際需求,進一步優化模型的性能和準確性。