Skip to content

Overview

This library also hosts some common tools to make it easier to investigate your training data or trained models from a jupyter notebook.

RasaClassifier

The RasaClassifier takes a pretrained Rasa model and turns it into a scikit-learn compatible estimator. It expects text as input and it will predict an intent class.

Usage:

from rasa_nlu_examples.scikit import RasaClassifier

mod = RasaClassifier(model_path="path/to/model.tar.gz")
mod.predict(["hello there", "are you a bot?"])

fetch_info_from_message(self, text_input)

Show source code in scikit/classifier.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
    def fetch_info_from_message(self, text_input):
        """
        Fetch all the info from a single text input. Can be used to also retreive entities.

        Usage:

        ```python
        from rasa_nlu_examples.scikit import RasaClassifier

        mod = RasaClassifier(model_path="path/to/model.tar.gz")
        mod.fetch_info_from_message("hello there")
        ```
        """
        return self.interpreter.interpreter.parse(text_input)

Fetch all the info from a single text input. Can be used to also retreive entities.

Usage:

from rasa_nlu_examples.scikit import RasaClassifier

mod = RasaClassifier(model_path="path/to/model.tar.gz")
mod.fetch_info_from_message("hello there")

predict(self, X)

Show source code in scikit/classifier.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
    def predict(self, X):
        """
        Makes a class prediction, scikit-style.

        Note that we do not support `predict_proba` because our API allows for
        confidence values that do not lie in the [0, 1] range.

        Usage:

        ```python
        from rasa_nlu_examples.scikit import RasaClassifier

        mod = RasaClassifier(model_path="path/to/model.tar.gz")
        mod.predict(["hello there", "are you a bot?"])
        ```
        """
        return np.array([self.fetch_info_from_message(x)["intent"]["name"] for x in X])

Makes a class prediction, scikit-style.

Note that we do not support predict_proba because our API allows for confidence values that do not lie in the [0, 1] range.

Usage:

from rasa_nlu_examples.scikit import RasaClassifier

mod = RasaClassifier(model_path="path/to/model.tar.gz")
mod.predict(["hello there", "are you a bot?"])

dataframe_to_nlu_file(dataf, write_path, text_col='text', label_col='intent')

Show source code in scikit/common.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
def dataframe_to_nlu_file(dataf, write_path, text_col="text", label_col="intent"):
    """
    Converts a single DataFrame file with intents into a intents file for Rasa.

    Note that you cannot use this method to add entities.

    Usage:

    ```python
    import pandas as pd
    from rasa_nlu_examples.scikit import dataframe_to_nlu_file

    df = pd.DataFrame([
        {"text": "i really really like this", "intent": "positive"},
        {"text": "i enjoy this", "intent": "positive"},
        {"text": "this is not my thing", "intent": "negative"}
    ])

    dataframe_to_nlu_file(df, write_path="path/to/nlu.yml")
    ```

    This will yield a file with the following contents:

    ```yaml
    version: 2.0
    nlu:
    - intent: negative
      examples: |
      - this is not my thing
    - intent: positive
      examples: |
      - i really really like this
      - i enjoy this
    ```
    """
    result = {"version": str(2.0), "nlu": []}
    for idx, group in dataf.groupby(label_col):
        intent = group[label_col].iloc[0]
        result["nlu"].append(
            {
                "intent": intent,
                "examples": [t for t in group[text_col]],
            }
        )
    dump = (
        yaml.dump(result, sort_keys=False, width=1000)
        .replace("examples:", "examples: |")
        .replace("  -", "   -")
    )
    return pathlib.Path(write_path).write_text(dump)

Converts a single DataFrame file with intents into a intents file for Rasa.

Note that you cannot use this method to add entities.

Usage:

import pandas as pd
from rasa_nlu_examples.scikit import dataframe_to_nlu_file

df = pd.DataFrame([
    {"text": "i really really like this", "intent": "positive"},
    {"text": "i enjoy this", "intent": "positive"},
    {"text": "this is not my thing", "intent": "negative"}
])

dataframe_to_nlu_file(df, write_path="path/to/nlu.yml")

This will yield a file with the following contents:

version: 2.0
nlu:
- intent: negative
  examples: |
  - this is not my thing
- intent: positive
  examples: |
  - i really really like this
  - i enjoy this

nlu_path_to_dataframe(path)

Show source code in scikit/common.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def nlu_path_to_dataframe(path):
    """
    Converts a single nlu file with intents into a dataframe.

    Usage:

    ```python
    from rasa_nlu_examples.scikit import nlu_path_to_dataframe

    df = nlu_path_to_dataframe("path/to/nlu/nlu.yml")
    ```
    """
    from rasa.nlu.convert import convert_training_data

    data = []
    p = pathlib.Path(path)
    name = p.parts[-1]
    name = name[: name.find(".")]
    convert_training_data(str(p), f"{name}.json", output_format="json", language="en")
    blob = json.loads(pathlib.Path(f"{name}.json").read_text())
    for d in blob["rasa_nlu_data"]["common_examples"]:
        data.append({"text": d["text"], "label": d["intent"]})
    pathlib.Path(f"{name}.json").unlink()
    return pd.DataFrame(data)

Converts a single nlu file with intents into a dataframe.

Usage:

from rasa_nlu_examples.scikit import nlu_path_to_dataframe

df = nlu_path_to_dataframe("path/to/nlu/nlu.yml")