SWORDS
收藏Swords ⚔️: Stanford Word Substitution Benchmark
数据集概述
Swords ⚔️ 是一个用于词汇替代任务的基准测试,旨在找到上下文中目标词的适当替代词。例如:
Context:
My favorite thing about her is her straightforward honesty.Target word:
straightforwardSubstitutes:
sincere, genuine, frank, candid, direct, forthright, ...
数据集下载
Swords ⚔️ 的开发集和测试集可以从以下链接下载:
Swords ⚔️ 采用 CC-BY-3.0-US 许可。该基准包括来自 CoInCo 基准 和 MASC 语料库 的内容,这些内容均采用相同的许可。
数据格式
基准数据以简单的 JSON 格式分发,包含所有 contexts、targets 和 substitutes 作为键。每个 context/target/substitute 都关联一个唯一的 ID,这是一个其内容的 SHA1 哈希值。内容如下:
- 每个 context 包含:
context: 上下文文本extra: 关于此上下文的额外信息
- 每个 target 包含:
context_id: 此目标来自的上下文的 IDtarget: 目标词文本offset: 目标词在其上下文中的字符级整数偏移量pos: 目标词的词性extra: 关于此目标的额外信息
- 每个 substitute 包含:
target_id: 此替代词对应的目标的 IDsubstitute: 替代词文本extra: 关于此替代词的额外信息
每个替代词 ID 的标签位于 substitute_labels 键中。
示例代码
以下是读取此格式的示例 Python 代码:
python from collections import defaultdict import gzip import json
Load benchmark
with gzip.open(swords-v1.1_dev.json.gz, r) as f: swords = json.load(f)
Gather substitutes by target
tid_to_sids = defaultdict(list) for sid, substitute in swords[substitutes].items(): tid_to_sids[substitute[target_id]].append(sid)
Iterate through targets
for tid, target in swords[targets].items(): context = swords[contexts][target[context_id]] substitutes = [swords[substitutes][sid] for sid in tid_to_sids[tid]] labels = [swords[substitute_labels][sid] for sid in tid_to_sids[tid]] scores = [l.count(TRUE) / len(l) for l in labels] print(- * 80) print(context[context]) print(- * 20) print({} ({}).format(target[target], target[pos])) print(, .join([{} ({}%).format(substitute[substitute], round(score * 100)) for substitute, score in sorted(zip(substitutes, scores), key=lambda x: -x[1])])) break
历史基准数据
为了方便,我们将以前的词汇替代基准数据打包在为 Swords ⚔️ 设计的相同 JSON 格式中。CoInCo 基准数据可以在此处下载:
其他基准数据如 SemEval07 和 TWSI 可以通过运行以下脚本创建:
评估新方法
以下是评估新词汇替代方法的示例代码:
生成设置
在生成设置中,词汇替代方法必须为给定目标输出一个排序的替代词候选列表:
python import gzip import json import random import warnings
with gzip.open(swords-v1.1_dev.json.gz, r) as f: swords = json.load(f)
def generate( context, target, target_offset, target_pos=None): substitutes = [be, have, do, say, get, make, go, know, take, see] scores = [random.random() for _ in substitutes] return list(zip(substitutes, scores))
result = {substitutes_lemmatized: True, substitutes: {}} errors = 0 for tid, target in swords[targets].items(): context = swords[contexts][target[context_id]] try: result[substitutes][tid] = generate( context[context], target[target], target[offset], target_pos=target.get(pos)) except: errors += 1 continue
if errors > 0: warnings.warn(f{errors} targets were not evaluated due to errors)
with open(swords-v1.1_dev_mygenerator.lsr.json, w) as f: f.write(json.dumps(result))
排名设置
在排名设置中,词汇替代方法必须根据上下文相关性对基准数据中的候选词列表进行排序:
python import gzip import json import random import warnings
with gzip.open(swords-v1.1_dev.json.gz, r) as f: swords = json.load(f)
def score( context, target, target_offset, substitute, target_pos=None): return random.random()
result = {substitutes_lemmatized: True, substitutes: {}} errors = 0 for sid, substitute in swords[substitutes].items(): tid = substitute[target_id] target = swords[targets][tid] context = swords[contexts][target[context_id]] if tid not in result[substitutes]: result[substitutes][tid] = [] try: substitute_score = score( context[context], target[target], target[offset], substitute[substitute], target_pos=target.get(pos)) result[substitutes][tid].append((substitute[substitute], substitute_score)) except: errors += 1 continue
if errors > 0: warnings.warn(f{errors} substitutes were not evaluated due to errors)
with open(swords-v1.1_dev_myranker.lsr.json, w) as f: f.write(json.dumps(result))
评估方法
要评估上述生成器,请将 swords-v1.1_dev_mygenerator.lsr.json 复制到 notebooks 目录并运行:
sh ./cli.sh eval swords-v1.1_dev --result_json_fp notebooks/swords-v1.1_dev_mygenerator.lsr.json --output_metrics_json_fp notebooks/mygenerator.metrics.json
要评估上述排名器,请将 swords-v1.1_dev_myranker.lsr.json 复制到 notebooks 目录并运行:
sh ./cli.sh eval swords-v1.1_dev --result_json_fp notebooks/swords-v1.1_dev_myranker.lsr.json --output_metrics_json_fp notebooks/myranker.metrics.json --metrics gap_rat

- 1Swords: A Benchmark for Lexical Substitution with Improved Data Coverage and Quality斯坦福大学 · 2021年



