Upload 6 files
Browse files- plotcom/.DS_Store +0 -0
- plotcom/README.md +36 -0
- plotcom/eval.py +91 -0
- plotcom/test.jsonl +0 -0
- plotcom/train.jsonl +0 -0
- plotcom/val.jsonl +0 -0
plotcom/.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
plotcom/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Plot Completion Dataset
|
| 2 |
+
|
| 3 |
+
### Data Example
|
| 4 |
+
|
| 5 |
+
```
|
| 6 |
+
{
|
| 7 |
+
"story":
|
| 8 |
+
"在一个金碧辉煌的天国王宫里住着两位公主,她们都非常的美丽善良。<MASK>大王就跟国王说,你要是不把你的女儿交出来我就杀了你,正当国王在想办法的时候,一个白马王子出现了,他直接把山寨的大王杀了,跟国王说,我要娶两位公主,国王也同意让女儿嫁了白马王子。从此以后,两位天国的公主与白马王子过上了幸福的日子。",
|
| 9 |
+
"plot":
|
| 10 |
+
"有一次,山寨的大王要来天国王宫里提亲,可是国王并不同意把自己的女儿嫁给山寨的大王做山寨夫人。"
|
| 11 |
+
}
|
| 12 |
+
```
|
| 13 |
+
|
| 14 |
+
- "story" (`str`):input story,`<MASK>` means the position of the removed sentence.
|
| 15 |
+
- "plot" (`str`):the removed sentence.
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
### Evaluation
|
| 20 |
+
|
| 21 |
+
The prediction result should have the same format with `test.jsonl`
|
| 22 |
+
|
| 23 |
+
```shell
|
| 24 |
+
python eval.py prediction_file test.jsonl
|
| 25 |
+
```
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
We use bleu and distinct as the evaluation metrics. The output of the script `eval.py` is a dictionary as follows:
|
| 30 |
+
|
| 31 |
+
```python
|
| 32 |
+
{'bleu-1': '_', 'bleu-2': '_', 'bleu-3': '_', 'bleu-4': '_', 'distinct-1': '_', 'distinct-2': '_', 'distinct-3': '_', 'distinct-4': '_'}
|
| 33 |
+
```
|
| 34 |
+
|
| 35 |
+
- Dependencies: jieba=0.42.1, nltk=3.6.2, numpy=1.20.3
|
| 36 |
+
|
plotcom/eval.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import argparse
|
| 3 |
+
import sys
|
| 4 |
+
import numpy as np
|
| 5 |
+
import jieba
|
| 6 |
+
import nltk
|
| 7 |
+
from nltk.translate.bleu_score import sentence_bleu, SmoothingFunction
|
| 8 |
+
from nltk import ngrams
|
| 9 |
+
|
| 10 |
+
def bleu(data):
|
| 11 |
+
"""
|
| 12 |
+
compute rouge score
|
| 13 |
+
Args:
|
| 14 |
+
data (list of dict including reference and candidate):
|
| 15 |
+
Returns:
|
| 16 |
+
res (dict of list of scores): rouge score
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
res = {}
|
| 20 |
+
for i in range(1, 5):
|
| 21 |
+
res["sentence-bleu-%d"%i] = []
|
| 22 |
+
res["corpus-bleu-%d"%i] = nltk.translate.bleu_score.corpus_bleu([[d["reference"].strip().split()] for d in data], [d["candidate"].strip().split() for d in data], weights=tuple([1./i for j in range(i)]))
|
| 23 |
+
for tmp_data in data:
|
| 24 |
+
origin_candidate = tmp_data['candidate']
|
| 25 |
+
origin_reference = tmp_data['reference']
|
| 26 |
+
assert isinstance(origin_candidate, str)
|
| 27 |
+
if not isinstance(origin_reference, list):
|
| 28 |
+
origin_reference = [origin_reference]
|
| 29 |
+
|
| 30 |
+
for i in range(1, 5):
|
| 31 |
+
res["sentence-bleu-%d"%i].append(sentence_bleu(references=[r.strip().split() for r in origin_reference], hypothesis=origin_candidate.strip().split(), weights=tuple([1./i for j in range(i)])))
|
| 32 |
+
|
| 33 |
+
for key in res:
|
| 34 |
+
if "sentence" in key:
|
| 35 |
+
res[key] = np.mean(res[key])
|
| 36 |
+
|
| 37 |
+
return res
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def distinct(eval_data):
|
| 42 |
+
result = {}
|
| 43 |
+
for i in range(1, 5):
|
| 44 |
+
all_ngram, all_ngram_num = {}, 0.
|
| 45 |
+
for k, tmp_data in enumerate(eval_data):
|
| 46 |
+
ngs = ["_".join(c) for c in ngrams(tmp_data["candidate"].strip().split(), i)]
|
| 47 |
+
all_ngram_num += len(ngs)
|
| 48 |
+
for s in ngs:
|
| 49 |
+
if s in all_ngram:
|
| 50 |
+
all_ngram[s] += 1
|
| 51 |
+
else:
|
| 52 |
+
all_ngram[s] = 1
|
| 53 |
+
result["distinct-%d"%i] = len(all_ngram) / float(all_ngram_num)
|
| 54 |
+
return result
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def load_file(filename):
|
| 59 |
+
data = []
|
| 60 |
+
with open(filename, "r") as f:
|
| 61 |
+
for line in f.readlines():
|
| 62 |
+
data.append(json.loads(line))
|
| 63 |
+
f.close()
|
| 64 |
+
return data
|
| 65 |
+
|
| 66 |
+
def proline(line):
|
| 67 |
+
return " ".join([w for w in jieba.cut("".join(line.strip().split()))])
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def compute(golden_file, pred_file, return_dict=True):
|
| 71 |
+
golden_data = load_file(golden_file)
|
| 72 |
+
pred_data = load_file(pred_file)
|
| 73 |
+
|
| 74 |
+
if len(golden_data) != len(pred_data):
|
| 75 |
+
raise RuntimeError("Wrong Predictions")
|
| 76 |
+
|
| 77 |
+
eval_data = [{"reference": proline(g["plot"]), "candidate": proline(p["plot"])} for g, p in zip(golden_data, pred_data)]
|
| 78 |
+
res = bleu(eval_data)
|
| 79 |
+
res.update(distinct(eval_data))
|
| 80 |
+
for key in res:
|
| 81 |
+
res[key] = "_"
|
| 82 |
+
return res
|
| 83 |
+
|
| 84 |
+
def main():
|
| 85 |
+
argv = sys.argv
|
| 86 |
+
print("预测结果:{}, 测试集: {}".format(argv[1], argv[2]))
|
| 87 |
+
print(compute(argv[2], argv[1]))
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
if __name__ == '__main__':
|
| 91 |
+
main()
|
plotcom/test.jsonl
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
plotcom/train.jsonl
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
plotcom/val.jsonl
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|