Kir*_*met 12 php python code-translation transpiler
有没有可以自动将这个python代码转换为PHP的软件转换器?
#!/usr/bin/python
import math
def calcNumEntropyBits(s):
        if len(s) <= 0: return 0.0
        symCount = {}
        for c in s:
                if c not in symCount: symCount[c] = 1
                else: symCount[c] += 1
        entropy = 0.0
        for c,n in symCount.iteritems():
                prob = n / float(len(s))
                entropy += prob * (math.log(prob)/math.log(2))
        if entropy >= 0.0: return 0.0
        else: return -(entropy*len(s))
def testEntropy(s):
        print "Bits of entropy in '%s' is %.2f" % (s, calcNumEntropyBits(s))
testEntropy('hello world')
testEntropy('bubba dubba')
testEntropy('aaaaaaaaaaa')
testEntropy('aaaaabaaaaa')
testEntropy('abcdefghijk')
bco*_*sca 14
我不知道任何Python-to-PHP转换器,但它应该是一个简单的端口任务,相似之处很容易发现:
function calcNumEntropyBits($s) {
        if (strlen($s) <= 0) return 0.0;
        $symCount = array();
        foreach (str_split($s) as $c) {
                if (!in_array($c,$symCount)) $symCount[$c] = 1;
                else $symCount[$c] ++;
        }
        $entropy = 0.0;
        foreach ($symCount as $c=>$n) {
                $prob = $n / (float)strlen($s);
                $entropy += $prob * log($prob)/log(2);
        }
        if ($entropy >= 0.0) return 0.0;
        else return -($entropy*strlen($s));
}
function testEntropy($s):
        printf("Bits of entropy in '%s' is %.2f",$s,calcNumEntropyBits($s));
testEntropy('hello world');
testEntropy('bubba dubba');
testEntropy('aaaaaaaaaaa');
testEntropy('aaaaabaaaaa');
testEntropy('abcdefghijk');
第一个函数中的最后几行也可以写成标准的PHP三元表达式:
return ($entropy >= 0.0)? 0.0: -($entropy*strlen($s));
我创建了一个名为py2php的python-to-php转换器.它可以自动翻译基本逻辑,然后你需要调整库调用等.仍然是实验性的.
这是从OP提供的python中自动生成的PHP.
<?php
require_once('py2phplib.php');
require_once( 'math.php');
function calcNumEntropyBits($s) {
    if ((count($s) <= 0)) {
        return 0.0;
    }
    $symCount = array();
    foreach( $s as $c ) {
        if (!$symCount.__contains__($c)) {
            $symCount[$c] = 1;
        }
        else {
            $symCount[$c] += 1;
        }
    }
    $entropy = 0.0;
    foreach( $symCount->iteritems() as $temp_c ) {
        $prob = ($n / float(count($s)));
        $entropy += ($prob * (math::log($prob) / math::log(2)));
    }
    if (($entropy >= 0.0)) {
        return 0.0;
    }
    else {
        return -($entropy * count($s));
    }
}
function testEntropy($s) {
    pyjslib_printFunc(sprintf('Bits of entropy in \'%s\' is %.2f', new pyjslib_Tuple([$s, calcNumEntropyBits($s)])));
}
testEntropy('hello world');
testEntropy('bubba dubba');
testEntropy('aaaaaaaaaaa');
testEntropy('aaaaabaaaaa');
testEntropy('abcdefghijk');
由于数学导入和__contains__,它无法正常运行,但这些都很容易手动修复.
我用大约1/2的方式在Python中制作一个PHP解释器,我可以告诉你,有几十个主要的边缘情况可以解决成千上万的可能性,这使得将Python移植到PHP几乎是不可能的.Python具有比PHP更强大的语法,而在语言方面更进一步,Python的stdlib可能是其中任何其他语言中最先进的语法之一.
我的建议是让你的问题更进一步,为什么你需要在PHP中使用一组基于Python的逻辑.尝试移植/翻译代码的替代方法可能包括从PHP到Python的子处理,使用Gearman让Python在后端工作,而PHP处理视图逻辑,或者更复杂的解决方案是在服务总线或消息队列之间实现PHP应用程序和Python服务.
PS.为任何可读性问题道歉,刚刚完成了为期2天的冲刺.