验证代码树结构是否自然,发现不同语言表现差异大
Bringing Structure to Naturalness: On the Naturalness of ASTs
- 用树形结构建模代码,检验其统计可预测性
- Ruby代码树模型媲美n-gram,Java/Python则更差
- 无需人工特征即可实现接近顶尖的缺陷预测
源代码呈现多种形态。已有研究证明代码比自然语言更具可预测性,并在词元层面揭示了其统计可预测性:代码具有自然性。近年来,代码的结构(如控制流、语法图、抽象语法树等)被成功应用于代码补全、代码摘要、方法命名等多项任务,显著提升性能。这些工作隐含假设:代码的结构化表示同样具备统计可预测性,即结构化视角下的代码也具有自然性。本文主张该假设应被明确检验,并提出直接研究‘结构自然性假说’。除了指出已有研究中的隐含假设并形式化该假说外,本文还提供了树结构的实证证据:对于某些语言(如Ruby),基于抽象语法树的TreeLSTM模型在性能上可与n-gram模型媲美,且‘免费’解决先前研究指出的语法词元问题;而对于其他语言(如Java或Python),树模型表现反而更差,表明下游任务提升与语言建模任务之间并无相关性。此外,本文展示了如何利用此类自然性信号,在无需手动特征工程的情况下,实现接近当前最优的即时缺陷预测效果。
原文摘要 · Abstract (English)
Source code comes in different shapes and forms. Previous research has already shown code to be more predictable than natural language as well as highlighted its statistical predictability at the token level: source code can be natural. More recently, the structure of code -- control flow, syntax graphs, abstract syntax trees etc. -- has been successfully used to improve the state-of-the-art on numerous tasks: code suggestion, code summarisation, method naming etc. This body of work implicitly assumes that structured representations of code are similarly statistically predictable, i.e. that a structured view of code is also natural. We consider that this view should be made explicit and propose directly studying the Structured Naturalness Hypothesis. Beyond just naming existing research that assumes this hypothesis and formulating it, we also provide evidence in the case of trees: TreeLSTM models over ASTs for some languages, such as Ruby, are competitive with $n$-gram models while handling the syntax token issue highlighted by previous research 'for free'. For other languages, such as Java or Python, we find tree models to perform worse, suggesting that downstream task improvement is uncorrelated to the language modelling task. Further, we show how such naturalness signals can be employed for near state-of-the-art results on just-in-time defect prediction while forgoing manual feature engineering work.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。