# Buggy code
def _features_for_patterns(
    self, message: Message, attribute: Text
) -> scipy.sparse.coo_matrix:
    """Checks which known patterns match the message.

    Given a sentence, returns a vector of {1,0} values indicating which
    regexes did match. Furthermore, if the
    message is tokenized, the function will mark all tokens with a dict
    relating the name of the regex to whether it was matched."""
    tokens = message.get(TOKENS_NAMES[attribute], [])
    seq_length = len(tokens)

    vec = np.zeros([seq_length, len(self.known_patterns)])

    for pattern_index, pattern in enumerate(self.known_patterns):
        matches = re.finditer(pattern["pattern"], message.text)
        matches = list(matches)

        for token_index, t in enumerate(tokens):
            patterns = t.get("pattern", default={})
            patterns[pattern["name"]] = False

            if t.text == CLS_TOKEN:
                # make sure to set all patterns for the CLS token to False
                # the attribute patterns is needed later on and in the tests
                t.set("pattern", patterns)
                continue

            for match in matches:
                if t.start < match.end() and t.end > match.start():
                    patterns[pattern["name"]] = True
                    vec[token_index][pattern_index] = 1.0
                    if attribute in [RESPONSE_ATTRIBUTE, TEXT_ATTRIBUTE]:
                        # CLS token vector should contain all patterns
                        vec[-1][pattern_index] = 1.0

            t.set("pattern", patterns)

    return scipy.sparse.coo_matrix(vec)


# Traceback (ValueError)
Traceback (most recent call last):
File "c:\users\saurabhd\appdata\local\programs\python\python36\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "c:\users\saurabhd\appdata\local\programs\python\python36\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Users\saurabhd\AppData\Local\Programs\Python\Python36\Scripts\rasa.exe\__main__.py", line 7, in <module>
File "c:\users\saurabhd\appdata\local\programs\python\python36\lib\site-packages\rasa\__main__.py", line 76, in main
cmdline_arguments.func(cmdline_arguments)
File "c:\users\saurabhd\appdata\local\programs\python\python36\lib\site-packages\rasa\cli\train.py", line 76, in train
kwargs=extract_additional_arguments(args),
File "c:\users\saurabhd\appdata\local\programs\python\python36\lib\site-packages\rasa\train.py", line 46, in train
kwargs=kwargs,
File "c:\users\saurabhd\appdata\local\programs\python\python36\lib\asyncio\base_events.py", line 484, in run_until_complete
return future.result()
File "c:\users\saurabhd\appdata\local\programs\python\python36\lib\site-packages\rasa\train.py", line 97, in train_async
kwargs,
File "c:\users\saurabhd\appdata\local\programs\python\python36\lib\site-packages\rasa\train.py", line 184, in _train_async_internal
kwargs=kwargs,
File "c:\users\saurabhd\appdata\local\programs\python\python36\lib\site-packages\rasa\train.py", line 241, in _do_training
persist_nlu_training_data=persist_nlu_training_data,
File "c:\users\saurabhd\appdata\local\programs\python\python36\lib\site-packages\rasa\train.py", line 470, in _train_nlu_with_validated_data
persist_nlu_training_data=persist_nlu_training_data,
File "c:\users\saurabhd\appdata\local\programs\python\python36\lib\site-packages\rasa\nlu\train.py", line 86, in train
interpreter = trainer.train(training_data, **kwargs)
File "c:\users\saurabhd\appdata\local\programs\python\python36\lib\site-packages\rasa\nlu\model.py", line 191, in train
updates = component.train(working_data, self.config, **context)
File "c:\users\saurabhd\appdata\local\programs\python\python36\lib\site-packages\rasa\nlu\featurizers\sparse_featurizer\regex_featurizer.py", line 67, in train
self._text_features_with_regex(example, attribute)
File "c:\users\saurabhd\appdata\local\programs\python\python36\lib\site-packages\rasa\nlu\featurizers\sparse_featurizer\regex_featurizer.py", line 76, in _text_features_with_regex
message, extras, feature_name=SPARSE_FEATURE_NAMES[attribute]
File "c:\users\saurabhd\appdata\local\programs\python\python36\lib\site-packages\rasa\nlu\featurizers\featurizer.py", line 67, in _combine_with_existing_sparse_features
f"Cannot concatenate sparse features as sequence dimension does not "
ValueError: Cannot concatenate sparse features as sequence dimension does not match: 7 != 1. Make sure to set 'return_sequence' to the same value for all your featurizers.

# Fixed code
def _features_for_patterns(
    self, message: Message, attribute: Text
) -> Optional[scipy.sparse.coo_matrix]:
    """Checks which known patterns match the message.

    Given a sentence, returns a vector of {1,0} values indicating which
    regexes did match. Furthermore, if the
    message is tokenized, the function will mark all tokens with a dict
    relating the name of the regex to whether it was matched."""

    # Attribute not set (e.g. response not present)
    if not message.get(attribute):
        return None

    tokens = message.get(TOKENS_NAMES[attribute], [])
    seq_length = len(tokens)

    vec = np.zeros([seq_length, len(self.known_patterns)])

    for pattern_index, pattern in enumerate(self.known_patterns):
        matches = re.finditer(pattern["pattern"], message.text)
        matches = list(matches)

        for token_index, t in enumerate(tokens):
            patterns = t.get("pattern", default={})
            patterns[pattern["name"]] = False

            if t.text == CLS_TOKEN:
                # make sure to set all patterns for the CLS token to False
                # the attribute patterns is needed later on and in the tests
                t.set("pattern", patterns)
                continue

            for match in matches:
                if t.start < match.end() and t.end > match.start():
                    patterns[pattern["name"]] = True
                    vec[token_index][pattern_index] = 1.0
                    if attribute in [RESPONSE_ATTRIBUTE, TEXT_ATTRIBUTE]:
                        # CLS token vector should contain all patterns
                        vec[-1][pattern_index] = 1.0

            t.set("pattern", patterns)

    return scipy.sparse.coo_matrix(vec)