This commit is contained in:
Timothy Jaeryang Baek
2026-03-17 17:58:01 -05:00
parent fcf7208352
commit de3317e26b
220 changed files with 17200 additions and 22836 deletions

View File

@@ -1,16 +1,16 @@
import re
def extract_mentions(message: str, triggerChar: str = "@"):
def extract_mentions(message: str, triggerChar: str = '@'):
# Escape triggerChar in case it's a regex special character
triggerChar = re.escape(triggerChar)
pattern = rf"<{triggerChar}([A-Z]):([^|>]+)"
pattern = rf'<{triggerChar}([A-Z]):([^|>]+)'
matches = re.findall(pattern, message)
return [{"id_type": id_type, "id": id_value} for id_type, id_value in matches]
return [{'id_type': id_type, 'id': id_value} for id_type, id_value in matches]
def replace_mentions(message: str, triggerChar: str = "@", use_label: bool = True):
def replace_mentions(message: str, triggerChar: str = '@', use_label: bool = True):
"""
Replace mentions in the message with either their label (after the pipe `|`)
or their id if no label exists.
@@ -27,5 +27,5 @@ def replace_mentions(message: str, triggerChar: str = "@", use_label: bool = Tru
return label if use_label and label else id_value
# Regex captures: idType, id, optional label
pattern = rf"<{triggerChar}([A-Z]):([^|>]+)(?:\|([^>]+))?>"
pattern = rf'<{triggerChar}([A-Z]):([^|>]+)(?:\|([^>]+))?>'
return re.sub(pattern, replacer, message)