예제 #1
0
    public static void Delegate()
    {
        AsyncServer server = new AsyncServer();

        // TcpServer server = new TcpServer();
        server.start(); // Waits until a connection is established
        string      input    = "";
        CommItem    comm     = new CommItem();
        List <Task> taskList = new List <Task>();

        while (server.isConnected() && comm.taskType != 1)
        {
            input = server.readMessage();
            if (input != "")
            {
                Console.WriteLine("Got: " + input);
            }
            comm = Conversation.Interpret(input);
            if (comm.taskType != 0)
            {
                server.writeMessage("Alexis: " + comm.commString);
            }
        }
        server.close();
    }
예제 #2
0
        public static void RemoveInteraction(ref List <CommItem> list, int position, ItemManager[] managers)
        {
            if (position < list.Count)
            {
                CommItem tobeRemoved = list[position];

                ItemManager manager = managers.SingleOrDefault(x => x.Is(tobeRemoved));

                if (manager != null)
                {
                    list.RemoveAt(position);
                    manager.SetAsPlanned(tobeRemoved.Name, false);
                    SharedHelper.Log("Interaction removed: " + tobeRemoved.Name);
                }
                else
                {
                    SharedHelper.LogError("Could not remove interaction from list:" + tobeRemoved.Name + " " + tobeRemoved.Category + " " + tobeRemoved.SubCategory);
                }
            }
            else
            {
                SharedHelper.LogError("Could not remove interaction at position: " + position);
            }
        }
예제 #3
0
        public void ReGenerateMainSequence(ref Queue <CommItem> Interactions, ItemManager[] p_providers)
        {
            this.providers = p_providers;

            #region debug planned
            string availableItems = "Available items before sampling:\n";
            foreach (var manager in providers)
            {
                availableItems += "Available items for : " + manager.ToString() + " (" + manager.AvailableItems() + ")\n";
                if (!manager.AreAllUnPlanned())
                {
                    SharedHelper.LogError("Flag planned not removed for: " + manager.ToString());
                }
            }

            if ((JokesProvider.GetAll().Where(x => x.IsPlanned == false)).Count() != JokesProvider.GetAll().Count())
            {
                SharedHelper.LogError("Flag planned not removed for: JokeProvider ");
            }
            int availableJokes = JokesProvider.GetAll().Where(x => x.IsPlanned == false && x.IsUsed == false).Count();
            availableItems += "Available items for : " + "JokeProvider" + " (" + availableJokes + ")\n";
            SharedHelper.LogWarning(availableItems);
            #endregion

            PureFacts pfManager = (PureFacts)providers.SingleOrDefault(x => x is PureFacts);
            if (pfManager == null)
            {
                SharedHelper.LogError("No PureFact manager in ReGenerateMainSequence.");
            }

            //Creates a distribution over "EasilyOffended", IsRomanticJoke
            dists.InitJokesDistribution((PureFact)pfManager.GetByName("EasilyOffended"), false, true);

            //Interactions are built newely generated suggestions and actions
            allSuggestions = GenerateSuggestions();
            allActions     = GenerateActions();

            while (allActions.Count > 0 && allSuggestions.Count > 0)
            {
                if (this.AdjFunc(Interactions.Count))
                {
                    allActions = GenerateActions();
                }

                //==================================================================================================

                CommItem citem = new CommItem();
                citem.Category = allActions.Dequeue();

                //SharedHelper.Log("action: " + action);

                string suggestion = "";
                if (citem.Category == ActionsEnum.MakeSuggestion)
                {
                    suggestion = allSuggestions.Dequeue();
                }
                citem.SubCategory = suggestion;

                //Correction
                if (citem.Category == ActionsEnum.AskPureFactQuestionAboutUser)
                {
                    citem.Category = ActionsEnum.PureFact; citem.SubCategory = ActionsEnum.AskPureFactQuestionAboutUser;
                }
                if (citem.Category == ActionsEnum.SharePureFactInfoAboutBot)
                {
                    citem.Category = ActionsEnum.PureFact; citem.SubCategory = ActionsEnum.SharePureFactInfoAboutBot;
                }

                #region Process All
                ItemManager manager = providers.SingleOrDefault(x => x.Is(citem));

                if (manager != null && !(manager is PureFacts))  //TODO: to be improved
                {
                    Item item = manager.GetItem();
                    if (item != null)
                    {
                        manager.SetAsPlanned(item.Name);
                        citem = new CommItem(item);

                        if (manager is SongsProvider)
                        {
                            citem.TextToSay = ((Song)item).Name;
                        }

                        if (manager is MoviesProvider)
                        {
                            citem.TextToSay = phrases.MovieAnnouncement((Movie)item);
                        }

                        //if (manager is SportsProvider)
                        //    SharedHelper.Log("Sport item added in Joi sampler: " + citem.TextToSay);

                        InteractionsStat.AddScheduledInteraction(citem.Category + citem.SubCategory);
                        //SharedHelper.LogWarning("Added " + citem.Category + citem.SubCategory + " with text: " + citem.TextToSay);
                    }
                    else
                    {
                        InteractionsStat.AddMissingInteraction(citem.Category + citem.SubCategory);
                        SharedHelper.LogWarning("Not enough " + citem.Category + citem.SubCategory + " during planning.");
                    }
                }
                #endregion
                else
                {
                    #region Set Suggestion
                    if (citem.Category == ActionsEnum.MakeSuggestion)
                    {
                        //string suggestion = allSuggestions.Dequeue();

                        //SharedHelper.Log("suggestion: " + suggestion);

                        #region set joke
                        if (suggestion == SuggestionsEnum.TellJoke)
                        {
                            //the fact that joke has bee planned, does not mean it has been executed

                            Joke joke = dists.NextJoke();

                            if (joke != null)
                            {
                                citem = new CommItem(joke);

                                //Planned is handled by "dists"
                                //It gives a distribution from all unplanned and unused

                                JokesProvider.SetJokeAsPlanned(joke.Name);

                                //joke.IsPlanned = true;
                                //citem.Name = joke.Name;
                                //citem.TextToSay = joke.Text;
                                //citem.SubCategory = suggestion;
                                citem.IsJokePureFact   = joke.IsPureFact; //these jokes come from the PureFacts collection
                                citem.UIAnswer         = joke.PureFactUI;
                                citem.FacialExpression = joke.FaceExpression;

                                InteractionsStat.AddScheduledInteraction(SuggestionsEnum.TellJoke);
                            }
                            else
                            {
                                citem.TextToSay = "__nointeraction__";
                                //UnityEngine.Debug.LogWarning("Not enough jokes during planning.");
                                InteractionsStat.AddMissingInteraction(SuggestionsEnum.TellJoke);
                            }
                        }
                        #endregion

                        #region set Go Out
                        if (suggestion == SuggestionsEnum.GoOut)
                        {
                            citem.TextToSay = phrases.GoOutAnnoucement();
                            InteractionsStat.AddScheduledInteraction(SuggestionsEnum.GoOut);
                        }
                        #endregion

                        //#region set Watch movie
                        //if (suggestion == SuggestionsEnum.WatchMovie)
                        //{
                        //    Movie movie = MoviesProvider.Get();
                        //    if (movie != null)
                        //    {
                        //        movie.IsPlanned = true;
                        //        //citem.Name = movie.Name;
                        //        //citem.TextToSay = phrases.MovieAnnouncement(movie);
                        //        citem = new CommItem(movie);

                        //        //UnityEngine.Debug.LogWarning("Movie scheduled: " + citem.TextToSay);
                        //        InteractionsStat.AddScheduledInteraction(SuggestionsEnum.WatchMovie);
                        //    }
                        //    else
                        //    {
                        //        citem.TextToSay = "__nointeraction__";
                        //        SharedHelper.LogWarning("Not enough movies during planning.");
                        //        InteractionsStat.AddMissingInteraction(SuggestionsEnum.WatchMovie);
                        //    }
                        //}
                        //#endregion

                        #region set Weather forecast
                        if (suggestion == SuggestionsEnum.TellWeatherForecast)
                        {
                            citem.TextToSay = "The weather forecast should be good."; //TODO not translated
                        }
                        #endregion

                        //#region set Song
                        //if (suggestion == SuggestionsEnum.ListenToSong)
                        //{
                        //    //the fact that song has bee planned, does not mean it has been executed
                        //    Song song = SongsProvider.GetSong();
                        //    if (song != null)
                        //    {
                        //        song.IsPlanned = true;
                        //        citem.Name = song.Name;
                        //        citem.TextToSay = song.Name;

                        //        InteractionsStat.AddScheduledInteraction(SuggestionsEnum.ListenToSong);
                        //    }
                        //    else
                        //    {
                        //        citem.TextToSay = "__nointeraction__";
                        //        SharedHelper.LogWarning("Not enough songs during planning.");
                        //        InteractionsStat.AddMissingInteraction(SuggestionsEnum.ListenToSong);
                        //    }
                        //}
                        //#endregion
                        //else
                        //#region set Go to gym
                        //if (suggestion == SuggestionsEnum.DoSport)
                        //{
                        //    ItemManager manager = providers.Single(x => x.Is(citem));

                        //    if (manager == null) SharedHelper.LogWarning("Manager is null");

                        //    Sport sport = (Sport)manager.GetItem();
                        //    if (sport != null)
                        //    {
                        //        manager.SetAsPlanned(sport.Name);
                        //        citem = new CommItem(sport);

                        //        InteractionsStat.AddScheduledInteraction(SuggestionsEnum.DoSport);
                        //    }
                        //    else
                        //    {
                        //        citem.TextToSay = "__nointeraction__";
                        //        SharedHelper.LogWarning("Not enough sports during planning.");
                        //        InteractionsStat.AddMissingInteraction(SuggestionsEnum.DoSport);
                        //    }
                        //}
                        //#endregion
                    } //end action suggestions
                    #endregion
                    else if (citem.Category == ActionsEnum.AskUncertanFactQuestion)
                    {
                        //the actual question is selected at run time
                        citem.TextToSay  = "###place holder for UncertanFactQuestion";
                        citem.IsPureFact = false;

                        InteractionsStat.AddScheduledInteraction(ActionsEnum.AskUncertanFactQuestion);
                    }
                    else if (citem.SubCategory == ActionsEnum.AskPureFactQuestionAboutUser) //ABOUT USER
                    {
                        int pfabul = PureFactsAboutUserLeftCount();

                        if (pfabul > 0)
                        {
                            PureFact sf = pfManager.GetPureFactAbouUser();

                            if (sf == null)
                            {
                                SharedHelper.LogError("There are pure facts about the user left, but no pure fact has been selected.");
                            }
                            else
                            {
                                citem.TextToSay = sf.Question;
                                //SharedHelper.Log("q name: " + q [0].Name);
                                citem.Name       = sf.Name;
                                citem.IsPureFact = true;

                                pfManager.SetAsPlanned(sf.Name);

                                citem.UIAnswer = sf.UI;
                            }
                            InteractionsStat.AddScheduledInteraction(citem.Category + citem.SubCategory);
                        }
                        else
                        {
                            InteractionsStat.AddMissingInteraction(citem.Category + citem.SubCategory);
                        }
                    }
                    else if (citem.SubCategory == ActionsEnum.SharePureFactInfoAboutBot) //ABOUT BOT
                    {
                        int pfabbl = PureFactsAboutBotLeftCount();

                        if (pfabbl > 0)
                        {
                            PureFact sf = GetPureFactAbouBot();
                            if (sf == null)
                            {
                                SharedHelper.LogError("There are pure facts about the bot left, but no pure fact has been selected.");
                            }
                            else
                            {
                                //SharedHelper.Log("Found " + Actions.SharePureFactInfoAboutBot + ": " + q.Length.ToString());
                                citem.TextToSay  = sf.Acknowledgement;
                                citem.IsPureFact = true;
                                citem.Name       = sf.Name;

                                pfManager.SetAsPlanned(sf.Name);
                            }
                            InteractionsStat.AddScheduledInteraction(citem.Category + citem.SubCategory);
                        }
                        else
                        {
                            InteractionsStat.AddMissingInteraction(citem.Category + citem.SubCategory);
                        }
                    }
                    else if (citem.Category == ActionsEnum.ChangeVisualAppearance)
                    {
                        citem.TextToSay = context.BasePhrases.ChangeClothesAnnouncement();
                        InteractionsStat.AddScheduledInteraction(ActionsEnum.ChangeVisualAppearance);
                    }
                    else if (citem.Category == ActionsEnum.ExpressMentalState)
                    {
                        //choose mental state to share using a distribution (uniform?)
                        //currently only one state is processed
                        string selectedMentalState = "InAGoodMood";

                        if (selectedMentalState == "InAGoodMood")
                        {
                            //Take into account the: ProbVariables.Bot.InAGoodMood;
                            //class Statement where the constructor takes prob variable as or UncertainFact
                            //or just phrase that internally takes into account current values of the prob variable and other normal variables
                            citem.TextToSay = "###place holder for InAGoodMood";
                        }

                        InteractionsStat.AddScheduledInteraction(ActionsEnum.ExpressMentalState);
                    }
                    else
                    {
                        SharedHelper.LogError("Unknown action: " + citem.Category);
                    }
                }

                #region Add interaction item to queue

                if (string.IsNullOrEmpty(citem.TextToSay))
                {
                    SharedHelper.LogError("Text is empty! Action was: " + citem.Category + "|" + citem.SubCategory);
                }
                else
                {
                    Interactions.Enqueue(citem);
                }

                #endregion

                #region debug
                PureFact[] LeftPureFacts = (from item in pfManager.GetAll()
                                            let pf = (PureFact)item
                                                     where (pf.Type == PureFactType.AboutBot || pf.Type != PureFactType.AboutUser) &&
                                                     pf.IsPlanned == false && pf.IsUsed == false
                                                     select pf).ToArray();

                //if (!Flags.DecreaseDistributionOfAskPureFactQuestionAboutUserDone && Interactions.Count > 45 && (LeftPureFacts.Length - PersistentData.PureFactsLoadedOnStartup()) > 0)
                //    KorraBaseHelper.LogError("All pure facts should have been already planned. Left not used pure facts: " + LeftPureFacts.Length);

                #endregion
            }
        }
예제 #4
0
        public Queue <CommItem> ProcessItems(Queue <CommItem> input, ItemManager[] managers)
        {
            PureFacts pfManager = (PureFacts)managers.SingleOrDefault(x => x is PureFacts);

            if (pfManager == null)
            {
                SharedHelper.LogError("No Pure Fact Manager in SpeechAdaptationEN."); return(null);
            }

            List <CommItem> list = input.ToList();

            if (!FlagsShared.InitialGreetingPerformed)
            {
                list.Insert(0, new CommItem {
                    TextToSay = phrases.SayHello(), IsGreeting = true
                });
                FlagsShared.InitialGreetingPerformed = true;
            }

            string username = pfManager.GetValueByName("UserName");

            for (int i = 0; i < list.Count; i++)
            {
                //add joke announcement
                //if (list[i].Action == Actions.MakeSuggestion && list[i].Suggestion == Suggestions.TellJoke)
                //{
                //    CommItem item = list[i];
                //    item.TextToSay = AddJokeAnnouncement(item.TextToSay);
                //    list[i] = item;
                //}

                //add song announcement
                if (list[i].Category == ActionsEnum.MakeSuggestion && list[i].SubCategory == SuggestionsEnum.ListenToSong)
                {
                    CommItem item = list[i];
                    item.TextToSay = AddSongAnnouncement(item.TextToSay, OneSongAlreadyPlanned);
                    list[i]        = item;

                    OneSongAlreadyPlanned = true;
                }

                // add user name for the interaction
                if (list[i].Category == ActionsEnum.AskUncertanFactQuestion ||
                    list[i].SubCategory == ActionsEnum.AskPureFactQuestionAboutUser ||
                    list[i].Category == ActionsEnum.ChangeVisualAppearance ||
                    (list[i].Category == ActionsEnum.MakeSuggestion && list[i].SubCategory != "" && list[i].SubCategory != SuggestionsEnum.TellJoke)
                    )
                {
                    CommItem item = list[i];
                    item.TextToSay = AddCallByName(item.TextToSay, username);
                    list[i]        = item;
                }
            }

            DisablePlayMusicAftertInitialGreeting(ref list, managers);

            KorraModelHelper.CoupleTwoInteractionsTogether(ref list, "UserName", "BotName");

            KorraModelHelper.CoupleTwoInteractionsTogether(ref list, "UserLikesVideoGames", "UserThinksVideoGameIsGoodPresent");

            return(new Queue <CommItem>(list));
        }
예제 #5
0
        /// <summary>
        /// Configures when facial expressions are used
        /// </summary>
        /// <param name="item"></param>
        public void SetFacialExpression(CommItem item)
        {
            #region Get Fact Manager
            PureFacts pfManager = (PureFacts)ItemProviders.SingleOrDefault(x => x is PureFacts);
            if (pfManager == null)
            {
                SharedHelper.LogError("No manager in Facts Manager in SetFacialExpression for model " + this.Name);
                return;
            }
            #endregion

            //PureFacts and Jokes will facial expression set at this stage or by Dynamic Function
            //it updates the facial expression, if it is empty

            if (!string.IsNullOrEmpty(item.FacialExpression))
            {
                KorraModelHelper.SetFacialExpressionFlag(item.FacialExpression);
            }
            else //here in most cases we default to smile
            {
                if (item.IsReactionToUser) //default to smile on reaction
                {
                    KorraModelHelper.SetFacialExpressionFlag(FaceExp.SmileAfterTalking);
                }
                else
                //Handle PureFacts AboutBot
                if (item.IsPureFact && !item.IsJokePureFact)
                {
                    PureFact fact = (PureFact)pfManager.GetByName(item.Name);

                    if (fact != null && fact.Type == PureFactType.AboutBot)
                    {
                        KorraModelHelper.SetFacialExpressionFlag(FaceExp.SmileAfterTalking);
                    }
                }
                else
                if (item.Category == ActionsEnum.ChangeVisualAppearance)
                {
                    KorraModelHelper.SetFacialExpressionFlag(FaceExp.SmileAfterTalking);
                }
                else
                if (item.Category == ActionsEnum.ConvinceBuyStatement)
                {
                    KorraModelHelper.SetFacialExpressionFlag(FaceExp.SmileAfterTalking);
                }
                else
                if (item.Category == ActionsEnum.ExpressMentalState)
                {
                    KorraModelHelper.SetFacialExpressionFlag(FaceExp.SmileAfterTalking);
                }
                else
                //Hnadle Jokes (Normal or PureFacts)
                if (item.SubCategory == SuggestionsEnum.TellJoke && !item.IsJokePureFact)
                {
                    if (!item.IsJokePureFact) //Normal joke
                    {
                        Joke joke = JokesProvider.GetJokeByName(item.Name);

                        if (joke != null && !string.IsNullOrEmpty(joke.FaceExpression)) //check for custom facial expression
                        {
                            KorraModelHelper.SetFacialExpressionFlag(joke.FaceExpression);
                        }
                        else
                        {
                            KorraModelHelper.SetFacialExpressionFlag(FaceExp.SmileAfterTalking); //default to smile
                        }
                    }
                    else //PureFact joke that had no facial expression set
                    {
                        KorraModelHelper.SetFacialExpressionFlag(FaceExp.SmileAfterTalking); //default to smile
                    }
                }
                else
                if (item.SubCategory == SuggestionsEnum.ListenToSong ||
                    item.SubCategory == SuggestionsEnum.DoSport ||
                    item.SubCategory == SuggestionsEnum.WatchMovie ||
                    item.SubCategory == SuggestionsEnum.GoOut)
                {
                    KorraModelHelper.SetFacialExpressionFlag(FaceExp.SmileAfterTalking);
                }
            }
        }
예제 #6
0
 public void InspectNextInteraction(CommItem nextInteraction)
 {
 }
예제 #7
0
        /// <summary>
        /// Add as first element, it will be the first interaction to be used next
        /// </summary>
        /// <param name="item"></param>
        public static void InsertFirstInteractionList(ref Queue <CommItem> interactions, CommItem item)
        {
            //if (afterReaction)
            //{
            //    var list = interactions.ToList();

            //    int i = 0; //locate the first non "ReactionToUser" item
            //    for (i = 0; i < interactions.Count; i++)
            //        if (!list[i].IsReactionToUser)
            //            break;


            //    list.Insert(i, item);
            //    interactions = new Queue<CommItem>(list);
            //}
            //else
            {
                var list = interactions.ToList();
                list.Insert(0, item);
                interactions = new Queue <CommItem>(list);
            }

            //if it is a reaction, then it should be in the item
            //CurrentTimePause = CurrentAIModel.GetCognitiveDist().GetNextInteactionPause(true);
        }