Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

where calculate_em_str_multi define? #3328

Closed
LiemLin opened this issue Sep 21, 2022 · 7 comments
Closed

where calculate_em_str_multi define? #3328

LiemLin opened this issue Sep 21, 2022 · 7 comments
Assignees

Comments

@LiemLin
Copy link

LiemLin commented Sep 21, 2022

lambda row: calculate_em_str_multi(

@w5688414
Copy link
Contributor

w5688414 commented Sep 21, 2022

I am not sure why you meet many problems for the missing code, could you tell me more details why you met such problems.

def calculate_em_str_multi(gold_labels, prediction):
    for gold_label in gold_labels:
        result = int(normalize_answer(gold_label) == normalize_answer(prediction))
        if result == 1.0:
            return 1.0
    return 0.0

def normalize_answer(s: str):
    """
    Lower text and remove punctuation, articles and extra whitespace.
    """

    def remove_articles(text):
        regex = re.compile(r"\b(a|an|the)\b", re.UNICODE)
        return re.sub(regex, " ", text)

    def white_space_fix(text):
        return " ".join(text.split())

    def remove_punc(text):
        exclude = set(string.punctuation)
        return "".join(ch for ch in text if ch not in exclude)

    def lower(text):
        return text.lower()

    return white_space_fix(remove_articles(remove_punc(lower(s))))

@w5688414 w5688414 self-assigned this Sep 21, 2022
@LiemLin
Copy link
Author

LiemLin commented Sep 21, 2022

git clone https://github.com/PaddlePaddle/PaddleNLP.git

When I opened the project, I found that many methods were missing definitions

@w5688414
Copy link
Contributor

git clone https://github.com/PaddlePaddle/PaddleNLP.git

When I opened the project, I found that many methods were missing definitions

Which IDE did you use?

@LiemLin
Copy link
Author

LiemLin commented Sep 21, 2022

Pycharm at Mac OS

def calculate_em_str_multi(gold_labels, prediction):
for gold_label in gold_labels:
result = int(normalize_answer(gold_label) == normalize_answer(prediction))
if result == 1.0:
return 1.0
return 0.0

Do I put this function define in pipelines/base.py ?

@LiemLin
Copy link
Author

LiemLin commented Sep 21, 2022

and this function calculate_f1_str_multi no define yet

@w5688414
Copy link
Contributor

w5688414 commented Sep 21, 2022

or you can remove this function like this pr #3330

import collections
import re
import string

def get_tokens(s: str):
    if not s:
        return []
    return normalize_answer(s).split()


def compute_f1(a_gold: str, a_pred: str):
    gold_toks = get_tokens(a_gold)
    pred_toks = get_tokens(a_pred)
    common: collections.Counter = collections.Counter(gold_toks) & collections.Counter(pred_toks)
    num_same = sum(common.values())
    if len(gold_toks) == 0 or len(pred_toks) == 0:
        # If either is no-answer, then F1 is 1 if they agree, 0 otherwise
        return int(gold_toks == pred_toks)
    if num_same == 0:
        return 0
    precision = 1.0 * num_same / len(pred_toks)
    recall = 1.0 * num_same / len(gold_toks)
    f1 = (2 * precision * recall) / (precision + recall)
    return f1

def calculate_f1_str_multi(gold_labels, prediction):
    results = []
    for gold_label in gold_labels:
        result = compute_f1(gold_label, prediction)
        results.append(result)
    if len(results) > 0:
        return max(results)
    else:
        return 0.0

@LiemLin
Copy link
Author

LiemLin commented Sep 21, 2022

ok thanks

@LiemLin LiemLin closed this as completed Sep 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants