Skip to content
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 8 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions notebooks/test_gzip_cls.ipynb
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
}
21 changes: 21 additions & 0 deletions pythainlp/cls/__init__.py
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
55 changes: 55 additions & 0 deletions pythainlp/cls/param_free.py
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
20 changes: 20 additions & 0 deletions tests/test_cls.py
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))
Copy link
Contributor

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?

Copy link
Contributor

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:

image

Copy link
Contributor Author

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.

Copy link
Contributor Author

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)

Copy link
Member

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.

Copy link
Contributor

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.

I generally agree, but I just wanted to make the test suite slightly less confusing.