MorphoDataset
The MorphoDataset
class loads a morphological dataset in a vertical format.
- The data consists of three datasets
train
dev
test
- Each dataset is a torch.utils.data.Dataset providing
__len__
: number of sentences in the dataset__getitem__
: return the requested sentence as anElement
instance, which is a dictionary with keys "words"/"lemmas"/"tags", each being a list of stringswords
,lemmas
,tags
: instances of typeFactor
containing the following fields:strings
: a Python list containing input sentences, each being a list of strings (words/lemmas/tags)string_vocab
: a npfl138.Vocabulary object capable of mapping words to indices. It is constructed on the train set and shared by the dev and test setschar_vocab
: a npfl138.Vocabulary object capable of mapping characters to indices. It is constructed on the train set and shared by the dev and test sets
cle_batch
: a method for creating inputs for character-level embeddings. It takes a list of sentences, each being a list of string words, and produces a tuple of two tensors:unique_words
with shape[num_unique_words, max_word_length]
containing each unique word as a sequence of character idswords_indices
with shape[num_sentences, max_sentence_length]
containing for every word its index inunique_words
npfl138.datasets.morpho_dataset.MorphoDataset
Source code in npfl138/datasets/morpho_dataset.py
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 |
|
PAD
class-attribute
instance-attribute
PAD: int = 0
The index of the padding token in the vocabulary, always present.
UNK
class-attribute
instance-attribute
UNK: int = 1
The index of the unknown token in the vocabulary, always present.
BOW
class-attribute
instance-attribute
BOW: int = 2
A special beginning-of-word token, always present in character vocabularies.
EOW
class-attribute
instance-attribute
EOW: int = 3
A special end-of-word token, always present in character vocabularies.
Element
class-attribute
instance-attribute
The type of a single dataset element, i.e., a single sentence.
Factor
A factor of the dataset, i.e., words, lemmas or tags.
Source code in npfl138/datasets/morpho_dataset.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
Dataset
Bases: Dataset
Source code in npfl138/datasets/morpho_dataset.py
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 |
|
__len__
__len__() -> int
Return the number of sentences in the dataset.
Source code in npfl138/datasets/morpho_dataset.py
134 135 136 |
|
__getitem__
Return the index
-th element of the dataset as a dictionary.
Source code in npfl138/datasets/morpho_dataset.py
138 139 140 141 142 |
|
cle_batch
Create a batch suitable for computation of character-level word embeddings.
Parameters:
Returns:
-
unique_words
(Tensor
) –A tensor with shape
[num_unique_words, max_word_length]
containing each unique word as a sequence of character ids. -
words_indices
(Tensor
) –A tensor with shape
[num_sentences, max_sentence_length]
containing for every word from the batch its index inunique_words
.
Source code in npfl138/datasets/morpho_dataset.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
|
cle_batch_packed
cle_batch_packed(
words: list[list[str]],
) -> tuple[PackedSequence, PackedSequence]
Create a batch suitable for computation of character-level word embeddings.
This function is very similar to cle_batch
, but it returns packed sequences instead
of padded sequences.
Parameters:
Returns:
-
unique_words
(PackedSequence
) –A PackedSequence containing each unique word as a sequence of character ids.
-
words_indices
(PackedSequence
) –A PackedSequence containing for every word from
-
tuple[PackedSequence, PackedSequence]
–the batch its index in
unique_words
.
Source code in npfl138/datasets/morpho_dataset.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
|
__init__
__init__(dataset, max_sentences=None)
Load the dataset
dataset, downloading it if necessary.
Parameters:
-
dataset
–The name of the dataset, for example
czech_pdt
. -
max_sentences
–The maximum number of sentences to load.
Source code in npfl138/datasets/morpho_dataset.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
|
evaluate
staticmethod
Evaluate the predictions
against the gold dataset.
Returns:
-
accuracy
(float
) –The accuracy of the predictions in percentages.
Source code in npfl138/datasets/morpho_dataset.py
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 |
|
evaluate_file
staticmethod
Evaluate the file with predictions against the gold dataset.
Returns:
-
accuracy
(float
) –The accuracy of the predictions in percentages.
Source code in npfl138/datasets/morpho_dataset.py
251 252 253 254 255 256 257 258 259 |
|