imgutils.tagging.order
sort_tags
- imgutils.tagging.order.sort_tags(tags: List[str] | Mapping[str, float], mode: Literal['original', 'shuffle', 'score'] = 'score') → List[str][source]
Sort the input list or mapping of tags by specified mode.
Tags can represent people counts (e.g., ‘1girl’, ‘2boys’), and ‘solo’ tags.
- Parameters:
tags (Union[List[str], Mapping[str, float]]) – List or mapping of tags to be sorted.
mode (Literal['original', 'shuffle', 'score']) – The mode for sorting the tags. Options: ‘original’ (original order), ‘shuffle’ (random shuffle), ‘score’ (sorted by score if available).
- Returns:
Sorted list of tags based on the specified mode.
- Return type:
List[str]
- Raises:
ValueError – If an unknown sort mode is provided.
TypeError – If the input tags are of unsupported type or if mode is ‘score’ and the input is a list (as it does not have scores).
- Examples:
Sorting tags in original order:
>>> from imgutils.tagging import sort_tags >>> >>> tags = ['1girls', 'solo', 'red_hair', 'cat ears'] >>> sort_tags(tags, mode='original') ['solo', '1girls', 'red_hair', 'cat ears'] >>> >>> tags = {'1girls': 0.9, 'solo': 0.95, 'red_hair': 1.0, 'cat_ears': 0.92} >>> sort_tags(tags, mode='original') ['solo', '1girls', 'red_hair', 'cat_ears']
Sorting tags by score (for a mapping of tags with scores):
>>> from imgutils.tagging import sort_tags >>> >>> tags = {'1girls': 0.9, 'solo': 0.95, 'red_hair': 1.0, 'cat_ears': 0.92} >>> sort_tags(tags) ['solo', '1girls', 'red_hair', 'cat_ears']
Shuffling tags (output is not unique)
>>> from imgutils.tagging import sort_tags >>> >>> tags = ['1girls', 'solo', 'red_hair', 'cat ears'] >>> sort_tags(tags, mode='shuffle') ['solo', '1girls', 'red_hair', 'cat ears'] >>> >>> tags = {'1girls': 0.9, 'solo': 0.95, 'red_hair': 1.0, 'cat_ears': 0.92} >>> sort_tags(tags, mode='shuffle') ['solo', '1girls', 'cat_ears', 'red_hair']