chat: added tags_frequency() function and test

This commit is contained in:
2023-09-01 12:35:32 +02:00
parent d93598a74f
commit ddfe29b951
2 changed files with 17 additions and 3 deletions
+10 -1
View File
@@ -127,7 +127,16 @@ class Chat:
tags: set[Tag] = set()
for m in self.messages:
tags |= m.filter_tags(prefix, contain)
return tags
return set(sorted(tags))
def tags_frequency(self, prefix: Optional[str] = None, contain: Optional[str] = None) -> dict[Tag, int]:
"""
Get the frequency of all tags of all messages, optionally filtered by prefix or substring.
"""
tags: list[Tag] = []
for m in self.messages:
tags += [tag for tag in m.filter_tags(prefix, contain)]
return {tag: tags.count(tag) for tag in sorted(tags)}
def tokens(self) -> int:
"""