Skip to content

Commit

Permalink
HW3 init
Browse files Browse the repository at this point in the history
  • Loading branch information
boewoei0123 committed May 17, 2021
1 parent f9b6ca0 commit f0439ce
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 0 deletions.
62 changes: 62 additions & 0 deletions HW3/evaluation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# ADL21-HW3
Dataset & evaluation script for ADL 2021 homework 3

## Dataset
[download link](https://drive.google.com/file/d/186ejZVADY16RBfVjzcMcz9bal9L3inXC/view?usp=sharing)

## Installation
```
git clone https://github.com/ntu-adl-ta/ADL21-HW3.git
cd ADL21-HW3
pip install -e tw_rouge
```


## Usage
### Use the Script
```
usage: eval.py [-h] [-r REFERENCE] [-s SUBMISSION]
optional arguments:
-h, --help show this help message and exit
-r REFERENCE, --reference REFERENCE
-s SUBMISSION, --submission SUBMISSION
```

Example:
```
python eval.py -r public.jsonl -s submission.jsonl
{
"rouge-1": {
"f": 0.21999419163162043,
"p": 0.2446195813913345,
"r": 0.2137398792982201
},
"rouge-2": {
"f": 0.0847583291303246,
"p": 0.09419044877345074,
"r": 0.08287844474014894
},
"rouge-l": {
"f": 0.21017939117006337,
"p": 0.25157090570020846,
"r": 0.19404349000921203
}
}
```


### Use Python Library
```
>>> from tw_rouge import get_rouge
>>> get_rouge('我是人', '我是一個人')
{'rouge-1': {'f': 0.7499999953125, 'p': 1.0, 'r': 0.6}, 'rouge-2': {'f': 0.33333332888888895, 'p': 0.5, 'r': 0.25}, 'rouge-l': {'f': 0.7499999953125, 'p': 1.0, 'r': 0.6}}
>>> get_rouge(['我是人'], [ 我是一個人'])
{'rouge-1': {'f': 0.7499999953125, 'p': 1.0, 'r': 0.6}, 'rouge-2': {'f': 0.33333332888888895, 'p': 0.5, 'r': 0.25}, 'rouge-l': {'f': 0.7499999953125, 'p': 1.0, 'r': 0.6}}
>>> get_rouge(['我是人'], ['我是一個人'], avg=False)
[{'rouge-1': {'f': 0.7499999953125, 'p': 1.0, 'r': 0.6}, 'rouge-2': {'f': 0.33333332888888895, 'p': 0.5, 'r': 0.25}, 'rouge-l': {'f': 0.7499999953125, 'p': 1.0, 'r': 0.6}}]
```


## Reference
[cccntu/tw_rouge](https://github.com/cccntu/tw_rouge)
31 changes: 31 additions & 0 deletions HW3/evaluation/eval.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import json
import argparse
from tw_rouge import get_rouge


def main(args):
refs, preds = {}, {}

with open(args.reference) as file:
for line in file:
line = json.loads(line)
refs[line['id']] = line['title']

with open(args.submission) as file:
for line in file:
line = json.loads(line)
preds[line['id']] = line['title']

keys = refs.keys()
refs = [refs[key] for key in keys]
preds = [preds[key] for key in keys]

print(json.dumps(get_rouge(preds, refs), indent=2))


if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-r', '--reference')
parser.add_argument('-s', '--submission')
args = parser.parse_args()
main(args)
10 changes: 10 additions & 0 deletions HW3/evaluation/tw_rouge/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

The MIT License (MIT)
Copyright (c) 2021, Jonathan Chang

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

17 changes: 17 additions & 0 deletions HW3/evaluation/tw_rouge/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# ROUGE score calculator with tradition chinese word segmentation

## Install

`pip install` this repo

## Usage

```
>>> from tw_rouge import get_rouge
>>> get_rouge('我是人', '我是一個人')
{'rouge-1': {'f': 0.7499999953125, 'p': 1.0, 'r': 0.6}, 'rouge-2': {'f': 0.33333332888888895, 'p': 0.5, 'r': 0.25}, 'rouge-l': {'f': 0.7499999953125, 'p': 1.0, 'r': 0.6}}
>>> get_rouge(['我是人'], [ 我是一個人'])
{'rouge-1': {'f': 0.7499999953125, 'p': 1.0, 'r': 0.6}, 'rouge-2': {'f': 0.33333332888888895, 'p': 0.5, 'r': 0.25}, 'rouge-l': {'f': 0.7499999953125, 'p': 1.0, 'r': 0.6}}
>>> get_rouge(['我是人'], ['我是一個人'], avg=False)
[{'rouge-1': {'f': 0.7499999953125, 'p': 1.0, 'r': 0.6}, 'rouge-2': {'f': 0.33333332888888895, 'p': 0.5, 'r': 0.25}, 'rouge-l': {'f': 0.7499999953125, 'p': 1.0, 'r': 0.6}}]
```
11 changes: 11 additions & 0 deletions HW3/evaluation/tw_rouge/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from setuptools import find_packages, setup

setup(
name="tw_rouge",
packages=find_packages(),
version="0.1.0",
description="rouge metric for traditional chinese",
author="Jonathan Chang",
license="MIT",
install_requires=["ckiptagger[tf,gdown]", "rouge"],
)
1 change: 1 addition & 0 deletions HW3/evaluation/tw_rouge/tw_rouge/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .twrouge import get_rouge
36 changes: 36 additions & 0 deletions HW3/evaluation/tw_rouge/tw_rouge/twrouge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os

from ckiptagger import WS, data_utils
from rouge import Rouge

cache_dir = os.environ.get("XDG_CACHE_HOME", os.path.join(os.getenv("HOME"), ".cache"))
download_dir = os.path.join(cache_dir, "ckiptagger")
data_dir = os.path.join(cache_dir, "ckiptagger/data")
os.makedirs(download_dir, exist_ok=True)
if not os.path.exists(os.path.join(data_dir, "model_ws")):
data_utils.download_data_gdown(download_dir)

ws = WS(data_dir)


def tokenize_and_join(sentences):
return [" ".join(toks) for toks in ws(sentences)]


rouge = Rouge()


def get_rouge(preds, refs, avg=True, ignore_empty=False):
"""wrapper around: from rouge import Rouge
Args:
preds: string or list of strings
refs: string or list of strings
avg: bool, return the average metrics if set to True
ignore_empty: bool, ignore empty pairs if set to True
"""
if not isinstance(preds, list):
preds = [preds]
if not isinstance(refs, list):
refs = [refs]
preds, refs = tokenize_and_join(preds), tokenize_and_join(refs)
return rouge.get_scores(preds, refs, avg=avg, ignore_empty=ignore_empty)

0 comments on commit f0439ce

Please sign in to comment.