-
Notifications
You must be signed in to change notification settings - Fork 273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a param-free classification model #821
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
47f3f67
Add parameter-free classification model
c4n e074bec
refactor using black
c4n fb24e55
Fix typo
c4n e0bd22d
Create test_cls.py
wannaphong 057b9df
change example
c4n f9e5e23
Update test_cls.py
wannaphong 4a7076b
Create cls.rst
wannaphong acf4533
Update test_cls.py
wannaphong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "1480d345", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import pythainlp.cls.param_free" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "78fca9fa", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"training_data = [\n", | ||
" (\"รายละเอียดตามนี้เลยค่าา ^^\", \"Neutral\"),\n", | ||
" (\"กลัวพวกมึงหาย อดกินบาบิก้อน\", \"Neutral\"),\n", | ||
" (\"บริการแย่มากก เป็นหมอได้ไง😤\", \"Negative\"),\n", | ||
" (\"ขับรถแย่มาก\", \"Negative\"),\n", | ||
" (\"ดีนะครับ\", \"Positive\"),\n", | ||
" (\"ลองแล้วรสนี้อร่อย... ชอบๆ\", \"Positive\"),\n", | ||
" (\"ฉันรู้สึกโกรธ เวลามือถือแบตหมด\", \"Negative\"),\n", | ||
" (\"เธอภูมิใจที่ได้ทำสิ่งดี ๆ และดีใจกับเด็ก ๆ\", \"Positive\"),\n", | ||
" (\"นี่เป็นบทความหนึ่ง\", \"Neutral\")\n", | ||
"]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "94c1495f", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"model = pythainlp.cls.param_free.GzipModel(training_data)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"id": "f4d047db", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"'Positive'" | ||
] | ||
}, | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"model.predict(\"ควย\", k=3)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "6529c3e9", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.9" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (C) 2016-2023 PyThaiNLP Project | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
""" | ||
pythainlp.cls | ||
""" | ||
|
||
__all__ = ["GzipModel"] | ||
|
||
from pythainlp.cls.param_free import GzipModel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (C) 2016-2023 PyThaiNLP Project | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import gzip | ||
import numpy as np | ||
from typing import Dict, List, Tuple, Union | ||
|
||
|
||
class GzipModel: | ||
""" | ||
This class is a reimplemenatation of “Low-Resource” Text Classification: A Parameter-Free Classification Method with Compressors (Jiang et al., Findings 2023) | ||
|
||
:param list training_data: list [(text_sample,label)] | ||
""" | ||
|
||
def __init__(self, training_data: List[Tuple[str, str]]): | ||
self.training_data = np.array(training_data) | ||
self.Cx2_list = self.train() | ||
|
||
def train(self): | ||
Cx2_list = list() | ||
for i in range(len(self.training_data)): | ||
Cx2_list.append( | ||
len(gzip.compress(self.training_data[i][0].encode("utf-8"))) | ||
) | ||
return Cx2_list | ||
|
||
def predict(self, x1: str, k: int = 1): | ||
Cx1 = len(gzip.compress(x1.encode("utf-8"))) | ||
disance_from_x1 = [] | ||
for i in range(len(self.Cx2_list)): | ||
x2 = self.training_data[i][0] | ||
Cx2 = self.Cx2_list[i] | ||
x1x2 = "".join([x1, x2]) | ||
Cx1x2 = len(gzip.compress(x1x2.encode("utf-8"))) | ||
# normalized compression distance | ||
ncd = (Cx1x2 - min(Cx1, Cx2)) / max(Cx1, Cx2) | ||
disance_from_x1.append(ncd) | ||
sorted_idx = np.argsort(np.array(disance_from_x1)) | ||
top_k_class = self.training_data[sorted_idx[:k], 1] | ||
_, counts = np.unique(top_k_class, return_counts=True) | ||
predict_class = top_k_class[counts.argmax()] | ||
return predict_class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# -*- coding: utf-8 -*- | ||
import unittest | ||
from pythainlp.cls import GzipModel | ||
|
||
|
||
class TestClsPackage(unittest.TestCase): | ||
def test_GzipModel(self): | ||
training_data = [ | ||
("รายละเอียดตามนี้เลยค่าา ^^", "Neutral"), | ||
("กลัวพวกมึงหาย อดกินบาบิก้อน", "Neutral"), | ||
("บริการแย่มากก เป็นหมอได้ไง😤", "Negative"), | ||
("ขับรถแย่มาก", "Negative"), | ||
("ดีนะครับ", "Positive"), | ||
("ลองแล้วรสนี้อร่อย... ชอบๆ", "Positive"), | ||
("ฉันรู้สึกโกรธ เวลามือถือแบตหมด", "Negative"), | ||
("เธอภูมิใจที่ได้ทำสิ่งดี ๆ และดีใจกับเด็ก ๆ", "Positive"), | ||
("นี่เป็นบทความหนึ่ง", "Neutral") | ||
] | ||
model = GzipModel(training_data) | ||
self.assertIsNotNone(model.predict("รู้สึกดีจัง", k=3)) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it better to modify this assert to also verify the correctness of the prediction?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also, I get the result from the test:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we can expect much from this few-shot classifier. May be sentiment analysis is not the best application to test either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
News classification seems to work well (at least on the paper)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. I updated the test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I generally agree, but I just wanted to make the test suite slightly less confusing.