An automatic abbreviation method and system for long method names based on abbreviations
By constructing an automatic abbreviation system for long method names based on context and machine learning, and utilizing a probabilistic abbreviation dictionary and a Bernoulli Bayes model, the system solves the code quality problem caused by improper use of abbreviations in existing technologies, achieving efficient and accurate method name abbreviation and improving code readability and maintainability.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- PEKING UNIV
- Filing Date
- 2024-05-23
- Publication Date
- 2026-04-14
AI Technical Summary
Existing technologies lack automated methods or tools to help programmers use abbreviations effectively, leading to decreased code readability and maintainability. Junior programmers find it difficult to use abbreviations appropriately, which affects code quality.
We construct an automatic abbreviation system for long method names based on context and machine learning. By utilizing a probabilistic abbreviation dictionary and a Bernoulli Bayes model, combined with a large-scale open-source code library, we can automatically mine and migrate existing abbreviations to automatically abbreviate method names.
It significantly improves the accuracy of abbreviated long method names, enhances code readability and maintainability, and is clearly superior to existing methods.
Smart Images

Figure CN118567623B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of information technology, specifically relating to a method and system for automatically abbreviating long method names based on abbreviations. Background Technology
[0002] Code identifiers are a crucial component of software source code and a key source of information for understanding the code. Therefore, the quality of identifiers has a decisive impact on the quality of software code, particularly on the readability, understandability, and maintainability of the software system. Abbreviations are widely used in code identifiers, exerting a complex and significant influence on code readability and understandability. Abbreviations are shortened versions of normal words (or phrases). To reduce the length of code identifiers, programmers often use abbreviations to replace certain full terms or phrases. Experienced programmers use abbreviations to shorten the length of complex identifiers, thereby facilitating code input and formatting, and improving development efficiency and readability. However, the widespread use of abbreviations can also have negative effects, such as reducing code readability and maintainability. When software maintainers read code containing abbreviations, if they cannot quickly and accurately understand the semantics of the abbreviations, it may lead to slow code reading or even misunderstanding of the code, resulting in software defects. Accurate and efficient abbreviation expansion methods and tools are an effective way to mitigate the negative impacts of abbreviations.
[0003] Using abbreviations appropriately is an effective way to leverage their positive value and reduce their negative impact. However, where to use abbreviations and which abbreviations to use often relies entirely on manual determination by the programmer, lacking effective supporting tools. For most junior programmers, this decision is often difficult and may lead to inappropriate abbreviation use, thereby reducing code quality. Although many general rules have been summarized to guide the use of abbreviations, there are currently no automated methods or tools to help programs automatically abbreviate given identifiers. Summary of the Invention
[0004] To address the aforementioned problems, this invention proposes an automatic abbreviation method and system for long method names based on abbreviations. Based on existing abbreviation expansion tools and open-source code, a large-scale probabilistic abbreviation dictionary is constructed. The probability of abbreviating the full name and the probability of it being replaced by each abbreviation are statistically analyzed. First, context-based abbreviation is performed. Then, an automatic abbreviation method for long method names based on the probabilistic abbreviation dictionary is designed to abbreviate the long method names. Finally, a machine learning-based automatic abbreviation model for long method names is used to further abbreviate the initially abbreviated method names.
[0005] The technical solution adopted in this invention is as follows:
[0006] An automatic abbreviation method for long method names based on abbreviations includes the following steps:
[0007] Shorten long method names based on context;
[0008] Construct a probabilistic abbreviation dictionary, and use the probabilistic abbreviation dictionary to shorten long method names after context-based abbreviation;
[0009] We construct a machine learning-based automatic abbreviation model for long method names, and use this model to abbreviate long method names that have been abbreviated based on a probabilistic abbreviation dictionary.
[0010] Furthermore, the context-based abbreviation of long method names refers to referencing how sibling methods that share the same context with the current method use abbreviations, and then transferring the usage of abbreviations in the sibling methods to the current method.
[0011] Furthermore, the context-based abbreviation of long method names includes:
[0012] Find all abbreviations and their corresponding full names from the sibling methods of the current method mn, count the frequency of each "abbreviation-full name" pair, and then...<abb,fterm,freq> The abbreviations are recorded in the form of an abbreviation list abbList, where abb represents the abbreviation, fterm represents the full name of the abbreviation, and freq represents the frequency of the abbreviation.
[0013] Sort the elements of abbList in descending order of frequency, and then try to use each triplet in abbList in turn.<abb,fterm,freq> To abbreviate mn, if mn contains the full term fterm, then use the abbreviation abbreviation abbreviation to replace fterm.
[0014] Furthermore, the construction of the probabilistic abbreviation dictionary includes:
[0015] This process involves collecting and statistically analyzing abbreviations and full terms appearing in open-source projects. An abbreviation expansion tool is used to expand the abbreviation "abbr" in the code to automatically obtain its recommended full term "fterm," thereby constructing a large-scale probabilistic abbreviation dictionary with complete probability values. Each entry in the probabilistic abbreviation dictionary is represented as a quintuple.
[0016] <fterm,abb,freq,p1,p2>
[0017] Where freq and p1 represent the frequency and probability of the full term fterm in the method name being abbreviated to abb, respectively; p2 represents the probability of the abbreviation abb in the method name being expanded to fterm.
[0018] Furthermore, the shortening of long method names based on context-based abbreviation using a probabilistic abbreviation dictionary includes:
[0019] The method name mn is split into a series of tokens, then the tokens are arranged and combined, and the resulting combinations are sorted by length, with the longest combination placed at the beginning of the list phrases.
[0020] For each phrase tm in the phrase, find the entries containing that phrase from the probabilistic abbreviation dictionary.<tm,*,*,*,*> The full names of these terms must be completely consistent with the phrase tm, and the retrieved terms will be sorted in reverse order according to their frequency value p1, ensuring that the term with the highest frequency value p1 is placed first.
[0021] For the term with the highest frequency value p1, if its frequency value p1 is greater than a predefined threshold, the term will be used.<tm,abb,freq,p1,p2> The abbreviation "abb" replaces the full name "tm" in the method name "mn", thus achieving automatic abbreviation of the method name.
[0022] Furthermore, the machine learning-based automatic abbreviation model for long method names is a trained Bernoulli Bayes model.
[0023] Furthermore, the probability of abbreviating each full term in the method name is predicted using a Bernoulli Bayes model. The term with the highest output probability is selected. If its probability is greater than a predefined threshold β, the method name is abbreviated according to the term selected by the Bernoulli Bayes model.
[0024] An automatic abbreviation system for long method names based on abbreviations, comprising:
[0025] The context-based shortening module is used to shorten long method names based on context.
[0026] The abbreviation module based on the probabilistic abbreviation dictionary is used to build a large-scale probabilistic abbreviation dictionary and to abbreviate long method names after context-based abbreviation based on the probabilistic abbreviation dictionary.
[0027] The machine learning-based abbreviation module is used to build an automatic abbreviation model for long method names. This model abbreviations long method names that have been abbreviated based on a probabilistic abbreviation dictionary.
[0028] The beneficial effects of this invention are as follows:
[0029] This invention proposes an automatic abbreviation method and system for long method names based on abbreviations. First, it identifies other methods in the project that are highly similar to the current method, mines their already used abbreviations, and migrates them to the current method name, thus achieving abbreviation. Furthermore, this method automatically mines abbreviations from a large-scale open-source codebase, constructing the first large-scale probabilistic abbreviation dictionary with complete probabilities. The method also utilizes machine learning techniques and the large-scale probabilistic abbreviation dictionary to further abbreviate long method names, making them conform to length requirements. This invention can effectively and significantly improve the accuracy of automatic abbreviation of long method names. Attached Figure Description
[0030] Figure 1 This is a diagram illustrating the working principle of the automatic abbreviation method for long method names based on abbreviations. Detailed Implementation
[0031] To make the above-mentioned objects, features and advantages of the present invention more apparent and understandable, the present invention will be further described in detail below with reference to specific embodiments and accompanying drawings.
[0032] Figure 1 This diagram illustrates the working principle of an automatic abbreviation method for long method names based on abbreviations. For the input long method name, this method uses three sub-algorithms to abbreviate it: context-based abbreviation, abbreviation based on a probabilistic abbreviation dictionary, and machine learning-based abbreviation. Before using these three algorithms, the following two preprocessing steps are required: First, a large-scale probabilistic abbreviation dictionary, pAbbDict, with complete probability values needs to be constructed; second, a machine learning-based automatic abbreviation model for long method names needs to be pre-trained using training data to obtain the trained learning model, NBM.
[0033] After completing the preparation work (preprocessing), this method shortens the long input method name mn according to the following steps:
[0034] (1) Find all abbreviations and their corresponding full names from the signatures of the sibling methods of mn (i.e., all methods declared in the same class), count the frequency of each "abbreviation-full name" pair, and then use the results to determine the frequency of each pair.<abb,fterm,freq> The abbreviations are recorded in the form of an abbreviation list called abbList, where abb represents the abbreviation, fterm represents the full term corresponding to the abbreviation, and freq represents the frequency (number of occurrences) of the abbreviation. The elements of abbList are sorted in descending order of freq, and then each triple in abbList is attempted to be used sequentially.<abb,fterm,freq> To abbreviate mn: If mn contains the full name fterm, then replace fterm with the abbreviation abbreviation abbreviation. The method name after this abbreviation is denoted as mn'.
[0035] (2) This method abbreviates mn' based on the large-scale probabilistic abbreviation dictionary pAbbDict. The probabilistic abbreviation dictionary identifies frequently used full names with highly consistent usage patterns (corresponding abbreviations). If mn' contains such a full name, it is replaced with the corresponding abbreviation. The replaced method name is denoted as "mn".
[0036] (3) This method uses a pre-trained Bernoulli Bayes model (NBM) to process all the full words (or phrases) in the method name "mn", clarifying which words need to be abbreviated and how to abbreviate them. After each successful abbreviation, NBM continues to iterate through the abbreviated method name until NBM determines that all the full words in the method name do not need to be abbreviated. The method name finally output by the model is the short method name recommended by this method.
[0037] The main steps of the automatic abbreviation method for long method names based on abbreviations in this invention are described in detail below:
[0038] 1. Context-based method name abbreviation
[0039] Even the same full term may require abbreviation in some method names but not in others. This is because whether a word / phrase in a method name should be abbreviated (i.e., replaced with its corresponding abbreviation) is often influenced by various external factors, such as the domain of the software project, the corporate culture of the development team, and the developers' own preferences. These factors are collectively referred to as context. However, current technology struggles to capture (and quantify) this context, and it is unclear how this context influences the abbreviation decision for words (or phrases).
[0040] To address this challenge, the core idea of this method is to refer to how sibling methods that share the same context as a given method (m) use abbreviations, and then transfer similar usages to the current method m. This mechanism is called Context-Based Abbreviation Method, or CAC for short.
[0041] For example, for a method `m` from class `ec`, CAC abbreviates its method name `mn` as follows: First, CAC iterates through the signatures of all methods in class `ec`. The method signature mainly includes the method's return type, method name, parameter names, and parameter types. CAC uses the tool `tfExpander` to expand the abbreviations in the method signature. Based on `tfExpander`, CAC collects abbreviation-full name combinations from the method signatures.<abb,fterm> Here, abb represents the abbreviation appearing in the signature, and fterm is the corresponding full name. If a certain abbreviation abbreviation appears n times, and the full name recommended by tfExpander is fterm each time, then CAC will record n identical pairs.<abb,fterm> To simplify this record format, CAC removes redundant binary tuples and uses a simple triple.<abb,fterm,freq> This indicates the usage and frequency of abbreviations. If two triplet pairs appear...<abb1,fterm1,freq1> and<abb2,fterm1,freq2> This means that the full name fterm1 is sometimes abbreviated as abb1 and sometimes as abb2. This could lead to confusion in subsequent method name abbreviations based on triples, as it's unclear whether to abbreviate the full name to abb1 or abb2. Therefore, CAC removes the triple with lower frequency, ensuring that each full name has at most one associated triple. CAC sorts all triples in abbList, placing the most frequent triples first. Then, CAC processes each sorted triple...<abb,fterm,freq> Shorten long method names: If the method name contains fterm, replace it with abb.
[0042] 2. Automatic Abbreviation of Method Names Based on a Probabilistic Abbreviation Dictionary
[0043] If an abbreviation does not appear in the context described in the previous section, even if it is a widely recognized and commonly used abbreviation, the context-based automatic abbreviation method cannot use it to shorten long method names. To solve this problem, this method uses a probabilistic abbreviation dictionary-based approach. This method first mines from a large-scale open-source codebase to automatically construct a large-scale abbreviation dictionary with complete probability values. Based on this abbreviation dictionary, long method names are shortened. This method mainly consists of two parts: 1) mining a large amount of open-source code and automatically constructing a large-scale abbreviation dictionary with complete probability values; 2) further shortening long method names based on this abbreviation dictionary.
[0044] (1) Constructing a probabilistic abbreviation dictionary
[0045] This method mines open-source projects, analyzes and expands the abbreviations appearing in all method names, thereby collecting and statistically analyzing the occurrence of abbreviations and full names in open-source projects. For the abbreviation "abbr" in the code, this method uses the abbreviation expansion tool "tfExpander" to expand "abbr" to automatically obtain its recommended full name "fterm", and then constructs a large-scale abbreviation dictionary "pAbbDict" with complete probability values. Each entry in this dictionary can be represented as a quintuple:
[0046] <fterm,abb,freq,p1,p2>
[0047] Where freq and p1 represent the frequency and probability of the full term fterm in the method name being abbreviated to abb, respectively; p2 represents the probability of the abbreviation abb in the method name being expanded to fterm.
[0048] (2) Abbreviation of method names based on a dictionary of abbreviations
[0049] This method, based on the constructed abbreviation dictionary pAbbDict, abbreviates long method names mn according to the following steps: First, the method name mn is split into a series of tokens (characters). Then, the tokens are arranged and combined, and the resulting combinations (phrases) are sorted by length, with the longest phrase placed at the beginning of the phrases list. For each phrase tm in the phrase, this method attempts to find entries containing that phrase from the abbreviation dictionary pAbbDict.<tm,*,*,*,*> The full names of these terms must be exactly the same as the phrase tm. The retrieved terms are then sorted in reverse order based on their frequency value p1, ensuring that the term with the highest frequency value p1 is listed first. If the frequency value p1 of a term (the term with the highest frequency value p1) is greater than a predefined threshold, this method will use the term...<tm,abb,freq,p1,p2> The abbreviation "abb" in the method name is replaced with the full name "tm" in the method name "mn", thus achieving automatic abbreviation of the method name.
[0050] 3. Automatic abbreviation of method names based on machine learning
[0051] Context-based and probabilistic abbreviation-based automatic abbreviation methods both achieve high accuracy. However, they may miss many words or phrases that need abbreviation. Therefore, this method also employs a machine learning-based automatic abbreviation method for long method names to overcome the limitations of the previous two methods. Based on a large amount of real-world data (training data), existing machine learning techniques are capable of learning how to automatically abbreviate long method names. This method uses a simple and stable Bernoulli Bayes model to predict the probability that each full term in the method name needs abbreviation. If the machine learning model identifies multiple abbreviation opportunities, this method selects only the most reliable one (i.e., the term with the highest output probability from the model). If its probability p is greater than a predefined threshold β, this method abbreviations the method name mn′ based on the term selected by the learned model.
[0052] Key points of this invention:
[0053] This invention proposes an automatic abbreviation method for long method names based on abbreviations. The method first identifies other methods in the project that are highly similar to the current method, mines their already used abbreviations, and migrates them to the current method name, thus achieving abbreviation. Furthermore, the method automatically mines abbreviations from a large-scale open-source codebase, constructing the first large-scale probabilistic abbreviation dictionary with complete probabilities. Finally, machine learning techniques and the large-scale probabilistic abbreviation dictionary are used to further abbreviate long method names to meet length requirements.
[0054] The beneficial effects of this invention are:
[0055] The method of this invention was evaluated using method names from seven open-source applications. First, 100 method names containing abbreviations were randomly selected from each application. Then, experienced developers were invited to manually expand the abbreviations in the method names into complete words or phrases. This resulted in a dataset containing 700 long method names. For each long method name in the dataset, the method of this invention was used to abbreviate it, and the shortened name was compared with the original name used by the application developers. Of the 659 short method names recommended by this method, 613 were correctly abbreviated, meaning they were identical to the original names, significantly outperforming existing identifier abbreviation methods. The Transformer method, in comparison, showed significantly lower accuracy in automatic abbreviation of long method names compared to the proposed method. Transformer successfully abbreviated only 219 of the 700 long method names, with identifier-level precision and recall of 40% and 31%, respectively, far lower than the precision and recall of the method proposed in this invention.
[0056] Another embodiment of the present invention provides an automatic abbreviation system for long method names based on abbreviations, comprising:
[0057] The context-based shortening module is used to shorten long method names based on context.
[0058] The abbreviation module based on the probabilistic abbreviation dictionary is used to build a large-scale probabilistic abbreviation dictionary and to abbreviate long method names after context-based abbreviation based on the probabilistic abbreviation dictionary.
[0059] The machine learning-based abbreviation module is used to build an automatic abbreviation model for long method names. This model abbreviations long method names that have been abbreviated based on a probabilistic abbreviation dictionary.
[0060] For the specific implementation process of each module, please refer to the description of the method of the present invention above.
[0061] Another embodiment of the present invention provides a computer device (computer, server, smartphone, etc.) including a memory and a processor, the memory storing a computer program configured to be executed by the processor, the computer program including instructions for performing the steps of the method of the present invention.
[0062] Another embodiment of the present invention provides a computer-readable storage medium (such as ROM / RAM, disk, optical disk) storing a computer program that, when executed by a computer, implements the various steps of the method of the present invention.
[0063] The specific embodiments of the present invention disclosed above are intended to help understand the content of the present invention and to implement it accordingly. Those skilled in the art will understand that various substitutions, changes, and modifications are possible without departing from the spirit and scope of the present invention. The present invention should not be limited to the content disclosed in the embodiments of this specification; the scope of protection of the present invention is defined by the claims.
Claims
1. A method for automatically abbreviating long method names based on abbreviations, characterized in that, Includes the following steps: Shorten long method names based on context; Construct a probabilistic abbreviation dictionary, and use the probabilistic abbreviation dictionary to shorten long method names after context-based abbreviation; Construct a machine learning-based automatic abbreviation model for long method names, and use the machine learning-based automatic abbreviation model to abbreviate long method names after they have been abbreviated based on a probabilistic abbreviation dictionary; The context-based abbreviation of long method names includes: Find all abbreviations and their corresponding full names from the sibling methods of the current method mn, count the frequency of each "abbreviation-full name" pair, and then...<abb,fterm,freq> The abbreviations are recorded in the form of an abbreviation list abbList, where abb represents the abbreviation, fterm represents the full name of the abbreviation, and freq represents the frequency of the abbreviation. Sort the elements of abbList in descending order of frequency, and then try to use each triplet in abbList in turn.<abb,fterm,freq> To abbreviate 'mn', if 'mn' contains the full name 'fterm', then the abbreviation 'abb' is used to replace 'fterm'; the abbreviation of long method names based on context using a probabilistic abbreviation dictionary includes: The method name mn is split into a series of tokens, then the tokens are arranged and combined, and the resulting combinations are sorted by length, with the longest combination placed at the beginning of the list phrases. For each phrase tm in the phrase, find the entries containing that phrase from the probabilistic abbreviation dictionary.<tm,*,*,*,*> The full names of these terms must be completely consistent with the phrase tm, and the retrieved terms will be sorted in reverse order according to their frequency value p1, ensuring that the term with the highest frequency value p1 is placed first. For the term with the highest frequency value p1, if its frequency value p1 is greater than a predefined threshold, the term will be used.<tm,abb,freq,p1,p2> The abbreviation "abb" replaces the full name "tm" in the method name "mn", thus achieving automatic abbreviation of the method name.
2. The method according to claim 1, characterized in that, The context-based abbreviation of long method names refers to referencing how sibling methods that share the same context with the current method use abbreviations, and then transferring the usage of abbreviations in the sibling methods to the current method.
3. The method according to claim 1, characterized in that, The construction of the probabilistic abbreviation dictionary includes: This process involves collecting and statistically analyzing abbreviations and full terms appearing in open-source projects. An abbreviation expansion tool is used to expand the abbreviation "abbr" in the code to automatically obtain its recommended full term "fterm," thereby constructing a large-scale probabilistic abbreviation dictionary with complete probability values. Each entry in the probabilistic abbreviation dictionary is represented as a quintuple. <fterm,abb,freq,p1,p2> Where freq and p1 represent the frequency and probability of the full term fterm in the method name being abbreviated to abb, respectively; p2 represents the probability of the abbreviation abb in the method name being expanded to fterm.
4. The method according to claim 1, characterized in that, The machine learning-based automatic abbreviation model for long method names is a pre-trained Bernoulli Bayes model.
5. The method according to claim 4, characterized in that, The Bernoulli Bayes model is used to predict the probability that each full term in the method name needs to be abbreviated. The term with the highest probability output by the model is selected. If its probability is greater than a predefined threshold β, the method name is abbreviated according to the term selected by the Bernoulli Bayes model.
6. An automatic abbreviation system for long method names based on abbreviations, characterized in that, include: The context-based shortening module is used to shorten long method names based on context. The abbreviation module based on the probabilistic abbreviation dictionary is used to build a large-scale probabilistic abbreviation dictionary and to abbreviate long method names after context-based abbreviation based on the probabilistic abbreviation dictionary. The machine learning-based abbreviation module is used to build a machine learning-based automatic abbreviation model for long method names. This model abbreviations long method names that have been abbreviated based on a probabilistic abbreviation dictionary. The context-based abbreviation of long method names includes: Find all abbreviations and their corresponding full names from the sibling methods of the current method mn, count the frequency of each "abbreviation-full name" pair, and then...<abb,fterm,freq> The abbreviations are recorded in the form of an abbreviation list abbList, where abb represents the abbreviation, fterm represents the full name of the abbreviation, and freq represents the frequency of the abbreviation. Sort the elements of abbList in descending order of frequency, and then try to use each triplet in abbList in turn.<abb,fterm,freq> To abbreviate 'mn', if 'mn' contains the full name 'fterm', then the abbreviation 'abb' is used to replace 'fterm'; the abbreviation of long method names based on context using a probabilistic abbreviation dictionary includes: The method name mn is split into a series of tokens, then the tokens are arranged and combined, and the resulting combinations are sorted by length, with the longest combination placed at the beginning of the list phrases. For each phrase tm in the phrase, find the entries containing that phrase from the probabilistic abbreviation dictionary.<tm,*,*,*,*> The full names of these terms must be completely consistent with the phrase tm, and the retrieved terms will be sorted in reverse order according to their frequency value p1, ensuring that the term with the highest frequency value p1 is placed first. For the term with the highest frequency value p1, if its frequency value p1 is greater than a predefined threshold, the term will be used.<tm,abb,freq,p1,p2> The abbreviation "abb" replaces the full name "tm" in the method name "mn", thus achieving automatic abbreviation of the method name.
7. A computer device, characterized in that, It includes a memory and a processor, the memory storing a computer program configured to be executed by the processor, the computer program including instructions for performing the method of any one of claims 1 to 5.
8. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program, which, when executed by a computer, implements the method according to any one of claims 1 to 5.
Citation Information
Patent Citations
Source code-oriented abbreviation expansion necessity automatic judgment method
CN113419720A
Identifier normalization method based on deep learning
CN115358218A