# -*- coding: utf-8 -*-## Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.# Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at# http://www.apache.org/licenses/LICENSE-2.0# Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License."""This module contains classes related to encoding labels for models defined in the modelssubpackage."""importloggingfrom.helpersimportregister_labelfrom.taggers.taggersimport(get_entities_from_tags,get_tags_from_entities,)from..system_entity_recognizerimportSystemEntityRecognizerlogger=logging.getLogger(__name__)
[docs]classLabelEncoder:"""The label encoder is responsible for converting between rich label objects such as a ProcessedQuery and basic formats a model can interpret. A MindMeld model uses its label encoder at fit time to encode labels into a form it can deal with, and at predict time to decode predictions into objects """def__init__(self,config):"""Initializes an encoder Args: config (ModelConfig): The model """self.config=config
[docs]@staticmethoddefencode(labels,**kwargs):"""Transforms a list of label objects into a vector of classes. Args: labels (list): A list of labels to encode """delkwargsreturnlabels
[docs]@staticmethoddefdecode(classes,**kwargs):"""Decodes a vector of classes into a list of labels Args: classes (list): A list of classes Returns: list: The decoded labels """delkwargsreturnclasses
[docs]classEntityLabelEncoder(LabelEncoder):def__init__(self,config):"""Initializes an encoder Args: config (ModelConfig): The model configuration """self.config=configdef_get_tag_scheme(self):returnself.config.model_settings.get("tag_scheme","IOB").upper()
[docs]defencode(self,labels,**kwargs):"""Gets a list of joint app and system IOB tags from each query's entities. Args: labels (list): A list of labels associated with each query kwargs (dict): A dict containing atleast the "examples" key, which is a list of queries to process Returns: list: A list of list of joint app and system IOB tags from each query's entities """examples=kwargs["examples"]scheme=self._get_tag_scheme()# Here each label is a list of entities for the corresponding exampleall_tags=[]foridx,labelinenumerate(labels):all_tags.append(get_tags_from_entities(examples[idx],label,scheme))returnall_tags
[docs]defdecode(self,tags_by_example,**kwargs):"""Decodes the labels from the tags passed in for each query Args: tags_by_example (list): A list of tags per query kwargs (dict): A dict containing at least the "examples" key, which is a list of queries to process Returns: list: A list of decoded labels per query """examples=kwargs["examples"]labels=[get_entities_from_tags(examples[idx],tags,SystemEntityRecognizer.get_instance())foridx,tagsinenumerate(tags_by_example)]returnlabels