给定以下(中缀)表达式:
(country = be or country = nl) and
(language = en or language = nl) and
message contains twitter
我想创建以下4个中缀符号:
message contains twitter and country = be and language = en
message contains twitter and country = be and language = en
message contains twitter and country = nl and language = nl
message contains twitter and country = nl and language = nl
所以,基本上,我想摆脱所有的OR。
我已经有了第一个表达式的后缀表示法,所以我目前正在尝试处理它以获得所需的表示法。然而,这种特殊的情况会引起麻烦。
(为了说明,这个查询的后缀符号是:)
country be = country nl = or language en = language = nl or and message twitter contains and
有人知道实现这个的算法吗?
将问题分为两步:后缀到多个后缀,后缀到中缀。每一步都是通过"解释"一个后缀表达式来执行的。
对于后缀到多后缀解释器:堆栈值是后缀表达式的集合。解释规则如下:
<predicate>: push a one-element collection containing <predicate>.
AND: pop the top two collections into C1 and C2. With two nested loops,
create a collection containing x y AND for all x in C1 and y in C2.
Push this collection.
OR: pop the top two collections into C1 and C2. Push the union of C1 and C2.
对于后缀到中缀解释器:堆栈值是中缀表达式。
<predicate>: push <predicate>.
AND: pop two expressions into x and y. Push the expression (x) and (y).
这些步骤可以组合在一起,但是我想展示这种技术的两个例子。
使用树表示可能是最简单的。使用分流场算法建立一个二叉树表示的方程。树中的一个节点可能是:
class Node {
const OP = 'operator';
const LEAF = 'leaf';
$type = null; // Will be eight Node::OP or Node::LEAF
$op = null; // could be 'or' or 'and' 'contains';
$value = null; // used for leaf eg 'twitter'
$left = null;
$right = null;
}
,尽管你可以使用子类。在分流场算法中,您希望改变输出步骤以生成树。
一旦你有了一个树表示,你需要几个算法。
首先你需要一个算法来复制树
public function copy($node) {
if($node->type == Node::LEAF) {
$node2 = new Node();
$node2->type = Node::LEAF;
$node2->value = $node->value;
return $node2;
}
else {
$left = copy($node->left);
$right = copy($node->right);
$node2 = new Node();
$node2->type = Node::OP;
$node2->op = $node->op;
$node2->left = $node->left;
$node2->right = $node->right;
return $node2;
}
}
下一步算法寻找第一个"或"算子节点。
function findOr($node) {
if($node->type == Node::OP && $node->op == 'or') {
return $node;
} else if($node->type == Node::OP ) {
$leftRes = findOr($node->$left);
if( is_null($leftRes) ) {
$rightRes = findOr($node->$right); // will be null or a found node
return $rightRes;
} else {
return $leftRes; // found one on the left, no need to walk rest of tree
}
} else {
return null;
}
}
,最后一个算法copyLR给出左(true)或右(false)分支。当返回左分支或右分支时,除非节点匹配$target,否则它的行为就像复制一样。
public function copyLR($node,$target,$leftRight) {
if($node == $target) {
if($leftRight)
return copy($node->left);
else
return copy($node->right);
}
else if($node->type == Node::LEAF) {
$node2 = new Node();
$node2->type = Node::LEAF;
$node2->value = $node->value;
return $node2;
}
else {
$left = copy($node->left,$target,$leftRight);
$right = copy($node->right,$target,$leftRight);
$node2 = new Node();
$node2->type = Node::OP;
$node2->op = $node->op;
$node2->left = $node->left;
$node2->right = $node->right;
return $node2;
}
}
碎片现在放在一起
$root = parse(); // result from the parsing step
$queue = array($root);
$output = array();
while( count($queue) > 0) {
$base = array_shift($queue);
$target = findOr($base);
if(is_null($target)) {
$output[] = $base; // no or operators found so output
} else {
// an 'or' operator found
$left = copyLR($base,$target,true); // copy the left
$right = copyLR($base,$target,false); // copy the right
array_push($left); // push both onto the end of the queue
array_push($right);
}
}