fmeval.reporting.util
1from typing import List, Tuple 2 3from fmeval.eval_algorithms import EvalAlgorithm 4from fmeval.reporting.constants import ( 5 DATASET_DETAILS, 6 GENERAL_STRING_REPLACEMENTS, 7 SCORE_STRING_REPLACEMENTS, 8 EVAL_NAME_STRING_REPLACEMENTS, 9 COLUMN_NAME_STRING_REPLACEMENTS, 10 PLOT_TITLE_STRING_REPLACEMENTS, 11 AVOID_REMOVE_UNDERSCORE, 12) 13 14 15def format_string( 16 text: str, 17 remove_underscore: bool = True, 18 as_title: bool = False, 19 as_score: bool = False, 20 as_plot_title: bool = False, 21 as_eval_name: bool = False, 22 as_column_name: bool = False, 23) -> str: 24 """ 25 :param text: text, name of the score or eval. 26 :param remove_underscore: Boolean indicating if underscores should be replaced with spaces. 27 :param as_title: Boolean indicating if the text is a title, if set to True will capitalize each word. 28 :param as_score: Boolean indicating if "score" should be appended to the text. 29 :param as_plot_title: Boolean indicating if this is a plot title. 30 :param as_eval_name: Boolean indicating if this is the name of an evaluation. 31 :param as_column_name: Boolean indicating if this is the name of a table column. 32 :return: formatted score name. 33 """ 34 formatted_text = _replace_strings(text, GENERAL_STRING_REPLACEMENTS) 35 if as_plot_title and EvalAlgorithm.PROMPT_STEREOTYPING.value in formatted_text: 36 formatted_text = _replace_strings(formatted_text, PLOT_TITLE_STRING_REPLACEMENTS) 37 remove_underscore = False 38 if as_column_name: 39 formatted_text = _replace_strings(formatted_text, COLUMN_NAME_STRING_REPLACEMENTS) 40 if as_eval_name: 41 formatted_text = _replace_strings(formatted_text, EVAL_NAME_STRING_REPLACEMENTS) 42 if remove_underscore: 43 if text not in AVOID_REMOVE_UNDERSCORE: # pragma: no branch 44 formatted_text = formatted_text.replace("_", " ") 45 if as_score: 46 formatted_text = _replace_strings(formatted_text, SCORE_STRING_REPLACEMENTS) 47 formatted_text = formatted_text if "score" in formatted_text.lower() else f"{formatted_text} score" 48 if as_title: 49 # Capitalize each word while preserving original capitalization within words 50 formatted_text = " ".join(w if w.isupper() else w[0].upper() + w[1:] for w in formatted_text.split()) 51 return formatted_text 52 53 54def _replace_strings(text: str, replacements: List[Tuple[str, str]]) -> str: 55 """ 56 :param text: The text which contains substrings that may be replaced. 57 :param replacements: The tuples with format (original substring, replacement substring). 58 :return: The text with the strings replaced if they exist. 59 """ 60 for (old, new) in replacements: 61 text = text.replace(old, new) 62 return text 63 64 65def format_dataset_name(dataset_name: str, hyperlink: bool = False, html: bool = True, color: str = "#006DAA") -> str: 66 """ 67 :param dataset_name: The name of the dataset. 68 :param hyperlink: Boolean indicating if hyperlink should be added to dataset name. 69 :param html: Boolean indicating if hyperlink should be added in HTML format. 70 :param color: The color of the text. 71 :return: Properly capitalized dataset name. 72 """ 73 if dataset_name not in DATASET_DETAILS: 74 return dataset_name 75 proper_dataset_name = DATASET_DETAILS[dataset_name].name 76 if hyperlink: 77 dataset_link = DATASET_DETAILS[dataset_name].url 78 proper_dataset_name = add_hyperlink(proper_dataset_name, dataset_link, html, color) 79 return proper_dataset_name 80 81 82def add_hyperlink(text: str, link: str, html: bool = True, color: str = "#006DAA") -> str: 83 """ 84 :param text: The text to add the hyperlink to. 85 :param link: The URL to link to the text. 86 :param html: Boolean indicating if hyperlink should be added in HTML format. 87 :param color: The color of the text. 88 """ 89 return f'<a style="color:{color};" href="{link}">{text}</a>' if html else f"[{text}]({link})"
def
format_string( text: str, remove_underscore: bool = True, as_title: bool = False, as_score: bool = False, as_plot_title: bool = False, as_eval_name: bool = False, as_column_name: bool = False) -> str:
16def format_string( 17 text: str, 18 remove_underscore: bool = True, 19 as_title: bool = False, 20 as_score: bool = False, 21 as_plot_title: bool = False, 22 as_eval_name: bool = False, 23 as_column_name: bool = False, 24) -> str: 25 """ 26 :param text: text, name of the score or eval. 27 :param remove_underscore: Boolean indicating if underscores should be replaced with spaces. 28 :param as_title: Boolean indicating if the text is a title, if set to True will capitalize each word. 29 :param as_score: Boolean indicating if "score" should be appended to the text. 30 :param as_plot_title: Boolean indicating if this is a plot title. 31 :param as_eval_name: Boolean indicating if this is the name of an evaluation. 32 :param as_column_name: Boolean indicating if this is the name of a table column. 33 :return: formatted score name. 34 """ 35 formatted_text = _replace_strings(text, GENERAL_STRING_REPLACEMENTS) 36 if as_plot_title and EvalAlgorithm.PROMPT_STEREOTYPING.value in formatted_text: 37 formatted_text = _replace_strings(formatted_text, PLOT_TITLE_STRING_REPLACEMENTS) 38 remove_underscore = False 39 if as_column_name: 40 formatted_text = _replace_strings(formatted_text, COLUMN_NAME_STRING_REPLACEMENTS) 41 if as_eval_name: 42 formatted_text = _replace_strings(formatted_text, EVAL_NAME_STRING_REPLACEMENTS) 43 if remove_underscore: 44 if text not in AVOID_REMOVE_UNDERSCORE: # pragma: no branch 45 formatted_text = formatted_text.replace("_", " ") 46 if as_score: 47 formatted_text = _replace_strings(formatted_text, SCORE_STRING_REPLACEMENTS) 48 formatted_text = formatted_text if "score" in formatted_text.lower() else f"{formatted_text} score" 49 if as_title: 50 # Capitalize each word while preserving original capitalization within words 51 formatted_text = " ".join(w if w.isupper() else w[0].upper() + w[1:] for w in formatted_text.split()) 52 return formatted_text
Parameters
- text: text, name of the score or eval.
- remove_underscore: Boolean indicating if underscores should be replaced with spaces.
- as_title: Boolean indicating if the text is a title, if set to True will capitalize each word.
- as_score: Boolean indicating if "score" should be appended to the text.
- as_plot_title: Boolean indicating if this is a plot title.
- as_eval_name: Boolean indicating if this is the name of an evaluation.
- as_column_name: Boolean indicating if this is the name of a table column.
Returns
formatted score name.
def
format_dataset_name( dataset_name: str, hyperlink: bool = False, html: bool = True, color: str = '#006DAA') -> str:
66def format_dataset_name(dataset_name: str, hyperlink: bool = False, html: bool = True, color: str = "#006DAA") -> str: 67 """ 68 :param dataset_name: The name of the dataset. 69 :param hyperlink: Boolean indicating if hyperlink should be added to dataset name. 70 :param html: Boolean indicating if hyperlink should be added in HTML format. 71 :param color: The color of the text. 72 :return: Properly capitalized dataset name. 73 """ 74 if dataset_name not in DATASET_DETAILS: 75 return dataset_name 76 proper_dataset_name = DATASET_DETAILS[dataset_name].name 77 if hyperlink: 78 dataset_link = DATASET_DETAILS[dataset_name].url 79 proper_dataset_name = add_hyperlink(proper_dataset_name, dataset_link, html, color) 80 return proper_dataset_name
Parameters
- dataset_name: The name of the dataset.
- hyperlink: Boolean indicating if hyperlink should be added to dataset name.
- html: Boolean indicating if hyperlink should be added in HTML format.
- color: The color of the text.
Returns
Properly capitalized dataset name.
def
add_hyperlink(text: str, link: str, html: bool = True, color: str = '#006DAA') -> str:
83def add_hyperlink(text: str, link: str, html: bool = True, color: str = "#006DAA") -> str: 84 """ 85 :param text: The text to add the hyperlink to. 86 :param link: The URL to link to the text. 87 :param html: Boolean indicating if hyperlink should be added in HTML format. 88 :param color: The color of the text. 89 """ 90 return f'<a style="color:{color};" href="{link}">{text}</a>' if html else f"[{text}]({link})"
Parameters
- text: The text to add the hyperlink to.
- link: The URL to link to the text.
- html: Boolean indicating if hyperlink should be added in HTML format.
- color: The color of the text.