我有一个为信息检索系统创建布尔表(关联矩阵)的项目,所以我创建了3个文件,我从每个文件中提取单词并删除它们中的停用词,然后使用c#语言将它们添加到listView中,我所需要的就是能够从textBox中获取查询并将其转换为合适的布尔表达式,以便它将返回满足布尔表达式的文档的名称。例如:“艾哈迈德和爱而不是阿里”这应该给我艾哈迈德&&爱&&!对于doc1来说,这将是真的。

发布于 2016-04-10 02:58:46
使用String.Replace
string src = "Ahmed and love and not ali";
string res = src.Replace("and", "&&")
.Replace("not", "!");
string[] parts = res.Split(' '); // { Ahmed , && , love , && , ! , ali }
//Do Shunting-Yard algorithm to convert infix expression to postfix
ToPostfix(parts); // Ahmed love && ali ! &&
bool result = PerformRPN(parts); //Calculate postfix, you should read the Reverse Polish Notation article链接到维基(更新了调车场参考,以更接近OP问题的版本):
Shunting-Yard algorithm
算法中有几个关键概念,我将进一步解释
返回bool IsOperator(string part) {
part=="&&“|| part==" ||”|| part == "!“|| ...}
不是运算符
如果你还需要帮助,请告诉我
Extra: (正如Jared建议的)
您应该在字典中初始化表值
Dictionary<string, bool> doc1 = new Dictionary<string, bool>();
doc1.Add("Ahmed", doc1BooleanArray[0]);
doc1.Add("love", doc1BooleanArray[1]);
...因此,当您需要获取操作数值来计算表达式时,只需使用:
//Get operand_part = "Ahmed"
bool val = doc1[operand_part]发布于 2016-04-10 04:25:54
我能想到的最简单的事情就是使用DataTable。
这应该正确地处理所有的“快乐路径”。正如它在注释中所说的,您可能需要进行额外的验证,以显式地在您认为无效的输入中出错。
class Program
{
static void Main(string[] args)
{
// Unit test the EvaluateBinaryExpression method.
Console.Out.WriteLine(EvaluateBinaryExpression("true")); // true
Console.Out.WriteLine(EvaluateBinaryExpression("false")); // false
Console.Out.WriteLine(EvaluateBinaryExpression("true or false")); // true
Console.Out.WriteLine(EvaluateBinaryExpression("false or false")); // false
Console.Out.WriteLine(EvaluateBinaryExpression("true and false")); // false
Console.Out.WriteLine(EvaluateBinaryExpression("true and true")); // true
Console.Out.WriteLine(EvaluateBinaryExpression("true and not false")); // false
//
// This should give me Ahmed && love && ! Ali which will be true for doc1.
// TODO: get these values out of the table.
var doc1Results = new Dictionary<string, bool>()
{
{ "Ahmed", true },
{ "love", true },
{ "ali", false }
};
// Then just call it like this
Console.Out.WriteLine(EvaluateResults("Ahmed and love and not ali", doc1Results)); // true
}
public static bool EvaluateResults(string expression, IDictionary<string, bool> results)
{
// TODO validate input expression and results
// Make sure the expression is padded with whitespace for replacing.
var replaced = " " + expression + " ";
foreach (var kvp in results)
{
var search = " " + kvp.Key + " ";
var replacement = " " + kvp.Value.ToString() + " ";
// TODO make sure casing, etc. doesn't matter.
replaced = replaced.Replace(search, replacement);
}
return EvaluateBinaryExpression(replaced);
}
public static bool EvaluateBinaryExpression(string expression)
{
// TODO what if this throws an error?
var dt = new System.Data.DataTable();
dt.Columns.Add("Test", typeof(object));
dt.Rows.Add(new object[] { null });
dt.Columns[0].Expression = expression;
var value = dt.Rows[0][0] as bool?;
return value.Value;
}
}https://stackoverflow.com/questions/36521040
复制相似问题