将Python代码转换为PHP

时间:2010-11-07 05:18:05

标签: 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')

7 个答案:

答案 0 :(得分: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));

答案 1 :(得分:7)

我创建了一个名为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__,它无法正常运行,但这些都很容易手动修复。

答案 2 :(得分:6)

我在使用Python制作PHP解释器方面做了大约1/2,我可以告诉你,有几十种主要的边缘情况可以解决成千上万的可能性,这使得几乎不可能将Python移植到PHP。 Python具有比PHP更强大的语法,而在语言方面更进一步,Python的stdlib可能是其中任何其他语言中最先进的语法之一。

我的建议是让你的问题更进一步,为什么你需要在PHP中使用一组基于Python的逻辑。尝试移植/转换代码的替代方法可能包括从PHP到Python的子处理,使用Gearman让Python在后端工作,而PHP处理视图逻辑,或者更复杂的解决方案是在服务总线或消息队列之间实现PHP应用程序和Python服务。

PS。为任何可读性问题道歉,刚刚完成了为期2天的冲刺。

答案 3 :(得分:2)

不存在此类工具,您必须自己移植代码

答案 4 :(得分:1)

我想知道同样的问题,我在GitHubGist网站上找到了这个PyToPhp.py文件。这很简单,似乎是开始的起点。

我要去看看!!!

答案 5 :(得分:0)

我从https://github.com/dan-da/py2php更改了库py2php并将其分叉到https://github.com/bunkahle/py2php的新存储库中 现在,您还可以使用已翻译为PHP代码的python数学库。仍然需要对代码进行修改才能使其正常工作。

答案 6 :(得分:0)

def compute_cost(X, y, theta):
    """ Vectorized implementation of MSE cost function.

    Arguments:
        - X: Inputs, a 2d array of shape (m, n + 1)
        - y: Target values, a 2d array of shape (m, 1)
        - theta: Parameters, a 2d array of shape (n + 1, 1)

    Outputs:
        - the mean squared error value
    """
    raise Implemented('You should implement compute_cost function')