Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update "def text_length" #2878

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions sentence_transformers/SentenceTransformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1319,16 +1319,19 @@ def push_to_hub(
# This isn't expected to ever be reached.
return folder_url

def _text_length(self, text: list[int] | list[list[int]]) -> int:
# def text_length(self, text: Union[List[int], List[List[int]]]) -> int:
def text_length(self, text: Union[str, List[str], List[int], List[List[int]]]):
"""
Help function to get the length for the input text. Text can be either
a list of ints (which means a single text as input), or a tuple of list of ints
(representing several text inputs to the model).
a string, a list of strings, a list of ints (which means a single text as input),
or a tuple of list of ints (representing several text inputs to the model).
"""

if isinstance(text, dict): # {key: value} case
# if isinstance(text, dict): # {key: value} case
if isinstance(text, str):
return len(text)
elif isinstance(text, dict): # {key: value} case
return len(next(iter(text.values())))
elif not hasattr(text, "__len__"): # Object has no len() method
elif not hasattr(text, "_len_"): # Object has no len() method
return 1
elif len(text) == 0 or isinstance(text[0], int): # Empty string or list of ints
return len(text)
Expand Down