Predicting the Programming Language of Questions and Snippets of StackOverflow Using Natural Language Processing

09/21/2018
by   Kamel Alreshedy, et al.
University of Victoria
2

Stack Overflow is the most popular Q&A website among software developers. As a platform for knowledge sharing and acquisition, the questions posted in Stack Overflow usually contain a code snippet. Stack Overflow relies on users to properly tag the programming language of a question and it simply assumes that the programming language of the snippets inside a question is the same as the tag of the question itself. In this paper, we propose a classifier to predict the programming language of questions posted in Stack Overflow using Natural Language Processing (NLP) and Machine Learning (ML). The classifier achieves an accuracy of 91.1 combining features from the title, body and the code snippets of the question. We also propose a classifier that only uses the title and body of the question and has an accuracy of 81.1 only that achieves an accuracy of 77.7 Machine Learning techniques on the combination of text and the code snippets of a question provides the best performance. These results demonstrate also that it is possible to identify the programming language of a snippet of few lines of source code. We visualize the feature space of two programming languages Java and SQL in order to identify some special properties of information inside the questions in Stack Overflow corresponding to these languages.

READ FULL TEXT VIEW PDF
POST COMMENT

Comments

There are no comments yet.

Authors

page 1

page 5

page 7

09/21/2018

SCC: Automatic Classification of Code Snippets

Determining the programming language of a source code file has been cons...
03/26/2018

StaQC: A Systematically Mined Question-Code Dataset from Stack Overflow

Stack Overflow (SO) has been a great source of natural language question...
04/14/2020

An Analysis of Python's Topics, Trends, and Technologies Through Mining Stack Overflow Discussions

Python is a popular, widely used, and general-purpose programming langua...
10/03/2021

DeepSCC: Source Code Classification Based on Fine-Tuned RoBERTa

In software engineering-related tasks (such as programming language tag ...
03/21/2022

PTM4Tag: Sharpening Tag Recommendation of Stack Overflow Posts with Pre-trained Models

Stack Overflow is often viewed as the most influential Software Question...
05/23/2018

Learning to Mine Aligned Code and Natural Language Pairs from Stack Overflow

For tasks like code synthesis from natural language, code retrieval, and...
06/06/2018

Studying the Difference Between Natural and Programming Language Corpora

Code corpora, as observed in large software systems, are now known to be...
This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

I Introduction

In the last decade, Stack Overflow has become a widely used resource in software development. Today inexperienced programmers rely on Stack Overflow to address questions they have regarding their software development activities.

Along with the growth of Stack Overflow, the number of programming languages in use has increased. In its 2018 Developer’s Survey, Stack Overflow lists 38 different programming languages in its list of most “loved”, “dreaded” and “wanted” languages. The TIOBE Programming Language index tracks more than 100 languages [22].

Forums like Stack Overflow rely on the tags of questions to match them to users who can provide answers. However, new users in Stack Overflow or novice developers may not tag their posts correctly. This leads to posts being downvoted and flagged by moderators even though the question may be relevant and adds value to the community. In some cases, Stack Overflow questions that are related to programming languages may lack a programming language tag. For example, Pandas is a popular Python library that provides data structures and powerful data analysis tools; however, its Stack Overflow questions usually do not include a Python tag. This could create confusion among developers who may be new to a programming language and might not be familiar with all of its popular libraries. The problem of missing language tags could be addressed if posts are automatically tagged with their associated programming languages.

Another related problem is the identification of the programming language of a snippet of code. Given a few lines of code, it is often necessary to identify the language in which they are written. Stack Overflow relies on the tag of a question to determine how to typeset any snippet in it. If a question is not tagged with any programming language, then the code is not typeset; however, the moment a tag is added, the code is rendered with different colours for the different syntactic constructs of the language. If a question has snippets in two or more languages, Stack Overflow will only use the first programming language tag to typeset all the snippets of the question.

The problem of identifying the language of snippets spans beyond Stack Overflow. Snippets are widely included in blog posts and stored in cut-and-paste tools (such as Github’s Gists and Pastebin). They might also be stored locally by the user in tools such as QSnippets or Dash. Gists require the user to give each snippet a filename, which it uses to classify the programming language. Pastebin, like most snippet management tools, requires the user to select the language of the snippet manually. In both cases, the onus is on the developer to specify the language. Most tools that use the source code in documentation (such as recommenders) typically require that the document is already tagged with a programming language to be processed (and assumes all the snippets in it are written in that language).

In this paper, we evaluate the use of Machine Learning (ML) models to predict the programming languages in Stack Overflow questions. Our research questions are:

  1. RQ1. Can we predict the programming language of a question in Stack Overflow?

  2. RQ2. Can we predict the programming language of a question in Stack Overflow without using code snippets inside it?

  3. RQ3. Can we predict the programming language of code snippets in Stack Overflow questions?

For the first research question, we are interested in evaluating how machine learning performs while trying to identity the language of a question when all the information in a Stack Overflow question is used; this includes its title, body (textual information) and code snippets in it. The purpose of the second research question is to determine if the inclusion of code snippets is an essential factor to determine the programming language that a question refers to. And finally, the purpose of the third research question is to evaluate the ability to use machine learning to predict the language of a snippet of source code; a successful predictor will have applications beyond Stack Overflow, as it could also be applied to snippet management tools and code search engines that scan documentation and blogs for relevant information.

The main contributions of the paper are as follows:

  1. A prediction method that uses a combination of code snippets and textual information in a Stack Overflow question. This classifiers achieves an accuracy of 91.1%, precision is 0.91 and recall 0.91 in predicting the tag of programming language.

  2. A classifier that uses only textual information in Stack Overflow questions to predict their programming language. This classifier achieves an accuracy of 81.1%, a precision of 0.83 and a recall of 0.81 which is much higher than the previous best model (Baquero et al. [13]).

  3. A prediction model based on Random Forest

    [24]

    and XGBoost

    [23] classifiers that predicts the programming language using only a code snippet in a Stack Overflow question. This model is shown to provide an accuracy of 77.7%, a precision of 0.79 and a recall of 0.77.

  4. Use of Word2Vec [15] to study the features of two programming languages, Java and SQL; these features are projected into a

    dimensional vector space using Word2Vec and visualized using t-SNE.

The rest of the paper is organized as follows. We begin by describing the dataset extraction and processing in Section II. Our methodology is described in Section III. The results and discussion are presented in Section IV and Section V respectively. Sections VI and VII discuss related and future work. Finally, threats to validity and conclusions are outlined in the last two sections of the paper (Sections VIII and IX).

Ii Dataset Extraction and Processing

In this section, the details of the Stack Overflow dataset are discussed. Then, the preprocessing steps used for data extraction and processing are explained.

Ii-a Stack Overflow Selection

As of July 2017, Stack Overflow had 37.21 million posts, of which 14.45 million are questions with 50.9k different tags. In this paper, the programming language tags in Stack Overflow are of interest. The most popular 24 programming languages as per the 2017 Stack Overflow developer survey were selected for analysis [17]. They constitute about 93% of the questions in Stack Overflow. The languages selected for our study are: Assembly, C, C#, C++, CoffeeScript, Go, Groovy, Haskell, Java, JavaScript, Lua, Matlab, Objective-c, Perl, PHP, Python, R, Ruby, Scala, SQL, Swift, TypeScript, Vb.Net, Vba.

Ii-B Extraction and Processing of Stack Overflow Questions

The Stack Overflow July 2017 data dump was used for analysis. In our study, questions with more than one programming language tag were removed to avoid potential problems during training. Questions chosen contained at least one code snippet, and the code snippet had at least characters. For each programming languages random questions were extracted; however, two programming languages had less than questions: Coffee Script (4,267) and Lua (8,460). The total number of questions selected was 232,727. Fig. 1 (a) shows an example of Stack Overflow post 111https://stackoverflow.com/questions/1642697/. It contains (1) the title of the post (2) the text body (3) the code snippet and (4) the tags of the post. It should be noted that the tags of the question were removed and not included as a part of text features during the training process to eliminate any inherent bias.

(a) Before applying NLP techniques.

maven tomcat version way maven tomcat version command tomcat instance version tomcat way plugin docs

(b) After applying NLP techniques.
Fig. 1: An example of a Stack Overflow Question.

The .xml data was parsed using xmltodict and the Python Beautiful Soup library to extract the code snippets and text from each question separately. See Fig 2. A Stack Overflow question consists of a title, body and code snippet. In some cases, a question contained multiple code snippets; these were combined into one. The questions were divided into three datasets: their titles, their bodies and their snippets.

Fig. 2: The dataset extraction process.

The title and body (to which we refer to as textual information) and code snippet were used to answer the first research question. The textual information was used to answer the second research question. Finally, the code snippets were used to answer the last research question.

Machine learning models cannot be trained on raw text because their performance is affected by noise present in the data. The textual information (title and body) need to be cleaned and prepared before the machine learning model can be trained to provide a better prediction. Few preprocessing steps were required to clean the text. First, the non-alphanumeric characters such as punctuation, numbers and symbols were removed. Second, the entity names were identified using the dependency parsing of the Spacy Library  [18]. An entity name is proper noun (for example, the name of an organization, company, library, function etc.). Third, the stop words such as after, about, all, and from etc. were removed. Fourth, since the entity name can have different forms (such as study, studies, studied and studious), it is useful to train using one of these words and predict the text containing any of the word forms. To achieve this goal, stemming and lemmatization was performed using the NLTK library in Python  [8]. At the end of all the preprocessing steps, the remaining words were used as features to help train machine learning model. Fig. 1(a) is the original Stack Overflow post and Fig. 1(b) is Stack Overflow post after application of NLP techniques.

Fig. 3: Box plots showing the number of lines of code in the extracted code snippets for all the languages. It should also be noted here that there were at least 400 posts which had more than 200 lines of code, however, were not included while making this plot.

The extracted set of questions provide a good coverage of different versions of a programming languages. For example, code snippets were extracted for the python tags: python-3.x, python-2.7, python-3.5, python-2.x, python-3.6, python-3.3 and python-2.6, for the Java tags java-8 and java-7, and for the C++ tags c++11, c++03, c++98 and c++14. The snippets extracted had a significant variation in lines of code, as shown in Fig. 3. The number of lines of code in the snippets varied from to .

Iii Methodology

Textual information (title and body) and code snippets extracted from the Stack Overflow questions were split using the Term Frequency-Inverse Document Frequency (Tf-IDF) vectorizer from the Scikit-learn library [9]. The Minimum Document Frequency (min-df) was set to 10, which means that only words present in at least ten documents were selected (a document can be either the code snippet, the textual information, or both—code snippet and textual information). This step eliminates infrequent words from the dataset which helps machine learning models learn from the most important vocabulary. The Maximum Document Frequency (max-df) was set to default because the stop words were already removed in the data preprocessing step discussed in Section II.

Iii-a Classifiers

The ML algorithms Random Forest Classifier (RFC) and XGBoost (a gradient boosting algorithm) were employed. These algorithms provided the higher accuracy compared to the other algorithms we explored, ExtraTree and MultiNomialNB. The performance metrics used in this paper for the classifiers are precision, recall, accuracy, F1 score and confusion matrix.

Iii-A1 Random Forest Classifier (RFC)

RFC [24]

is an ensemble algorithm which combines more than one classifer. This classifier generates a number of decision trees from randomly selected subsets of training dataset. Each subset provides a decision tree that votes to make the final decision during test. The final decision made depends on the decision of majority of trees. One advantage of this classifier is that if one or few of trees make a wrong decision, it will not affect the accuracy of the result significantly. Also, it avoids the overfitting problem seen in the Decision Tree model.The total number of trees in the forest is extremely important parameter because a large number of trees in the forest give high accuracy.

Iii-A2 XGBoost

XGBoost [23], standing for “Extreme Gradient Boosting”, is a tree based model similar to Decision Tree and RFC. The idea behind boosting is to modify the weak learner to be a better learner. Recall that Random Forest is a simple ensemble algorithm that generates many subtrees and each tree predicts the output independently. The final output will be decided by the majority of the votes from the subtrees. However, the XGBoost is more intelligent because each subtree makes the prediction sequentially. Hence, each subtree learns from the mistakes that were made by the previous subtree. The idea of XGBoost came from gradient boosting, but XGBoost uses the regularized model to help control overfitting and give a better performance.

The machine learning models were tuned using RandomSearchCV, which is a tool for parameter search in the Scikit-learn library. The XGBoost algorithm has many parameters, such as minimum child weight, max depth, L1 and L2 regularization, and evaluation metrics such as Receiver Operating Characteristic (ROC), accuracy and F1 score. RFC is a bagging classifier and has a parameter number of estimators which is the number of subtrees used to fit the model. It is important to tune the models by varying these parameters. However, parameter tuning is computationally expensive using a technique such as grid search. Therefore, a deep learning technique called Random Search (RS) tuning is used to train the models. All model parameters were fixed after RS tuning on the cross-validation sets (stratified ten-fold cross-validation). For this purpose, the datasets were split into training and test data using the ratio 80:20.

An important contribution of this paper is the study of the vocabulary and feature space for two programming languages (Java and SQL). A word2vec model [15] was used to visualize the features from code snippets and textual information (title and body) datasets using Gensim, which is a Python framework for vector space modelling [14]. The resulting model represented each word in the vocabulary in a dimensional vector space. The selection of as the number of dimensions for the trained word-vector is as per the recommendation of the original paper by T. Mikolov [21]. It is impossible to visualize concepts in such a large space, so T-SNE [16] was used to reduce the number of dimensions to . The top frequent 3% words were selected from the vectors for ‘Java and SQL’ and analyzed using word similarity and cosine distance. The code snippets and textual information features which are close to each other in the vector space were selected for Java and SQL using the cosine distance. Then, these features were visualized to understand the similarities and differences between Java and SQL.

Iv Results

In this section, the results obtained for the three research questions are described in detail.

RQ1. Can we predict the programming language of a question in Stack Overflow?

To answer this question, XGBoost and RFC classifiers were trained on the combination of textual information and code snippet datasets. The XGBoost classifier achieves an accuracy of 91.1%, and the average score for precision, recall and F1 score are 0.91, 0.91 and 0.91 respectively; on the other hand, the FRC achieves an accuracy of 86.3%, and the average score for precision, recall and F1 score are 0.87, 0.86 and 0.86 respectively. The results for XGBoost classifier are discussed in further detail because it provides the best performance. In Table I, the performance metrics for each programming language with respect to precision, recall and F1 score are given for XGBoost. Most programming languages have a high F1 score: Swift (0.97), GO (0.97), Groovy (0.97) and Coffeescript (0.97) had the highest, while java (0.75), SQL (0.78), C# (0.80) and Scala (0.88) had the lowest. Fig. 4 shows the performance of the XGBoost classifier as a confusion matrix.

Programming Precision Recall F1-score
Swift 0.98 0.96 0.97
Go 0.98 0.96 0.97
Groovy 0.99 0.95 0.97
Coffeescript 0.98 0.96 0.97
Javascript 0.97 0.95 0.96
C 0.98 0.95 0.96
C++ 0.97 0.93 0.95
Objective-c 0.97 0.94 0.95
Assembly 0.96 0.95 0.95
Haskell 0.95 0.95 0.95
Python 0.97 0.91 0.94
Vb.net 0.95 0.93 0.94
PHP 0.94 0.91 0.93
Ruby 0.89 0.93 0.91
Perl 0.91 0.91 0.91
Matlab 0.92 0.90 0.91
R 0.91 0.89 0.90
Lua 0.94 0.86 0.90
Typescript 0.90 0.88 0.89
Vba 0.85 0.91 0.88
Scala 0.85 0.92 0.88
C# 0.81 0.79 0.80
CQL 0.73 0.85 0.78
Java 0.70 0.82 0.75
TABLE I: Performance for the proposed classifier trained on textual information and code snippet features.
Fig. 4: Confusion matrix for the XGboost classifier trained on code snippet and textual information features. The diagonal represent the percentage of programming language that was correctly predicted.

RQ2. Can we predict the programming language of a question in Stack Overflow without using code snippets inside it?

To answer this research question, two machine learning models were trained using the XGBoost and RFC classifiers on the dataset that contained only the textual information. The XGBoost classifier achieved an accuracy of 81.1%, and the average score for precision, recall and F1 score were 0.83, 0.81 and 0.81 respectively. In Table II, the performance metrics for each programming language with respect to precision, recall and F1 score are given for XGBoost. RFC achieved slightly lower performance than XGboost, with an average score for precision, recall and F1 score of 0.76, 0.74 and 0.75 respectively. Note that the accuracy of XGBoost using textual information decreased by about 10% compared to using both the textual information and its code snippet. The top performing languages based on the F1 score are coffeescript (0.94), javascript (0.92), swift (0.92), Go (0.92), Haskell (0.92), C (0.91) Objective-C (0.90) and Assembly (0.89). Note further that the F1 scores of most of the programming languages in the table decreased by approximately 5% with a few exceptions (such as vb.net, vba, PHP, Lua, C#, SQL and Java). It should be further noted that the languages CoffeeScript, Javascript, Swift, Go and C have a high F1 score and performed very well in both the (RQ1) and (RQ2). Java and SQL have the worst performance metrics in (RQ1) and (RQ2), especially in (RQ2) the F1 score decreased by as much as 25%.

Programming Precision Recall F1-score
Coffeescript 0.96 0.91 0.94
Javascript 0.94 0.89 0.92
Swift 0.94 0.89 0.92
Go 0.95 0.89 0.92
Haskell 0.92 0.91 0.92
C 0.93 0.88 0.91
Objective-c 0.94 0.87 0.90
Assembly 0.92 0.87 0.89
Python 0.95 0.82 0.88
Groovy 0.95 0.82 0.88
C++ 0.92 0.83 0.87
Ruby 0.86 0.88 0.87
R 0.88 0.82 0.85
Perl 0.88 0.81 0.84
Matlab 0.88 0.80 0.84
Scala 0.80 0.90 0.84
Typescript 0.86 0.80 0.83
Vb.net 0.82 0.82 0.82
Vba 0.76 0.81 0.78
PHP 0.82 0.72 0.77
Lua 0.73 0.59 0.65
C# 0.65 0.61 0.63
SQL 0.42 0.75 0.54
Java 0.43 0.58 0.49
TABLE II: Performance for the proposed classifier trained on textual information features.
Programming Precision Recall F1-score
Javascript 0.94 0.88 0.91
Coffeescript 0.92 0.86 0.89
PHP 0.91 0.85 0.88
Go 0.92 0.84 0.87
Groovy 0.91 0.84 0.87
Swift 0.91 0.82 0.86
C 0.86 0.82 0.84
Vb.net 0.89 0.81 0.84
Haskell 0.87 0.8 0.83
C++ 0.86 0.78 0.82
Vba 0.82 0.77 0.80
Lua 0.87 0.74 0.80
Assembly 0.76 0.76 0.76
Python 0.85 0.67 0.75
Ruby 0.72 0.79 0.75
Matlab 0.79 0.72 0.75
Scala 0.71 0.79 0.75
SQL 0.70 0.77 0.73
C# 0.78 0.68 0.73
Perl 0.75 0.72 0.73
R 0.72 0.70 0.71
Typescript 0.68 0.66 0.67
Java 0.64 0.69 0.66
Objective-c 0.42 0.85 0.56
TABLE III: Performance for the proposed classifier trained on code snippet features.
Model Description Accuracy Precision Recall F1 score
Previous
 Baquero [18] code snippets

A model trained using Support Vector Machine on

question questions from Stack Overflow using code features
44.6% 0.45 0.44 0.44
 Baquero [18] textual information
A model trained using Support Vector Machine on
questions from Stack Overflow using text features
60.8% 0.68 0.60 0.60
Proposed
 code snippet features
XGBoost classifier trained on Stack Overflow
questions using code snippet features
77.7% 0.79 0.77 0.78
 textual information features
XGBoost classifier trained on Stack Overflow
questions using textual information features
81.1% 0.83 0.81 0.81
 code snippet and textual information features
XGBoost classifier trained on Stack Overflow questions
using code snippets and textual information features
91.1% 0.91 0.91 0.91
TABLE IV: A Comparison of Previous and Proposed classifiers

RQ3. Can we predict the programming language of code snippets in Stack Overflow questions?
To predict the programming language from a given code snippet, two ML classifiers were trained on the code snippet dataset. XGBoost achieved an accuracy of 77.7%, and the average score for precision, recall and F1 score are 0.79, 0.77 and 0.77 respectively. In Table III, the performance metrics for each programming language with respect to precision, recall and F1 score are given while using XGBoost. RFC achieved accuracies of accuracy of 70.1%, and the average score for precision, recall and F1 score are 0.72, 0.72 and 0.70 respectively. The results obtained for the code snippet dataset show the worst performance for both classifiers. The programming languages JavaScript (0.91), CoffeeScript (0.89) and PHP (.88) had a good F1 score. The F1 score of PHP in (RQ1) is close to the average and in (RQ2) is one of the worst; however, in (RQ3) the PHP language has the third highest F1 score. Objective-C has the worst F1 score and precision (0.56 and 0.42); but its recall is extremely high (0.85). When the programming language of a code snippet is extremely hard to identity, XGBoost frequently misclassified it as Objective-C, while we observed that RFC misclassified such snippets as Typescript. We have looked manually at some of these code snippets and are not able to identify the programming language easily222https://stackoverflow.com/questions/855360/333https://stackoverflow.com/questions/942772/444https://stackoverflow.com/questions/9986404/555https://stackoverflow.com/questions/2115227/. This is the main motivation for combining the textual information and code snippet in (RQ1). If the classifier gets confused when predicting the programming langauges from a code snippet, the textual information (title and body) will help the machine learning model to make a better prediction. Fig. 5 shows the confusion matrix for the XGBoost classifier. Table V shows how the accuracy improves as the minimum size of the code snippet in the dataset is increased from 10 to 100 characters.

Fig. 5: Confusion matrix for the XGboost classifier trained on code snippet features. The diagonal represent the percentage of programming language that was correctly predicted.

The comparison between the results of textual information dataset and code snippet dataset shows a slightly difference in accuracy of 5% (in average). On the other hand, using the combination of textual information and code snippet significantly increased the accuracy by 10% compared to using only the textual information and 14% compared to using only code snippets. Since many Stack Overflow posts can have a large textual information and a small code snippet or vice versa, combining the two gives a high accuracy in (RQ1).

V Discussion

The most important observation in the previous section is that for the research question (RQ1), XGBoost achieves high accuracy of 91.1%, while for (RQ2) and (RQ3), it only achieves an accuracy of 81.1% and 77.7% respectively. This observations highlights the importance of using the combining of textual information and code snippets in predicting tags in comparison to textual information or code snippet only. In some cases, Stack Overflow posts contain very small code snippet making it extremely hard to identify its language as many programming languages sharing the same syntax.

The Minimum Characters Accuracy Precision Recall F1-score
More than 10 77.7% 0.79 0.77 0.78
More than 25 79.1% 0.80 0.97 0.79
More than 50 81.7% 0.82 0.81 0.81
More than 75 83.1% 0.83 0.83 0.83
More than 100 84.7% 0.85 0.84 0.84
TABLE V: Effect of the minimum number of characters in code snippet on accuracy

Dependency parsing and extracting of entity names using a Neural Network (NN) through Spacy appeared to help reduce noise and to extract important features from Stack Overflow questions. This is likely the main reason for the significant improvement in performance compared to previous approaches in the literature.

The analysis of the feature space of the top performing languages indicates that these languages have unique code snippet features (keywords/identifiers) and textual information features (libraries, functions). For example, when the textual information based features were visualized for Haskell, words such as ‘GHC’, ‘GHCI’, ‘Yesod’ and ‘Monad’ were obtained. ‘GHC’ and ‘GHCI’ are compilers for Haskell, ‘Yesod’ is a web-based framework and ‘Monad’ is a functional programming paradigm (Haskell is a functional programming language). Most of the top performing languages have a small feature space (vocabulary) as compared to more popular languages such as Java, Vba and C# which have a large number of libraries and standard functions, and support multiple programming paradigms resulting in a large feature space. A large feature space adds more complexity to the ML models.

The Word2Vec models were trained in two different datasets, the code snippets and textual information, to visualize their features. Fig. 6 shows the features for Java and Fig. 7

shows the features for SQL. There were more than 200 features extracted using Word2Vec for each language, but for clarity, only about 70 features are shown. In the vector space, features which are used in the same context are close. For example, in the Java code snippets (Fig. 

6), ‘public’,‘class’,‘implements’ and ‘extends’ are all close to one another in the vector space since they are typically used together in a line of code. Further, Fig. 7 shows that in SQL code snippets features such as ‘foreign’, ‘primary’, ‘key’, ‘constraint’ and ‘reference’ are all used in the context of setting relationships between tables.

(a) Java code snippet features.
(b) Java textual information features.
Fig. 6: Code snippet and textual information features of Java represented in two dimensions after using t-SNE on a trained Word2Vec model.
(a) SQL code snippet features.
(b) SQL textual information features.
Fig. 7: Code snippet and text information features of SQL represented in two dimensions using t-SNE on a trained Word2Vec model.

The smaller cosine distance between features does not necessarily mean they are used in the same line of code but are used in the same context, e.g. declaring relationships in tables. This is because, in Stack Overflow, developers describe their issues by including code based identifiers and features along with text not just in code blocks. Another critical observation from the Java textual information features is that ‘Android’ and ‘spring’ (a framework for web and desktop applications) are located at opposite sides of the plot (maximum cosine distance). This means that an Android developer rarely uses the spring framework for development. Conversely, ‘spring’ is located close to ‘web’, ‘service’, ‘url’, ‘request’ and ‘xml’, which indicates that spring framework is frequently used in web development in Java.

Vi Related Work

Baquero et al. [13] proposed a classifier to predict the programming language of a Stack Overflow question. They extracted a set of questions from Stack Overflow that contained text and code snippets, questions for each of programming languages. They trained two classifiers using a Support Vector Machine model on two different datasets which are text body and code snippet features. The evaluation achieved an accuracy of 60% for text body features and 44% for code snippet features which are much lower than the results obtained in this paper. Table IV summarizes our results in comparison to [13] as, to the best of our knowledge, it is the only previous work in the literature that tackles the problem of predicting programming language tags for Stack Overflow questions.

Kennedy et al. [20] studied the problem of using natural language identification to identify the programming language of entire source code files from GitHub (rather than questions from Stack Overflow). Their classifier is based on five statistical language models from NLP and identifies programming languages and can achieve a high accuracy of 97.5%. In our work, programming languages are predicted using small code snippets rather than source code file. Similarly, Khasnabish et al. [19] proposed a model to detect

programming languages using source code files. Four algorithms were used to train and test the model using Bayesian learning techniques, i.e. NB, Bayesian Network (BN) and Multinomial Naive Bayes (MNB). It was shown that MNB provides the highest accuracy of

%.

Some editors such as Sublime and Atom add highlights to code based on the programming language. However, this requires an explicit extension, e.g. .html, .css, .py. Portfolio [3] is a search engine that supports programmers in finding functions that implement high-level requirements in query terms. This engine does not identify the language, but it analyzes code snippets and extracts functions which can be reused. Holmes et al. [5] developed a tool called Strathcona that can find similar snippets of code.

Rekha et al. [7] proposed a hybrid auto-tagging system that suggests tags to users who create questions. When the post contains a code snippet, the system detects the programming language based on the code snippets and suggests many tags to users. Multinomial Naive Bayes (MNB) was trained and tested for the proposed classifier which achieved 72% accuracy. Saha et al. [1] converted Stack Overflow questions into vectors, and then trained a Support Vector Machine using these vectors and suggested tags used the model obtained. The tag prediction accuracy with this model is 68.47%. Although it works well for some specific tags, it is not effective with some popular tags such as Java. Stanley and Byrne [2]

used a cognitive-inspired Bayesian probabilistic model to choose the most suitable tag for a post. This is the tag with the highest probability of being correct given the a priori tag probabilities. However, this model normalizes the top for all questions, so it is unable to differentiate between a post where the top predicted tag is certain, and a post where the top predicted tag is questionable. As a consequence, the accuracy is only 65%.

Vii Future Work

The study of programming language prediction from textual information and code snippets is still new, and much remains to be done. Most of the existing tools focus on file extensions rather than the code itself. In recent years, there has been tremendous progress made in the field of deep learning, especially for time series or sequence-based models such as Recurrent Neural Networks (RNNs) and Long Short-Term Memory (LSTM) networks. RNN and LSTM models can be trained using source code one character at a time as input, but they can have a high computational cost.

NLP and ML techniques perform much better in predicting languages compared to tools that predict directly from code snippets. Stack Overflow text is somewhat unique in the sense that it captures the tone, sentiments and vocabulary of the developer community. This vocabulary varies depending on the programming language. Therefore, it is important that the vocabulary for each programming language is captured, understood and separated. It is worth exploring if a CNN combined with Word2Vec can be used for this task.

In the future, our model will be evaluated using programming blog posts, library documentation and bug repositories. This would help us understand how general the model is.

Viii Threats to Validity

Construct Validity: In creating the datasets from Stack Overflow, only the most popular programming languages were extracted, and this was based solely on the programming language tag. However, some tags synonymous with languages were not included in the extraction process. For example, ‘SQL SERVER’, ‘PLSQL’ and ‘MICROSOFT SQL SERVER’ are related to ‘SQL’ but were discarded.

Internal validity: After the datasets were extracted, dependency parsing was used to select entity names so as to include only the most relevant code snippet and text features. The use of dependency parsing can result in the loss of critical vocabulary and might affect our results. However, we manually analyzed the vocabulary before and after the dependency parsing to ensure that information related to the languages was not lost. Further, selecting additional features such as lines of code and programming paradigm could have improved our results but was not considered.

External validity: The focus of this paper was to obtain a classifier for predicting languages due to the lack of open source tools for this task. Stack Overflow was used in this study as the data source but other sources such as GitHub repositories were not explored. Therefore, no conclusions can be made about the results with other sources of code snippets and text on programming languages. Furthermore, some common programming languages such as Cobol and Pascal were not considered in this study.

Ix Conclusions

This work tackles the important problem of predicting programming languages from code snippets and textual information. In particular, it chose to focus on predicting the programming language of Stack Overflow questions. Our results show that training and testing the classifier by combining the textual information and code snippet achieves the highest accuracy of 91.1%. Other experiments using either textual information or code snippets, achieve accuracies of 81.1% and 77.7% respectively. This implies that information from textual features is easier for a machine learning model to learn as compared to information from code snippet features. Our results also show that it is possible to identify the programming language of a snippet of few lines of source code. We believe that our classifier could be applied in other scenarios such as code search engines and snippet management tool.

References

  • [1] A. K. Saha, R. K. Saha, and K. A. Schneider, “A discriminative model approach for suggesting tags automatically for stack overflow questions,” In Proc. of the 10th Working Conf. on Mining Software Repositories (MSR), 2013, pp. 73-–76.
  • [2] C. Stanley and M. D. Byrne, “Predicting Tags for StackOverflow Posts,” In Proc. of Int. Conf. on Cognitive Modeling (ICCM), 2013. pp. 414–419
  • [3] C. McMillan, M. Grechanik, D. Poshyvanyk, Q. Xie, and C. Fu, “Portfolio: A search engine for finding functions and their usages,” In Proc. of 33rd Int. Conf. on Software Engineering (ICSE), 2011, pp. 1043–-1045.
  • [4] M. Revelle, B. Dit, and D. Poshyvanyk, “Using Data Fusion and Web Mining to Support Feature Location in Software,” In Proc. of 18th IEEE Int. Conf. on Program Comprehension (ICPC), 2010, pp. 14-–23.
  • [5] R. Holmes, R. J. Walker, and G. C. Murphy, “Strathcona example recommendation tool,” ACM SIGSOFT Software Engineering Notes, 30(5), 2005, pp. 237-–240.
  • [6] C. B. Seaman, “The Information Gathering Strategies of Software Maintainers,” In Proc. of the 18th Int. Conf. on Software Maintenance (ICSM), 2002, pp. 141–149.
  • [7] V. S. Rekha, N. Divya, and P. S. Bagavathi, “A Hybrid Auto-tagging System for StackOverflow Forum Questions,” in Proc. of the 2014 Int. Conf. on Interdisciplinary Advances in Applied Computing (ICONIAAC), 2014, pp. 56:1-–56:5.
  • [8] E. Loper and S. Bird, “NLTK: The Natural Language Toolkit,” In Proc. of 42nd Annual Meeting of the Association for Computational Linguistics (ACL), 2004. pp. 63–70
  • [9] F. Pedregosa, G. Varoquaux, A. Gramfort, V. Michel, B. Thirion, O. Grisel, M. Blondel, P. Prettenhofer, R. Weiss, V. Dubourg, J. Vanderplas, A. Passos, D. Cournapeau, M. Brucher, M. Perrot, and E. Duchesnay, “Scikit-learn: Machine Learning in Python,” J. Machine Learning Research 12, 2011, pp. 2825–2830.
  • [10] L. Mamykina, B. Manoim, M. Mittal, G. Hripcsak, and B. Hartmann, “Design lessons from the fastest Q&A site in the west,” In Proc. of the SIGCHI Conference on Human factors in Computing Systems (CHI), 2011, pp. 2857-–2866.
  • [11] M. Asaduzzaman, A. S. Mashiyat, C. K. Roy, and K. A. Schneider, “Answering questions about unanswered questions of stack overflow,” In Proc. of the 10th Working Conference on Mining Software Repositories (MSR), 2013, pp. 97–100.
  • [12] SoStats, 2017. [Online]. Available: https://sostats.github.io/last30days/
  • [13] J. F. Baquero, J. E. Camargo, F. Restrepo-Calle, J. H. Aponte, and F. A. González, “Predicting the Programming Language: Extracting Knowledge from Stack Overflow Posts,” In Proc. of Colombian Conf. on Computing (CCC), 2017, Springer, pp. 199–21.
  • [14] R. Rehurek and P. Sojka, “Gensim: Python Framework for Vector Space Modelling,” NLP Centre, Faculty of Informatics, Masaryk University, Brno, Czech Republic, 2011.
  • [15]

    T. Mikolov, I. Sutskever, K. Chen, G. S. Corrado, and J. Dean, “Distributed Representations of Words and Phrases and their Compositionality,” In

    Proc. of 26th Annual Conference on Neural Information Processing Systems (NIPS), 2013, pp. 3111–3119.
  • [16] L. J. P. van der Maaten and G. E. Hinton, “Visualizing Data Using t-SNE,” J. Machine Learning Research 9, pp. 2431–2456, Nov. 2008.
  • [17] Developer Survey Results 2017. [Online]. Available: https://insights.stackoverflow.com/survey/2017#technology
  • [18] M. Honnibal and M. Johnson, “An Improved Non-monotonic Transition System for Dependency Parsing," In Proc. of Conf. on Empirical Methods in Natural Language Processing (EMNLP), 2015, pp. 1373–-1378.
  • [19] J. N. Khasnabish, M. Sodhi, J. Deshmukh, and G. Srinivasaraghavan, “Detecting Programming Language from Source Code Using Bayesian Learning Techniques,” In

    Proc. of Int. Conf. on Machine Learning and Data Mining in Pattern Recognition

    (MLDM), 2014, Springer, pp. 513–522.
  • [20] J. Kennedy, V Dam and V. Zaytsev, “Software Language Identification with Natural Language Classifiers,” In Proc. of IEEE Int. Conf. on Software Analysis, Evolution, and Reengineering (SANER), 2016, pp. 624–628.
  • [21] T. Mikolov, K.Chen, G. Corrado, and J. Dean.“Efficient Estimation of Word Representations in Vector Space,” In Proc. of Int. Conf, on Learning Representations (ICLR), Workshops Track, 2013.
  • [22] TIOBE Programming Community Index, https://www.tiobe.com, Mar. 2018.
  • [23] T. Chen and C. Guestrin, “XGBoost: A Scalable Tree Boosting System,” In Proc. of 22nd SIGKDD Conf. on Knowledge Discovery and Data Mining (KDD), 2016. pp. 785–794.
  • [24] L. Breiman, “Random Forests,” Machine Learning, 45(1), pp. 5-–32, 2001.