Skip to main content
QUICK REVIEW

[论文解读] SCC: Automatic Classification of Code Snippets

Kamel Alreshedy, Dhanush Dharmaretnam|arXiv (Cornell University)|Sep 21, 2018
Software Engineering Research参考文献 5被引用 6
一句话总结

本文提出 SCC,一种基于 Stack Overflow 代码片段进行训练的多项式朴素贝叶斯分类器,用于自动分类小型代码块中的编程语言。SCC 达到了 75% 的准确率,优于专有的 PLI 工具在相同任务中仅 55.5% 的准确率,同时能有效区分 C、C++ 和 C# 等相似语言,并以显著的精确度识别 C# 的不同版本。

ABSTRACT

Determining the programming language of a source code file has been considered in the research community; it has been shown that Machine Learning (ML) and Natural Language Processing (NLP) algorithms can be effective in identifying the programming language of source code files. However, determining the programming language of a code snippet or a few lines of source code is still a challenging task. Online forums such as Stack Overflow and code repositories such as GitHub contain a large number of code snippets. In this paper, we describe Source Code Classification (SCC), a classifier that can identify the programming language of code snippets written in 21 different programming languages. A Multinomial Naive Bayes (MNB) classifier is employed which is trained using Stack Overflow posts. It is shown to achieve an accuracy of 75% which is higher than that with Programming Languages Identification (PLI a proprietary online classifier of snippets) whose accuracy is only 55.5%. The average score for precision, recall and the F1 score with the proposed tool are 0.76, 0.75 and 0.75, respectively. In addition, it can distinguish between code snippets from a family of programming languages such as C, C++ and C#, and can also identify the programming language version such as C# 3.0, C# 4.0 and C# 5.0.

研究动机与目标

  • 解决在短小代码片段中准确识别编程语言的挑战,该任务相较于分类完整源代码文件更具难度。
  • 开发一种开源、可访问的工具,用于对在线论坛和代码共享平台中缺少语言标签的代码片段进行分类。
  • 超越现有专有解决方案(如 PLI),后者在完整文件上表现良好,但在小段代码片段上表现不佳。
  • 实现细粒度分类,例如区分 C、C# 和 C++,或识别 C# 的具体版本(3.0、4.0、5.0)。
  • 为代码片段语言分类提供一种可复现、透明的开源替代方案,以替代闭源工具。

提出的方法

  • 使用从 Stack Overflow 帖子中提取的代码片段数据集,训练一个多项式朴素贝叶斯(MNB)分类器。
  • 特征从分词后的代码片段中提取,重点关注每种语言独有的关键字和语法模式。
  • 模型在 21 种编程语言上进行评估,包括语言家族(C、C++、C#)和版本(C# 3.0、4.0、5.0)。
  • 通过多个语言类别中的精确率、召回率、F1 分数和准确率来衡量性能。
  • 使用相同的 150 个 Stack Overflow 代码片段测试集,与专有的 PLI 工具进行对比。
  • 提取每种语言的前 10 个特征(例如 C# 的 'class'、'console'),以分析模型的决策过程。

实验结果

研究问题

  • RQ1机器学习模型能否在从短小代码片段中分类编程语言方面实现高准确率,而非仅针对完整源代码文件?
  • RQ2基于开源 MNB 的分类器(SCC)在代码片段分类任务中,与专有工具(PLI)相比表现如何?
  • RQ3SCC 能否有效区分密切相关的编程语言,如 C、C++ 和 C#?
  • RQ4SCC 能否识别特定编程语言的版本,如 C# 3.0、4.0 和 5.0?
  • RQ5在小段代码片段中,区分不同编程语言的最具判别力的特征是什么?

主要发现

  • SCC 在从代码片段中分类 21 种编程语言时,整体准确率达到 75%。
  • 所有语言的平均 F1 分数为 0.75,精确率和召回率分别为 0.76 和 0.75。
  • SCC 显著优于 PLI,后者在相同测试集上的准确率仅为 55.5%。
  • 在区分 C、C# 和 C++ 时,SCC 达到 80% 的准确率,显示出在语言家族分类任务中的强大性能。
  • SCC 对 C# 版本(3.0、4.0、5.0)的识别准确率达到 61%,证明了其在版本级别分类中的可行性。
  • 模型识别出语言特异性关键字作为最具判别力的特征,如 C# 的 'console',C++ 的 'cout',以及 C 的 'class'。

更好的研究,从现在开始

从阅读论文到最终审阅,大幅缩短您的研究时间。

无需绑定信用卡

本解读由 AI 生成,并经人工编辑审核。