1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
import warnings
warnings.filterwarnings("ignore", category=FutureWarning)
import numpy as np
from typing import List, Union, Tuple
from packaging import version
from sklearn import __version__ as sklearn_version
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.feature_extraction.text import CountVectorizer
from keybert._mmr import mmr
from keybert._maxsum import max_sum_distance
from keybert._highlight import highlight_document
from keybert.backend._utils import select_backend
class KeyBERT:
"""
A minimal method for keyword extraction with BERT
The keyword extraction is done by finding the sub-phrases in
a document that are the most similar to the document itself.
First, document embeddings are extracted with BERT to get a
document-level representation. Then, word embeddings are extracted
for N-gram words/phrases. Finally, we use cosine similarity to find the
words/phrases that are the most similar to the document.
The most similar words could then be identified as the words that
best describe the entire document.
"""
def __init__(self, model="all-MiniLM-L6-v2"):
"""KeyBERT initialization
Arguments:
model: Use a custom embedding model.
The following backends are currently supported:
* SentenceTransformers
* 🤗 Transformers
* Flair
* Spacy
* Gensim
* USE (TF-Hub)
You can also pass in a string that points to one of the following
sentence-transformers models:
* https://www.sbert.net/docs/pretrained_models.html
"""
self.model = select_backend(model)
def extract_keywords(
self,
docs: Union[str, List[str]],
candidates: List[str] = None,
keyphrase_ngram_range: Tuple[int, int] = (1, 1),
stop_words: Union[str, List[str]] = "english",
top_n: int = 5,
min_df: int = 1,
use_maxsum: bool = False,
use_mmr: bool = False,
diversity: float = 0.5,
nr_candidates: int = 20,
vectorizer: CountVectorizer = None,
highlight: bool = False,
seed_keywords: List[str] = None,
) -> Union[List[Tuple[str, float]], List[List[Tuple[str, float]]]]:
"""Extract keywords and/or keyphrases
To get the biggest speed-up, make sure to pass multiple documents
at once instead of iterating over a single document.
Arguments:
docs: The document(s) for which to extract keywords/keyphrases
candidates: Candidate keywords/keyphrases to use instead of extracting them from the document(s)
keyphrase_ngram_range: Length, in words, of the extracted keywords/keyphrases.
NOTE: This is not used if you passed a `vectorizer`.
stop_words: Stopwords to remove from the document.
NOTE: This is not used if you passed a `vectorizer`.
top_n: Return the top n keywords/keyphrases
min_df: Minimum document frequency of a word across all documents
if keywords for multiple documents need to be extracted.
NOTE: This is not used if you passed a `vectorizer`.
use_maxsum: Whether to use Max Sum Distance for the selection
of keywords/keyphrases.
use_mmr: Whether to use Maximal Marginal Relevance (MMR) for the
selection of keywords/keyphrases.
diversity: The diversity of the results between 0 and 1 if `use_mmr`
is set to True.
nr_candidates: The number of candidates to consider if `use_maxsum` is
set to True.
vectorizer: Pass in your own `CountVectorizer` from
`sklearn.feature_extraction.text.CountVectorizer`
highlight: Whether to print the document and highlight its keywords/keyphrases.
NOTE: This does not work if multiple documents are passed.
seed_keywords: Seed keywords that may guide the extraction of keywords by
steering the similarities towards the seeded keywords.
Returns:
keywords: The top n keywords for a document with their respective distances
to the input document.
Usage:
To extract keywords from a single document:
```python
from keybert import KeyBERT
kw_model = KeyBERT()
keywords = kw_model.extract_keywords(doc)
```
To extract keywords from multiple documents,
which is typically quite a bit faster:
```python
from keybert import KeyBERT
kw_model = KeyBERT()
keywords = kw_model.extract_keywords(docs)
```
"""
# Check for a single, empty document
if isinstance(docs, str):
if docs:
docs = [docs]
else:
return []
# Extract potential words using a vectorizer / tokenizer
if vectorizer:
count = vectorizer.fit(docs)
else:
try:
count = CountVectorizer(
ngram_range=keyphrase_ngram_range,
stop_words=stop_words,
min_df=min_df,
).fit(docs)
except ValueError:
return []
# Scikit-Learn Deprecation: get_feature_names is deprecated in 1.0
# and will be removed in 1.2. Please use get_feature_names_out instead.
if version.parse(sklearn_version) >= version.parse("1.0.0"):
words = count.get_feature_names_out()
else:
words = count.get_feature_names()
df = count.transform(docs)
# Extract embeddings
doc_embeddings = self.model.embed(docs)
word_embeddings = self.model.embed(words)
# Find keywords
all_keywords = []
for index, _ in enumerate(docs):
try:
# Select embeddings
candidate_indices = df[index].nonzero()[1]
candidates = [words[index] for index in candidate_indices]
candidate_embeddings = word_embeddings[candidate_indices]
doc_embedding = doc_embeddings[index].reshape(1, -1)
# Guided KeyBERT with seed keywords
if seed_keywords is not None:
seed_embeddings = self.model.embed([" ".join(seed_keywords)])
doc_embedding = np.average(
[doc_embedding, seed_embeddings], axis=0, weights=[3, 1]
)
# Maximal Marginal Relevance (MMR)
if use_mmr:
keywords = mmr(
doc_embedding,
candidate_embeddings,
candidates,
top_n,
diversity,
)
# Max Sum Distance
elif use_maxsum:
keywords = max_sum_distance(
doc_embedding,
candidate_embeddings,
candidates,
top_n,
nr_candidates,
)
# Cosine-based keyword extraction
else:
distances = cosine_similarity(doc_embedding, candidate_embeddings)
keywords = [
(candidates[index], round(float(distances[0][index]), 4))
for index in distances.argsort()[0][-top_n:]
][::-1]
all_keywords.append(keywords)
# Capturing empty keywords
except ValueError:
all_keywords.append([])
# Highlight keywords in the document
if len(all_keywords) == 1:
if highlight:
highlight_document(docs[0], all_keywords[0], count)
all_keywords = all_keywords[0]
return all_keywords
|