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

Get length of encoding object #139

Closed
tomhosking opened this issue Feb 12, 2020 · 3 comments
Closed

Get length of encoding object #139

tomhosking opened this issue Feb 12, 2020 · 3 comments

Comments

@tomhosking
Copy link

@tomhosking tomhosking commented Feb 12, 2020

What's a good way to get the length of an Encoding object? Neither of the following intuitive methods work:

from tokenizers import BertWordPieceTokenizer

tok = BertWordPieceTokenizer('./data/bert-vocabs/bert-base-cased-vocab.txt')

output = tok.encode('This is a sentence')
>> Encoding(num_tokens=6, attributes=[ids, type_ids, tokens, offsets, attention_mask, special_tokens_mask, overflowing, original_str, normalized_str])

output.num_tokens
>> AttributeError: 'Encoding' object has no attribute 'num_tokens'

len(output)
>> TypeError: object of type 'Encoding' has no len()
@julien-c

This comment has been minimized.

Copy link
Member

@julien-c julien-c commented Feb 12, 2020

What do you mean by length of an Encoding? The number of tokens?

@tomhosking

This comment has been minimized.

Copy link
Author

@tomhosking tomhosking commented Feb 12, 2020

Sorry, yes - the number of tokens. It's shown in the string representation as num_tokens but isn't an attribute of the Encoding object. I can do len(output.tokens) but this isn't as pythonic as I would have expected.

@n1t0

This comment has been minimized.

Copy link
Member

@n1t0 n1t0 commented Feb 18, 2020

Thank you @tomhosking for suggesting this. We added the __len__ implementation on the Encoding object in d8a73c8. It will be included in the next release!

@n1t0 n1t0 closed this Feb 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.