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
5 |
+
tags:
|
6 |
+
- keyphrase-extraction
|
7 |
+
datasets:
|
8 |
+
- midas/inspec
|
9 |
+
metrics:
|
10 |
+
- seqeval
|
11 |
+
widget:
|
12 |
+
- text: "Keyphrase extraction is a technique in text analysis where you extract the important keyphrases from a text. Since this is a time-consuming process, Artificial Intelligence is used to automate it. Currently, classical machine learning methods, that use statistics and linguistics, are widely used for the extraction process. The fact that these methods have been widely used in the community has the advantage that there are many easy-to-use libraries. Now with the recent innovations in deep learning methods (such as recurrent neural networks and transformers, GANS, β¦), keyphrase extraction can be improved. These new methods also focus on the semantics and context of a document, which is quite an improvement."
|
13 |
+
example_title: "Example 1"
|
14 |
+
- text: "In this work, we explore how to learn task specific language models aimed towards learning rich representation of keyphrases from text documents. We experiment with different masking strategies for pre-training transformer language models (LMs) in discriminative as well as generative settings. In the discriminative setting, we introduce a new pre-training objective - Keyphrase Boundary Infilling with Replacement (KBIR), showing large gains in performance (up to 9.26 points in F1) over SOTA, when LM pre-trained using KBIR is fine-tuned for the task of keyphrase extraction. In the generative setting, we introduce a new pre-training setup for BART - KeyBART, that reproduces the keyphrases related to the input text in the CatSeq format, instead of the denoised original input. This also led to gains in performance (up to 4.33 points inF1@M) over SOTA for keyphrase generation. Additionally, we also fine-tune the pre-trained language models on named entity recognition(NER), question answering (QA), relation extraction (RE), abstractive summarization and achieve comparable performance with that of the SOTA, showing that learning rich representation of keyphrases is indeed beneficial for many other fundamental NLP tasks."
|
15 |
+
example_title: "Example 2"
|
16 |
+
model-index:
|
17 |
+
- name: DeDeckerThomas/keyphrase-extraction-distilbert-inspec
|
18 |
+
results:
|
19 |
+
- task:
|
20 |
+
type: keyphrase-extraction
|
21 |
+
name: Keyphrase Extraction
|
22 |
+
dataset:
|
23 |
+
type: midas/inspec
|
24 |
+
name: inspec
|
25 |
+
metrics:
|
26 |
+
- type: seqeval
|
27 |
+
value: 0.509
|
28 |
+
name: F1-score
|
29 |
+
---
|
30 |
+
# π Keyphrase Extraction model: distilbert-inspec
|
31 |
+
Keyphrase extraction is a technique in text analysis where you extract the important keyphrases from a text. Since this is a time-consuming process, Artificial Intelligence is used to automate it.
|
32 |
+
Currently, classical machine learning methods, that use statistics and linguistics, are widely used for the extraction process. The fact that these methods have been widely used in the community has the advantage that there are many easy-to-use libraries.
|
33 |
+
Now with the recent innovations in deep learning methods (such as recurrent neural networks and transformers, GANS, β¦), keyphrase extraction can be improved. These new methods also focus on the semantics and context of a document, which is quite an improvement.
|
34 |
+
|
35 |
+
|
36 |
+
## π Model Description
|
37 |
+
This model is a fine-tuned distilbert model on the Inspec dataset. More information can be found here: https://huggingface.co/distilbert-base-uncased.
|
38 |
+
|
39 |
+
The model is fine-tuned as a token classification problem where the text is labeled using the BIO scheme.
|
40 |
+
|
41 |
+
| Label | Description |
|
42 |
+
| ----- | ------------------------------- |
|
43 |
+
| B-KEY | At the beginning of a keyphrase |
|
44 |
+
| I-KEY | Inside a keyphrase |
|
45 |
+
| O | Outside a keyphrase |
|
46 |
+
|
47 |
+
Kulkarni, Mayank, Debanjan Mahata, Ravneet Arora, and Rajarshi Bhowmik. "Learning Rich Representation of Keyphrases from Text." arXiv preprint arXiv:2112.08547 (2021).
|
48 |
+
|
49 |
+
Sahrawat, Dhruva, Debanjan Mahata, Haimin Zhang, Mayank Kulkarni, Agniv Sharma, Rakesh Gosangi, Amanda Stent, Yaman Kumar, Rajiv Ratn Shah, and Roger Zimmermann. "Keyphrase extraction as sequence labeling using contextualized embeddings." In European Conference on Information Retrieval, pp. 328-335. Springer, Cham, 2020.
|
50 |
+
|
51 |
+
## β Intended uses & limitations
|
52 |
+
### π Limitations
|
53 |
+
* This keyphrase extraction model is very domain-specific and will perform very well on abstracts of scientific papers. It's not recommended to use this model for other domains, but you are free to test it out.
|
54 |
+
* Only works for English documents.
|
55 |
+
* For a custom model, please consult the training notebook for more information (link incoming).
|
56 |
+
|
57 |
+
### β How to use
|
58 |
+
```python
|
59 |
+
from transformers import (
|
60 |
+
TokenClassificationPipeline,
|
61 |
+
AutoModelForTokenClassification,
|
62 |
+
AutoTokenizer,
|
63 |
+
)
|
64 |
+
from transformers.pipelines import AggregationStrategy
|
65 |
+
import numpy as np
|
66 |
+
|
67 |
+
# Define keyphrase extraction pipeline
|
68 |
+
class KeyphraseExtractionPipeline(TokenClassificationPipeline):
|
69 |
+
def __init__(self, model, *args, **kwargs):
|
70 |
+
super().__init__(
|
71 |
+
model=AutoModelForTokenClassification.from_pretrained(model),
|
72 |
+
tokenizer=AutoTokenizer.from_pretrained(model),
|
73 |
+
*args,
|
74 |
+
**kwargs
|
75 |
+
)
|
76 |
+
|
77 |
+
def postprocess(self, model_outputs):
|
78 |
+
results = super().postprocess(
|
79 |
+
model_outputs=model_outputs,
|
80 |
+
aggregation_strategy=AggregationStrategy.SIMPLE,
|
81 |
+
)
|
82 |
+
return np.unique([result.get("word").strip() for result in results])
|
83 |
+
|
84 |
+
```
|
85 |
+
|
86 |
+
```python
|
87 |
+
# Load pipeline
|
88 |
+
model_name = "DeDeckerThomas/keyphrase-extraction-distilbert-inspec"
|
89 |
+
extractor = KeyphraseExtractionPipeline(model=model_name)
|
90 |
+
```
|
91 |
+
```python
|
92 |
+
# Inference
|
93 |
+
text = """
|
94 |
+
Keyphrase extraction is a technique in text analysis where you extract the important keyphrases from a text.
|
95 |
+
Since this is a time-consuming process, Artificial Intelligence is used to automate it.
|
96 |
+
Currently, classical machine learning methods, that use statistics and linguistics, are widely used for the extraction process.
|
97 |
+
The fact that these methods have been widely used in the community has the advantage that there are many easy-to-use libraries.
|
98 |
+
Now with the recent innovations in deep learning methods (such as recurrent neural networks and transformers, GANS, β¦),
|
99 |
+
keyphrase extraction can be improved. These new methods also focus on the semantics and context of a document, which is quite an improvement.
|
100 |
+
""".replace(
|
101 |
+
"\n", ""
|
102 |
+
)
|
103 |
+
|
104 |
+
keyphrases = extractor(text)
|
105 |
+
|
106 |
+
print(keyphrases)
|
107 |
+
```
|
108 |
+
|
109 |
+
```
|
110 |
+
# Output
|
111 |
+
['Artificial Intelligence' 'GANS' 'Keyphrase extraction'
|
112 |
+
'classical machine learning' 'deep learning methods'
|
113 |
+
'keyphrase extraction' 'linguistics' 'recurrent neural networks'
|
114 |
+
'semantics' 'statistics' 'text analysis' 'transformers']
|
115 |
+
```
|
116 |
+
|
117 |
+
## π Training Dataset
|
118 |
+
Inspec is a keyphrase extraction/generation dataset consisting of 2000 English scientific papers from the scientific domains of Computers and Control and Information Technology published between 1998 to 2002. The keyphrases are annotated by professional indexers or editors.
|
119 |
+
|
120 |
+
You can find more information here: https://huggingface.co/datasets/midas/inspec
|
121 |
+
|
122 |
+
## π·ββοΈ Training procedure
|
123 |
+
For more in detail information, you can take a look at the training notebook (link incoming).
|
124 |
+
|
125 |
+
### Training parameters
|
126 |
+
|
127 |
+
| Parameter | Value |
|
128 |
+
| --------- | ------|
|
129 |
+
| Learning Rate | 1e-4 |
|
130 |
+
| Epochs | 50 |
|
131 |
+
| Early Stopping Patience | 3 |
|
132 |
+
|
133 |
+
### Preprocessing
|
134 |
+
The documents in the dataset are already preprocessed into list of words with the corresponding labels. The only thing that must be done is tokenization and the realignment of the labels so that they correspond with the right subword tokens.
|
135 |
+
```python
|
136 |
+
# Labels
|
137 |
+
label_list = ["B", "I", "O"]
|
138 |
+
lbl2idx = {"B": 0, "I": 1, "O": 2}
|
139 |
+
idx2label = {0: "B", 1: "I", 2: "O"}
|
140 |
+
|
141 |
+
def preprocess_fuction(all_samples_per_split):
|
142 |
+
tokenized_samples = tokenizer.batch_encode_plus(
|
143 |
+
all_samples_per_split[dataset_document_column],
|
144 |
+
padding="max_length",
|
145 |
+
truncation=True,
|
146 |
+
is_split_into_words=True,
|
147 |
+
max_length=max_length,
|
148 |
+
)
|
149 |
+
total_adjusted_labels = []
|
150 |
+
for k in range(0, len(tokenized_samples["input_ids"])):
|
151 |
+
prev_wid = -1
|
152 |
+
word_ids_list = tokenized_samples.word_ids(batch_index=k)
|
153 |
+
existing_label_ids = all_samples_per_split[dataset_biotags_column][k]
|
154 |
+
i = -1
|
155 |
+
adjusted_label_ids = []
|
156 |
+
|
157 |
+
for wid in word_ids_list:
|
158 |
+
if wid is None:
|
159 |
+
adjusted_label_ids.append(lbl2idx["O"])
|
160 |
+
elif wid != prev_wid:
|
161 |
+
i = i + 1
|
162 |
+
adjusted_label_ids.append(lbl2idx[existing_label_ids[i]])
|
163 |
+
prev_wid = wid
|
164 |
+
else:
|
165 |
+
adjusted_label_ids.append(
|
166 |
+
lbl2idx[
|
167 |
+
f"{'I' if existing_label_ids[i] == 'B' else existing_label_ids[i]}"
|
168 |
+
]
|
169 |
+
)
|
170 |
+
|
171 |
+
total_adjusted_labels.append(adjusted_label_ids)
|
172 |
+
tokenized_samples["labels"] = total_adjusted_labels
|
173 |
+
return tokenized_samples
|
174 |
+
```
|
175 |
+
|
176 |
+
### Postprocessing
|
177 |
+
For the post-processing, you will need to filter out the B and I labeled tokens and concat the consecutive B and Is. As last you strip the keyphrase to ensure all spaces are removed.
|
178 |
+
```python
|
179 |
+
# Define post_process functions
|
180 |
+
def concat_tokens_by_tag(keyphrases):
|
181 |
+
keyphrase_tokens = []
|
182 |
+
for id, label in keyphrases:
|
183 |
+
if label == "B":
|
184 |
+
keyphrase_tokens.append([id])
|
185 |
+
elif label == "I":
|
186 |
+
if len(keyphrase_tokens) > 0:
|
187 |
+
keyphrase_tokens[len(keyphrase_tokens) - 1].append(id)
|
188 |
+
return keyphrase_tokens
|
189 |
+
|
190 |
+
|
191 |
+
def extract_keyphrases(example, predictions, tokenizer, index=0):
|
192 |
+
keyphrases_list = [
|
193 |
+
(id, idx2label[label])
|
194 |
+
for id, label in zip(
|
195 |
+
np.array(example["input_ids"]).squeeze().tolist(), predictions[index]
|
196 |
+
)
|
197 |
+
if idx2label[label] in ["B", "I"]
|
198 |
+
]
|
199 |
+
|
200 |
+
processed_keyphrases = concat_tokens_by_tag(keyphrases_list)
|
201 |
+
extracted_kps = tokenizer.batch_decode(
|
202 |
+
processed_keyphrases,
|
203 |
+
skip_special_tokens=True,
|
204 |
+
clean_up_tokenization_spaces=True,
|
205 |
+
)
|
206 |
+
return np.unique([kp.strip() for kp in extracted_kps])
|
207 |
+
|
208 |
+
```
|
209 |
+
## π Evaluation results
|
210 |
+
|
211 |
+
One of the traditional evaluation methods is the precision, recall and F1-score @k,m where k is the number that stands for the first k predicted keyphrases and m for the average amount of predicted keyphrases.
|
212 |
+
The model achieves the following results on the Inspec test set:
|
213 |
+
|
214 |
+
| Dataset | P@5 | R@5 | F1@5 | P@10 | R@10 | F1@10 | P@M | R@M | F1@M |
|
215 |
+
|:-----------------:|:----:|:----:|:----:|:----:|:----:|:-----:|:----:|:----:|:----:|
|
216 |
+
| Inspec Test Set | 0.45 | 0.40 | 0.39 | 0.33 | 0.53 | 0.38 | 0.47 | 0.57 | 0.49 |
|
217 |
+
|
218 |
+
For more information on the evaluation process, you can take a look at the keyphrase extraction evaluation notebook.
|
219 |
+
|
220 |
+
## π¨ Issues
|
221 |
+
Please feel free to contact Thomas De Decker for any problems with this model.
|