Upload keyword_pubmed.py
Browse files- keyword_pubmed.py +135 -0
keyword_pubmed.py
ADDED
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
""" Loading script for the Keyword PubMed dataset."""
|
2 |
+
|
3 |
+
|
4 |
+
import os
|
5 |
+
from pathlib import Path
|
6 |
+
import re
|
7 |
+
|
8 |
+
import datasets
|
9 |
+
|
10 |
+
|
11 |
+
class KeywordPubmedDataset(datasets.GeneratorBasedBuilder):
|
12 |
+
|
13 |
+
VERSION = datasets.Version("1.0.0")
|
14 |
+
|
15 |
+
|
16 |
+
|
17 |
+
BUILDER_CONFIGS = [
|
18 |
+
datasets.BuilderConfig(name="sentence", version=VERSION, description="Comprises sentences that contain a keyword"),
|
19 |
+
datasets.BuilderConfig(name="document", version=VERSION, description="Contains all the sentences in a document that contains at least a keyword"),
|
20 |
+
]
|
21 |
+
|
22 |
+
DEFAULT_CONFIG_NAME = "document" # It's not mandatory to have a default configuration. Just use one if it make sense.
|
23 |
+
|
24 |
+
def _info(self):
|
25 |
+
if self.config.name == "sentence":
|
26 |
+
features = datasets.Features(
|
27 |
+
{
|
28 |
+
"sentence": datasets.Value("string"),
|
29 |
+
"pmcid": datasets.Value("string"),
|
30 |
+
"keyword_rank": datasets.Value("int32"),
|
31 |
+
}
|
32 |
+
)
|
33 |
+
else:
|
34 |
+
features = datasets.Features(
|
35 |
+
{
|
36 |
+
"sentence": datasets.Value("string"),
|
37 |
+
"pmcid": datasets.Value("string"),
|
38 |
+
"keyword_rank": datasets.Value("int32"),
|
39 |
+
}
|
40 |
+
)
|
41 |
+
return datasets.DatasetInfo(
|
42 |
+
# This is the description that will appear on the datasets page.
|
43 |
+
description= "Dataset for MLM comprising sentences that contain a keyword relevant to the domain",
|
44 |
+
# This defines the different columns of the dataset and their types
|
45 |
+
features=features, # Here we define them above because they are different between the two configurations
|
46 |
+
# If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
|
47 |
+
# specify them. They'll be used if as_supervised=True in builder.as_dataset.
|
48 |
+
# supervised_keys=("sentence", "label"),
|
49 |
+
# Homepage of the dataset for documentation
|
50 |
+
# homepage=_HOMEPAGE,
|
51 |
+
# License for the dataset if available
|
52 |
+
# license=_LICENSE,
|
53 |
+
# Citation for the dataset
|
54 |
+
# citation=_CITATION,
|
55 |
+
)
|
56 |
+
|
57 |
+
def _split_generators(self, dl_manager):
|
58 |
+
|
59 |
+
|
60 |
+
if self.config.data_dir:
|
61 |
+
data_dir = self.config.data_dir
|
62 |
+
else:
|
63 |
+
data_dir = dl_manager.download_and_extract('data_files.tar.gz')
|
64 |
+
|
65 |
+
# Load the keywords from the file
|
66 |
+
with open(os.path.join(data_dir, 'keywords.txt'), 'r') as f:
|
67 |
+
keyword_ranks = {line.strip().split(":")[0].lower():rank for rank, line in enumerate(f)}
|
68 |
+
keywords = set(keyword_ranks.keys())
|
69 |
+
|
70 |
+
return [
|
71 |
+
datasets.SplitGenerator(
|
72 |
+
name=datasets.Split.TRAIN,
|
73 |
+
# These kwargs will be passed to _generate_examples
|
74 |
+
gen_kwargs={
|
75 |
+
"dirpath": os.path.join(data_dir, "train"),
|
76 |
+
"keywords": keywords,
|
77 |
+
"ranks": keyword_ranks,
|
78 |
+
},
|
79 |
+
),
|
80 |
+
|
81 |
+
datasets.SplitGenerator(
|
82 |
+
name=datasets.Split.VALIDATION,
|
83 |
+
# These kwargs will be passed to _generate_examples
|
84 |
+
gen_kwargs={
|
85 |
+
"dirpath": os.path.join(data_dir, "dev"),
|
86 |
+
"keywords": keywords,
|
87 |
+
"ranks": keyword_ranks,
|
88 |
+
},
|
89 |
+
),
|
90 |
+
]
|
91 |
+
|
92 |
+
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
93 |
+
def _generate_examples(self, dirpath, keywords, ranks):
|
94 |
+
item_ix = 0
|
95 |
+
for filepath in Path(dirpath).iterdir():
|
96 |
+
filepath = Path(filepath)
|
97 |
+
if filepath.suffix == ".txt":
|
98 |
+
pmcid = filepath.name.split(".")[0]
|
99 |
+
with filepath.open(encoding="utf-8") as f:
|
100 |
+
for sentence in f:
|
101 |
+
sentence = sentence.strip()
|
102 |
+
if sentence: # Ignore blanks
|
103 |
+
sentence = re.sub("\s+", " ", sentence)
|
104 |
+
has_keyword, rank = self._has_keyword(sentence, keywords, ranks)
|
105 |
+
if self.config.name == "sentence":
|
106 |
+
# Yields examples as (key, example) tuples
|
107 |
+
if has_keyword:
|
108 |
+
yield item_ix, {
|
109 |
+
"sentence": sentence,
|
110 |
+
"keyword_rank": rank,
|
111 |
+
"pmcid": pmcid
|
112 |
+
}
|
113 |
+
item_ix += 1
|
114 |
+
|
115 |
+
else: # Else document
|
116 |
+
yield item_ix, {
|
117 |
+
"sentence": sentence,
|
118 |
+
"keyword_rank": rank,
|
119 |
+
"pmcid": pmcid
|
120 |
+
}
|
121 |
+
item_ix += 1
|
122 |
+
|
123 |
+
def _has_keyword(self, sentence, keywords, ranks):
|
124 |
+
# Lowercase and split the sentence
|
125 |
+
words = sentence.lower().split()
|
126 |
+
# Check every word until it finds a keyword
|
127 |
+
for word in words:
|
128 |
+
if word in keywords:
|
129 |
+
return True, ranks[word]
|
130 |
+
return False, -1
|
131 |
+
|
132 |
+
|
133 |
+
if __name__ == "__main__":
|
134 |
+
ds = KeywordPubmedDataset()
|
135 |
+
ds.download_and_prepare()
|