【问题标题】:Loop methodn not giving accurate resuts循环方法没有给出准确的结果
【发布时间】:2019-09-11 05:39:39
【问题描述】:

我在运行时过程中创建了文件 John.txt,并且有 John 的属性。文件的文本是“朋友,银行家”,但是当我执行并询问人工智能“约翰在哪里”时?尽管我编写了方法MatchProffesionWithProperties,但它没有返回任何结果。我哪里错了?

namespace Machine
{
    class Program
    {

        static bool HavingGreeted = false;

        static string person;

        static Dictionary<string, string> KeyValuePairs;

        static void MatchProffesionsWithProperties(string person,DayOfWeek dayOfWeek)
        {
            var strings = File.ReadAllLines(person + ".txt");

            foreach(var stringi in strings)
            {
                var whitespacearray = stringi.Split(' ');

                var commaarray = stringi.Split(',');

                foreach(var property in whitespacearray)
                {
                    if (KeyValuePairs.ContainsKey(property))
                    {
                        if (dayOfWeek == DayOfWeek.Monday || dayOfWeek == DayOfWeek.Tuesday || dayOfWeek == DayOfWeek.Wednesday || dayOfWeek == DayOfWeek.Thursday || dayOfWeek == DayOfWeek.Friday)
                        {
                            Console.WriteLine("{0} should be at the {1}", person, KeyValuePairs[property]);
                        }
                    }
                }
                foreach(var property in commaarray)
                {
                    if (KeyValuePairs.ContainsKey(property))
                    {
                        if (dayOfWeek == DayOfWeek.Monday || dayOfWeek == DayOfWeek.Tuesday || dayOfWeek == DayOfWeek.Wednesday || dayOfWeek == DayOfWeek.Thursday || dayOfWeek == DayOfWeek.Friday)
                        {
                            Console.WriteLine("{0} should be at the {1}", person, KeyValuePairs[property]);
                        }
                    }
                }

            }
        }

        static void ProcessQuestionForPerson(string word)
        {
            var array = word.Split(' ');

            if (word.Contains("Wh"))
            {
                person = array[2].Substring(0,array[2].Length-1);

            }
            else
            {
                person = array[1];
            }

            if (!File.Exists(person+".txt"))
            {
                Console.WriteLine("Who is {0}?", person);

                string data = Console.ReadLine();

                var filestream = File.Create(person + ".txt");

                byte[] b = ASCIIEncoding.ASCII.GetBytes(data);

                filestream.Write(b, 0, b.Length);

                filestream.Close();

            }
            else
            {
                if (word.StartsWith("Who"))
                {
                    string data = File.ReadAllText(person + ".txt");

                    Console.WriteLine("{0} is a {1}", person, data);

                }
                if (word.StartsWith("Where"))
                {
                    MatchProffesionsWithProperties(person, DateTime.Today.DayOfWeek);
                }
            }


        }

        static void Greet(int hour)
        {
            if (hour > 0 && hour < 12)
            {
                Console.WriteLine("Good Morning");
            }
            if (hour > 12 && hour < 17)
            {
                Console.WriteLine("Good Afternoon");

            }
            if (hour > 17 && hour <= 23)
            {
                Console.WriteLine("Good Evening");
            }
        }


        static void ProcessQuestionForMyself(string word)
        {
            var array = word.Split(' ');

            //Process the components of the sentence

            string type = array[1];

            // get the type of the sentence

            string value = array[3].Substring(0,array[3].Length-1);

            // get the value of the sentence

            string questiontype = array[3].Substring(0,array[3].Length-1);

            //get the type of the question

            if (word.EndsWith("."))
            {
                string correctvalue = File.ReadAllText(type+".txt");

                // read from file the correct value

                if (type == "name")
                {
                    if (correctvalue == GetLowerUpper(value)[0] || correctvalue == GetLowerUpper(value)[1])
                    {
                        Console.WriteLine("Correct.My name is {0}.", value);

                        // print response
                    }
                    else
                    {
                        Console.WriteLine("{0} is not my name.", value);

                        // print response
                    }
                }
                if (type == "age")
                {
                    if (correctvalue == value)
                    {
                        Console.WriteLine("Correct.I am {0} years old.", value);

                        // print response
                    }
                    else
                    {
                        Console.WriteLine("{0} is not my age.", value);

                        // print response
                    }
                }

            }
            if (word.EndsWith("?"))
            {
                string answer = File.ReadAllText(questiontype+".txt");

                Console.WriteLine(answer);
            }

        }


        static string[]  GetLowerUpper(string word)
        {
            string upperword = word.Replace(word[0].ToString(), word[0].ToString().ToUpper());

            string lowerword = word.ToLower();

            //Create strings for answer and question0


            string[] array = new string[2] { upperword, lowerword };

            return array;
        }

        static void MainThread()
        {
            if (HavingGreeted == false)
            {
                Greet(DateTime.Now.Hour);

                HavingGreeted = true;
            }


            string data = Console.ReadLine();


            if (data.Contains(GetLowerUpper("your")[0]) || data.Contains(GetLowerUpper("your")[1]))
            {
                //Append to me

                ProcessQuestionForMyself(data);

            }

            if (!data.Contains("your"))
            {
                //Append to someone else

                ProcessQuestionForPerson(data);
            }


            // Repeat thread

            Thread repeatmainthread = new Thread(new ThreadStart(MainThread));

            repeatmainthread.Start();
        }

        static void Main(string[] args)
        {
            KeyValuePairs = new Dictionary<string, string>();

            KeyValuePairs.Add("Banker", "Bank");

            Thread mainthread = new Thread(new ThreadStart(MainThread));

            mainthread.Start();
        }
    }
}

预期的结果是 AI 告诉我 John 应该在银行。

【问题讨论】:

  • 如果你们中的任何人观看感兴趣的人,是的,我正在尝试复制机器。

标签: c# loops artificial-intelligence


【解决方案1】:

KeyValuePairs.ContainsKey(property) 区分大小写,因此“banker”不会匹配“Banker”。您可以通过将KeyValuePairs 的初始化更改为:

KeyValuePairs = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);

在为该人创建新文件后,将 ProcessQuestionForPerson 更改为仍然询问“在哪里……”问题:

static void ProcessQuestionForPerson(string word)
{
    var array = word.Split(' ');
    bool fileExisted = false;

    if (word.Contains("Wh"))
    {
        person = array[2].Substring(0,array[2].Length-1);
    }
    else
    {
        person = array[1];
    }

    fileExisted = File.Exists(person+".txt");

    if (!fileExisted)
    {
        Console.WriteLine("Who is {0}?", person);

        string data = Console.ReadLine();

        var filestream = File.Create(person + ".txt");

        byte[] b = ASCIIEncoding.ASCII.GetBytes(data);

        filestream.Write(b, 0, b.Length);

        filestream.Close();
    }

    if (word.StartsWith("Who") && fileExisted)   //We don't want to answer "Who" when we've just answered it ourselves (remove && fileExisted if you do)
    {
        string data = File.ReadAllText(person + ".txt");

        Console.WriteLine("{0} is a {1}", person, data);
    }

    if (word.StartsWith("Where"))
    {
        MatchProffesionsWithProperties(person, DateTime.Today.DayOfWeek);
    }
}

【讨论】:

  • 几乎只能让该方法在我的机器上运行(使用“Where”而不是“where”)。 :-)
  • ProcessQuestionForPerson 可能还需要更改,尤其是在每次运行程序时都删除 John.txt 时。如果该文件不存在,它将为该人创建一个但不会回答问题。
  • 是的,但是如果你看到程序的其余部分,如果文件 John.txt 不存在,人工智能会询问约翰是谁并采取不同的方向
  • @SoftwarePlayer,如果文件不存在,它似乎只是创建文件然后等待另一个问题(不回答原始问题)。您能否解释一下在这两种解决方案之后您仍然遇到的问题?
猜你喜欢
  • 2020-08-23
  • 2012-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-12
相关资源
最近更新 更多