File size: 4,744 Bytes
af1fa5c
 
 
 
 
 
 
 
 
 
 
 
 
eb8ecc4
a0a0756
 
af1fa5c
 
a0a0756
af1fa5c
a0a0756
af1fa5c
 
a0a0756
b16de67
af1fa5c
 
 
 
a0a0756
eb8ecc4
 
 
 
 
 
af1fa5c
eb8ecc4
af1fa5c
eb8ecc4
 
 
 
 
a0a0756
eb8ecc4
107f45c
af1fa5c
 
a0a0756
 
af1fa5c
 
 
 
 
 
 
 
 
28bbd1a
 
 
 
 
 
 
 
 
 
 
 
 
 
a0a0756
af1fa5c
 
40d109e
 
 
 
 
 
 
 
 
a0a0756
 
 
40d109e
 
 
 
a0a0756
 
 
 
40d109e
 
a0a0756
 
 
 
 
 
 
 
 
 
af1fa5c
40d109e
 
 
04a0234
40d109e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca9e0e6
40d109e
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""nl2bash metric."""
import re
import string

import datasets
import numpy as np

import evaluate


_DESCRIPTION = """
returns a score that indicates how close the bash command generated is to the actual command with a perfect score out of 1.0
"""

_KWARGS_DESCRIPTION = """
Args:
    predictions: List of predicted texts.
    references: List of reference texts. 
    cmd_weight: The weight you want to put on getting the command correct
    opt_weight: The weight you want to put on getting the option correct  
    arg_weight: The weight you want to put on getting the arg correct
    ignore_case=False,
    ignore_numbers=False,
Returns:
    nl2bash metric: Dictionary containing nl2bash score. Possible values are between 0.0 and 1.0, inclusive.
Examples:


    >>> metric = evaluate.load("Josh98/nl2bash_m")
    >>> preds = ["ls -l /home/userr", "ls -l /home/josh", "lss /home/josh some argument"]
    >>> refs = [["ls -l /home/user"], ["ls -l --v /home/josh"], ["ls /home/josh"]]
    >>> results = exact_match.compute(references=refs, predictions=preds)
    >>> print(round(results["nl2bash"], 2))
    0.708
"""

_CITATION = """
"""


@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
class nl2bash_m(evaluate.Metric):
    def _info(self):
        return evaluate.MetricInfo(
            description=_DESCRIPTION,
            citation=_CITATION,
            inputs_description=_KWARGS_DESCRIPTION,
            features=[
                datasets.Features(
                    {
                        "predictions": datasets.Value("string", id="sequence"),
                        "references": datasets.Sequence(datasets.Value("string", id="sequence"), id="references"),
                    }
                ),
                datasets.Features(
                    {
                        "predictions": datasets.Value("string", id="sequence"),
                        "references": datasets.Value("string", id="sequence"),
                    }
                ),
            ],
            reference_urls=[],
        )

    def get_score(self, pred, ref):
        if not pred and not ref: return 1
        cor = 0
        for i in range(min(len(pred), len(ref))):
            if (pred[i] == ref[i]):
                cor += 1
        
        return cor/max(len(pred), len(ref))

    def _compute(
        self,
        predictions,
        references, 
        cmd_weight = 0.65,
        opt_weight = 0.25,
        arg_weight = 0.15,
        ignore_case=False,
        ignore_numbers=False,
    ):

        predictions = np.asarray(predictions)
        references = np.asarray(references)

        if ignore_case:
            predictions = np.char.lower(predictions)
            references = np.char.lower(references)

        if ignore_numbers:
            repl_table = string.digits.maketrans("", "", string.digits)
            predictions = np.char.translate(predictions, table=repl_table)
            references = np.char.translate(references, table=repl_table)


        final_score = 0

        for pred, ref in zip(predictions, references):
            pred_words, ref_words = pred.split(), ref[0].split()
            # Get the cmd of predicted and ref 
            cmd_corr = 1 if pred_words.pop(0)==ref_words.pop(0) else 0

            # Get the option of predicted and ref
            pred_option = [ x for x in pred_words if x[0] == '-']
            ref_option = [ x for x in ref_words if x[0] == '-']
            
            # Get the arguments of predicted and ref
            pred_args = [ x for x in pred_words if x[0] != '-']
            ref_args = [ x for x in ref_words if x[0] != '-']

            # Calculate scores
            cmd_score = cmd_weight * cmd_corr
            opt_score = opt_weight * self.get_score(pred_option, ref_option)
            arg_score = arg_weight * self.get_score(pred_args, ref_args)

            score = cmd_score + opt_score + arg_score
            final_score += score

        final_score = final_score/len(predictions)
        print("f_s: ", final_score)
 

        return {"nl2bash_m": (final_score)}