C#检查txt是否包含字符串并输出其下一行
C# Check if txt contains string and output the line below it
我正在开发一种聊天机器人,并且
我有一个 txt 文件,其中包含 Q,A,Q,A,Q,A 格式的问题和答案:
狗是猫?
不,他们不是
猫是狗?
不,他们不是
我需要检查文本文件是否包含输入,然后将输出值设置为问题下方一行的答案。这是我目前所拥有的。
static string path = Path.Combine(Directory.GetCurrentDirectory(), "memory.txt");
static IEnumerable<string> lines = File.ReadLines(path);
static string inputValue;
static string outputValue = " ";
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 (File.ReadAllLines(path).Contains(inputValue))
{
outputValue = //This is what i have to figure out
}
else
{
}
}
Console.Write("Computer: ");
Console.WriteLine(outputValue);
outputValue = " ";
}
}
// load the file in the list
List<string> lines = File.ReadAllLines(path);
// get the position of the question
int question_position = lines.IndexOf(inputValue);
// check if the question was found AND if there is a line below it with the answer
if (question_position >= 0 && lines.Count() > question_position + 1)
{
// assign the answer to the outputValue
outputValue = lines[question_position + 1];
}
我建议使用 Linq,SkipWhile
:
outputValue = File
.ReadLines(path)
.SkipWhile(line => line != inputValue)
.Skip(1)
.FirstOrDefault();
if (outputValue != null) {
//TODO: outputValue has been found, put relevant code here
}
不要使用File.ReadAllLines
,因为那样会将整个文件读入内存。逐行阅读而不是阅读整个文件。问题可能在第一行,所以为什么要将整个文件读入内存。
string outputValue = "";
var lines = System.IO.File.ReadLines( path );
foreach( var thisLine in lines)
{
if( thisLine.Contains( inputValue ) )
{
// Get the answer from the next line
var answer = lines.Take( 1 ).FirstOrDefault();
if( !string.IsNullOrWhiteSpace( answer ) )
{
outputValue = answer;
}
}
}
您可以阅读我的回答 here 以了解有关 ReadAllLines
与 ReadLines
之间区别的更多详细信息。
我正在开发一种聊天机器人,并且 我有一个 txt 文件,其中包含 Q,A,Q,A,Q,A 格式的问题和答案:
狗是猫?
不,他们不是
猫是狗?
不,他们不是
我需要检查文本文件是否包含输入,然后将输出值设置为问题下方一行的答案。这是我目前所拥有的。
static string path = Path.Combine(Directory.GetCurrentDirectory(), "memory.txt");
static IEnumerable<string> lines = File.ReadLines(path);
static string inputValue;
static string outputValue = " ";
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 (File.ReadAllLines(path).Contains(inputValue))
{
outputValue = //This is what i have to figure out
}
else
{
}
}
Console.Write("Computer: ");
Console.WriteLine(outputValue);
outputValue = " ";
}
}
// load the file in the list
List<string> lines = File.ReadAllLines(path);
// get the position of the question
int question_position = lines.IndexOf(inputValue);
// check if the question was found AND if there is a line below it with the answer
if (question_position >= 0 && lines.Count() > question_position + 1)
{
// assign the answer to the outputValue
outputValue = lines[question_position + 1];
}
我建议使用 Linq,SkipWhile
:
outputValue = File
.ReadLines(path)
.SkipWhile(line => line != inputValue)
.Skip(1)
.FirstOrDefault();
if (outputValue != null) {
//TODO: outputValue has been found, put relevant code here
}
不要使用File.ReadAllLines
,因为那样会将整个文件读入内存。逐行阅读而不是阅读整个文件。问题可能在第一行,所以为什么要将整个文件读入内存。
string outputValue = "";
var lines = System.IO.File.ReadLines( path );
foreach( var thisLine in lines)
{
if( thisLine.Contains( inputValue ) )
{
// Get the answer from the next line
var answer = lines.Take( 1 ).FirstOrDefault();
if( !string.IsNullOrWhiteSpace( answer ) )
{
outputValue = answer;
}
}
}
您可以阅读我的回答 here 以了解有关 ReadAllLines
与 ReadLines
之间区别的更多详细信息。