|
| 1 | +from collections import defaultdict |
| 2 | +from typing import Dict, List, Optional |
| 3 | + |
| 4 | +from nebulento.container import IntentContainer |
| 5 | +from nebulento.fuzz import MatchStrategy |
| 6 | + |
| 7 | + |
| 8 | +class DomainIntentContainer: |
| 9 | + """ |
| 10 | + A domain-aware intent recognition engine that organizes intents and entities |
| 11 | + into specific domains, providing flexible and hierarchical intent matching. |
| 12 | + """ |
| 13 | + |
| 14 | + def __init__(self, fuzzy_strategy=MatchStrategy.DAMERAU_LEVENSHTEIN_SIMILARITY, |
| 15 | + ignore_case=True): |
| 16 | + """ |
| 17 | + Initialize the DomainIntentContainer. |
| 18 | +
|
| 19 | + Attributes: |
| 20 | + domain_engine (IntentContainer): A top-level intent container for cross-domain calculations. |
| 21 | + domains (Dict[str, IntentContainer]): A mapping of domain names to their respective intent containers. |
| 22 | + training_data (Dict[str, List[str]]): A mapping of domain names to their associated training samples. |
| 23 | + """ |
| 24 | + self.fuzzy_strategy = fuzzy_strategy |
| 25 | + self.ignore_case = ignore_case |
| 26 | + self.domain_engine = IntentContainer(fuzzy_strategy=fuzzy_strategy, ignore_case=ignore_case) |
| 27 | + self.domains: Dict[str, IntentContainer] = {} |
| 28 | + self.training_data: Dict[str, List[str]] = defaultdict(list) |
| 29 | + self.must_train = True |
| 30 | + |
| 31 | + def remove_domain(self, domain_name: str): |
| 32 | + """ |
| 33 | + Remove a domain and its associated intents and training data. |
| 34 | +
|
| 35 | + Args: |
| 36 | + domain_name (str): The name of the domain to remove. |
| 37 | + """ |
| 38 | + if domain_name in self.training_data: |
| 39 | + self.training_data.pop(domain_name) |
| 40 | + if domain_name in self.domains: |
| 41 | + self.domains.pop(domain_name) |
| 42 | + if domain_name in self.domain_engine.intent_names: |
| 43 | + self.domain_engine.remove_intent(domain_name) |
| 44 | + |
| 45 | + def register_domain_intent(self, domain_name: str, intent_name: str, intent_samples: List[str]): |
| 46 | + """ |
| 47 | + Register an intent within a specific domain. |
| 48 | +
|
| 49 | + Args: |
| 50 | + domain_name (str): The name of the domain. |
| 51 | + intent_name (str): The name of the intent to register. |
| 52 | + intent_samples (List[str]): A list of sample sentences for the intent. |
| 53 | + """ |
| 54 | + if domain_name not in self.domains: |
| 55 | + self.domains[domain_name] = IntentContainer(fuzzy_strategy=self.fuzzy_strategy, |
| 56 | + ignore_case=self.ignore_case) |
| 57 | + self.domains[domain_name].add_intent(intent_name, intent_samples) |
| 58 | + self.training_data[domain_name] += intent_samples |
| 59 | + self.must_train = True |
| 60 | + |
| 61 | + def remove_domain_intent(self, domain_name: str, intent_name: str): |
| 62 | + """ |
| 63 | + Remove a specific intent from a domain. |
| 64 | +
|
| 65 | + Args: |
| 66 | + domain_name (str): The name of the domain. |
| 67 | + intent_name (str): The name of the intent to remove. |
| 68 | + """ |
| 69 | + if domain_name in self.domains: |
| 70 | + self.domains[domain_name].remove_intent(intent_name) |
| 71 | + |
| 72 | + def register_domain_entity(self, domain_name: str, entity_name: str, entity_samples: List[str]): |
| 73 | + """ |
| 74 | + Register an entity within a specific domain. |
| 75 | +
|
| 76 | + Args: |
| 77 | + domain_name (str): The name of the domain. |
| 78 | + entity_name (str): The name of the entity to register. |
| 79 | + entity_samples (List[str]): A list of sample phrases for the entity. |
| 80 | + """ |
| 81 | + if domain_name not in self.domains: |
| 82 | + self.domains[domain_name] = IntentContainer(fuzzy_strategy=self.fuzzy_strategy, |
| 83 | + ignore_case=self.ignore_case) |
| 84 | + self.domains[domain_name].add_entity(entity_name, entity_samples) |
| 85 | + |
| 86 | + def remove_domain_entity(self, domain_name: str, entity_name: str): |
| 87 | + """ |
| 88 | + Remove a specific entity from a domain. |
| 89 | +
|
| 90 | + Args: |
| 91 | + domain_name (str): The name of the domain. |
| 92 | + entity_name (str): The name of the entity to remove. |
| 93 | + """ |
| 94 | + if domain_name in self.domains: |
| 95 | + self.domains[domain_name].remove_entity(entity_name) |
| 96 | + |
| 97 | + def calc_domain(self, query: str): |
| 98 | + """ |
| 99 | + Calculate the best matching domain for a query. |
| 100 | +
|
| 101 | + Args: |
| 102 | + query (str): The input query. |
| 103 | +
|
| 104 | + Returns: |
| 105 | + MatchData: The best matching domain. |
| 106 | + """ |
| 107 | + return self.domain_engine.calc_intent(query) |
| 108 | + |
| 109 | + def calc_intent(self, query: str, domain: Optional[str] = None): |
| 110 | + """ |
| 111 | + Calculate the best matching intent for a query within a specific domain. |
| 112 | +
|
| 113 | + Args: |
| 114 | + query (str): The input query. |
| 115 | + domain (Optional[str]): The domain to limit the search to. Defaults to None. |
| 116 | +
|
| 117 | + Returns: |
| 118 | + MatchData: The best matching intent. |
| 119 | + """ |
| 120 | + domain: str = domain or self.domain_engine.calc_intent(query).name |
| 121 | + if domain in self.domains: |
| 122 | + return self.domains[domain].calc_intent(query) |
| 123 | + return {"best_match": None, |
| 124 | + "conf": 0, |
| 125 | + "match_strategy": self.fuzzy_strategy, |
| 126 | + "utterance": query, |
| 127 | + "name": None} |
0 commit comments