๐ Text Classification with Transformers
This project utilizes the transformers
library for text classification, specifically for sentiment analysis of Korean reviews. It provides a simple way to classify reviews as positive or negative.
๐ Quick Start
To get started with this text classification task, you need to follow these steps. First, import the necessary libraries and load the pre - trained model and tokenizer. Then, you can define a list of reviews and use the classification pipeline to predict the sentiment of each review.
๐ป Usage Examples
Basic Usage
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification, TextClassificationPipeline
tokenizer = AutoTokenizer.from_pretrained("Copycats/koelectra-base-v3-generalized-sentiment-analysis")
model = AutoModelForSequenceClassification.from_pretrained("Copycats/koelectra-base-v3-generalized-sentiment-analysis")
sentiment_classifier = TextClassificationPipeline(tokenizer=tokenizer, model=model)
review_list = [
'์ด์๊ณ ์ข์์~~~์ป๊ธฐ๋ ํธํ๊ณ ์์ด๊ณ ์ด์๋ค๊ณ ์๊ธฐ๋ฐฉ์ ๊ฐ๋ค๋๊ณ ์์จ์~^^',
'์์ง ์
์ด๋ณด์ง ์์์ง๋ง ๊ต์ฅํ ๊ฐ๋ฒผ์์~~ ๋ค๋ฅธ ๋ฆฌ๋ทฐ์ฒ๋ผ ์ด๊นก์ด ์ข ๋๋ค์ใ
๋ง์กฑํฉ๋๋ค. ์์ฒญ ๋น ๋ฅธ๋ฐ์ก ๊ฐ์ฌ๋๋ ค์ :)',
'์ฌ๊ตฌ๋งค ํ๊ฑด๋ฐ ๋๋ฌด๋๋ฌด ๊ฐ์ฑ๋น์ธ๊ฑฐ ๊ฐ์์!! ๋ค์์ ๋ ์๊ฐ๋๋ฉด 3๊ฐ์งธ ๋ ์ด๋ฏ..ใ
ใ
',
'๊ฐ์ต๋์ด ๋๋ฌด ์ ์ด์. ๋ฐฉ์ด ์์ง ์๋ค๋ฉด ๋ฌด์กฐ๊ฑด ํฐ๊ฑธ๋ก๊ตฌ๋งคํ์ธ์. ๋ฌผ๋๋ ์กฐ๊ธ๋ฐ์ ์๋ค์ด๊ฐ์ ์ฐ๊ธฐ๋ ๋ถํธํจ',
'ํ๋ฒ์
์๋๋ฐ ์์ ๋ด์ ์ ๋ค ํ๋ฆฌ๊ณ ์ค๋ฐฅ๋ ๊ณ์ ๋์ต๋๋ค. ๋ง๊ฐ ์ฒ๋ฆฌ ๋๋ฌด ์๋ง ์๋๊ฐ์?',
'๋ฐ๋ปํ๊ณ ์ข๊ธดํ๋ฐ ๋ฐฐ์ก์ด ๋๋ ค์',
'๋ง์ ์๋๋ฐ ๊ฐ๊ฒฉ์ด ์๋ ํธ์ด์์'
]
for idx, review in enumerate(review_list):
pred = sentiment_classifier(review)
print(f'{review}\n>> {pred[0]}')
The output of the above code will be as follows:
์ด์๊ณ ์ข์์~~~์ป๊ธฐ๋ ํธํ๊ณ ์์ด๊ณ ์ด์๋ค๊ณ ์๊ธฐ๋ฐฉ์ ๊ฐ๋ค๋๊ณ ์์จ์~^^
>> {'label': '1', 'score': 0.9945501685142517}
์์ง ์
์ด๋ณด์ง ์์์ง๋ง ๊ต์ฅํ ๊ฐ๋ฒผ์์~~ ๋ค๋ฅธ ๋ฆฌ๋ทฐ์ฒ๋ผ ์ด๊นก์ด ์ข ๋๋ค์ใ
๋ง์กฑํฉ๋๋ค. ์์ฒญ ๋น ๋ฅธ๋ฐ์ก ๊ฐ์ฌ๋๋ ค์ :)
>> {'label': '1', 'score': 0.995430588722229}
์ฌ๊ตฌ๋งค ํ๊ฑด๋ฐ ๋๋ฌด๋๋ฌด ๊ฐ์ฑ๋น์ธ๊ฑฐ ๊ฐ์์!! ๋ค์์ ๋ ์๊ฐ๋๋ฉด 3๊ฐ์งธ ๋ ์ด๋ฏ..ใ
ใ
>> {'label': '1', 'score': 0.9959582686424255}
๊ฐ์ต๋์ด ๋๋ฌด ์ ์ด์. ๋ฐฉ์ด ์์ง ์๋ค๋ฉด ๋ฌด์กฐ๊ฑด ํฐ๊ฑธ๋ก๊ตฌ๋งคํ์ธ์. ๋ฌผ๋๋ ์กฐ๊ธ๋ฐ์ ์๋ค์ด๊ฐ์ ์ฐ๊ธฐ๋ ๋ถํธํจ
>> {'label': '0', 'score': 0.9984619617462158}
ํ๋ฒ์
์๋๋ฐ ์์ ๋ด์ ์ ๋ค ํ๋ฆฌ๊ณ ์ค๋ฐฅ๋ ๊ณ์ ๋์ต๋๋ค. ๋ง๊ฐ ์ฒ๋ฆฌ ๋๋ฌด ์๋ง ์๋๊ฐ์?
>> {'label': '0', 'score': 0.9991756677627563}
๋ฐ๋ปํ๊ณ ์ข๊ธดํ๋ฐ ๋ฐฐ์ก์ด ๋๋ ค์
>> {'label': '1', 'score': 0.6473883390426636}
๋ง์ ์๋๋ฐ ๊ฐ๊ฒฉ์ด ์๋ ํธ์ด์์
>> {'label': '1', 'score': 0.5128092169761658}
Label Explanation
- label 0 : negative review
- label 1 : positive review
๐ License
This project is licensed under the Apache 2.0 license.