Skip to content

Commit

Permalink
refactor: update protocol names
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkolenz committed Dec 8, 2023
1 parent 04f4b96 commit 07d3eb2
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 35 deletions.
4 changes: 2 additions & 2 deletions cbrkit/case_sim/generic.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from typing import Any

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


def equality() -> CasebaseSimFunc[Any, Any]:
def equality() -> CaseSimBatchFunc[Any, Any]:
@apply
def wrapped_func(case: Any, query: Any) -> SimilarityValue:
return case == query
Expand Down
4 changes: 2 additions & 2 deletions cbrkit/case_sim/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from cbrkit.typing import (
AggregateFunc,
Casebase,
CasebaseSimFunc,
CaseName,
CaseSimBatchFunc,
CaseSimFunc,
CaseType,
SimilarityMap,
Expand All @@ -17,7 +17,7 @@

def apply(
func: CaseSimFunc[CaseType],
) -> CasebaseSimFunc[Any, CaseType]:
) -> CaseSimBatchFunc[Any, CaseType]:
def wrapped_func(
casebase: Casebase[CaseName, CaseType],
query: CaseType,
Expand Down
18 changes: 10 additions & 8 deletions cbrkit/case_sim/tabular.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from cbrkit.typing import (
AggregateFunc,
Casebase,
CasebaseSimFunc,
CaseSimBatchFunc,
CaseType,
DataSimFunc,
DataSimBatchFunc,
SimilarityMap,
SimilarityValue,
)
Expand All @@ -37,17 +37,19 @@ def _value_getter(obj: TabularData, key: Any) -> Any:


def factory(
attributes: Mapping[str, DataSimFunc[Any]] | None = None,
types: Mapping[type[Any], DataSimFunc[Any]] | None = None,
types_fallback: DataSimFunc[Any] | None = None,
attributes: Mapping[str, DataSimBatchFunc[Any]] | None = None,
types: Mapping[type[Any], DataSimBatchFunc[Any]] | None = None,
types_fallback: DataSimBatchFunc[Any] | None = None,
aggregate: AggregateFunc = aggregate_default,
value_getter: Callable[[Any, str], Any] = _value_getter,
key_getter: Callable[[Any], Iterator[str]] = _key_getter,
) -> CasebaseSimFunc[Any, TabularData]:
attributes_map: Mapping[str, DataSimFunc[Any]] = (
) -> CaseSimBatchFunc[Any, TabularData]:
attributes_map: Mapping[str, DataSimBatchFunc[Any]] = (
{} if attributes is None else attributes
)
types_map: Mapping[type[Any], DataSimFunc[Any]] = {} if types is None else types
types_map: Mapping[type[Any], DataSimBatchFunc[Any]] = (
{} if types is None else types
)

def wrapped_func(
casebase: Casebase[Any, CaseType], query: CaseType
Expand Down
4 changes: 2 additions & 2 deletions cbrkit/data_sim/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
from typing import Any

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


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

@apply
Expand Down
6 changes: 3 additions & 3 deletions cbrkit/data_sim/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from cbrkit.data_sim.helpers import apply
from cbrkit.typing import (
DataSimFunc,
DataSimBatchFunc,
DataType,
SimilaritySequence,
SimilarityValue,
Expand All @@ -14,7 +14,7 @@ def table(
*args: tuple[DataType, DataType, SimilarityValue],
symmetric: bool = True,
default: SimilarityValue = 0.0,
) -> DataSimFunc[DataType]:
) -> DataSimBatchFunc[DataType]:
table: defaultdict[DataType, defaultdict[DataType, SimilarityValue]] = defaultdict(
lambda: defaultdict(lambda: default)
)
Expand All @@ -31,7 +31,7 @@ def wrapped_func(*args: tuple[DataType, DataType]) -> SimilaritySequence:
return wrapped_func


def equality() -> DataSimFunc[Any]:
def equality() -> DataSimBatchFunc[Any]:
@apply
def wrapped_func(x: Any, y: Any) -> SimilarityValue:
return x == y
Expand Down
4 changes: 2 additions & 2 deletions cbrkit/data_sim/helpers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from cbrkit.typing import (
DataPairSimFunc,
DataSimBatchFunc,
DataSimFunc,
DataType,
SimilaritySequence,
)


def apply(func: DataPairSimFunc[DataType]) -> DataSimFunc[DataType]:
def apply(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,14 +1,14 @@
import math

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

Number = float | int

__all__ = ["linear", "threshold", "exponential", "sigmoid"]


def linear(max: float, min: float = 0.0) -> DataSimFunc[Number]:
def linear(max: float, min: float = 0.0) -> DataSimBatchFunc[Number]:
"""Linear similarity function.
Args:
Expand All @@ -25,7 +25,7 @@ def wrapped_func(x: Number, y: Number) -> SimilarityValue:
return wrapped_func


def threshold(threshold: float) -> DataSimFunc[Number]:
def threshold(threshold: float) -> DataSimBatchFunc[Number]:
"""Threshold similarity function.
Args:
Expand All @@ -41,7 +41,7 @@ def wrapped_func(x: Number, y: Number) -> SimilarityValue:
return wrapped_func


def exponential(alpha: float = 1.0) -> DataSimFunc[Number]:
def exponential(alpha: float = 1.0) -> DataSimBatchFunc[Number]:
"""Exponential similarity function.
Args:
Expand All @@ -57,7 +57,7 @@ def wrapped_func(x: Number, y: Number) -> SimilarityValue:
return wrapped_func


def sigmoid(alpha: float = 1.0, theta: float = 1.0) -> DataSimFunc[Number]:
def sigmoid(alpha: float = 1.0, theta: float = 1.0) -> DataSimBatchFunc[Number]:
"""Sigmoid similarity function.
Args:
Expand Down
12 changes: 6 additions & 6 deletions cbrkit/data_sim/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
from cbrkit.data_sim._taxonomy import Taxonomy, TaxonomyMeasure
from cbrkit.data_sim.helpers import apply
from cbrkit.typing import (
DataSimFunc,
DataSimBatchFunc,
FilePath,
SimilaritySequence,
SimilarityValue,
)


def spacy(model_name: str = "en_core_web_lg") -> DataSimFunc[str]:
def spacy(model_name: str = "en_core_web_lg") -> DataSimBatchFunc[str]:
import spacy

nlp = spacy.load(model_name)
Expand All @@ -28,7 +28,7 @@ def wrapped_func(*args: tuple[str, str]) -> SimilaritySequence:

def taxonomy(
path: FilePath, measure: TaxonomyMeasure = "wu_palmer"
) -> DataSimFunc[str]:
) -> DataSimBatchFunc[str]:
taxonomy = Taxonomy(path)

@apply
Expand All @@ -38,7 +38,7 @@ def wrapped_func(x: str, y: str) -> SimilarityValue:
return wrapped_func


def levenshtein(score_cutoff: float | None = None) -> DataSimFunc[str]:
def levenshtein(score_cutoff: float | None = None) -> DataSimBatchFunc[str]:
import Levenshtein

@apply
Expand All @@ -48,7 +48,7 @@ def wrapped_func(x: str, y: str) -> SimilarityValue:
return wrapped_func


def jaro(score_cutoff: float | None = None) -> DataSimFunc[str]:
def jaro(score_cutoff: float | None = None) -> DataSimBatchFunc[str]:
import Levenshtein

@apply
Expand All @@ -60,7 +60,7 @@ def wrapped_func(x: str, y: str) -> SimilarityValue:

def jaro_winkler(
score_cutoff: float | None = None, prefix_weight: float | None = None
) -> DataSimFunc[str]:
) -> DataSimBatchFunc[str]:
import Levenshtein

@apply
Expand Down
4 changes: 2 additions & 2 deletions cbrkit/retrieve.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from cbrkit import load
from cbrkit.typing import (
Casebase,
CasebaseSimFunc,
CaseName,
CaseSimBatchFunc,
CaseType,
RetrievalResultProtocol,
RetrieveFunc,
Expand Down Expand Up @@ -74,7 +74,7 @@ def retrieve(


def retriever(
similarity_func: CasebaseSimFunc[CaseName, CaseType],
similarity_func: CaseSimBatchFunc[CaseName, CaseType],
casebase_limit: int | None = None,
) -> RetrieveFunc[CaseName, CaseType]:
def wrapped_func(
Expand Down
6 changes: 3 additions & 3 deletions cbrkit/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
)


class CasebaseSimFunc(Protocol[CaseName, CaseType_contra]):
class CaseSimBatchFunc(Protocol[CaseName, CaseType_contra]):
def __call__(
self, casebase: Casebase[CaseName, CaseType_contra], query: CaseType_contra
) -> SimilarityMap[CaseName]:
Expand All @@ -45,14 +45,14 @@ def __call__(
...


class DataSimFunc(Protocol[DataType_contra]):
class DataSimBatchFunc(Protocol[DataType_contra]):
def __call__(
self, *args: tuple[DataType_contra, DataType_contra]
) -> SimilaritySequence:
...


class DataPairSimFunc(Protocol[DataType_contra]):
class DataSimFunc(Protocol[DataType_contra]):
def __call__(self, x: DataType_contra, y: DataType_contra) -> SimilarityValue:
...

Expand Down

0 comments on commit 07d3eb2

Please sign in to comment.