Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
alwx committed Nov 3, 2020
1 parent 837f2ca commit d21cf92
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 32 deletions.
5 changes: 3 additions & 2 deletions docs/docs/migrate-from/google-dialogflow-to-rasa.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ To send a request to the server, run:

```bash
curl 'localhost:5005/model/parse?emulation_mode=dialogflow' -d '{"text": "hello"}'
The `emulation_mode` parameter tells Rasa that you want your json
response to have the same format as you would get from dialogflow.
The `emulation_mode` parameter tells Rasa that you want your JSON response to have the same format as you would
get from DialogFlow sessions.detectIntent method (the format is
described [here](https://cloud.google.com/dialogflow/es/docs/reference/rest/v2/DetectIntentResponse)).
You can also leave it out to get the result in the usual Rasa format.

## Terminology:
Expand Down
6 changes: 4 additions & 2 deletions rasa/nlu/emulators/dialogflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ def normalise_response_json(self, data: Dict[Text, Any]) -> Dict[Text, Any]:
"""
entities = {
entity_type: []
for entity_type in {x[ENTITY_ATTRIBUTE_TYPE] for x in data[ENTITIES]}
for entity_type in {
entity[ENTITY_ATTRIBUTE_TYPE] for entity in data[ENTITIES]
}
}

for entity in data[ENTITIES]:
Expand All @@ -49,6 +51,6 @@ def normalise_response_json(self, data: Dict[Text, Any]) -> Dict[Text, Any]:
"fulfillmentText": "",
"fulfillmentMessages": [],
"outputContexts": [],
"intentDetectionConfidence": data[INTENT][PREDICTED_CONFIDENCE_KEY]
"intentDetectionConfidence": data[INTENT][PREDICTED_CONFIDENCE_KEY],
},
}
50 changes: 24 additions & 26 deletions rasa/shared/nlu/training_data/formats/dialogflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,27 @@ def read(self, fn: Text, **kwargs: Any) -> "TrainingData":
)

root_js = rasa.shared.utils.io.read_json_file(fn)
examples_js = self._read_examples_js(fn, language, fformat)
examples = self._read_examples(fn, language, fformat)

if not examples_js:
if not examples:
rasa.shared.utils.io.raise_warning(
f"No training examples found for dialogflow file {fn}!",
docs=DOCS_URL_MIGRATE_GOOGLE,
)
return TrainingData()
elif fformat == DIALOGFLOW_INTENT:
return self._read_intent(root_js, examples_js)
return self._read_intent(root_js, examples)
else: # path for DIALOGFLOW_ENTITIES
return self._read_entities(root_js, examples_js)
return self._read_entities(root_js, examples)

def _read_intent(
self, intent_js: Dict[Text, Any], examples_js: List[Dict[Text, Any]]
self, intent: Dict[Text, Any], examples: List[Dict[Text, Any]]
) -> "TrainingData":
"""Reads the intent and examples from respective jsons."""
intent = intent_js.get("name")
intent = intent.get("name")

training_examples = []
for ex in examples_js:
for ex in examples:
text, entities = self._join_text_chunks(ex["data"])
training_examples.append(Message.build(text, intent, entities))

Expand Down Expand Up @@ -100,47 +100,45 @@ def _flatten(list_of_lists: List[List[Any]]) -> List[Any]:

@staticmethod
def _extract_lookup_tables(
entity_js: Dict[Text, Any], examples_js: List[Dict[Text, Any]]
entity: Dict[Text, Any], examples: List[Dict[Text, Any]]
) -> Optional[List[Dict[Text, Any]]]:
"""Extract the lookup table from the entity synonyms."""
synonyms = [e["synonyms"] for e in examples_js if "synonyms" in e]
"""Extracts the lookup table from the entity synonyms."""
synonyms = [e["synonyms"] for e in examples if "synonyms" in e]
synonyms = DialogflowReader._flatten(synonyms)
elements = [synonym for synonym in synonyms if "@" not in synonym]

if len(elements) == 0:
return None
return [{"name": entity_js.get("name"), "elements": elements}]
return [{"name": entity.get("name"), "elements": elements}]

@staticmethod
def _extract_regex_features(
entity_js: Dict[Text, Any], examples_js: List[Dict[Text, Any]]
) -> Optional[List[Dict[Text, Any]]]:
entity: Dict[Text, Any], examples: List[Dict[Text, Any]]
) -> List[Dict[Text, Any]]:
"""Extract the regex features from the entity synonyms."""
synonyms = [e["synonyms"] for e in examples_js if "synonyms" in e]
synonyms = [e["synonyms"] for e in examples if "synonyms" in e]
synonyms = DialogflowReader._flatten(synonyms)
return [
{"name": entity_js.get("name"), "pattern": synonym} for synonym in synonyms
{"name": entity.get("name"), "pattern": synonym} for synonym in synonyms
]

@staticmethod
def _read_entities(
entity_js: Dict[Text, Any], examples_js: List[Dict[Text, Any]]
entity: Dict[Text, Any], examples: List[Dict[Text, Any]]
) -> "TrainingData":
entity_synonyms = transform_entity_synonyms(examples_js)
entity_synonyms = transform_entity_synonyms(examples)

if entity_js["isRegexp"]:
regex_features = DialogflowReader._extract_regex_features(
entity_js, examples_js
)
if entity["isRegexp"]:
regex_features = DialogflowReader._extract_regex_features(entity, examples)
return TrainingData([], entity_synonyms, regex_features, [],)
else:
lookup_tables = DialogflowReader._extract_lookup_tables(
entity_js, examples_js
)
lookup_tables = DialogflowReader._extract_lookup_tables(entity, examples)
return TrainingData([], entity_synonyms, [], lookup_tables,)

@staticmethod
def _read_examples_js(fn: Text, language: Text, fformat: Text) -> Any:
def _read_examples(
fn: Text, language: Text, fformat: Text
) -> Optional[List[Dict[Text, Any]]]:
"""Infer and load the example file based on the root
filename and root format."""

Expand All @@ -155,5 +153,5 @@ def _read_examples_js(fn: Text, language: Text, fformat: Text) -> Any:
else:
return None

def reads(self, s, **kwargs):
def reads(self, s: Text, **kwargs: Any) -> "TrainingData":
raise NotImplementedError
4 changes: 2 additions & 2 deletions tests/nlu/emulators/test_dialogflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ def test_dialogflow_response():
"fulfillmentText": "",
"fulfillmentMessages": [],
"outputContexts": [],
"intentDetectionConfidence": data["intent"]["confidence"]
}
"intentDetectionConfidence": data["intent"]["confidence"],
},
}

0 comments on commit d21cf92

Please sign in to comment.