From 5242fc776a877f3ce7e83db6ceb84fcf48a914d3 Mon Sep 17 00:00:00 2001
From: Matheus Faria <matheus.sousa.faria@gmail.com>
Date: Thu, 22 Mar 2018 21:35:29 -0300
Subject: [PATCH] Fix non-root level interactions being trained on root level

---
 scripts/bot/classifier.coffee | 65 +++++++++++++++++++----------------
 1 file changed, 35 insertions(+), 30 deletions(-)

diff --git a/scripts/bot/classifier.coffee b/scripts/bot/classifier.coffee
index 535ef9e..ae608ca 100644
--- a/scripts/bot/classifier.coffee
+++ b/scripts/bot/classifier.coffee
@@ -17,34 +17,38 @@ actionHandler = require './action-handler'
 root_classifier = {}
 error_count = 0
 
+ROOT_LEVEL_NAME = "root"
+
 classifyInteraction = (interaction, classifier) ->
-  if Array.isArray interaction.expect
-    for doc in interaction.expect
-      if interaction.multi == true
-        classifier.addDocument(doc, interaction.name + '|' + doc)
-      else
-        classifier.addDocument(doc, interaction.name)
-
-    if Array.isArray interaction.next?.interactions
-      interaction.next.classifier = new natural.LogisticRegressionClassifier(
-        PorterStemmer
-      )
-      for nextInteractionName in interaction.next.interactions
-        nextInteraction = global.config.interactions.find (n) ->
-          return n.name is nextInteractionName
-        if not nextInteraction?
-          console.log 'No valid interaction for', nextInteractionName
-          continue
-        classifyInteraction nextInteraction, interaction.next.classifier
-      interaction.next.classifier.train()
-
-    if interaction.multi == true
-      interaction.classifier = new natural.LogisticRegressionClassifier(
-        PorterStemmer
-      )
-      for doc in interaction.expect
-        interaction.classifier.addDocument(doc, doc)
-      interaction.classifier.train()
+  if not interaction.expect?
+    console.warn("\t!! Interaction with no expects: " + interaction.name)
+    return
+
+  console.log('\tProcessing interaction: ' + interaction.name)
+
+  if not Array.isArray interaction.expect
+    interaction.expect = [interaction.expect]
+
+  for doc in interaction.expect
+    if typeof(doc) != 'string'
+      doc = '' + doc
+    classifier.addDocument(doc, interaction.name)
+
+  if interaction.next?.interactions? and not interaction.next?.classifier?
+    if not Array.isArray interaction.next.interactions
+      interactions.next.interactions = [interactions.next.interactions]
+
+    interaction.next.classifier = new natural.LogisticRegressionClassifier(
+      PorterStemmer
+    )
+    for nextInteractionName in interaction.next.interactions
+      nextInteraction = global.config.interactions.find (n) ->
+        return n.name is nextInteractionName
+      if not nextInteraction?
+        console.log 'No valid interaction for', nextInteractionName
+        continue
+      classifyInteraction nextInteraction, interaction.next.classifier
+    interaction.next.classifier.train()
 
 classifier.train = () ->
   console.log 'Processing interactions'
@@ -53,11 +57,12 @@ classifier.train = () ->
   root_classifier = new natural.LogisticRegressionClassifier(PorterStemmer)
 
   for interaction in global.config.interactions
-    if interaction.level != 'context'
+    if (not interaction.level? or
+        (Array.isArray(interaction.level) and
+         interaction.level.includes(ROOT_LEVEL_NAME)) or
+        interaction.level == ROOT_LEVEL_NAME)
       classifyInteraction interaction, root_classifier
 
-    console.log('\tProcessing interaction: ' + interaction.name)
-
   console.log 'Training Bot (This could be take a while...)'
   root_classifier.train()
 
-- 
GitLab