Semantic Source Code Segmentation using Small and Large Language Models

Abdelhalim Hafedh Dahou, Ansgar Scherp, Sebastian Kurten, Brigitte Mathiak, Madhu ChauhanarXiv 2025 · DOI

Introduction

As software repositories grow in complexity, navigating large scripts becomes increasingly difficult. While traditional tools focus on syntactic boundaries like function definitions or brackets, semantic source code segmentation aims to break scripts into logically coherent functional blocks, such as "Data Wrangling" or "Visualization." This task is particularly vital for languages like R, which is prevalent in social sciences and psychology but often lacks the standardized engineering practices found in major Python repositories.

Example of an input R script segmented into semantic functional blocks such as Loading Data and Data Wrangling.

The StatCodeSeg Dataset

A primary contribution of this research is StatCodeSeg, a novel human-annotated dataset designed to benchmark semantic segmentation in R. The dataset comprises 160 R files totaling 13,819 lines sourced from the Open Science Framework (OSF). Domain experts categorized every line into one of seven functional classes: Loading Library, Loading Data, Data Wrangling, Analysis, Visualization, Saving to Output, and Comment.

Methodology

The researchers evaluated two distinct strategies for automated segmentation:

  1. Line-by-Line Classification: A sliding window approach where the model predicts the function of a target line based on its immediate context (surrounding lines).
  2. Range-Based Segmentation: A holistic approach where the model identifies the start and end indices for every functional block within an entire script.

The study compared fine-tuned Small Language Models (SLMs), such as CodeBERT (125M parameters) and an encoder-only version of CodeT5+, against state-of-the-art Large Language Models (LLMs) including GPT-4o, Gemini 1.5 Pro, and Claude 3.5 Sonnet using zero-shot and few-shot (CARP) prompting.

Results and Discussion

The experimental results revealed a surprising trend: fine-tuned SLMs consistently outperformed massive LLMs. Despite not being explicitly pre-trained on R, CodeBERT achieved the highest accuracy of 77.14% on the R dataset after fine-tuning on a relatively small labeled set of 4,130 lines.

Performance metrics for various models on the StatCodeSeg dataset, highlighting the superior performance of fine-tuned SLMs.

As shown in the results table, CodeBERT leads in nearly all metrics, including Macro and Micro F1 scores. Among the LLMs, Gemini 1.5 Pro was the most competitive in line-by-line tasks (75.92% accuracy), while Claude 3.5 Sonnet performed best in range-based scenarios (76.81% accuracy). The struggle of LLMs in this domain is attributed to a "data-shift" in social science coding styles, which often deviate from the "clean code" paradigms prevalent in the LLMs' training data.

Conclusion

This research demonstrates that for specialized tasks like semantic code segmentation in low-resource languages, fine-tuned smaller models can be more effective and efficient than general-purpose LLMs. Future work will focus on expanding StatCodeSeg to include other research-oriented languages like SPSS, STATA, and SAS, and further refining prompt engineering to bridge the gap for LLMs.

Summary updated

BibTeX
@misc{dahou2025semantic,
  author = {Abdelhalim Hafedh Dahou and Ansgar Scherp and Sebastian Kurten and Brigitte Mathiak and Madhu Chauhan},
  title = {{Semantic Source Code Segmentation using Small and Large Language Models}},
  year = {2025},
  eprint = {2507.08992},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2507.08992},
}
Figure