【发布时间】: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