Skip to content

Commit 1f877ab

Browse files
committed
Feature: Create type alias for dict of token features.
1 parent 39133a1 commit 1f877ab

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

ingredient_parser/en/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from .parser import inspect_parser_en, parse_ingredient_en
22
from .postprocess import PostProcessor
3-
from .preprocess import PreProcessor
3+
from .preprocess import FeatureDict, PreProcessor
44

55
__all__ = [
6+
"FeatureDict",
67
"PostProcessor",
78
"PreProcessor",
89
"inspect_parser_en",

ingredient_parser/en/preprocess.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545

4646
CONSECUTIVE_SPACES = re.compile(r"\s+")
4747

48+
# Type alias for dict of token features.
49+
FeatureDict = dict[str, str | bool]
50+
4851

4952
class PreProcessor:
5053
"""Recipe ingredient sentence PreProcessor class.
@@ -972,7 +975,7 @@ def _ngram_features(self, token: str, prefix: str) -> dict[str, str]:
972975

973976
return ngram_features
974977

975-
def _token_features(self, token: Token) -> dict[str, str | bool]:
978+
def _token_features(self, token: Token) -> FeatureDict:
976979
"""Return the features for the token at the given index in the sentence.
977980
978981
If the token at the given index appears in the corpus parameter, the token is
@@ -986,12 +989,12 @@ def _token_features(self, token: Token) -> dict[str, str | bool]:
986989
987990
Returns
988991
-------
989-
dict[str, str | bool]
992+
FeatureDict
990993
Dictionary of features for token at index.
991994
"""
992995

993996
index = token.index
994-
features: dict[str, str | bool] = {}
997+
features: FeatureDict = {}
995998

996999
features["bias"] = ""
9971000
features["sentence_length"] = str(self._sentence_length_bucket())
@@ -1092,12 +1095,12 @@ def _token_features(self, token: Token) -> dict[str, str | bool]:
10921095

10931096
return features
10941097

1095-
def sentence_features(self) -> list[dict[str, str | bool]]:
1098+
def sentence_features(self) -> list[FeatureDict]:
10961099
"""Return dict of features for each token in sentence.
10971100
10981101
Returns
10991102
-------
1100-
list[dict[str, str | bool]]
1103+
list[FeatureDict]
11011104
List of feature dicts for each token in sentence.
11021105
"""
11031106
logger.debug("Generating features for tokens.")

0 commit comments

Comments
 (0)