From 07d3eb265ab1d6eb5c43b10e9b06eb474f740793 Mon Sep 17 00:00:00 2001 From: Mirko Lenz Date: Fri, 8 Dec 2023 15:23:41 +0100 Subject: [PATCH] refactor: update protocol names --- cbrkit/case_sim/generic.py | 4 ++-- cbrkit/case_sim/helpers.py | 4 ++-- cbrkit/case_sim/tabular.py | 18 ++++++++++-------- cbrkit/data_sim/collections.py | 4 ++-- cbrkit/data_sim/generic.py | 6 +++--- cbrkit/data_sim/helpers.py | 4 ++-- cbrkit/data_sim/numeric.py | 10 +++++----- cbrkit/data_sim/strings.py | 12 ++++++------ cbrkit/retrieve.py | 4 ++-- cbrkit/typing.py | 6 +++--- 10 files changed, 37 insertions(+), 35 deletions(-) diff --git a/cbrkit/case_sim/generic.py b/cbrkit/case_sim/generic.py index 1c1aa3f..f54c20b 100644 --- a/cbrkit/case_sim/generic.py +++ b/cbrkit/case_sim/generic.py @@ -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 diff --git a/cbrkit/case_sim/helpers.py b/cbrkit/case_sim/helpers.py index 62cfcc0..089e99f 100644 --- a/cbrkit/case_sim/helpers.py +++ b/cbrkit/case_sim/helpers.py @@ -5,8 +5,8 @@ from cbrkit.typing import ( AggregateFunc, Casebase, - CasebaseSimFunc, CaseName, + CaseSimBatchFunc, CaseSimFunc, CaseType, SimilarityMap, @@ -17,7 +17,7 @@ def apply( func: CaseSimFunc[CaseType], -) -> CasebaseSimFunc[Any, CaseType]: +) -> CaseSimBatchFunc[Any, CaseType]: def wrapped_func( casebase: Casebase[CaseName, CaseType], query: CaseType, diff --git a/cbrkit/case_sim/tabular.py b/cbrkit/case_sim/tabular.py index 2029a76..a39e390 100644 --- a/cbrkit/case_sim/tabular.py +++ b/cbrkit/case_sim/tabular.py @@ -8,9 +8,9 @@ from cbrkit.typing import ( AggregateFunc, Casebase, - CasebaseSimFunc, + CaseSimBatchFunc, CaseType, - DataSimFunc, + DataSimBatchFunc, SimilarityMap, SimilarityValue, ) @@ -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 diff --git a/cbrkit/data_sim/collections.py b/cbrkit/data_sim/collections.py index 1de2f3f..0b2288c 100644 --- a/cbrkit/data_sim/collections.py +++ b/cbrkit/data_sim/collections.py @@ -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 diff --git a/cbrkit/data_sim/generic.py b/cbrkit/data_sim/generic.py index 1a4af0c..56036bf 100644 --- a/cbrkit/data_sim/generic.py +++ b/cbrkit/data_sim/generic.py @@ -3,7 +3,7 @@ from cbrkit.data_sim.helpers import apply from cbrkit.typing import ( - DataSimFunc, + DataSimBatchFunc, DataType, SimilaritySequence, SimilarityValue, @@ -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) ) @@ -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 diff --git a/cbrkit/data_sim/helpers.py b/cbrkit/data_sim/helpers.py index fdaf87a..0604deb 100644 --- a/cbrkit/data_sim/helpers.py +++ b/cbrkit/data_sim/helpers.py @@ -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] diff --git a/cbrkit/data_sim/numeric.py b/cbrkit/data_sim/numeric.py index 17a05d6..aae79ca 100644 --- a/cbrkit/data_sim/numeric.py +++ b/cbrkit/data_sim/numeric.py @@ -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: @@ -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: @@ -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: @@ -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: diff --git a/cbrkit/data_sim/strings.py b/cbrkit/data_sim/strings.py index 837b0d4..a01428a 100644 --- a/cbrkit/data_sim/strings.py +++ b/cbrkit/data_sim/strings.py @@ -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) @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/cbrkit/retrieve.py b/cbrkit/retrieve.py index cb6b858..4262d75 100644 --- a/cbrkit/retrieve.py +++ b/cbrkit/retrieve.py @@ -5,8 +5,8 @@ from cbrkit import load from cbrkit.typing import ( Casebase, - CasebaseSimFunc, CaseName, + CaseSimBatchFunc, CaseType, RetrievalResultProtocol, RetrieveFunc, @@ -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( diff --git a/cbrkit/typing.py b/cbrkit/typing.py index 6aadd29..c40fc4f 100644 --- a/cbrkit/typing.py +++ b/cbrkit/typing.py @@ -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]: @@ -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: ...