public void GetRecommendationsSingleRequest(string modelId, long buildId, string itemIds) { updateUI(true); string AccountKey = "564eabe9165948af88bf705a56dcd42e"; // <--- Set to your API key here. string BaseUri = "https://westus.api.cognitive.microsoft.com/recommendations/v4.0"; RecommendationsApiWrapper recommender = new RecommendationsApiWrapper(AccountKey, BaseUri); var itemSets = recommender.GetRecommendations(modelId, buildId, itemIds, 6); recomendaciones.Clear(); var listView = new ListView(); listView.ItemsSource = recomendaciones; if (itemSets.RecommendedItemSetInfo != null) { foreach (RecommendedItemSetInfo recoSet in itemSets.RecommendedItemSetInfo) { foreach (var item in recoSet.Items) { recomendaciones.Add(new Knowledge.ModeloVista() { DisplayName = item.Name, Codigo = item.Id }); } } } else { } updateUI(false); }
public IDictionary <string, string> Execute(IEnumerable <ResolvedTable> inputTables, IEnumerable <ResolvedTable> outputTables, IDictionary <string, string> extendedProperties, IActivityLogger logger) { logger.Write(System.Diagnostics.TraceEventType.Information, "Start activity"); modelid = extendedProperties["modelid"]; accountKey = extendedProperties["accountKey"]; baseUri = extendedProperties["baseuri"]; logger.Write(System.Diagnostics.TraceEventType.Information, "modelId: {0}; AccountKey:{1}; BaseUri: {2}", modelid, accountKey, baseUri); logger.Write(System.Diagnostics.TraceEventType.Information, "Start with Export"); try { ResolvedTable inputDataset = datasets.Single(dataset => dataset.Name == activity.Inputs.Single().Name); ResolvedTable = datasets.Single(dataset => dataset.Name == activity.Outputs.Single().Name); RecommendationsApiWrapper api = new RecommendationsApiWrapper(accountKey, baseUri); } catch (Exception ex) { logger.Write(System.Diagnostics.TraceEventType.Error, "Error occurred: {0} - {1}", ex.Message, ex.InnerException.ToString()); } logger.Write(System.Diagnostics.TraceEventType.Information, "end"); return(new Dictionary <string, string>()); }
public async Task <HttpResponseMessage> Post([FromBody] Activity message) { ConnectorClient connector = new ConnectorClient(new Uri(message.ServiceUrl)); TMDbClient client = new TMDbClient("f345648a16265462c06f2d0136d0c9bd"); var builder = new ContainerBuilder(); builder.Register(c => new CachingBotDataStore(c.Resolve <ConnectorStore>(), CachingBotDataStoreConsistencyPolicy.LastWriteWins)) .As <IBotDataStore <BotData> >() .AsSelf() .InstancePerLifetimeScope(); builder.Update(Conversation.Container); if (message.Type == "message" && message.Text[0] != '/') { string MovieString = "Sorry, I am not getting you..."; StockLUIS StLUIS = await GetEntityFromLUIS(message.Text); if (StLUIS.topScoringIntent != null) { if (StLUIS.topScoringIntent.intent == "Greeting") { StateClient stateClient = message.GetStateClient(); BotData userData = await stateClient.BotState.GetUserDataAsync(message.ChannelId, message.From.Id); string ActualName = userData.GetProperty <string>("Name"); if (userData.GetProperty <string>("Name") != null) { if (StLUIS.entities.Count() > 0) { MovieString = StLUIS.entities[0].entity + ", " + ActualName; } else { MovieString = "Hello, " + userData.Data; } } else { if (StLUIS.entities.Count() > 0) { MovieString = StLUIS.entities[0].entity + ", what's your name?"; } else { MovieString = "Hello, what's your name?"; } } } if (StLUIS.topScoringIntent.intent == "Name") { foreach (Entity2 NameName in StLUIS.entities) { if (NameName.type == "Greeting") { Activity talk = message.CreateReply(StLUIS.entities[0].entity); await connector.Conversations.ReplyToActivityAsync(talk); } if (NameName.type == "Name") { //userData.Data = NameName.entity; StateClient stateClient = message.GetStateClient(); BotData userData = await stateClient.BotState.GetUserDataAsync(message.ChannelId, message.From.Id); userData.SetProperty <string>("Name", NameName.entity); await stateClient.BotState.SetUserDataAsync(message.ChannelId, message.From.Id, userData); } MovieString = "Nice to meet you, " + NameName.entity; } } if (StLUIS.topScoringIntent.intent == "ChangeName") { StateClient stateClient = message.GetStateClient(); MovieString = "ok, so what's your name?"; await stateClient.BotState.DeleteStateForUserAsync(message.ChannelId, message.From.Id); } if (StLUIS.topScoringIntent.intent == "thank") { MovieString = "You are welcome"; } if (StLUIS.topScoringIntent.intent == "None") { MovieString = "I'm sorry, i can't do this :("; } if (StLUIS.topScoringIntent.intent == "mood") { foreach (Entity2 Type in StLUIS.entities) { if (Type.type == "Greeting") { Activity talk = message.CreateReply(StLUIS.entities[0].entity); await connector.Conversations.ReplyToActivityAsync(talk); } } MovieString = "i think You should watch a film :)"; } if (StLUIS.topScoringIntent.intent == "wazzap") { MovieString = "I'm fine"; foreach (Entity2 Type in StLUIS.entities) { if (Type.type == "Greeting") { Activity talk = message.CreateReply(StLUIS.entities[0].entity); await connector.Conversations.ReplyToActivityAsync(talk); } } } int IDofMovie = 1; if (StLUIS.topScoringIntent.intent == "WatchAFilm" || StLUIS.topScoringIntent.intent == "makeAList") { bool flag = false; foreach (Entity2 Type in StLUIS.entities) { flag = false; bool flagwazzap = false; if (Type.type == "Greeting") { Activity talk = message.CreateReply(StLUIS.entities[0].entity); await connector.Conversations.ReplyToActivityAsync(talk); } if (Type.type == "wazzap" && flagwazzap == false) { flagwazzap = true; Activity talk = message.CreateReply("I'm fine"); await connector.Conversations.ReplyToActivityAsync(talk); } if (Type.type == "Genre") { foreach (Genre name in client.GetMovieGenresAsync("en").Result) { flag = true; if (name.Name[1] == Type.entity[1] && name.Name[2] == Type.entity[2]) { IDofMovie = name.Id; break; } } } if (Type.type == "KindaAdventure") { flag = true; Activity talk = message.CreateReply("I think adventure movie fits Your description. I will make a list of adventure movies for you"); await connector.Conversations.ReplyToActivityAsync(talk); foreach (Genre name in client.GetMovieGenresAsync("en").Result) { if (name.Name == "Adventure") { IDofMovie = name.Id; break; } } break; } if (Type.type == "KindaCrime") { flag = true; Activity talk = message.CreateReply("I think there are several genres that can satisfy Your needs. Mystery and Crime."); await connector.Conversations.ReplyToActivityAsync(talk); Activity talk2 = message.CreateReply("here is a list of crime films"); await connector.Conversations.ReplyToActivityAsync(talk2); foreach (Genre name in client.GetMovieGenresAsync("en").Result) { if (name.Name == "Crime") { IDofMovie = name.Id; MovieString = await GetStock(IDofMovie); Activity reply = message.CreateReply(MovieString); await connector.Conversations.ReplyToActivityAsync(reply); break; } } Activity talk1 = message.CreateReply("here is a list of mystery films"); await connector.Conversations.ReplyToActivityAsync(talk1); foreach (Genre name in client.GetMovieGenresAsync("en").Result) { if (name.Name == "Mystery") { IDofMovie = name.Id; MovieString = await GetStock(IDofMovie); break; } } break; } } if (!flag) { Activity question = message.CreateReply(StLUIS.dialog.prompt); await connector.Conversations.ReplyToActivityAsync(question); } else { MovieString = await GetStock(IDofMovie); Activity reply = message.CreateReply(MovieString); await connector.Conversations.ReplyToActivityAsync(reply); Activity ask = message.CreateReply("I'd appreciate if you named the numbers of movies you liked"); await connector.Conversations.ReplyToActivityAsync(ask); } } if (StLUIS.topScoringIntent.intent == "number") { recommender = new RecommendationsApiWrapper(AccountKey, BaseUri); MovieString = "I can also recommend you some other films based on your choices"; Activity ask = message.CreateReply("oh, that will help me to learm something new :)"); await connector.Conversations.ReplyToActivityAsync(ask); foreach (Entity2 Type in StLUIS.entities) { if (Type.type == "builtin.number") { RememberBest.Clear(); foreach (Pair <int, int> Id in NumberAndID) { if (int.Parse(Type.entity) == Id.Second) { RememberBest.Add(Id.First); var row = toJson(message.From.Id, Id.First); string jsontext = JsonConvert.SerializeObject(row); recommender.AddUsageEvent(modelId, jsontext); } } } } } if (StLUIS.topScoringIntent.intent == "Another") { recommender = new RecommendationsApiWrapper(AccountKey, BaseUri); MovieString = "Sorry that the previous films didn't satisfy you. " + Environment.NewLine + "Here are some other films you might like based on your previous choices "; string modelName = "RecommendationsForMovieBot"; try { // Create a model if not already provided. if (String.IsNullOrEmpty(modelId)) { modelId = RecommendationsApp.CreateModel(modelName); } var itemSets = recommender.GetUserRecommendations(modelId, buildId, message.From.Id, 6); if (itemSets.RecommendedItemSetInfo != null) { foreach (RecommendedItemSetInfo recoSet in itemSets.RecommendedItemSetInfo) { foreach (var item in recoSet.Items) { MovieString = MovieString + item.Name + " " + Environment.NewLine; // додавання пропусків необхідне для роботи в емуляторі, оскільки тільки так можна перейти в новий рядок } } } else { MovieString = MovieString + "No recommendations found."; } recommender = RecommendationsApp.reco; } catch (Exception e) { Activity talking = message.CreateReply("Error encountered:"); await connector.Conversations.ReplyToActivityAsync(talking); talking = message.CreateReply(e.Message); await connector.Conversations.ReplyToActivityAsync(talking); } finally { //recommender.DeleteModel(modelId); } } if (StLUIS.topScoringIntent.intent == "Yes") { MovieString = "hope this will be usefull for you"; string modelName = "RecommendationsForMovieBot"; recommender = new RecommendationsApiWrapper(AccountKey, BaseUri); try { //recommender = new RecommendationsApiWrapper(AccountKey, BaseUri); // Create a model if not already provided. if (String.IsNullOrEmpty(modelId)) { modelId = RecommendationsApp.CreateModel(modelName); } // Get item-to-item recommendations and user-to-item recommendations one at a time string I2I = "People also like: " + Environment.NewLine, U2I = "You might also like: " + Environment.NewLine; foreach (int IdOfBest in RememberBest) { RecommendationsApp.GetRecommendationsSingleRequest(recommender, modelId, buildId, ref I2I, ref U2I, IdOfBest, message.From.Id); } Activity talking = message.CreateReply(I2I); await connector.Conversations.ReplyToActivityAsync(talking); if (U2I == "You might also like: " + Environment.NewLine) { talking = message.CreateReply("I don't know you well enough :("); await connector.Conversations.ReplyToActivityAsync(talking); } else { talking = message.CreateReply(U2I); await connector.Conversations.ReplyToActivityAsync(talking); } recommender = RecommendationsApp.reco; } catch (Exception e) { Activity talking = message.CreateReply("Error encountered:"); await connector.Conversations.ReplyToActivityAsync(talking); talking = message.CreateReply(e.Message); await connector.Conversations.ReplyToActivityAsync(talking); } finally { //recommender.DeleteModel(modelId); } } } if (StLUIS.topScoringIntent.intent != "WatchAFilm" && StLUIS.topScoringIntent.intent != "makeAList") { Activity reply = message.CreateReply(MovieString); await connector.Conversations.ReplyToActivityAsync(reply); } } else { await HandleSystemMessageAsync(message); } var response = Request.CreateResponse(HttpStatusCode.OK); return(response); }