Skip to content

Commit

Permalink
Index word loom items by their literal default language text, as well
Browse files Browse the repository at this point in the history
  • Loading branch information
uogbuji committed Jun 24, 2024
1 parent 95fb488 commit b2ea02a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pylib/__about__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
# SPDX-License-Identifier: Apache-2.0
# ogbujipt.about

__version__ = '0.9.2'
__version__ = '0.10.0'
13 changes: 10 additions & 3 deletions pylib/word_loom.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ def clone(self, value=None, deflang=None, altlang=None, meta=None, markers=None)
def load(fp_or_str, lang='en', preserve_key=False):
'''
Read a word loom and return the tables as top-level result mapping
Loads the TOML, then selects text by given language
Loads the TOML
Return a dict of the language items, indexed by the TOML key as well as its default language text
fp_or_str - file-like object or string containing TOML
lang - select oly texts in this language (default: 'en')
Expand All @@ -97,9 +99,12 @@ def load(fp_or_str, lang='en', preserve_key=False):
>>> loom = word_loom.load(fp)
>>> loom['test_prompt_joke'].meta
{'tag': 'humor', 'updated': '2024-01-01'}
>>> str(loom['test_prompt_joke'])
>>> actual_text = loom['test_prompt_joke']
>>> str(actual_text)
'Tell me a funny joke about {topic}\n'
>>> str(loom[str(actual_text)])
'Tell me a funny joke about {topic}\n'
>>> loom['test_prompt_joke'].in_lang('fr')
>>> loom[str(actual_text)].in_lang('fr')
'Dites-moi une blague drôle sur {topic}\n'
'''
# Ensure we have a file-like object
Expand Down Expand Up @@ -136,4 +141,6 @@ def load(fp_or_str, lang='en', preserve_key=False):
if preserve_key:
meta['_key'] = k
texts[k] = T(text, lang, altlang=altlang, meta=meta, markers=markers)
# Also index by literal text
texts[text] = T(text, lang, altlang=altlang, meta=meta, markers=markers)
return texts

0 comments on commit b2ea02a

Please sign in to comment.