Source code for nemo_rl.evals.answer_parsing

# Copyright (c) 2025, NVIDIA CORPORATION.  All rights reserved.
#
# 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.

"""Contains utility functions for answer parsing."""

MULTILINGUAL_ANSWER_PATTERN_TEMPLATE = (
    "(?i){}[ \t]*([A-D]|[أ-د]|[অ]|[ব]|[ড]|[ঢ]|[A]|[B]|[C]|[D])"
)
# All the different ways "Answer" is written in different languages
MULTILINGUAL_ANSWER_REGEXES = [
    r"Answer\s*:",
    r"Answer\s*:​​​​​​",  # Korean invisible character
    r"উত্তর\s*:",
    r"उत्तर\s*:",
    r"উত্তরঃ",
    r"উত্তর\s*:",
    r"Antwort\s*:",
    r"답변\s*:",
    r"정답\s*:",
    r"답\s*:",
    r"答案\s*:",
    r"答案\s*:",
    r"答\s*:",
    r"答\s*:",
    r"答复\s*:",
    r"答曰\s*:",
    r"الإجابة:",
    r"الجواب:",
    r"إجابة:",
    r"الإجابة النهائية:",
    r"الإجابة الصحيحة:",
    r"الإجابة الصحيحة هي:",
    r"الإجابة هي:",
    r"الجواب النهائي:",
    r"Respuesta\s*:",
    r"Risposta\s*:",
    r"答え\s*:",
    r"答え\s*:",
    r"回答\s*:",
    r"回答\s*:",
    r"解答\s*:",
    r"Jawaban\s*:",
    r"Réponse\s*:",
    r"Resposta\s*:",
    r"Jibu\s*:",
    r"Idahun\s*:",
    r"Ìdáhùn\s*:",
    r"Idáhùn\s*:",
    r"Àmọ̀nà\s*:",
    r"Àdáhùn\s*:",
    r"Ànúgọ\s*:",
    r"Àṣàyàn\s*:",
]


[docs] def normalize_extracted_answer(extracted_answer: str) -> str: return ( # In arabic these are the letters used for A-D in multiple choice questions extracted_answer.replace("أ", " A") .replace("ب", " B") .replace("ج", " C") .replace("د", " D") # In Bengali these are the letters used for A-D in multiple choice questions .replace("অ", " A") .replace("ব", " B") .replace("ড", " C") .replace("ঢ", " D") # In Japanese these are the letters sometimes used for A-D in multiple choice questions .replace("A", " A") .replace("B", " B") .replace("C", " C") .replace("D", " D") .strip() )
[docs] def normalize_response(response: str) -> str: """Normalize the response by removing markdown and LaTeX formatting that may prevent a match.""" return ( response.replace("**", "") .replace("$\\boxed{", "") .replace("}$", "") .replace("\\$", "") .replace("$\\text{", "") .replace("$", "") .replace("\\mathrm{", "") .replace("\\{", "") .replace("\\text", "") .replace("\\(", "") .replace("\\mathbf{", "") .replace("{", "") .replace("\\boxed", "") )