Improve mention detection to catch all variations
Enhanced MentionDetector to recognize bot name in more contexts: Detection patterns now include: - @botname anywhere (with word boundary) - botname with punctuation (: ! ? , .) - botname at start of message - botname anywhere as whole word (new catchall pattern) Content extraction improved to remove bot name from: - Start of message (with @mention or plain) - End of message (with optional punctuation) - Middle of message (with punctuation) Now responds to all these variations: - @kenearosmd Hi - kenearosmd: wie gehts? - kenearosmd was meinst du? - Hallo kenearosmd - kenearosmd! - Frage an kenearosmd hier
This commit is contained in:
parent
e9fc21e423
commit
0b2d166a58
1 changed files with 17 additions and 13 deletions
30
utils.py
30
utils.py
|
|
@ -14,13 +14,12 @@ class MentionDetector:
|
||||||
def __init__(self, bot_name="Eugen"):
|
def __init__(self, bot_name="Eugen"):
|
||||||
self.bot_name = bot_name
|
self.bot_name = bot_name
|
||||||
# Create patterns for various mention formats
|
# Create patterns for various mention formats
|
||||||
|
# More flexible patterns that catch most cases
|
||||||
self.patterns = [
|
self.patterns = [
|
||||||
rf"@{bot_name}", # @Eugen
|
rf"@{bot_name}\b", # @Eugen (with word boundary)
|
||||||
rf"{bot_name}:", # Eugen:
|
rf"\b{bot_name}[:!?.,]", # Eugen: Eugen! Eugen? Eugen, Eugen.
|
||||||
rf"{bot_name},", # Eugen,
|
rf"^{bot_name}\b", # Eugen at start of message
|
||||||
rf"^{bot_name}\s", # Eugen at start
|
rf"\b{bot_name}\b", # Eugen anywhere as whole word
|
||||||
rf"\s{bot_name}\s", # Eugen in middle
|
|
||||||
rf"\s{bot_name}$", # Eugen at end
|
|
||||||
]
|
]
|
||||||
# Case-insensitive compilation
|
# Case-insensitive compilation
|
||||||
self.compiled_patterns = [
|
self.compiled_patterns = [
|
||||||
|
|
@ -58,15 +57,20 @@ class MentionDetector:
|
||||||
if not message:
|
if not message:
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
# Remove common mention patterns
|
# Remove bot name mentions from the message
|
||||||
content = message
|
content = message
|
||||||
patterns_to_remove = [
|
|
||||||
rf"@{self.bot_name}[,:]?\s*",
|
|
||||||
rf"{self.bot_name}[,:]?\s*",
|
|
||||||
]
|
|
||||||
|
|
||||||
for pattern in patterns_to_remove:
|
# Remove @mention at start
|
||||||
content = re.sub(pattern, "", content, flags=re.IGNORECASE)
|
content = re.sub(rf"^@{self.bot_name}\b[,:]?\s*", "", content, flags=re.IGNORECASE)
|
||||||
|
|
||||||
|
# Remove bot name at start with optional punctuation
|
||||||
|
content = re.sub(rf"^{self.bot_name}\b[,:]?\s*", "", content, flags=re.IGNORECASE)
|
||||||
|
|
||||||
|
# Remove bot name at end with optional punctuation
|
||||||
|
content = re.sub(rf"\s*\b{self.bot_name}[,!?.]?\s*$", "", content, flags=re.IGNORECASE)
|
||||||
|
|
||||||
|
# Remove bot name in middle with punctuation
|
||||||
|
content = re.sub(rf"\s*\b{self.bot_name}[,:!?]\s*", " ", content, flags=re.IGNORECASE)
|
||||||
|
|
||||||
return content.strip()
|
return content.strip()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue