morria / code2seq-paths-php
生成适用于code2seq使用的函数路径
0.5
2023-03-30 15:48 UTC
Requires
- php: >7.4
- ext-ast: *
Requires (Dev)
- phan/phan: ^5.4
- phpunit/phpunit: ^10.0
README
读取PHP源代码并生成一个适用于code2seq的文件,用于模型训练和评估。
使用方法
要构建包,只需运行composer install
。请确保已安装PHP以及php-ast扩展。您还可以使用在.devcontainer
中定义所有依赖项的Docker容器。
构建完成后,您可以通过以下方式运行工具
./bin/code2seq-paths path/to/php/dir > method_contexts.c2s
从这里,您可以使用文件method_contexts.c2s
作为code2seq的输入。请注意,此处生成的文件需要在code2seq
中预处理。
待办事项:描述如何进行该操作。
标志
您可以通过./bin/code2seq-paths --help
查看可用标志。
Usage: ./bin/code2seq-paths [options] [files...]]
-h, --help
Get this help information
-s SEED, --seed SEED
The seed to use before shuffling the target contexts.
-l LENGTH, --max-length=LENGTH
The maximum length of a target context. Defaults to unbounded.
-i, --ids
Use IDs rather than names for context nodes. Defaults to false.
...
All other options will be treated as file names to
read as PHP source.
Docker
为了避免设置正确环境,您可以按以下方式从Docker镜像运行code2seq-paths-php
。
docker pull morria/code2seq-paths-php
docker run --name code2seq-paths-php --volume PATH_TO_SOURCE:/workspace morria/code2seq-paths-php /workspace/
要从源代码构建容器,请运行以下命令。
composer install
docker build -t morria/code2seq-paths-php .
输出格式
输出结构如下。
LINE := FUNCTION_NAME WS CONTEXT_LIST;
FUNCTION_NAME := string;
WS := ' ';
CONTEXT_LIST := SOURCE_TERMINAL ',' NODE_LIST ',' TARGET_TERMINAL;
SOURCE_TERMINAL := TOKEN_LIST;
TARGET_TERMINAL := TOKEN_LIST;
NODE_LIST := NODE
| NODE '|' NODE_LIST
;
NODE := string;
对于输入代码
<?php function f(int $x) { return $x; }
将生成以下输出
f long,parameter,x long,parameter|function|return|variable,x x,parameter,long x,parameter|function|return|variable,x x,variable|return|function|parameter,long x,variable|return|function|parameter,x
依赖项
要运行code2seq-paths
,您需要在系统上安装几个依赖项。
这些可以通过以下方式在Ubuntu机器上安装
sudo apt-get install php php-ast composer
或者,您可以使用.devcontainer/Dockerfile
构建一个适合运行code2seq-paths
的系统。
cd .devcontainer
docker compose build
开发
要运行测试,您可以运行./bin/test
,这将执行所有PHPUnit测试。