def tag(self, utterance, context_trie=None):
tokens = self.tokenizer.tokenize(utterance)<EOL>entities = []<EOL>if len(self.regex_entities) > <NUM_LIT:0>:<EOL><INDENT>for part, idx in self._iterate_subsequences(tokens):<EOL><INDENT>local_trie = Trie()<EOL>for regex_entity in self.regex_entities:<EOL><INDENT>match = regex_entity.match(part)<EOL>groups = match.groupdict() if match else {}<EOL>for key in list(groups):<EOL><INDENT>match_str = groups.get(key)<EOL>local_trie.insert(match_str, (match_str, key))<EOL><DEDENT><DEDENT>sub_tagger = EntityTagger(local_trie, self.tokenizer, max_tokens=self.max_tokens)<EOL>for sub_entity in sub_tagger.tag(part):<EOL><INDENT>sub_entity['<STR_LIT>'] += idx<EOL>sub_entity['<STR_LIT>'] += idx<EOL>for e in sub_entity['<STR_LIT>']:<EOL><INDENT>e['<STR_LIT>'] = <NUM_LIT:0.5><EOL><DEDENT>entities.append(sub_entity)<EOL><DEDENT><DEDENT><DEDENT>additional_sort = len(entities) > <NUM_LIT:0><EOL>context_entities = []<EOL>for i in xrange(len(tokens)):<EOL><INDENT>part = '<STR_LIT:U+0020>'.join(tokens[i:])<EOL>for new_entity in self.trie.gather(part):<EOL><INDENT>new_entity['<STR_LIT:data>'] = list(new_entity['<STR_LIT:data>'])<EOL>entities.append({<EOL>'<STR_LIT>': new_entity.get('<STR_LIT>'),<EOL>'<STR_LIT:key>': new_entity.get('<STR_LIT:key>'),<EOL>'<STR_LIT>': i,<EOL>'<STR_LIT>': [new_entity],<EOL>'<STR_LIT>': i + len(self.tokenizer.tokenize(new_entity.get('<STR_LIT>'))) - <NUM_LIT:1>,<EOL>'<STR_LIT>': False<EOL>})<EOL><DEDENT>if context_trie:<EOL><INDENT>for new_entity in context_trie.gather(part):<EOL><INDENT>new_entity['<STR_LIT:data>'] = list(new_entity['<STR_LIT:data>'])<EOL>new_entity['<STR_LIT>'] *= <NUM_LIT>  <EOL>context_entities.append({<EOL>'<STR_LIT>': new_entity.get('<STR_LIT>'),<EOL>'<STR_LIT:key>': new_entity.get('<STR_LIT:key>'),<EOL>'<STR_LIT>': i,<EOL>'<STR_LIT>': [new_entity],<EOL>'<STR_LIT>': i + len(self.tokenizer.tokenize(new_entity.get('<STR_LIT>'))) - <NUM_LIT:1>,<EOL>'<STR_LIT>': True<EOL>})<EOL><DEDENT><DEDENT><DEDENT>additional_sort = additional_sort or len(entities) > <NUM_LIT:0><EOL>if additional_sort:<EOL><INDENT>entities = self._sort_and_merge_tags(entities + context_entities)<EOL><DEDENT>return entities<EOL>
Tag known entities within the utterance.
Args:
    utterance(str): a string of natural language text
    context_trie(trie): optional, a trie containing only entities from context
        for this request

Returns: dictionary, with the following keys
    match(str): the proper entity matched
    key(str): the string that was matched to the entity
    start_token(int): 0-based index of the first token matched
    end_token(int): 0-based index of the last token matched
    entities(list): a list of entity kinds as strings (Ex: Artist, Location)