Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IMDB data fetcher #410

Merged
merged 1 commit into from
Jan 22, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion flair/data_fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,13 @@ def load_classification_corpus(

sentences_train: List[Sentence] = NLPTaskDataFetcher.read_text_classification_file(train_file)
sentences_test: List[Sentence] = NLPTaskDataFetcher.read_text_classification_file(test_file)
sentences_dev: List[Sentence] = NLPTaskDataFetcher.read_text_classification_file(dev_file)

if dev_file is not None:
sentences_dev: List[Sentence] = NLPTaskDataFetcher.read_text_classification_file(dev_file)
else:
sentences_dev: List[Sentence] = [sentences_train[i] for i in
NLPTaskDataFetcher.__sample(len(sentences_train), 0.1)]
sentences_train = [x for x in sentences_train if x not in sentences_dev]

return TaggedCorpus(sentences_train, sentences_dev, sentences_test)

Expand Down Expand Up @@ -560,6 +566,29 @@ def download_dataset(task: NLPTask):
f_in.extract(corpus_file, data_path)
shutil.move(f'{data_path}/{corpus_file}', data_path)

if task == NLPTask.IMDB:
imdb_acl_path = 'http://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz'
data_path = Path(flair.file_utils.CACHE_ROOT) / 'datasets' / task.value
data_file = data_path / 'train.txt'
if not data_file.is_file():
cached_path(imdb_acl_path, Path('datasets') / task.value)
import tarfile
with tarfile.open(Path(flair.file_utils.CACHE_ROOT) / 'datasets' / task.value / 'aclImdb_v1.tar.gz',
'r:gz') as f_in:
datasets = ['train', 'test']
labels = ['pos', 'neg']

for label in labels:
for dataset in datasets:
f_in.extractall(data_path, members=[m for m in f_in.getmembers()
if f'{dataset}/{label}' in m.name])
with open(f'{data_path}/{dataset}.txt', 'at') as f_p:
current_path = data_path / 'aclImdb' / dataset / label
for file_name in current_path.iterdir():
if file_name.is_file() and file_name.name.endswith('.txt'):
f_p.write(f'__label__{label} '
+ file_name.open('rt', encoding='utf-8').read() + '\n')

if task == NLPTask.WNUT_17:
wnut_path = 'https://noisy-text.github.io/2017/files/'
cached_path(f'{wnut_path}wnut17train.conll', Path('datasets') / task.value)
Expand Down