From 15f6e819e5022c5fe76ca125040d9940437f53df Mon Sep 17 00:00:00 2001 From: juk0de Date: Mon, 5 Feb 2024 07:56:34 +0100 Subject: [PATCH] glossary: added 'to_str()' function --- chatmastermind/glossary.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/chatmastermind/glossary.py b/chatmastermind/glossary.py index ef3fa18..b412ecf 100644 --- a/chatmastermind/glossary.py +++ b/chatmastermind/glossary.py @@ -138,3 +138,20 @@ class Glossary: self.entries[parts[0]] = parts[1] except Exception as e: raise GlossaryError(f"Error importing TSV: {e}") + + def to_str(self, with_entries: bool = False) -> str: + """ + Return the current glossary as a string. + """ + output: list[str] = [] + output.append(f'{self.name} (ID: {self.ID}):') + if self.desc: + output.append('- ' + self.desc) + output.append(f'- Languages: {self.source_lang} -> {self.target_lang}') + if with_entries: + output.append('- Entries:') + for source, target in self.entries.items(): + output.append(f' {source} : {target}') + else: + output.append(f'- Entries: {len(self.entries)}') + return '\n'.join(output)