This repository was archived by the owner on Sep 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 814
Experimental sequence tagging datasets #805
Merged
zhangguanheng66
merged 32 commits into
pytorch:master
from
akurniawan:new_sequence_tagging
Jun 22, 2020
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
3ab5c1e
Merge pull request #1 from pytorch/master
akurniawan b068312
add raw for sequence tagging
41b975f
WIP sequence tagging dataset
49dec4b
add specialized function to handle None case
3f988b1
expose raw datasets for sequence tagging
27bbeb7
finalized sequence tagging dataset
a99723f
add documentation
b7ba5b0
expose sequence tagging data
36d4652
add unit test for sequence tagging
b79839c
fix linting
a03eec4
remove filename arguments
481ea37
[WIP] adding conll test
8eeeff9
Merge branch 'master' of https://github.com/pytorch/text into new_seq…
fb3f8f5
move the test order with translation dataset and finalize conll testing
9a84a8a
add doc string for sequence tagging dataset
9d11bc1
remove spaces at the end of the file
b1c5ec4
reformat docstring
c38a0b8
remove tokenizer
51f7fbf
Merge branch 'master' of https://github.com/pytorch/text into new_seq…
12d4482
fix linting
247a14c
Merge branch 'master' of https://github.com/pytorch/text into new_seq…
0aad1c4
add cases where we don't have blank by the end of the file
b662081
- add validation for data_select
899f872
Merge branch 'master' of github.com:akurniawan/text into new_sequence…
a0ec2e7
Merge branch 'new_sequence_tagging' of github.com:akurniawan/text int…
13864ea
modify method name
e3d4256
add "valid" to data_select option validation
351ad46
add todo for assert_allclose
73ce74a
remove duplicate validation for transforms function
0358f2c
Merge branch 'master' of https://github.com/pytorch/text into new_seq…
e4ba11c
replace assert_allclose with self.assertEqual
2d01b15
Merge branch 'master' of https://github.com/pytorch/text into new_seq…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
torchtext/experimental/datasets/raw/sequence_tagging.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import torch | ||
|
|
||
| from torchtext.utils import download_from_url, extract_archive | ||
|
|
||
| URLS = { | ||
| "UDPOS": | ||
| 'https://bitbucket.org/sivareddyg/public/downloads/en-ud-v2.zip', | ||
| "CoNLL2000Chunking": [ | ||
| 'https://www.clips.uantwerpen.be/conll2000/chunking/train.txt.gz', | ||
| 'https://www.clips.uantwerpen.be/conll2000/chunking/test.txt.gz' | ||
| ] | ||
| } | ||
|
|
||
|
|
||
| def _create_data_from_iob(data_path, separator="\t"): | ||
| with open(data_path, encoding="utf-8") as input_file: | ||
| columns = [] | ||
| for line in input_file: | ||
| line = line.strip() | ||
| if line == "": | ||
| if columns: | ||
| yield columns | ||
| columns = [] | ||
| else: | ||
| for i, column in enumerate(line.split(separator)): | ||
| if len(columns) < i + 1: | ||
| columns.append([]) | ||
| columns[i].append(column) | ||
| if len(columns) > 0: | ||
| yield columns | ||
|
|
||
|
|
||
| def _construct_filepath(paths, file_suffix): | ||
| if file_suffix: | ||
| path = None | ||
| for p in paths: | ||
| path = p if p.endswith(file_suffix) else path | ||
| return path | ||
| return None | ||
|
|
||
|
|
||
| def _setup_datasets(dataset_name, separator, root=".data"): | ||
|
|
||
| extracted_files = [] | ||
| if isinstance(URLS[dataset_name], list): | ||
| for f in URLS[dataset_name]: | ||
| dataset_tar = download_from_url(f, root=root) | ||
| extracted_files.extend(extract_archive(dataset_tar)) | ||
| elif isinstance(URLS[dataset_name], str): | ||
| dataset_tar = download_from_url(URLS[dataset_name], root=root) | ||
| extracted_files.extend(extract_archive(dataset_tar)) | ||
| else: | ||
| raise ValueError( | ||
| "URLS for {} has to be in a form or list or string".format( | ||
| dataset_name)) | ||
|
|
||
| data_filenames = { | ||
| "train": _construct_filepath(extracted_files, "train.txt"), | ||
| "valid": _construct_filepath(extracted_files, "dev.txt"), | ||
| "test": _construct_filepath(extracted_files, "test.txt") | ||
| } | ||
|
|
||
| datasets = [] | ||
| for key in data_filenames.keys(): | ||
| if data_filenames[key] is not None: | ||
| datasets.append( | ||
| RawSequenceTaggingIterableDataset( | ||
| _create_data_from_iob(data_filenames[key], separator))) | ||
| else: | ||
| datasets.append(None) | ||
|
|
||
| return datasets | ||
|
|
||
|
|
||
| class RawSequenceTaggingIterableDataset(torch.utils.data.IterableDataset): | ||
| """Defines an abstraction for raw text sequence tagging iterable datasets. | ||
| """ | ||
| def __init__(self, iterator): | ||
| super(RawSequenceTaggingIterableDataset).__init__() | ||
|
|
||
| self._iterator = iterator | ||
| self.has_setup = False | ||
| self.start = 0 | ||
| self.num_lines = None | ||
|
|
||
| def setup_iter(self, start=0, num_lines=None): | ||
| self.start = start | ||
| self.num_lines = num_lines | ||
| self.has_setup = True | ||
|
|
||
| def __iter__(self): | ||
| if not self.has_setup: | ||
| self.setup_iter() | ||
|
|
||
| for i, item in enumerate(self._iterator): | ||
| if i >= self.start: | ||
| yield item | ||
| if (self.num_lines is not None) and (i == (self.start + | ||
| self.num_lines)): | ||
| break | ||
|
|
||
| def get_iterator(self): | ||
| return self._iterator | ||
|
|
||
|
|
||
| def UDPOS(*args, **kwargs): | ||
| """ Universal Dependencies English Web Treebank | ||
|
|
||
| Separately returns the training and test dataset | ||
|
|
||
| Arguments: | ||
| root: Directory where the datasets are saved. Default: ".data" | ||
|
|
||
| Examples: | ||
| >>> from torchtext.datasets.raw import UDPOS | ||
| >>> train_dataset, valid_dataset, test_dataset = UDPOS() | ||
| """ | ||
| return _setup_datasets(*(("UDPOS", "\t") + args), **kwargs) | ||
|
|
||
|
|
||
| def CoNLL2000Chunking(*args, **kwargs): | ||
| """ CoNLL 2000 Chunking Dataset | ||
|
|
||
| Separately returns the training and test dataset | ||
|
|
||
| Arguments: | ||
| root: Directory where the datasets are saved. Default: ".data" | ||
|
|
||
| Examples: | ||
| >>> from torchtext.datasets.raw import CoNLL2000Chunking | ||
| >>> train_dataset, valid_dataset, test_dataset = CoNLL2000Chunking() | ||
| """ | ||
| return _setup_datasets(*(("CoNLL2000Chunking", " ") + args), **kwargs) | ||
|
|
||
|
|
||
| DATASETS = {"UDPOS": UDPOS, "CoNLL2000Chunking": CoNLL2000Chunking} | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we return or yield something from this func?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, please take a look at line 22. due to the nature of the data, each sentences is separated with an empty line, therefore we will return one sentence if we found one. and I just add new commit to return leftovers