Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix max_seq_length issue in BERT models. #124

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
21 changes: 20 additions & 1 deletion keybert/_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def extract_keywords(
vectorizer: CountVectorizer = None,
highlight: bool = False,
seed_keywords: List[str] = None,
max_seq_length: int = None,
) -> Union[List[Tuple[str, float]], List[List[Tuple[str, float]]]]:
"""Extract keywords and/or keyphrases

Expand Down Expand Up @@ -151,7 +152,25 @@ def extract_keywords(
df = count.transform(docs)

# Extract embeddings
doc_embeddings = self.model.embed(docs)

# if the max_seq_length is smaller than the length of the document,
# the document will be truncated.
# To overcome this issue, the embedding of each document having size of the max_seq_length is calculated
# then we take the average of the embeddings of each document.
doc_embeddings = []
if max_seq_length:
for i in range (len(docs)):
splitted_doc = docs[i].split()
# this temporarily holds the embedding of each max_seq_length chunck
temp_doc_embedding = []
for i in range (0, len(splitted_doc), max_seq_length):
input_text = " ".join(splitted_doc[i:i+max_seq_length])
temp_doc_embedding.append(self.model.embed([input_text]))
#final embedding of the document
doc_embeddings.append(np.mean(temp_doc_embedding, axis=0))
else:
doc_embeddings = self.model.embed(docs)
# extract embeding for keyword candidates
word_embeddings = self.model.embed(words)

# Find keywords
Expand Down