enoriega commited on
Commit
3a4dbfa
1 Parent(s): 0a900fd

Updated the schema of the dataset to contain the indices of the words that are keywords

Browse files
Files changed (1) hide show
  1. keyword_pubmed.py +18 -8
keyword_pubmed.py CHANGED
@@ -10,7 +10,7 @@ import datasets
10
 
11
  class KeywordPubmedDataset(datasets.GeneratorBasedBuilder):
12
 
13
- VERSION = datasets.Version("1.0.0")
14
 
15
 
16
 
@@ -28,6 +28,7 @@ class KeywordPubmedDataset(datasets.GeneratorBasedBuilder):
28
  "sentence": datasets.Value("string"),
29
  "pmcid": datasets.Value("string"),
30
  "keyword_rank": datasets.Value("int32"),
 
31
  }
32
  )
33
  else:
@@ -36,6 +37,7 @@ class KeywordPubmedDataset(datasets.GeneratorBasedBuilder):
36
  "sentence": datasets.Value("string"),
37
  "pmcid": datasets.Value("string"),
38
  "keyword_rank": datasets.Value("int32"),
 
39
  }
40
  )
41
  return datasets.DatasetInfo(
@@ -101,14 +103,16 @@ class KeywordPubmedDataset(datasets.GeneratorBasedBuilder):
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
 
@@ -116,18 +120,24 @@ class KeywordPubmedDataset(datasets.GeneratorBasedBuilder):
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__":
 
10
 
11
  class KeywordPubmedDataset(datasets.GeneratorBasedBuilder):
12
 
13
+ VERSION = datasets.Version("1.1.0")
14
 
15
 
16
 
 
28
  "sentence": datasets.Value("string"),
29
  "pmcid": datasets.Value("string"),
30
  "keyword_rank": datasets.Value("int32"),
31
+ "keyword_indices": datasets.Sequence(datasets.Value("int32"))
32
  }
33
  )
34
  else:
 
37
  "sentence": datasets.Value("string"),
38
  "pmcid": datasets.Value("string"),
39
  "keyword_rank": datasets.Value("int32"),
40
+ "keyword_indices": datasets.Sequence(datasets.Value("int32"))
41
  }
42
  )
43
  return datasets.DatasetInfo(
 
103
  sentence = sentence.strip()
104
  if sentence: # Ignore blanks
105
  sentence = re.sub("\s+", " ", sentence)
106
+ kw_indices, rank = self._keyword_indices(sentence, keywords, ranks)
107
+ has_keyword = rank > -1
108
  if self.config.name == "sentence":
109
  # Yields examples as (key, example) tuples
110
  if has_keyword:
111
  yield item_ix, {
112
  "sentence": sentence,
113
  "keyword_rank": rank,
114
+ "pmcid": pmcid,
115
+ "keyword_indices": kw_indices
116
  }
117
  item_ix += 1
118
 
 
120
  yield item_ix, {
121
  "sentence": sentence,
122
  "keyword_rank": rank,
123
+ "pmcid": pmcid,
124
+ "keyword_indices": kw_indices
125
  }
126
  item_ix += 1
127
 
128
+ def _keyword_indices(self, sentence, keywords, ranks):
129
  # Lowercase and split the sentence
130
  words = sentence.lower().split()
131
+ indices = list()
132
+ top_rank = -1
133
  # Check every word until it finds a keyword
134
+ for w_ix, word in enumerate(words):
135
  if word in keywords:
136
+ indices.append(w_ix)
137
+ rank = ranks[word]
138
+ if rank < top_rank or top_rank == -1:
139
+ top_rank = rank
140
+ return indices, top_rank
141
 
142
 
143
  if __name__ == "__main__":