예제 #1
0
 /// <summary>
 /// Classifies text using IntentGateway and commonchat classifiers and combines the results
 /// </summary>
 /// <param name="intentGatewayService">Intent Gateway Service to use for classification</param>
 /// <param name="threshold">minimum threshold to be considered a valid classification</param>
 /// <param name="message">text to classify</param>
 /// <param name="ignoreCache">Flag to ignore the cached result for the phrase if available</param>
 /// <param name="classifierData">Data to pass to the intent gateway classifier</param>
 /// <param name="allowSmsClassifications">Set to true to allow classifications specific to SMS channel (ex: commonchat-Stop)</param>
 /// <returns></returns>
 public async Task <ClassificationResults> ClassifyAsync(IntentGatewayService intentGatewayService, double threshold, string message, bool ignoreCache, object classifierData, bool allowSmsClassifications)
 {
     return(await ClassifyAsync(IntentGatewayCategory, intentGatewayService, threshold, message, ignoreCache, classifierData, allowSmsClassifications));
 }
예제 #2
0
        async Task <ClassificationResults> ClassifyAsync(string classifiers, IntentGatewayService intentGatewayService, double threshold, string message, bool ignoreCache, object classifierData, bool allowSmsClassifications)
        {
            if (String.IsNullOrEmpty(message))
            {
                return(new ClassificationResults(0.0));
            }

            string hash = CreateHashValue(message, classifiers, threshold, classifierData);

            if (!ignoreCache)
            {
                var cacheResult = CheckCacheResult(message, hash);
                if (cacheResult != null)
                {
                    return(cacheResult);
                }
            }

            if (String.IsNullOrEmpty(classifiers))
            {
                classifiers = "main";
            }

            List <Task> tasks = new List <Task>();

            Task <ClassificationResponse[]> classifyTask = null;

            if (classifiers == IntentGatewayCategory)
            {
                classifyTask = intentGatewayService.Classify(classifierData);
            }
            else if (classifiers != CommonChatCategory)
            {
                classifyTask = InternalClassify(classifiers, message, threshold);
            }

            var commonTask = CheckForCommonChat(message, allowSmsClassifications);

            tasks.Add(commonTask);

            if (classifyTask != null)
            {
                tasks.Add(classifyTask);
            }

            await Task.WhenAll(tasks);

            var response       = classifyTask?.Result;
            var commonResponse = commonTask.Result;

            ClassificationResults results = new ClassificationResults(threshold)
            {
                ClassifierResults = response,
                CommonChatResults = commonResponse
            };

            await UpdateReasons(results.TopResults);

            if (!ignoreCache && memcacheService != null && (response?.Length > 0) && (commonResponse?.Length > 0))
            {
                memcacheService.SaveObject(hash, results, ChatConfiguration.ClassificationCacheTimeoutSeconds);
            }

            return(results);
        }