"""
Alias Service
Handles alias and synonym matching for common abbreviations and variations.
"""
import logging
from typing import Optional, Dict, List
from slot_resolution.core.models import ResolvedEntity
logger = logging.getLogger(__name__)
class AliasService:
"""
Service for alias and synonym matching.
Maintains a dictionary of common aliases and synonyms for entity names.
For example: "hi" -> "high", "IT" -> "Information Technology"
"""
# Default alias mappings
DEFAULT_ALIASES = {
"impact": {
"hi": "high",
"med": "medium",
"lo": "low",
"low": "low",
"medium": "medium",
"high": "high"
},
"urgency": {
"hi": "high",
"med": "medium",
"lo": "low",
"low": "low",
"medium": "medium",
"high": "high"
},
"priority": {
"hi": "high",
"med": "medium",
"lo": "low",
"p1": "critical",
"p2": "high",
"p3": "medium",
"p4": "low"
},
"department": {
"it": "information technology",
"hr": "human resources",
"ops": "operations",
"eng": "engineering",
"fin": "finance"
}
}
def __init__(self, custom_aliases: Optional[Dict[str, Dict[str, str]]] = None):
"""
Initialize the alias service.
Args:
custom_aliases: Optional custom alias mappings to merge with defaults
"""
self.aliases = self.DEFAULT_ALIASES.copy()
if custom_aliases:
# Merge custom aliases
for entity_type, alias_map in custom_aliases.items():
if entity_type in self.aliases:
self.aliases[entity_type].update(alias_map)
else:
self.aliases[entity_type] = alias_map
logger.info(
f"AliasService initialized with {len(self.aliases)} entity types"
)
def resolve_alias(
self,
entity_type: str,
normalized_query: str
) -> Optional[str]:
"""
Resolve alias to canonical form.
Args:
entity_type: Type of entity
normalized_query: Normalized search query
Returns:
Canonical form if alias found, None otherwise
"""
if entity_type not in self.aliases:
return None
alias_map = self.aliases[entity_type]
if normalized_query in alias_map:
canonical = alias_map[normalized_query]
logger.debug(
f"Alias resolved: {entity_type}.'{normalized_query}' -> '{canonical}'"
)
return canonical
return None
def add_alias(
self,
entity_type: str,
alias: str,
canonical: str
):
"""
Add a new alias mapping.
Args:
entity_type: Type of entity
alias: Alias to add
canonical: Canonical form
"""
if entity_type not in self.aliases:
self.aliases[entity_type] = {}
self.aliases[entity_type][alias] = canonical
logger.info(f"Added alias: {entity_type}.'{alias}' -> '{canonical}'")
def get_aliases(self, entity_type: str) -> Dict[str, str]:
"""
Get all aliases for an entity type.
Args:
entity_type: Type of entity
Returns:
Dictionary of alias mappings
"""
return self.aliases.get(entity_type, {})
def get_all_aliases(self) -> Dict[str, Dict[str, str]]:
"""
Get all alias mappings.
Returns:
Complete alias dictionary
"""
return self.aliases.copy()