NLP Behind Chatbots — Demystifying RasaNLU — #1 — Training

Bhavani Ravi
Jul 11, 2018 · 7 min read

Chatbots & Me

NLP & Me

Before you jump in

Reading codeThe starting point

$ python -m rasa_nlu.train \
--config sample_configs/config_spacy.yml \
--data data/examples/rasa/demo-rasa.json \
--path projects
$ python -m rasa_nlu.server --path projects

Training

1. Configuration

> cat config_spacy.ymllanguage: "en"
pipeline: "spacy_sklearn"

2. Loading the training data

{
"text": "show me chinese restaurants",
"intent": "restaurant_search",
"entities": [
{
"start": 8,
"end": 15,
"value": "chinese",
"entity": "cuisine",
"extractor": "ner_crf",
"confidence": 0.854,
"processors": []
}
]
}

3. Training the ML model

context = {}
for i, component in enumerate(self.pipeline):
updates = component.train(working_data, self.config,**context)
if updates:
context.update(updates)

Preprocessing

3.1 SpacyNLP

>>> import spacy
>>> nlp = spacy.load('en')

3.2 SpacyTokenizer

>>> tokens = nlp("Suggest me a chinese food")
["suggest", "Me", "a", "chinese", "food"]

3.3 SpacyFeaturizer

>>> features = [token.vector for token in tokens][ 1.77235818e+00  2.89104319e+00  1.34855950e+00  4.57144260e-01
-1.24784541e+00 3.25931263e+00 -6.40985250e-01 -1.46328235e+00
-5.12969136e-01 -2.17798877e+00 -3.69897425e-01 4.26086336e-01...

3.4 RegexFeaturizer

found = []
for i, exp in enumerate(self.known_patterns):
match = re.search(exp["pattern"], message.text)
if <match_in_token>:
found.append(1.0)
else:
found.append(0.0)

Entity Extraction

3.5 NER_CRF EntityExactor

"entities": [
{
"start": 8,
"end": 15,
"value": "chinese",
"entity": "cuisine",
"extractor": "ner_crf",
"confidence": 0.854,
"processors": []
}

3.6 EntitySynonymMapper

# Input -> Training_data.json"entity_synonyms": [
{
"value": "vegetarian",
"synonyms": ["veg", "vegg", "veggie"]
}
# Output -> entity_synonym.json{'veggie': 'vegetarian', 'vegg': 'vegetarian'}

Intent Classification

# training data
>>> X_train =
[ 1.77235818e+00 2.89104319e+00 1.34855950e+00 4.57144260e-01 -1.24784541e+00 3.25931263e+00 -6.40985250e-01 -1.46328235e+00...
# features
>>> Y = ["greet", "bye", "restaurant_search", "greet"...
>>> Y_train = LabelEncoder().fit_transform(Y)
>>> Y_train
[0, 1, 2 ,0...
# training the ML model
>>> clf = GridSearchCV(SVC(...))
>>> clf.train(X_train, Y_train)

4. Storing the model in a persisted path

crf_model.pkl
entity_synonyms.json
intent_classifier_sklearn.pkl
regex_featurizer.json
training_data.json
model_metadata.json

bhavaniravi

Everything I learn and experience in Tech

Bhavani Ravi

Written by

🔸 Software engineer @kissflow🔸 Code — Speak — Write — Teach 🔸 Python — Chatbots — ML 🔸 WomenInTech 🔸

bhavaniravi

Everything I learn and experience in Tech