A Python API real-time recommendation method based on context analysis
Through a context analysis method, the data flow sequence and co-occurrence rules of Python code are extracted and the random forest model is used for training, which solves the accuracy problem of Python real-time API recommendations and realizes efficient API recommendations in incomplete contexts.
Patent Information
- Application Number
- CN202010957757.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-09-11
- Publication Date
- 2025-08-15
- Estimated Expiration
- 2040-09-11
AI Technical Summary
Existing Python development tools cannot provide accurate API recommendations in real-time recommendation scenarios, especially because Python's type dynamics and path sensitivity, resulting in long and unrelated recommendation sequences, and lack of effective information for third-party libraries, making it difficult to effectively analyze in incomplete contexts.
Through a context analysis-based method, the data flow sequence, token similarity and co-occurrence rules of Python code are extracted as features, and a random forest model is used for training to generate an accurate API recommendation table, including type inference, candidate set generation and feature vector encoding.
In the recommended scenario within the project, the TOP-1 accuracy is achieved at 70.49% and the TOP-1 accuracy is achieved at 27.81% and the TOP-10 accuracy are 78.95% in the recommended scenario across the project, providing accurate API recommendations in a very short time.
Smart Images

Figure CN114254097B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of computer technology, particularly software technology. The present invention proposes a real-time Python API recommendation method based on context analysis. For locations in the form of "[Expression.]APIName," under real-time recommendation scenarios, i.e., only knowing the incomplete context before the current recommendation point, the present invention can provide an accurate Python API recommendation table in a very short time. "[Expression.]" is a restricted expression used to limit the called API methods, which can be a variable name, function name, etc., and ensures good accuracy and high efficiency both within a project and during cross-project development. Background Art
[0002] During software development, API recommendation can help developers quickly find the most suitable APIs for their current development scenario within a vast API library, significantly improving software development efficiency. By providing real-time recommendations for the most likely APIs to call based on the current development context, developers can significantly improve programming efficiency. While there is extensive research on API recommendation, most studies focus on static languages like Java. Currently, relatively little work has been conducted on API recommendation for dynamic languages like Python. This is due to the challenges presented by the dynamic nature of languages and the inherent characteristics of Python.
[0003] First, Python is type-dynamic. For the API recommendation task "[Expression.]APIName," the general recommendation strategy is to first infer the type of the call site [Expression.], determine the candidate set of APIs, and then make API recommendations. However, in Python, variable definitions do not explicitly declare their specific types (unless type annotations are used). As an interpreted, dynamic language, Python's specific type cannot be determined until the program is run. Furthermore, even if the type of a variable is known at a particular line, it can still change during execution. Second, Python is path-sensitive. Different execution paths may result in different data types, and the specific path taken is not known until execution. When the type of a variable cannot be determined, the only option is to output all methods of all possible types as the API recommendation sequence, resulting in a very lengthy recommendation sequence. Third, Python contains a large number of third-party libraries implemented in other languages (such as numpy), making source analysis difficult.
[0004] Popular Python development tools such as PyCharm and Jetbrains use python-skeletons to provide API definition files for third-party libraries, offering API recommendations for current code. Mypy, a static type checking tool for Python, uses python / typeshed to record information about Python libraries. Existing development tools often arrange their recommendations in alphabetical order, which doesn't provide programmers with the most relevant API recommendations. Furthermore, since many third-party Python libraries are implemented in other languages, the collection of Python library definition information is incomplete, making existing tools ineffective in providing developers with optimal recommendations.
[0005] Furthermore, real-time API recommendation is a challenging task. For real-time recommendation applications, the current project code context is often syntactically and semantically incomplete. Therefore, traditional program analysis, such as generating a parse tree (AST) from syntactic analysis, performing complete data flow analysis, control flow analysis, pointer analysis, and dynamic instrumentation analysis, cannot be performed. This poses significant challenges for both call point type inference and API recommendation (especially recommendation methods trained based on program analysis features).
[0006] At the same time, projects themselves are also unique. Real-time code recommendations are typically used to make recommendations during the development process. As a result, most projects lack historical code information and are unique, presenting challenges and difficulties for methods that rely on repetitive historical code changes or the use of machine learning / deep learning techniques.
[0007] To this end, the primary goal of this invention is to develop a technology that can provide accurate Python API recommendations in real-time scenarios. For locations like "[Expression.]APIName," accurate Python API recommendations can be provided in a very short time, even in real-time recommendation scenarios where only the incomplete context up to the current recommendation point is known. Summary of the Invention
[0008] The main work of the present invention is to propose a real-time Python API recommendation method based on context analysis. First, the present invention focuses on the problem of generating Python API recommendation candidate sets, and studies how to generate effective API candidate sets when the dynamic nature of Python types makes it impossible to obtain type information. Secondly, consider how to perform static data flow analysis on Python contexts with incomplete syntax / semantics, and extract valid data flow information limited to API recommendations as accurately as possible. Based on this, consider extracting three aspects of features from the current context, including data flow sequence, token similarity, and co-occurrence rules. Then, consider how to encode the extracted features to generate corresponding vectors. Finally, consider training the extracted features to provide an accurate and effective recommendation model. In response to the above problems, the contents of the present invention are as follows:
[0009] 1. Python API recommendation candidate set generation. Candidate set generation is the foundation of API recommendation. For the recommendation point "[Expression.]APIName," this paper first performs type inference on the object "[Expression]" that calls the API. Due to the dynamic nature of Python types, type inference often fails. If the object type can be successfully inferred, this paper collects all callable methods of that type as candidate APIs. Otherwise, this paper generates candidate sets from three aspects: standard library APIs, third-party library APIs, and methods defined in the existing context of the current project.
[0010] 2. Python static data flow analysis. The present invention found that constrained data flow sequence information can provide effective hints for API recommendations, and extracted data flow sequences containing only true positives from five aspects of constraints: assignment operations, loop structures, attribute access / calls, container access, and function parameter passing. Preliminary experimental results show that this data flow analysis method can achieve high precision (98.66%) and recall (98.48%). However, existing Python static analysis tools, such as Pysonar2, have a very low recall (43.88%) despite their high precision, and cannot be applied to actual data flow relationship extraction and API recommendation work.
[0011] 3. Feature extraction and vector generation based on context analysis. Data flow sequences containing APIs can provide hints for API recommendations. Therefore, based on the analysis method proposed in the previous step, the present invention extracts data flow sequences containing APIs from 100 popular Python projects in the open source code library Github, and uses the N-Gram model to train the extracted data flow sequences to learn the order of data flows containing the correct APIs. For a call point "[Expression.]APIName", the present invention first extracts all data flow sequences containing the current call point position in the existing context, and based on the candidate set generation method in step 1, for each candidate API in the candidate set, the API is substituted into the extracted data flow sequence to form a candidate data flow sequence, and the data flow sequence is sent as input to the pre-trained N-Gram model. The N-Gram model outputs the logarithmic probability score of the candidate data flow sequence, and uses the score as the first element of the feature vector. In addition, the similarity of each token in the data stream sequence can assist in API recommendation. For example, for the statement "logger = Logging.getlogger(...)", the function return value variable "logger" and the correct API "getlogger" share the same substring "logger". Therefore, the present invention uses the similarity score between each token in the data stream sequence and the candidate API as the second element of the feature vector. Finally, since the co-occurrence frequency of each token and the API in the corpus in the current context can also provide hints for recommendation, the present invention uses the co-occurrence frequency of the object "[Expression]" that calls the API and the candidate API in the corpus and the co-occurrence frequency of other tokens in the context and the API as the third and fourth elements of the feature vector, respectively.
[0012] 4. Recommendation model based on random forest. The present invention uses a random forest model to learn labeled feature vectors. First, the present invention collects the top-100 projects from the Github website according to the number of forks as a training set. For each recommendation point in the training set, type inference, candidate set generation, and corresponding feature vector generation based on each candidate API in the candidate set are performed in sequence. If the feature vector is generated by the correct API, the vector is marked as 1, otherwise it is marked as 0. Then, based on all generated use cases, the random forest model is used for learning, so that the model "learns" as many rules as possible for the corresponding feature vectors of the correct API. Based on the trained recommendation model, for a recommendation task, the present invention calculates the probability score of all candidate API corresponding feature vectors being marked as 1. Finally, the recommendation table is sorted according to the score and presented to the developer. Preliminary experimental results show that the present invention can achieve a TOP-1 accuracy of up to 70.49% in the intra-project recommendation scenario, and a TOP-1 accuracy of up to 27.81% and a TOP-10 accuracy of 78.95% in the cross-project recommendation scenario, demonstrating the effectiveness of the present invention. BRIEF DESCRIPTION OF THE DRAWINGS
[0013] Attachment Figure 1 Real-time recommendation flowchart for Python API based on context analysis.
[0014] This paper proposes a novel Python data flow analysis method that effectively analyzes incomplete context in real-time recommendation scenarios and provides effective Python API recommendations. The specific process is as follows: 1) Python API recommendation point identification. Call points of the form "[Expression.]APIName" are extracted from the Python code context. "[Expression.]" is a restricted expression used to limit the called API method, which can be a variable name, function name, etc. 2) Data extraction. For each call point, type inference is first performed on "[Expression]". If the inference is successful, the callable method with the inferred type is used as the candidate set. Otherwise, a candidate set is generated from the following three aspects: standard library APIs, third-party library APIs, and APIs defined in the current context. Next, context analysis is performed on each call point to collect three features: data flow sequence, token similarity, and co-occurrence rules. 3) Data encoding. The collected features are encoded into feature vectors. 4) Model construction. The feature vectors extracted from the training set are labeled. If the vector is generated based on the correct API, it is labeled as 1; otherwise, it is labeled as 0. All labeled vectors are then fed into a random forest model for training. 5) API Recommendation: Based on the trained random forest model, API recommendations can be made, and the recommended results are sorted by probability score and presented to developers. DETAILED DESCRIPTION
[0015] The present invention specifically comprises the following steps:
[0016] 1) First, we collected the top 100 Python projects with the most forks on Github and identified all API call points in the Python files within those projects that contained the string "[Expression.]APIName" as our dataset. We also collected files from another 100 popular Python projects to serve as the training set for the pre-trained N-Gram model in step 4).
[0017] 2) Perform type inference on the objects that call the API at the API recommendation points extracted in step 1). If the type inference is successful, all callable methods of the inferred type are used as candidate sets; otherwise, candidate sets are generated from the following three aspects: standard library API, third-party library API, and API defined in the current context.
[0018] 3) Perform data flow analysis on the code context before the API recommendation point extracted in step 1), and extract data flow sequences containing only true positives from five aspects of constraints: assignment operations, loop structures, property access / calls, container access, and function parameter passing.
[0019] 4) Based on the code context before the API recommendation point extracted in step 1) and the data flow sequence extracted in step 3), a feature vector is generated from three aspects: data flow sequence, token similarity, and co-occurrence rules. The co-occurrence rules are composed of two parts: the co-occurrence frequency of the object at the call point and the candidate API in the corpus, and the co-occurrence frequency of the current context token and the candidate API in the corpus.
[0020] 5) Based on the random forest algorithm model, a high-precision API recommendation model is trained and recommendations are made based on this model.
[0021] Step 1) Collect two datasets. The first dataset consists of the top 100 Python projects on Github, sorted by fork count. This dataset is used to train feature vectors related to API recommendations. For sample collection of API call points, each API call point and its context must meet the real-time recommendation scenario. This means that only the code context before the current call point is known, and the grammatical and semantic information of the context is incomplete. The second dataset consists of 100 popular Python projects randomly selected from Github. This dataset does not include any projects in the first dataset. This dataset is used to train the N-Gram model for data stream sequence order.
[0022] Step 2) Use Pytype, a Python type inference and checking tool, to infer the type of the object "[Expression]" calling the API at the API call point "[Expression.]APIName" collected in Step 1. Due to the dynamic nature of Python types, Pytype can only infer a small number of types. For each inferred type, callable methods of that type are collected as candidate APIs. Otherwise, based on the current context's import information, a candidate set is generated from three perspectives: standard library APIs, third-party library APIs, and APIs defined in the current context.
[0023] Step 3) Perform static data flow analysis on the incomplete Python code context. Because constrained data flow sequence information can provide effective insights for API recommendations, we extract data flow sequences containing only true positives based on five constraints: assignment operations, loop structures, attribute access / calls, container access, and function parameter passing. Note that data flow analysis here does not refer to absolutely accurate and comprehensive data flow extraction, but rather to extracting only five types of data flow sequences containing true positives that provide insights for API recommendations. The specific data flow constraints are as follows:
[0024]
[0025] Among them, 1∈L represents the location of each constraint, {u, v, x, y}∈VM represents any object or method, f∈F represents any function, U i , U j ∈AU represents a specific instance of any abstract constraint. The assignment operation constraint indicates that any method or object in the right operand expression of the assignment has a data flow relationship with the left operand object; the loop structure constraint indicates that any method or object in the iterator expression in the loop statement has a data flow relationship with the iterator; the property access / call constraint indicates that the object has a data flow relationship with the property it calls; the easy access constraint indicates that any object or method in the expression parameter of the access container has a data flow relationship with the container object; the function parameter transfer constraint indicates that any object or method in the parameter expression has a data flow relationship with the function; the constraint merging means that for the same object in multiple constraints, the set of all data flow relationships related to the object should include the data flow relationship related to each constraint; the constraint transfer follows the control flow sensitivity principle, and for all positions l1 before position l in the control flow graph, its data flow relationship set should be included in the data flow relationship set of l; the constraint preservation means that if the variable v at position l is not included in the above five constraints, it is considered that no data flow relationship related to v occurs at l, and the pseudo data flow generated at this time is deleted.
[0026] Step 4) First, the first and second elements of the feature vector are generated based on the data flow analysis method of step 3). Data flow sequences containing APIs can provide hints for API recommendations. Therefore, data flow sequences containing APIs are extracted from the 100 popular Python projects extracted in step 1), and the extracted data flow sequences are trained using the N-Gram model to learn the order of data flows containing the correct APIs. For a call point "[Expression.]APIName", all data flow sequences containing the current call point position in the existing context are extracted. Based on the candidate set generation method of step 2), for each candidate API in the candidate set, the API is substituted into the extracted data flow sequence to form a candidate data flow sequence, and the data flow sequence is sent as input to the pre-trained N-Gram model. The N-Gram model outputs the logarithmic probability score of the candidate data flow sequence, and the score is used as the first element of the feature vector. In addition, the similarity of each token in the data stream sequence can also provide hints for API recommendation. Therefore, considering the similarity score between each token in the data stream sequence and the candidate API, the following expression is used to calculate the longest common substring ratio between the token and the candidate API:
[0027]
[0028] Among them, x i Represents a token in the data stream sequence, d represents x i The distance from the API in the data stream sequence, lcs k Represents x i The longest common substring with api.
[0029] Then for a data stream sequence DFS=(x0,...,x n ), the total similarity between DFS and the candidate API can be calculated based on the above formula as the second element of the feature vector:
[0030]
[0031] In addition, step 4) considers co-occurrence rules as features, and uses the frequency score of the calling object and the candidate API at the same calling point in the corpus file as the third element. Among them, for the calling object, if its type inference process in step 2) is successful and its result is a standard built-in type, its type will replace the original object identifier, and the co-occurrence frequency of this type and the candidate API in the corpus will be calculated. For example, <list>.append(), <str>.split(), <file>Calls such as .open() appear frequently in the corpus and can provide hints for recommending related APIs. Therefore, the following expression is used to calculate the third feature vector element:
[0032]
[0033] Where x is the object that calls the API, N(x) counts the number of times x appears in the corpus, and N(api, x) counts the number of times the candidate API and x appear at the same call point in the corpus.
[0034] At the same time, step 4) uses the frequency of co-occurrence of the current context token with the candidate API in the same document in the corpus as the fourth element. For example, for the call point "with open(...) as f: f.APIName", the correct API answer here, read(), often co-occurs with its surrounding token set {with, open, as, f} in the corpus. For each token t, the co-occurrence frequency with the candidate API is calculated using the following expression:
[0035]
[0036] Among them, N(t) represents the number of documents in which token t appears in the corpus, and N(api, t) represents the number of times token t and the candidate API appear in the same document.
[0037] Based on the above formula, for the context token set S before the call point = (t0, ..., t n ), use the following expression to calculate the co-occurrence frequency of S and API as the fourth element of the feature vector:
[0038]
[0039] Among them, dist(t i ,api) calculate token t i The distance from the API in context.
[0040] Step 5) Label the feature vectors generated in step 4). If a candidate API in the vector is the correct API, the vector is marked as 1; otherwise, it is marked as 0. Next, all labeled feature vectors are used as a training set and trained using a random forest model, aiming to "learn" as many patterns marked as 1 as possible. Based on the trained recommendation model, the API recommendation task can be performed. For each API recommendation task, the feature vectors corresponding to all candidate APIs are input into the trained random forest model. The model is ranked based on the likelihood that each feature vector will be marked as 1, and a ranked API recommendation table is output to the developer.< / file> < / str> < / list>
Claims
1. A Python API real-time recommendation method based on context analysis, including the following four steps: 1) Identify context call points and generate candidate sets for API locations that need to be recommended; 2) Based on heuristic methods, static data flow analysis is performed on the syntactically / semantically incomplete Python context to extract valid data flow information limited to API recommendations; 3) Perform effective static analysis on the current context to extract three features: data stream sequence, token similarity, and co-occurrence rules, and generate corresponding feature vectors; 4) Based on the random forest algorithm, the generated vectors are learned and a list of recommended APIs is generated. Each generated feature vector is labeled. If the vector is generated by the correct API, it is marked as a positive sample; otherwise, it is marked as a negative sample. The labeled vectors are trained using a random forest model to learn the patterns of positive sample vectors. Recommendations are made based on the trained recommendation model, and each test case vector is ranked according to its probability of being marked as a positive sample. The sorted API list is recommended to developers.
2. A Python API real-time recommendation method based on context analysis according to claim 1, characterized in that In step 1), the context call point is identified, and a candidate set is generated for the API location that needs to be recommended. The call point is the location of "[Expression.]APIName", and "[Expression.]'" is a restricted expression used to limit the called API method; type inference is performed on the object calling the API at the call point. If the type inference is successful, the callable methods of the inferred type are collected as the candidate set. Otherwise, the candidate set is generated from three aspects: standard library API, third-party library API, and custom method based on the import information.
3. A Python API real-time recommendation method based on context analysis according to claim 1, characterized in that In step 2), dynamic language static data flow analysis is performed for the case where the context code syntax and semantics are incomplete in the real-time recommendation scenario; Data flow information containing only true positive data is extracted from five aspects: assignment operations, loop structures, property access / calls, container access, and function parameter passing.
4. The Python API real-time recommendation method based on context analysis according to claim 1 is characterized by In step 3), three features are extracted from the current incomplete context: data stream sequence, token similarity, and co-occurrence rules, and the corresponding feature vector is generated; Based on the candidate set generation method in step 1), for each candidate API in the candidate set, the API is substituted into the extracted data stream sequence to form a candidate data stream sequence. This is sent as input to the pre-trained N-Gram model. The N-Gram model outputs the log-probability score of the candidate data stream sequence and uses this score as the first element of the feature vector. The similarity score between each token in the data stream sequence extracted in step 2) and the candidate API is used as the second element of the feature vector. The co-occurrence frequency between the object "[Expression]" that calls the API and the candidate API in the corpus and the co-occurrence frequency between the token and the API in the context are taken as the third and fourth elements of the feature vector respectively.
Citation Information
Patent Citations
API recommendation method based on hierarchical context
CN110297657A
Python code reference information generation method based on program analysis and text analysis
CN110750297A