File size: 11,759 Bytes
626eca0 |
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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
import json
from pathlib import Path
from typing import Dict, List, Optional, Set, Union
import transformers as tr
class Labels:
"""
Class that contains the labels for a model.
Args:
_labels_to_index (:obj:`Dict[str, Dict[str, int]]`):
A dictionary from :obj:`str` to :obj:`int`.
_index_to_labels (:obj:`Dict[str, Dict[int, str]]`):
A dictionary from :obj:`int` to :obj:`str`.
"""
def __init__(
self,
_labels_to_index: Dict[str, Dict[str, int]] = None,
_index_to_labels: Dict[str, Dict[int, str]] = None,
**kwargs,
):
self._labels_to_index = _labels_to_index or {"labels": {}}
self._index_to_labels = _index_to_labels or {"labels": {}}
# if _labels_to_index is not empty and _index_to_labels is not provided
# to the constructor, build the inverted label dictionary
if not _index_to_labels and _labels_to_index:
for namespace in self._labels_to_index:
self._index_to_labels[namespace] = {
v: k for k, v in self._labels_to_index[namespace].items()
}
def get_index_from_label(self, label: str, namespace: str = "labels") -> int:
"""
Returns the index of a literal label.
Args:
label (:obj:`str`):
The string representation of the label.
namespace (:obj:`str`, optional, defaults to ``labels``):
The namespace where the label belongs, e.g. ``roles`` for a SRL task.
Returns:
:obj:`int`: The index of the label.
"""
if namespace not in self._labels_to_index:
raise ValueError(
f"Provided namespace `{namespace}` is not in the label dictionary."
)
if label not in self._labels_to_index[namespace]:
raise ValueError(f"Provided label {label} is not in the label dictionary.")
return self._labels_to_index[namespace][label]
def get_label_from_index(self, index: int, namespace: str = "labels") -> str:
"""
Returns the string representation of the label index.
Args:
index (:obj:`int`):
The index of the label.
namespace (:obj:`str`, optional, defaults to ``labels``):
The namespace where the label belongs, e.g. ``roles`` for a SRL task.
Returns:
:obj:`str`: The string representation of the label.
"""
if namespace not in self._index_to_labels:
raise ValueError(
f"Provided namespace `{namespace}` is not in the label dictionary."
)
if index not in self._index_to_labels[namespace]:
raise ValueError(
f"Provided label `{index}` is not in the label dictionary."
)
return self._index_to_labels[namespace][index]
def add_labels(
self,
labels: Union[str, List[str], Set[str], Dict[str, int]],
namespace: str = "labels",
) -> List[int]:
"""
Adds the labels in input in the label dictionary.
Args:
labels (:obj:`str`, :obj:`List[str]`, :obj:`Set[str]`):
The labels (single label, list of labels or set of labels) to add to the dictionary.
namespace (:obj:`str`, optional, defaults to ``labels``):
Namespace where the labels belongs.
Returns:
:obj:`List[int]`: The index of the labels just inserted.
"""
if isinstance(labels, dict):
self._labels_to_index[namespace] = labels
self._index_to_labels[namespace] = {
v: k for k, v in self._labels_to_index[namespace].items()
}
# normalize input
if isinstance(labels, (str, list)):
labels = set(labels)
# if new namespace, add to the dictionaries
if namespace not in self._labels_to_index:
self._labels_to_index[namespace] = {}
self._index_to_labels[namespace] = {}
# returns the new indices
return [self._add_label(label, namespace) for label in labels]
def _add_label(self, label: str, namespace: str = "labels") -> int:
"""
Adds the label in input in the label dictionary.
Args:
label (:obj:`str`):
The label to add to the dictionary.
namespace (:obj:`str`, optional, defaults to ``labels``):
Namespace where the label belongs.
Returns:
:obj:`List[int]`: The index of the label just inserted.
"""
if label not in self._labels_to_index[namespace]:
index = len(self._labels_to_index[namespace])
self._labels_to_index[namespace][label] = index
self._index_to_labels[namespace][index] = label
return index
else:
return self._labels_to_index[namespace][label]
def get_labels(self, namespace: str = "labels") -> Dict[str, int]:
"""
Returns all the labels that belongs to the input namespace.
Args:
namespace (:obj:`str`, optional, defaults to ``labels``):
Labels namespace to retrieve.
Returns:
:obj:`Dict[str, int]`: The label dictionary, from ``str`` to ``int``.
"""
if namespace not in self._labels_to_index:
raise ValueError(
f"Provided namespace `{namespace}` is not in the label dictionary."
)
return self._labels_to_index[namespace]
def get_label_size(self, namespace: str = "labels") -> int:
"""
Returns the number of the labels in the namespace dictionary.
Args:
namespace (:obj:`str`, optional, defaults to ``labels``):
Labels namespace to retrieve.
Returns:
:obj:`int`: Number of labels.
"""
if namespace not in self._labels_to_index:
raise ValueError(
f"Provided namespace `{namespace}` is not in the label dictionary."
)
return len(self._labels_to_index[namespace])
def get_namespaces(self) -> List[str]:
"""
Returns all the namespaces in the label dictionary.
Returns:
:obj:`List[str]`: The namespaces in the label dictionary.
"""
return list(self._labels_to_index.keys())
@classmethod
def from_file(cls, file_path: Union[str, Path, dict], **kwargs):
with open(file_path, "r") as f:
labels_to_index = json.load(f)
return cls(labels_to_index, **kwargs)
def save(self, file_path: Union[str, Path, dict], **kwargs):
with open(file_path, "w") as f:
json.dump(self._labels_to_index, f, indent=2)
class PassageManager:
def __init__(
self,
tokenizer: Optional[tr.PreTrainedTokenizer] = None,
passages: Optional[Union[Dict[str, Dict[str, int]], Labels, List[str]]] = None,
lazy: bool = True,
**kwargs,
):
if passages is None:
self.passages = Labels()
elif isinstance(passages, Labels):
self.passages = passages
elif isinstance(passages, dict):
self.passages = Labels(passages)
elif isinstance(passages, list):
self.passages = Labels()
self.passages.add_labels(passages)
else:
raise ValueError(
"`passages` should be either a Labels object or a dictionary."
)
self.tokenizer = tokenizer
self.lazy = lazy
self._tokenized_passages = {}
if not self.lazy:
self._tokenize_passages(self.passages)
def __len__(self) -> int:
return self.passages.get_label_size()
def get_index_from_passage(self, passage: str) -> int:
"""
Returns the index of the passage in input.
Args:
passage (:obj:`str`):
The passage to get the index from.
Returns:
:obj:`int`: The index of the passage.
"""
return self.passages.get_index_from_label(passage)
def get_passage_from_index(self, index: int) -> str:
""" "
Returns the passage from the index in input.
Args:
index (:obj:`int`):
The index to get the passage from.
Returns:
:obj:`str`: The passage.
"""
return self.passages.get_label_from_index(index)
def add_passages(
self,
passages: Union[str, List[str], Set[str], Dict[str, int]],
lazy: Optional[bool] = None,
) -> List[int]:
"""
Adds the passages in input in the passage dictionary.
Args:
passages (:obj:`str`, :obj:`List[str]`, :obj:`Set[str]`, :obj:`Dict[str, int]`):
The passages (single passage, list of passages, set of passages or dictionary of passages) to add to the dictionary.
lazy (:obj:`bool`, optional, defaults to ``None``):
Whether to tokenize the passages right away or not.
Returns:
:obj:`List[int]`: The index of the passages just inserted.
"""
return self.passages.add_labels(passages)
def get_passages(self) -> Dict[str, int]:
"""
Returns all the passages in the passage dictionary.
Returns:
:obj:`Dict[str, int]`: The passage dictionary, from ``str`` to ``int``.
"""
return self.passages.get_labels()
def get_tokenized_passage(
self, passage: Union[str, int], force_tokenize: bool = False, **kwargs
) -> Dict:
"""
Returns the tokenized passage in input.
Args:
passage (:obj:`Union[str, int]`):
The passage to tokenize.
force_tokenize (:obj:`bool`, optional, defaults to ``False``):
Whether to force the tokenization of the passage or not.
kwargs:
Additional keyword arguments to pass to the tokenizer.
Returns:
:obj:`Dict`: The tokenized passage.
"""
passage_index: Optional[int] = None
passage_str: Optional[str] = None
if isinstance(passage, str):
passage_index = self.passages.get_index_from_label(passage)
passage_str = passage
elif isinstance(passage, int):
passage_index = passage
passage_str = self.passages.get_label_from_index(passage)
else:
raise ValueError(
f"`passage` should be either a `str` or an `int`. Provided type: {type(passage)}."
)
if passage_index not in self._tokenized_passages or force_tokenize:
self._tokenized_passages[passage_index] = self.tokenizer(
passage_str, **kwargs
)
return self._tokenized_passages[passage_index]
def _tokenize_passages(self, **kwargs):
for passage in self.passages.get_labels():
self.get_tokenized_passage(passage, **kwargs)
def tokenize(self, text: Union[str, List[str]], **kwargs):
"""
Tokenizes the text in input using the tokenizer.
Args:
text (:obj:`str`, :obj:`List[str]`):
The text to tokenize.
**kwargs:
Additional keyword arguments to pass to the tokenizer.
Returns:
:obj:`List[str]`: The tokenized text.
"""
if self.tokenizer is None:
raise ValueError(
"No tokenizer was provided. Please provide a tokenizer to the passageManager."
)
return self.tokenizer(text, **kwargs)
|