-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyphrase_dataset.py
73 lines (59 loc) · 2.22 KB
/
keyphrase_dataset.py
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
import json
import os
class KeyphraseDataset:
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super(KeyphraseDataset, cls).__new__(cls)
return cls._instance
def __init__(self):
if not hasattr(self, 'initialized'):
if not os.path.isfile("keyphrase_set.json"):
KeyphraseDataset.download_set()
with open("keyphrase_set.json", 'r') as file:
data = json.load(file)
self.train_set=data['train']
self.validation_set=data['validation']
self.test_set=data['test']
self.initialized = True
@staticmethod
def download_set():
dataset = load_dataset("midas/kptimes", "raw")
train_set = KeyphraseDataset.extract_dataset(dataset["train"])
validation_set = KeyphraseDataset.extract_dataset(dataset["validation"])
test_set = KeyphraseDataset.extract_dataset(dataset["test"])
combined_set ={
'train': train_set,
'validation': validation_set,
'test': test_set
}
with open('keyphrase_set.json', 'w') as file:
json.dump(combined_set, file, indent=4)
@staticmethod
def check_length(text):
return len(text) >= 400 and len(text) <= 2000
@staticmethod
def extract_dataset(dataset):
new_set = []
for i in range(len(dataset)):
sample = dataset[i]
text = " ".join(sample["document"])
if not KeyphraseDataset.check_length(text):
continue
label = sample["extractive_keyphrases"] + sample["abstractive_keyphrases"]
if len(label)<2:
continue
new_set.append({
'text': text,
'label': label
})
return new_set
def get_samples(self, amount=50, type="train"):
if type == "train":
full_set = self.train_set
elif type == "validation":
full_set = self.validation_set
else:
full_set = self.test_set
assert len(full_set) >= amount, "Not enough samples in the dataset"
return full_set[:amount]