Ich arbeite an einem Chat-Bot und versuche, in einer Lernfunktion zu programmieren. Ich bin ziemlich neu im Codieren, aber ich vermute, dass mein Problem damit zu tun hat, dass ich keinen Lesestream schließe, aber ich habe keine Ahnung, wie. Ich hätte gerne Hilfe bei der Suche nach dem effizientesten Weg, um diese Schwierigkeit zu lösen. Hier ist mein Code.Kann keine Zeichenfolge in eine TXT-Datei schreiben, weil eine andere Datei darauf zugreift
class Program
{
static bool isRecording = false;
static bool isTraining = false;
static bool shutdown = false;
static string path = Path.Combine(Directory.GetCurrentDirectory(), "memory.txt");
static string inputValue;
static string outputValue = " ";
static void Main(string[] args)
{
Console.WriteLine("Computer: Hello! I am MyAI Version 2.0.");
Console.WriteLine("Training?");
inputValue = Console.ReadLine();
inputValue = inputValue.ToLower();
inputValue = inputValue.Trim(new Char[] { ' ', '.', ',', ':', ';', '*' });
if (inputValue == "yes")
{
isTraining = true;
Console.WriteLine("Training mode set to true.");
Console.ReadLine();
}
else
{
isTraining = false;
Console.WriteLine("Training mode set to false");
Console.ReadLine();
}
Console.Clear();
CollectInput();
}
static void CollectInput()
{
var lines = System.IO.File.ReadLines(path);
while (!shutdown)
{
Console.Write("User: ");
inputValue = Console.ReadLine();
inputValue = inputValue.ToLower();
inputValue = inputValue.Trim(new Char[] { ' ', '.', ',', ':', ';', '*' });
StringComparison comp = StringComparison.OrdinalIgnoreCase;
if (inputValue == "hi" || inputValue == "hello" || inputValue == "greetings")
{
outputValue = "hi";
}
else if (inputValue.Contains("how are you"))
{
outputValue = "good";
}
else
{
if (isTraining)
{
if (isRecording)
{
//This is where I would like to write inputValue to path
isRecording = false;
}
}
if (File.ReadAllLines(path).Contains(inputValue))
{
outputValue = File.ReadLines(path)
.SkipWhile(line => line != inputValue)
.Skip(1)
.FirstOrDefault();
}
else
{
if (inputValue.Contains("?"))
{
outputValue = inputValue;
//This is where I would like to write inputValue to path
isRecording = true;
}
else
{
outputValue = "what about it?";
}
}
}
Console.Write("Computer: ");
Console.WriteLine(outputValue);
outputValue = " ";
}
}
}
File.Open (Pfad, FileMode.Open); outputValue = File.ReadLines (Pfad) .SkipWhile (Zeile => Zeile! = Eingabewert) .Skip (1) .FirstOrDefault(); – Mattinthehat
@ Mattinthehat Sie öffnen immer noch eine neue Dateikennung bei 'outputValue = File.ReadLines (path)'. Suchen Sie nach Methoden, die das Handle verwenden, das von 'File.Open' zurückgegeben wird. Ich denke, Ihre IDE oder Ihr Compiler sollten Sie warnen, dass Sie den Rückgabewert von 'File.Open' verwerfen. –
Können Sie das genauer ausführen? Ich bin noch neu und ich kenne keinen besseren Weg, diesen Teil des Codes wiederherzustellen. Im Grunde prüft es die Datei auf die Eingabe, wenn sie gefunden wird, nimmt sie die darunter liegende Zeile. – Mattinthehat