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 = [
    "Answer\s*:",
    "Answer\s*:​​​​​​",  # Korean invisible character
    "উত্তর\s*:",
    "उत्तर\s*:",
    "উত্তরঃ",
    "উত্তর\s*:",
    "Antwort\s*:",
    "답변\s*:",
    "정답\s*:",
    "답\s*:",
    "答案\s*:",
    "答案\s*:",
    "答\s*:",
    "答\s*:",
    "答复\s*:",
    "答曰\s*:",
    "الإجابة:",
    "الجواب:",
    "إجابة:",
    "الإجابة النهائية:",
    "الإجابة الصحيحة:",
    "الإجابة الصحيحة هي:",
    "الإجابة هي:",
    "الجواب النهائي:",
    "Respuesta\s*:",
    "Risposta\s*:",
    "答え\s*:",
    "答え\s*:",
    "回答\s*:",
    "回答\s*:",
    "解答\s*:",
    "Jawaban\s*:",
    "Réponse\s*:",
    "Resposta\s*:",
    "Jibu\s*:",
    "Idahun\s*:",
    "Ìdáhùn\s*:",
    "Idáhùn\s*:",
    "Àmọ̀nà\s*:",
    "Àdáhùn\s*:",
    "Ànúgọ\s*:",
    "Àṣà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", "") )