forked from facebookresearch/ParlAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MultiNLI task (facebookresearch#476)
- Loading branch information
1 parent
f16622f
commit f9ee62e
Showing
5 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
# Copyright (c) 2017-present, Facebook, Inc. | ||
# All rights reserved. | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. An additional grant | ||
# of patent rights can be found in the PATENTS file in the same directory. |
This file contains 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,73 @@ | ||
# Copyright (c) 2017-present, Facebook, Inc. | ||
# All rights reserved. | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. An additional grant | ||
# of patent rights can be found in the PATENTS file in the same directory. | ||
|
||
from parlai.core.teachers import DialogTeacher | ||
from .build import build | ||
|
||
import os | ||
import copy | ||
import json | ||
|
||
|
||
MULTINLI = 'MultiNLI' | ||
MULTINLI_VERSION = '1.0' | ||
MULTINLI_PREFIX = 'multinli_' | ||
MULTINLI_PREMISE_PREFIX = 'Premise: ' | ||
MULTINLI_HYPO_PREFIX = 'Hypothesis: ' | ||
MULTINLI_LABELS = ['entailment', 'contradiction', 'neutral'] | ||
MULTINLI_PREMISE_KEY = 'sentence1' | ||
MULTINLI_HYPO_KEY = 'sentence2' | ||
MULTINLI_ANSWER_KEY = 'gold_label' | ||
|
||
|
||
def _path(opt): | ||
build(opt) | ||
|
||
dt = opt['datatype'].split(':')[0] | ||
|
||
if dt == 'train': | ||
suffix = 'train' | ||
# Using matched set as valid and mismatched set as test | ||
elif dt == 'valid': | ||
suffix = 'dev_matched' | ||
elif dt == 'test': | ||
suffix = 'dev_mismatched' | ||
else: | ||
raise RuntimeError('Not valid datatype.') | ||
|
||
data_path = os.path.join(opt['datapath'], MULTINLI, | ||
MULTINLI_PREFIX + MULTINLI_VERSION, | ||
MULTINLI_PREFIX + MULTINLI_VERSION + | ||
'_' + suffix + '.jsonl') | ||
|
||
return data_path | ||
|
||
|
||
class DefaultTeacher(DialogTeacher): | ||
def __init__(self, opt, shared=None): | ||
opt = copy.deepcopy(opt) | ||
data_path = _path(opt) | ||
opt['datafile'] = data_path | ||
self.id = 'MultiNLI' | ||
|
||
super().__init__(opt, shared) | ||
|
||
def setup_data(self, path): | ||
print('loading: ' + path) | ||
|
||
with open(path, 'r') as data_file: | ||
for pair_line in data_file: | ||
pair = json.loads(pair_line) | ||
premise = MULTINLI_PREMISE_PREFIX + pair[MULTINLI_PREMISE_KEY] | ||
hypo = MULTINLI_HYPO_PREFIX + pair[MULTINLI_HYPO_KEY] | ||
answer = [pair[MULTINLI_ANSWER_KEY]] | ||
|
||
if answer == '-': | ||
continue | ||
|
||
question = premise + '\n' + hypo | ||
|
||
yield (question, answer, None, MULTINLI_LABELS), True |
This file contains 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,37 @@ | ||
|
||
# Copyright (c) 2017-present, Facebook, Inc. | ||
# All rights reserved. | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. An additional grant | ||
# of patent rights can be found in the PATENTS file in the same directory. | ||
|
||
import parlai.core.build_data as build_data | ||
import os | ||
|
||
|
||
MULTINLI_BASE_URL = 'https://www.nyu.edu/projects/bowman/multinli/' | ||
|
||
|
||
def build(opt): | ||
dpath = os.path.join(opt['datapath'], 'MultiNLI') | ||
version = '1.0' | ||
|
||
if not build_data.built(dpath, version_string=version): | ||
print('[building data: ' + dpath + ']') | ||
|
||
if build_data.built(dpath): | ||
# an older version exists, so remove these outdated files. | ||
build_data.remove_dir(dpath) | ||
build_data.make_dir(dpath) | ||
|
||
# download the data. | ||
fname = 'multinli_' + version + '.zip' | ||
# dataset URL | ||
url = MULTINLI_BASE_URL + fname | ||
build_data.download(url, dpath, fname) | ||
|
||
# uncompress it | ||
build_data.untar(dpath, fname) | ||
|
||
# mark the data as built | ||
build_data.mark_done(dpath, version_string=version) |
This file contains 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 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