首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在C#中将文本转换为布尔表达式

在C#中将文本转换为布尔表达式
EN

Stack Overflow用户
提问于 2016-04-10 02:36:40
回答 2查看 887关注 0票数 2

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

EN

回答 2

Stack Overflow用户

发布于 2016-04-10 02:58:46

使用String.Replace

代码语言:javascript
复制
    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

Reverse Polish Notation

算法中有几个关键概念,我将进一步解释

  • IsOperator

返回bool IsOperator(string part) {

part=="&&“|| part==" ||”|| part == "!“|| ...}

  • IsOperand - is Operator

不是运算符

如果你还需要帮助,请告诉我

Extra: (正如Jared建议的)

您应该在字典中初始化表值

代码语言:javascript
复制
Dictionary<string, bool> doc1 = new Dictionary<string, bool>();
doc1.Add("Ahmed", doc1BooleanArray[0]);
doc1.Add("love", doc1BooleanArray[1]);
...

因此,当您需要获取操作数值来计算表达式时,只需使用:

代码语言:javascript
复制
//Get operand_part = "Ahmed"
bool val = doc1[operand_part]
票数 2
EN

Stack Overflow用户

发布于 2016-04-10 04:25:54

我能想到的最简单的事情就是使用DataTable

这应该正确地处理所有的“快乐路径”。正如它在注释中所说的,您可能需要进行额外的验证,以显式地在您认为无效的输入中出错。

代码语言:javascript
复制
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;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36521040

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档