//private TextProcessor() //{ //} //public static TextProcessor Instance //{ // get // { // if (instance == null) // { // instance = new TextProcessor(); // } // return instance; // } //} public async Task <ProcessingResult> Predict(string textToPredict) { textToPredict = textToPredict.Trim(); textToPredict = textToPredict.ToLower(); MobileServiceInvalidOperationException exception = null; try { // This code refreshes the entries in the list view by querying the IntentItems table. // The query excludes completed IntentItems. items = await intentTable .Where(IntentItem => IntentItem.Utterance == (textToPredict)) .ToCollectionAsync(); telemetryItems = await telemetryTable.Take(1) .ToCollectionAsync(); if (items.Count > 0) { var item = items[0]; var processingResult = new ProcessingResult(); processingResult.Entities = JsonConvert.DeserializeObject <Dictionary <string, string> >(item.JsonEntities); processingResult.Intent = item.Intent; processingResult.IsFromCache = true; await Track(TelemetryEvents.CACHE_HIT, textToPredict); await Track(TelemetryEvents.LUIS_INTENT, processingResult.Intent); return(processingResult); } } catch (MobileServiceInvalidOperationException e) { exception = e; Debug.WriteLine(e); throw e; } await Track(TelemetryEvents.CACHE_MISS, textToPredict); try { LuisResult res = await _client.Predict(textToPredict); var intent = processRes(res); var temp = new ProcessingResult(); temp.Confidence = intent.Confidence; temp.Entities = intent.Entities; temp.Intent = intent.Intent; temp.IsFromCache = true; var intentItem = new IntentItem(); intentItem.Intent = temp.Intent; intentItem.Utterance = textToPredict; intentItem.IsProcessed = true; intentItem.JsonEntities = JsonConvert.SerializeObject(temp.Entities); // Insert into database await InsertIntentItem(intentItem); await Track(TelemetryEvents.LUIS_INTENT, intentItem.Intent); return(intent); } catch (MobileServicePushFailedException push) { // Debug.WriteLine(push); throw push; } // TODO: Refactor to have a better error handling catch (System.Exception e) { // Check App ID, Luis Key , Luis URL // Trace error Debug.WriteLine(e.Message); Debug.WriteLine(e); } // Save offline var offlineIntent = new IntentItem(); offlineIntent.Utterance = textToPredict; offlineIntent.IsProcessed = false; await InsertIntentItem(offlineIntent); return(null); }
public IMobileServiceTableQuery <T> Take(int count) { return(_syncTable.Take(count)); }