本文介绍了扩展逻辑语句(相乘)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种扩展以下形式的逻辑表达式(以字符串形式)的方法:

I am looking for a way to expand a logical expression (in a string) of the form:

'(A或B)和((C和D)或E)'

'(A or B) and ((C and D) or E)'

在Python中生成所有肯定集的列表,即

in Python to produce a list of all positive sets, i.e.

['A and C and D',
'A and E',
'B and C and D',
'B and E']

但是我一直找不到如何做到这一点.我已经研究了pyparser,但是在这种情况下我无法确定哪个示例是相关的.使用某种逻辑操作,这可能非常容易,但是我不知道任何形式的逻辑.任何帮助,或对可能有用的资源的引用,将不胜感激.

but I have been unable to find how to do this. I have investigated pyparser, but I cannot work out which example is relevant in this case. This may be very easy with some sort of logic manipulation but I do not know any formal logic. Any help, or a reference to a resource that might help would be greatly appreciated.

推荐答案

这是pyparsing位,摘自示例 SimpleBool.py .首先,使用infixNotation(以前称为operatorPrecedence)定义一个表达式语法,该语法支持括号分组并识别运算的优先级:

Here's the pyparsing bit, taken from the example SimpleBool.py. First, use infixNotation (formerly known as operatorPrecedence) to define an expression grammar that supports parenthetical grouping, and recognizes precedence of operations:

from pyparsing import *

term = Word(alphas)

AND = Keyword("and")
OR = Keyword("or")

expr = infixNotation(term,
    [
    (AND, 2, opAssoc.LEFT),
    (OR, 2, opAssoc.LEFT),
    ])

sample = '(A or B) and ((C and D) or E)'

result = expr.parseString(sample)

from pprint import pprint
pprint(result.asList())

打印:

[[['A', 'or', 'B'], 'and', [['C', 'and', 'D'], 'or', 'E']]]

由此可见,表达式至少已正确解析.

From this, we can see that the expression is at least parsed properly.

接下来,我们将解析操作添加到操作层次结构的每个级别.对于这里的解析动作,我们实际上传递了类,以便解析器将不执行函数并返回某些值,而是调用类的构造函数和初始化器,并为特定的子表达式返回一个类实例:

Next, we add parse actions to each level of the hierarchy of operations. For parse actions here, we actually pass classes, so that instead of executing functions and returning some value, the parser will call the class constructor and initializer and return a class instance for the particular subexpression:

class Operation(object):
    def __init__(self, tokens):
        self._tokens = tokens[0]
        self.assign()

    def assign(self):
        """
        function to copy tokens to object attributes
        """

    def __repr__(self):
        return self.__class__.__name__ + ":" + repr(self.__dict__)
    __str__ = __repr__

class BinOp(Operation):
    def assign(self):
        self.op = self._tokens[1]
        self.terms = self._tokens[0::2]
        del self._tokens

class AndOp(BinOp):
    pass

class OrOp(BinOp):
    pass

expr = infixNotation(term,
    [
    (AND, 2, opAssoc.LEFT, AndOp),
    (OR, 2, opAssoc.LEFT, OrOp),
    ])

sample = '(A or B) and ((C and D) or E)'

result = expr.parseString(sample)
pprint(result.asList())

返回:

[AndOp:{'terms': [OrOp:{'terms': ['A', 'B'], 'op': 'or'}, 
                   OrOp:{'terms': [AndOp:{'terms': ['C', 'D'], 
                                    'op': 'and'}, 'E'], 'op': 'or'}],
'op': 'and'}]

现在,表达式已转换为子表达式的数据结构,我将其留给您来完成将方法添加到AndOp和OrOp的工作,以生成将整体评估为True的各种术语组合. (请查看 invregex.py 示例中的逻辑可以颠倒正则表达式,以获取有关如何将生成器函数添加到已解析的类以生成所需术语的不同组合的想法.)

Now that the expression has been converted to a data structure of subexpressions, I leave it to you to do the work of adding methods to AndOp and OrOp to generate the various combinations of terms that will evaluate overall to True. (Look at the logic in the invregex.py example that inverts regular expressions for ideas on how to add generator functions to the parsed classes to generate the different combinations of terms that you want.)

这篇关于扩展逻辑语句(相乘)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 09:32