swarmauri_similarity_gzip 0.1.0.dev3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- swarmauri_similarity_gzip-0.1.0.dev3/PKG-INFO +81 -0
- swarmauri_similarity_gzip-0.1.0.dev3/README.md +57 -0
- swarmauri_similarity_gzip-0.1.0.dev3/pyproject.toml +56 -0
- swarmauri_similarity_gzip-0.1.0.dev3/swarmauri_similarity_gzip/GzipSimilarity.py +82 -0
- swarmauri_similarity_gzip-0.1.0.dev3/swarmauri_similarity_gzip/__init__.py +3 -0
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: swarmauri_similarity_gzip
|
|
3
|
+
Version: 0.1.0.dev3
|
|
4
|
+
Summary: Gzip-based compression similarity for Swarmauri.
|
|
5
|
+
License-Expression: Apache-2.0
|
|
6
|
+
Keywords: swarmauri,similarity,compression,gzip,normalized-compression-distance,language-comparison
|
|
7
|
+
Author: Jacob Stewart
|
|
8
|
+
Author-email: jacob@swarmauri.com
|
|
9
|
+
Requires-Python: >=3.10,<3.13
|
|
10
|
+
Classifier: Development Status :: 1 - Planning
|
|
11
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
12
|
+
Classifier: Programming Language :: Python
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
19
|
+
Classifier: Topic :: Text Processing
|
|
20
|
+
Requires-Dist: swarmauri_base
|
|
21
|
+
Requires-Dist: swarmauri_core
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# GzipSimilarity
|
|
25
|
+
|
|
26
|
+

|
|
27
|
+
|
|
28
|
+
GzipSimilarity provides a dependency-light Swarmauri similarity implementation based on gzip compression and Normalized Compression Distance (NCD).
|
|
29
|
+
|
|
30
|
+
[](https://pepy.tech/projects/swarmauri_similarity_gzip)
|
|
31
|
+
[](https://hits.sh/github.com/swarmauri/swarmauri-sdk/tree/master/pkgs/standards/swarmauri_similarity_gzip/)
|
|
32
|
+
[](https://pypi.org/project/swarmauri_similarity_gzip/)
|
|
33
|
+
[](https://pypi.org/project/swarmauri_similarity_gzip/)
|
|
34
|
+
[](https://pypi.org/project/swarmauri_similarity_gzip/)
|
|
35
|
+
|
|
36
|
+
## Features
|
|
37
|
+
|
|
38
|
+
- Implements `SimilarityBase` from `swarmauri_base`.
|
|
39
|
+
- Compares text and binary values through shared gzip compressibility.
|
|
40
|
+
- Supports deterministic gzip sizing and configurable compression levels.
|
|
41
|
+
- Supports symmetric concatenation-order handling.
|
|
42
|
+
- Exposes both bounded similarity and raw NCD values.
|
|
43
|
+
|
|
44
|
+
## Installation
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
uv add swarmauri_similarity_gzip
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
pip install swarmauri_similarity_gzip
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Usage
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
from swarmauri_similarity_gzip import GzipSimilarity
|
|
58
|
+
|
|
59
|
+
similarity = GzipSimilarity()
|
|
60
|
+
|
|
61
|
+
score = similarity.similarity(
|
|
62
|
+
"The quick brown fox",
|
|
63
|
+
"Le renard brun rapide",
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
ncd = similarity.ncd(
|
|
67
|
+
"The quick brown fox",
|
|
68
|
+
"Le renard brun rapide",
|
|
69
|
+
)
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
`similarity()` returns a score in `[0.0, 1.0]`, where higher values indicate greater shared compressibility. `ncd()` exposes the underlying Normalized Compression Distance. The result is a compression-based similarity estimate and should not be interpreted as semantic similarity without corpus-specific validation.
|
|
73
|
+
|
|
74
|
+
For language comparison, use balanced samples, consistent UTF-8 encoding and preprocessing, and sufficiently large inputs to reduce gzip framing effects.
|
|
75
|
+
|
|
76
|
+
The implementation is based on Benedetto, Caglioti, and Loreto, [Language Trees and Zipping](https://arxiv.org/abs/cond-mat/0108530).
|
|
77
|
+
|
|
78
|
+
## License
|
|
79
|
+
|
|
80
|
+
Apache-2.0
|
|
81
|
+
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# GzipSimilarity
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
GzipSimilarity provides a dependency-light Swarmauri similarity implementation based on gzip compression and Normalized Compression Distance (NCD).
|
|
6
|
+
|
|
7
|
+
[](https://pepy.tech/projects/swarmauri_similarity_gzip)
|
|
8
|
+
[](https://hits.sh/github.com/swarmauri/swarmauri-sdk/tree/master/pkgs/standards/swarmauri_similarity_gzip/)
|
|
9
|
+
[](https://pypi.org/project/swarmauri_similarity_gzip/)
|
|
10
|
+
[](https://pypi.org/project/swarmauri_similarity_gzip/)
|
|
11
|
+
[](https://pypi.org/project/swarmauri_similarity_gzip/)
|
|
12
|
+
|
|
13
|
+
## Features
|
|
14
|
+
|
|
15
|
+
- Implements `SimilarityBase` from `swarmauri_base`.
|
|
16
|
+
- Compares text and binary values through shared gzip compressibility.
|
|
17
|
+
- Supports deterministic gzip sizing and configurable compression levels.
|
|
18
|
+
- Supports symmetric concatenation-order handling.
|
|
19
|
+
- Exposes both bounded similarity and raw NCD values.
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
uv add swarmauri_similarity_gzip
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install swarmauri_similarity_gzip
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from swarmauri_similarity_gzip import GzipSimilarity
|
|
35
|
+
|
|
36
|
+
similarity = GzipSimilarity()
|
|
37
|
+
|
|
38
|
+
score = similarity.similarity(
|
|
39
|
+
"The quick brown fox",
|
|
40
|
+
"Le renard brun rapide",
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
ncd = similarity.ncd(
|
|
44
|
+
"The quick brown fox",
|
|
45
|
+
"Le renard brun rapide",
|
|
46
|
+
)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
`similarity()` returns a score in `[0.0, 1.0]`, where higher values indicate greater shared compressibility. `ncd()` exposes the underlying Normalized Compression Distance. The result is a compression-based similarity estimate and should not be interpreted as semantic similarity without corpus-specific validation.
|
|
50
|
+
|
|
51
|
+
For language comparison, use balanced samples, consistent UTF-8 encoding and preprocessing, and sufficiently large inputs to reduce gzip framing effects.
|
|
52
|
+
|
|
53
|
+
The implementation is based on Benedetto, Caglioti, and Loreto, [Language Trees and Zipping](https://arxiv.org/abs/cond-mat/0108530).
|
|
54
|
+
|
|
55
|
+
## License
|
|
56
|
+
|
|
57
|
+
Apache-2.0
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "swarmauri_similarity_gzip"
|
|
3
|
+
version = "0.1.0.dev3"
|
|
4
|
+
description = "Gzip-based compression similarity for Swarmauri."
|
|
5
|
+
license = "Apache-2.0"
|
|
6
|
+
readme = { file = "README.md", content-type = "text/markdown" }
|
|
7
|
+
repository = "http://github.com/swarmauri/swarmauri-sdk"
|
|
8
|
+
requires-python = ">=3.10,<3.13"
|
|
9
|
+
authors = [{ name = "Jacob Stewart", email = "jacob@swarmauri.com" }]
|
|
10
|
+
keywords = [
|
|
11
|
+
"swarmauri",
|
|
12
|
+
"similarity",
|
|
13
|
+
"compression",
|
|
14
|
+
"gzip",
|
|
15
|
+
"normalized-compression-distance",
|
|
16
|
+
"language-comparison",
|
|
17
|
+
]
|
|
18
|
+
classifiers = [
|
|
19
|
+
"Development Status :: 1 - Planning",
|
|
20
|
+
"License :: OSI Approved :: Apache Software License",
|
|
21
|
+
"Programming Language :: Python",
|
|
22
|
+
"Programming Language :: Python :: 3",
|
|
23
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
24
|
+
"Programming Language :: Python :: 3.10",
|
|
25
|
+
"Programming Language :: Python :: 3.11",
|
|
26
|
+
"Programming Language :: Python :: 3.12",
|
|
27
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
28
|
+
"Topic :: Text Processing",
|
|
29
|
+
]
|
|
30
|
+
dependencies = [
|
|
31
|
+
"swarmauri_base",
|
|
32
|
+
"swarmauri_core",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
[tool.uv.sources]
|
|
36
|
+
swarmauri_base = { workspace = true }
|
|
37
|
+
swarmauri_core = { workspace = true }
|
|
38
|
+
|
|
39
|
+
[project.entry-points."swarmauri.similarities"]
|
|
40
|
+
GzipSimilarity = "swarmauri_similarity_gzip:GzipSimilarity"
|
|
41
|
+
|
|
42
|
+
[tool.pytest.ini_options]
|
|
43
|
+
timeout = 300
|
|
44
|
+
log_cli = true
|
|
45
|
+
log_cli_level = "INFO"
|
|
46
|
+
|
|
47
|
+
[build-system]
|
|
48
|
+
requires = ["poetry-core>=1.0.0"]
|
|
49
|
+
build-backend = "poetry.core.masonry.api"
|
|
50
|
+
|
|
51
|
+
[dependency-groups]
|
|
52
|
+
dev = [
|
|
53
|
+
"pytest>=8.0",
|
|
54
|
+
"pytest-timeout>=2.3.1",
|
|
55
|
+
"ruff>=0.9.9",
|
|
56
|
+
]
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import gzip
|
|
4
|
+
from typing import Literal
|
|
5
|
+
|
|
6
|
+
from pydantic import Field
|
|
7
|
+
from swarmauri_base.ComponentBase import ComponentBase
|
|
8
|
+
from swarmauri_base.similarities.SimilarityBase import SimilarityBase
|
|
9
|
+
from swarmauri_core.similarities.ISimilarity import ComparableType
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@ComponentBase.register_type(SimilarityBase, "GzipSimilarity")
|
|
13
|
+
class GzipSimilarity(SimilarityBase):
|
|
14
|
+
"""Gzip-based similarity using Normalized Compression Distance."""
|
|
15
|
+
|
|
16
|
+
type: Literal["GzipSimilarity"] = "GzipSimilarity"
|
|
17
|
+
compresslevel: int = Field(default=9, ge=0, le=9)
|
|
18
|
+
symmetric: bool = True
|
|
19
|
+
|
|
20
|
+
def similarity(self, x: ComparableType, y: ComparableType) -> float:
|
|
21
|
+
"""Return a bounded similarity score between two values."""
|
|
22
|
+
if self._to_bytes(x) == self._to_bytes(y):
|
|
23
|
+
return 1.0
|
|
24
|
+
|
|
25
|
+
ncd = self.ncd(x, y)
|
|
26
|
+
return max(0.0, min(1.0, 1.0 - ncd))
|
|
27
|
+
|
|
28
|
+
def ncd(self, x: ComparableType, y: ComparableType) -> float:
|
|
29
|
+
"""Return the underlying Normalized Compression Distance."""
|
|
30
|
+
x_bytes = self._to_bytes(x)
|
|
31
|
+
y_bytes = self._to_bytes(y)
|
|
32
|
+
|
|
33
|
+
if x_bytes == y_bytes:
|
|
34
|
+
return 0.0
|
|
35
|
+
|
|
36
|
+
x_size = self.compressed_size(x_bytes)
|
|
37
|
+
y_size = self.compressed_size(y_bytes)
|
|
38
|
+
forward_size = self.compressed_size(x_bytes + y_bytes)
|
|
39
|
+
|
|
40
|
+
if self.symmetric:
|
|
41
|
+
reverse_size = self.compressed_size(y_bytes + x_bytes)
|
|
42
|
+
joined_size = min(forward_size, reverse_size)
|
|
43
|
+
else:
|
|
44
|
+
joined_size = forward_size
|
|
45
|
+
|
|
46
|
+
denominator = max(x_size, y_size)
|
|
47
|
+
if denominator == 0:
|
|
48
|
+
return 0.0 if x_bytes == y_bytes else 1.0
|
|
49
|
+
|
|
50
|
+
return (joined_size - min(x_size, y_size)) / denominator
|
|
51
|
+
|
|
52
|
+
def compressed_size(self, value: ComparableType) -> int:
|
|
53
|
+
"""Return the deterministic gzip-compressed size of a value."""
|
|
54
|
+
return len(
|
|
55
|
+
gzip.compress(
|
|
56
|
+
self._to_bytes(value),
|
|
57
|
+
compresslevel=self.compresslevel,
|
|
58
|
+
mtime=0,
|
|
59
|
+
)
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
def check_bounded(self) -> bool:
|
|
63
|
+
"""Return whether this similarity measure is bounded."""
|
|
64
|
+
return True
|
|
65
|
+
|
|
66
|
+
@staticmethod
|
|
67
|
+
def _to_bytes(value: ComparableType) -> bytes:
|
|
68
|
+
if isinstance(value, str):
|
|
69
|
+
return value.encode("utf-8")
|
|
70
|
+
if isinstance(value, bytes):
|
|
71
|
+
return value
|
|
72
|
+
if isinstance(value, bytearray):
|
|
73
|
+
return bytes(value)
|
|
74
|
+
if isinstance(value, memoryview):
|
|
75
|
+
return value.tobytes()
|
|
76
|
+
raise TypeError(
|
|
77
|
+
"GzipSimilarity supports str, bytes, bytearray, and memoryview"
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
# Expose the registered type name at both class and instance level.
|
|
82
|
+
GzipSimilarity.type = "GzipSimilarity"
|