Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for text features #5200

Merged
merged 4 commits into from
Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/5199.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
If no text features are present in ``EmbeddingIntentClassifier`` return the intent ``None``.
16 changes: 16 additions & 0 deletions rasa/nlu/classifiers/embedding_intent_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,16 @@ def _calculate_message_sim(
# transform sim to python list for JSON serializing
return label_ids, message_sim.tolist()

@staticmethod
def _text_features_present(session_data: SessionDataType) -> bool:
return np.array(
[
f.nnz != 0 if isinstance(f, scipy.sparse.spmatrix) else f.any()
for features in session_data["text_features"]
for f in features
]
).any()

def predict_label(
self, message: "Message"
) -> Tuple[Dict[Text, Any], List[Dict[Text, Any]]]:
Expand All @@ -835,6 +845,12 @@ def predict_label(

# create session data from message and convert it into a batch of 1
session_data = self._create_session_data([message])

# if no text-features are present (e.g. incoming message is not in the
# vocab), do not predict a random intent
if not self._text_features_present(session_data):
tabergma marked this conversation as resolved.
Show resolved Hide resolved
return label, label_ranking

batch = train_utils.prepare_batch(
session_data, tuple_sizes=self.batch_tuple_sizes
)
Expand Down
26 changes: 26 additions & 0 deletions tests/nlu/classifiers/test_embedding_intent_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,29 @@ async def test_margin_loss_is_not_normalized(

# make sure top ranking is reflected in intent prediction
assert parse_data.get("intent") == intent_ranking[0]


@pytest.mark.parametrize(
"session_data, expected",
[
(
{
"text_features": [
np.array(
[
np.random.rand(5, 14),
np.random.rand(2, 14),
np.random.rand(3, 14),
]
)
]
},
True,
),
({"text_features": [np.array([0, 0, 0])]}, False),
({"text_features": [scipy.sparse.csr_matrix([0, 0, 0])]}, False),
({"text_features": [scipy.sparse.csr_matrix([0, 31, 0])]}, True),
],
)
def test_text_features_present(session_data, expected):
assert EmbeddingIntentClassifier._text_features_present(session_data) == expected