Skip to content

Commit

Permalink
refactor: rename apply to batchify
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkolenz committed Dec 8, 2023
1 parent 07d3eb2 commit 0f00919
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 22 deletions.
4 changes: 2 additions & 2 deletions cbrkit/case_sim/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from cbrkit.case_sim.generic import equality
from cbrkit.case_sim.helpers import aggregate, apply
from cbrkit.case_sim.helpers import aggregate, batchify
from cbrkit.case_sim.tabular import factory as tabular

__all__ = ["apply", "aggregate", "tabular", "equality"]
__all__ = ["batchify", "aggregate", "tabular", "equality"]
4 changes: 2 additions & 2 deletions cbrkit/case_sim/generic.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import Any

from cbrkit.case_sim.helpers import apply
from cbrkit.case_sim.helpers import batchify
from cbrkit.typing import CaseSimBatchFunc, SimilarityValue


def equality() -> CaseSimBatchFunc[Any, Any]:
@apply
@batchify
def wrapped_func(case: Any, query: Any) -> SimilarityValue:
return case == query

Expand Down
2 changes: 1 addition & 1 deletion cbrkit/case_sim/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
)


def apply(
def batchify(
func: CaseSimFunc[CaseType],
) -> CaseSimBatchFunc[Any, CaseType]:
def wrapped_func(
Expand Down
4 changes: 2 additions & 2 deletions cbrkit/data_sim/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from . import collections, generic, numeric, strings
from .generic import equality, table
from .helpers import apply, dist2sim
from .helpers import batchify, dist2sim

__all__ = [
"apply",
"batchify",
"dist2sim",
"table",
"equality",
Expand Down
4 changes: 2 additions & 2 deletions cbrkit/data_sim/collections.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from collections.abc import Collection, Set
from typing import Any

from cbrkit.data_sim.helpers import apply, dist2sim
from cbrkit.data_sim.helpers import batchify, dist2sim
from cbrkit.typing import DataSimBatchFunc


def jaccard() -> DataSimBatchFunc[Collection[Any]]:
from nltk.metrics import jaccard_distance

@apply
@batchify
def wrapped_func(x: Collection[Any], y: Collection[Any]) -> float:
if not isinstance(x, Set):
x = set(x)
Expand Down
4 changes: 2 additions & 2 deletions cbrkit/data_sim/generic.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections import defaultdict
from typing import Any

from cbrkit.data_sim.helpers import apply
from cbrkit.data_sim.helpers import batchify
from cbrkit.typing import (
DataSimBatchFunc,
DataType,
Expand Down Expand Up @@ -32,7 +32,7 @@ def wrapped_func(*args: tuple[DataType, DataType]) -> SimilaritySequence:


def equality() -> DataSimBatchFunc[Any]:
@apply
@batchify
def wrapped_func(x: Any, y: Any) -> SimilarityValue:
return x == y

Expand Down
2 changes: 1 addition & 1 deletion cbrkit/data_sim/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
)


def apply(func: DataSimFunc[DataType]) -> DataSimBatchFunc[DataType]:
def batchify(func: DataSimFunc[DataType]) -> DataSimBatchFunc[DataType]:
def wrapped_func(*args: tuple[DataType, DataType]) -> SimilaritySequence:
return [func(data1, data2) for (data1, data2) in args]

Expand Down
10 changes: 5 additions & 5 deletions cbrkit/data_sim/numeric.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import math

from cbrkit.data_sim.helpers import apply
from cbrkit.data_sim.helpers import batchify
from cbrkit.typing import DataSimBatchFunc, SimilarityValue

Number = float | int
Expand All @@ -18,7 +18,7 @@ def linear(max: float, min: float = 0.0) -> DataSimBatchFunc[Number]:
![linear](../../assets/numeric/linear.png)
"""

@apply
@batchify
def wrapped_func(x: Number, y: Number) -> SimilarityValue:
return (max - abs(x - y)) / (max - min)

Expand All @@ -34,7 +34,7 @@ def threshold(threshold: float) -> DataSimBatchFunc[Number]:
![threshold](../../assets/numeric/threshold.png)
"""

@apply
@batchify
def wrapped_func(x: Number, y: Number) -> SimilarityValue:
return 1.0 if abs(x - y) <= threshold else 0.0

Expand All @@ -50,7 +50,7 @@ def exponential(alpha: float = 1.0) -> DataSimBatchFunc[Number]:
![exponential](../../assets/numeric/exponential.png)
"""

@apply
@batchify
def wrapped_func(x: Number, y: Number) -> SimilarityValue:
return math.exp(-alpha * abs(x - y))

Expand All @@ -67,7 +67,7 @@ def sigmoid(alpha: float = 1.0, theta: float = 1.0) -> DataSimBatchFunc[Number]:
![sigmoid](../../assets/numeric/sigmoid.png)
"""

@apply
@batchify
def wrapped_func(x: Number, y: Number) -> SimilarityValue:
return 1.0 / (1.0 + math.exp((abs(x - y) - theta) / alpha))

Expand Down
10 changes: 5 additions & 5 deletions cbrkit/data_sim/strings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import itertools

from cbrkit.data_sim._taxonomy import Taxonomy, TaxonomyMeasure
from cbrkit.data_sim.helpers import apply
from cbrkit.data_sim.helpers import batchify
from cbrkit.typing import (
DataSimBatchFunc,
FilePath,
Expand Down Expand Up @@ -31,7 +31,7 @@ def taxonomy(
) -> DataSimBatchFunc[str]:
taxonomy = Taxonomy(path)

@apply
@batchify
def wrapped_func(x: str, y: str) -> SimilarityValue:
return taxonomy.similarity(x, y, measure)

Expand All @@ -41,7 +41,7 @@ def wrapped_func(x: str, y: str) -> SimilarityValue:
def levenshtein(score_cutoff: float | None = None) -> DataSimBatchFunc[str]:
import Levenshtein

@apply
@batchify
def wrapped_func(x: str, y: str) -> SimilarityValue:
return Levenshtein.ratio(x, y, score_cutoff=score_cutoff)

Expand All @@ -51,7 +51,7 @@ def wrapped_func(x: str, y: str) -> SimilarityValue:
def jaro(score_cutoff: float | None = None) -> DataSimBatchFunc[str]:
import Levenshtein

@apply
@batchify
def wrapped_func(x: str, y: str) -> SimilarityValue:
return Levenshtein.jaro(x, y, score_cutoff=score_cutoff)

Expand All @@ -63,7 +63,7 @@ def jaro_winkler(
) -> DataSimBatchFunc[str]:
import Levenshtein

@apply
@batchify
def wrapped_func(x: str, y: str) -> SimilarityValue:
return Levenshtein.jaro_winkler(
x, y, score_cutoff=score_cutoff, prefix_weight=prefix_weight
Expand Down

0 comments on commit 0f00919

Please sign in to comment.