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

Extends embed to allow sequence of texts #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 15 additions & 7 deletions bpemb/bpemb.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,22 +365,30 @@ def _encode(self, texts, fn):
texts = map(self.preprocess, texts)
return list(map(fn, texts))

def embed(self, text: str) -> np.ndarray:
def embed(self, texts: Union[str, Sequence[str]]) -> np.ndarray:
"""Byte-pair encode text and return the corresponding byte-pair
embeddings.

Parameters
----------
text: ``str'', required
The text to encode and embed.
texts: ``Union[str, Sequence[str]]'', required
The text or texts to encode and embed.

Returns
-------
A matrix of shape (l, d), where l is the length of the byte-pair
encoded text and d the embedding dimension.
If texts is a string, a matrix of shape (l, d), where l is the length
of the byte-pair encoded text and d the embedding dimension.
If texts is a sequence of strings, an array of shape (n,), where n is
the length of the text sequence, and each element is a matrix of shape
(l_i, d).
"""
ids = self.encode_ids(text)
return self.emb.vectors[ids]
ids = self.encode_ids(texts)
if isinstance(texts, str):
return self.emb.vectors[ids]
return np.asarray([
self.emb.vectors[text_ids]
for text_ids in ids
])

def decode(
self,
Expand Down