arXiv:2601.19264cs.SEcs.LG2026-01被引 3

通过分析代码缩进与空格特征,有效区分人类与机器生成的代码。

Whitespaces Don't Lie: Feature-Driven and Embedding-Based Approaches for Detecting Machine-Generated Code

  • 基于代码缩进、空格等显式特征构建可解释检测模型
  • 在60万样本上达到AUC 0.995、F1 0.971的高准确率
  • 适合需要透明性与可审计性的学术与工业场景

大型语言模型让从自然语言生成看似真实的源代码变得极为容易。这虽加速了软件开发和学习,但也带来了学术诚信、作者归属和负责任AI使用的新风险。本文研究如何区分人类编写的代码与机器生成的代码,比较了两种互补方法:基于轻量级可解释风格与结构特征的特征检测器,以及利用预训练代码编码器的嵌入检测器。基于一个包含60万条人类与AI生成代码样本的最新大规模基准数据集,发现特征模型表现优异(ROC-AUC 0.995,PR-AUC 0.995,F1 0.971),而使用CodeBERT嵌入的嵌入模型也极具竞争力(ROC-AUC 0.994,PR-AUC 0.994,F1 0.965)。分析表明,与缩进和空格相关的特征提供特别有判别力的线索,而嵌入捕捉深层语义模式,精度略高。这些发现揭示了可解释性与泛化能力之间的权衡,为在学术和工业场景中部署鲁棒的代码来源检测提供了实用指导。

原文摘要 · Abstract (English)

Large language models (LLMs) have made it remarkably easy to synthesize plausible source code from natural language prompts. While this accelerates software development and supports learning, it also raises new risks for academic integrity, authorship attribution, and responsible AI use. This paper investigates the problem of distinguishing human-written from machine-generated code by comparing two complementary approaches: feature-based detectors built from lightweight, interpretable stylometric and structural properties of code, and embedding-based detectors leveraging pretrained code encoders. Using a recent large-scale benchmark dataset of 600k human-written and AI-generated code samples, we find that feature-based models achieve strong performance (ROC-AUC 0.995, PR-AUC 0.995, F1 0.971), while embedding-based models with CodeBERT embeddings are also very competitive (ROC-AUC 0.994, PR-AUC 0.994, F1 0.965). Analysis shows that features tied to indentation and whitespace provide particularly discriminative cues, whereas embeddings capture deeper semantic patterns and yield slightly higher precision. These findings underscore the trade-offs between interpretability and generalization, offering practical guidance for deploying robust code-origin detection in academic and industrial contexts.

代码检测LLM安全可解释性

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。