WO2019028990A1 - 代码元素的命名方法、装置、电子设备及介质 - Google Patents

代码元素的命名方法、装置、电子设备及介质 Download PDF

Info

Publication number
WO2019028990A1
WO2019028990A1 PCT/CN2017/104537 CN2017104537W WO2019028990A1 WO 2019028990 A1 WO2019028990 A1 WO 2019028990A1 CN 2017104537 W CN2017104537 W CN 2017104537W WO 2019028990 A1 WO2019028990 A1 WO 2019028990A1
Authority
WO
WIPO (PCT)
Prior art keywords
code
naming
user
usage information
code element
Prior art date
Application number
PCT/CN2017/104537
Other languages
English (en)
French (fr)
Inventor
黄伟淦
Original Assignee
上海壹账通金融科技有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 上海壹账通金融科技有限公司 filed Critical 上海壹账通金融科技有限公司
Publication of WO2019028990A1 publication Critical patent/WO2019028990A1/zh

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/90Details of database functions independent of the retrieved data types
    • G06F16/903Querying
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F40/00Handling natural language data
    • G06F40/20Natural language analysis
    • G06F40/279Recognition of textual entities
    • G06F40/284Lexical analysis, e.g. tokenisation or collocates
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code

Definitions

  • the present application belongs to the field of software development technologies, and in particular, to a method, a device, an electronic device and a medium for naming code elements.
  • the embodiment of the present application provides a method, a device, an electronic device, and a medium for naming code elements, so as to solve the problem that the naming efficiency of code elements in the prior art is relatively low.
  • a first aspect of an embodiment of the present application provides a method for naming a code element, including:
  • the determined naming of the first code element is recommended to the user to cause the user to select a naming associated with the second code element from each of the recommended naming.
  • a second aspect of the embodiments of the present application provides a naming device for a code element, including:
  • An extraction module configured to extract a naming and an annotation of each first code element from a preset code library, where the preset code library includes a plurality of pieces of code, wherein the first code element includes a variable, a constant, a function, and a class And documents;
  • a first obtaining module configured to acquire element usage information input by a user, where the element usage information is used to describe a function of a second code element that the user needs to create;
  • a calculation module configured to separately calculate a similarity between each of the annotations and the element usage information, obtain the annotation whose similarity with the element usage information is greater than a preset threshold, and determine a location corresponding to the annotation The first code element;
  • a recommendation module configured to recommend the determined naming of the first code element to the user, so that the user selects a naming associated with the second code element from each recommended naming.
  • a third aspect of embodiments of the present application provides an electronic device including a memory, a processor, and computer readable instructions stored on the memory and executable on the processor, the processor executing the The computer sequence implements the steps of the naming method of the code elements as provided by the first aspect above.
  • a fourth aspect of the embodiments of the present application provides a computer readable storage medium storing computer readable instructions, the computer readable instructions being implemented by at least one processor, as described above The steps of the naming method of the code elements provided on the one hand.
  • the user can enable the user to Based on the similarly-coded code elements, the recommended naming with higher reference value is obtained, ensuring that the user can quickly determine a naming that best suits his or her own needs from the recommended naming, thereby creating a new code element based on the naming.
  • each recommended naming is the naming used by other developers to ensure that the program is highly readable, so users do not have to spend too much time
  • the embodiment of the present application improves the naming efficiency of code elements.
  • FIG. 1 is a flowchart of an implementation of a method for naming code elements provided by an embodiment of the present application
  • FIG. 2 is a specific implementation flowchart of a naming method S103 of a code element provided by an embodiment of the present application;
  • FIG. 3 is a specific implementation flowchart of a naming method S1031 of a code element provided by an embodiment of the present application;
  • FIG. 5 is a flowchart of an implementation of a method for naming code elements according to another embodiment of the present disclosure
  • FIG. 6 is a structural block diagram of a naming device for a code element according to an embodiment of the present application.
  • FIG. 7 is a structural block diagram of a naming device for a code element according to another embodiment of the present application.
  • FIG. 8 is a structural block diagram of a device for naming code elements according to another embodiment of the present application.
  • FIG. 9 is a schematic diagram of an electronic device according to an embodiment of the present application.
  • FIG. 1 shows an implementation flow of a naming method of a code element provided by an embodiment of the present application, where the method flow includes steps S101 to S104.
  • the specific implementation principles of each step are as follows:
  • S101 Extract a naming and a comment of each first code element from a preset code library, where the preset code library includes a plurality of pieces of code, wherein the first code element includes a variable, a constant, a function, a class, and a file.
  • the code represents the computer language instructions written by the developer in a language supported by the development tool.
  • the collection of multi-segment code is the code base.
  • the code base contains code for implementing different functions.
  • the code collection process may be, for example, storing the code uploaded by the program developer to the code base; downloading the code pre-stored in the plurality of third-party code sharing platforms, and storing the code in the code base.
  • the above third-party code sharing platform may be, for example, bitbucket and gitHub.
  • each piece of code in the code base there are various types of code elements, including but not limited to variables, constants, functions, classes, and files.
  • code elements including but not limited to variables, constants, functions, classes, and files.
  • the program developer in order to improve the readability of the program, the program developer will Each piece of code is annotated, and comments are often used to describe what the code can do or create intent.
  • the comments for each code element are also stored in the code base.
  • the static analysis tool can perform static analysis on each piece of code in the code library to obtain the naming and annotation of each code element in the code library. For example, parsing techniques, such as clang or llvm, convert each piece of code into a syntax tree, automatically sorting out the types, naming, and comments at the end of the line of code that exist in the code base.
  • parsing techniques such as clang or llvm
  • S102 Acquire element usage information input by the user, where the element usage information is used to describe a function of a second code element that the user needs to create.
  • the user wants to create a new code element in the application they need to develop, the user is asked to enter a usage description associated with the code element. For example, if a program developer wants to name a variable and the variable is mainly used to implement the function of cumulative counting, the obtained element usage information of the user input may be a "counter".
  • the element usage information input by the user includes, but is not limited to, different kinds of language characters such as Chinese or English.
  • S103 Calculate the similarity between each of the annotations and the element usage information, obtain the annotation whose similarity with the element usage information is greater than a preset threshold, and determine the first corresponding to the annotation. Code element.
  • the element usage information input by the user is compared with the annotation of each code element in the code base to determine the similarity between the function of the existing code element in the code base and the function of the code element required by the user. .
  • the annotation of each code element in the code library is sequentially acquired, and the similarity between the annotation and the element usage information input by the user is calculated, that is, the text similarity between the annotation and the element usage information is calculated.
  • the method of calculating text similarity includes but is not limited to cosine distance, Euclidean distance, Jaked distance, and probability distribution distance (K-L distance).
  • each annotation whose similarity is greater than a preset threshold is filtered out. If the similarity between the annotation of any code element and the element usage information input by the user is greater than a preset threshold, it indicates that the code element is closer to the function of the code element that the user needs to establish. Therefore, in the code base, the code element corresponding to the filtered comment is obtained.
  • the foregoing S103 specifically includes:
  • S1031 Generate, according to the annotation of the first code element and the element usage information input by the user, a first vector corresponding to the first code element and a second vector corresponding to the second code element.
  • S1032 Calculate a cosine similarity between the first vector and the second vector.
  • the annotation and the element usage information input by the user are respectively converted and processed according to Word2Vec, Doc2Vec, and the vector space model VSM, to obtain the first vector corresponding to the annotation and The second vector corresponding to the element usage information.
  • Word2Vec Word2Vec
  • Doc2Vec the vector space model VSM
  • the text similarity is specifically a cosine similarity. Since the annotation of the code element in the code base has been converted into the first vector, and the element usage information input by the user has been converted into the second vector, the similarity between the annotation of the calculation code element and the element usage information input by the user is calculated as the first vector and The cosine similarity of the second vector.
  • x i represents the ith element value of the first vector
  • y i represents the ith element value of the second vector
  • n represents the total number of elements of the first vector or the second vector.
  • S1033 Determine the first vector that has a cosine similarity with the second vector that is greater than a preset threshold, and obtain the determined first code element corresponding to the first vector.
  • each first vector in which the cosine similarity is greater than a preset threshold is selected.
  • the code elements corresponding to the respective first vectors obtained by the screening are determined.
  • the annotation and the element usage information expressed in text form are realized.
  • the dimensionality reduction processing because the cosine similarity can better reflect the similarity between the texts, and the calculation process is relatively simple, therefore, the calculation efficiency of the similarity between the annotation and the element usage information is improved.
  • S10311 Perform word segmentation processing on the element use information and the annotation to obtain a plurality of first word segments corresponding to the annotations and a plurality of second word segments corresponding to the element usage information.
  • the stop word in the annotation is removed, and each character remaining in the annotation is subjected to word segmentation processing to obtain a plurality of word segments corresponding to the annotation.
  • the stop words in the use information of the element are removed, and each of the remaining characters in the element use information is subjected to word segmentation processing to obtain a plurality of word segments corresponding to the element use information.
  • S10313 Synchronize the word frequency-reverse file frequency TF-IDF information of each participle in the commentary in the word bag model to generate a first vector corresponding to the comment according to the TF-IDF information.
  • S10314 separately calculate word frequency-reverse file frequency TF-IDF information of each participle in the element usage information in the word bag model, to generate a second vector corresponding to the element usage information according to the TF-IDF information.
  • the preset weight value corresponding to the word part is obtained, the frequency of the part word in the comment corresponding to the word bag model is determined, and the frequency of occurrence of the part word in the element use information is determined. .
  • the above frequency is the word frequency (TF)
  • the preset weight value is the inverse file frequency (IDF, Inverse Document Frequency).
  • the product of the word frequency corresponding to the participle in the comment and the frequency of the reverse file is used as the TF-IDF information of the participle in the comment, and the TF-IDF information is output as one element in the first vector.
  • the product of the word frequency corresponding to the word segmentation in the element use information and the reverse file frequency is used as the TF-IDF information of the word segment in the element use information, and the TF-IDF information is output as one element in the second vector. Therefore, when the word bag model includes N word segments, N elements will be included in the first vector and the second vector corresponding to the word bag model.
  • the implementation process of the foregoing S10311 to S10314 is as follows: for a comment corresponding to a certain code element in the code library, if the comment is “function running time: calculating the average value of the duration”, After stopping the word and performing word segmentation processing, the obtained first participle is “function/run/time/calculation/time/average”; if the element usage information input by the user is “run time”, it is removed After the word is stopped and the word segmentation process is performed, the obtained second participle is “run/cost/time”; the analysis of the above S10312 can be obtained, and the current time is included in the bag model corresponding to the comment and the element usage information, There are seven different word segments of "function/run/time/calculation/average/cost/time”, if the TF-IDF information of each participle in the comment is "1, 1, 2, 1, respectively” 1, 0, 0”, the TF-IDF information in the element usage information is "0, 1, 0, 0, 1, 1, 0", respectively
  • the higher the frequency of the word segmentation in the text and the higher the frequency of the reverse file the greater the importance of the word segment in the text, and the more the main content of the annotation or element usage information can be reflected.
  • the model is used to obtain the vector corresponding to each annotation and the element usage information, and each element in the vector is represented by the TF-IDF information of each participle in the word bag model, so that the cosine similarity calculated based on the vector can be
  • the main content of the text is closely related, thus accurately reflecting the similarity of the annotations and the information on the use of the elements.
  • S104 recommend the determined naming of the first code element to the user, so that the user pushes each In the recommendation naming, the naming associated with the second code element is selected.
  • the similarity between the annotation of the first code element determined from the code base and the element usage information of the second code element created by the user is higher, the functions, attributes, and usage of the first code element and the second code element The probability that the methods are the same is relatively high, so the naming of the first code element is also more suitable for the second code element. Therefore, the naming of the determined first code element is recommended to the user.
  • the number of the determined first code elements is also plural.
  • the naming of each of the first code elements is displayed to recommend each naming to the user, so that the user can select the naming of the second code element that he/she needs to create from the multiple namings displayed at the current time. Or, after selecting one of the recommended namings, add extra characters (such as serial numbers) to the naming to determine the final naming to be the naming of the second code element.
  • the user can enable the user to Based on the similarly-coded code elements, the recommended naming with higher reference value is obtained, ensuring that the user can quickly determine a naming that best suits his or her own needs from the recommended naming, thereby creating a new code element based on the naming.
  • each recommended naming is the naming used by other developers to ensure that the program is highly readable, so users do not have to spend too much time
  • the embodiment of the present application improves the naming efficiency of code elements.
  • the word segmentation result corresponding to the element usage information input by the user if the word segmentation includes a Chinese word segmentation or other non-English word segmentation, the Chinese word segmentation or other non-English word segmentation is converted into an English word one by one.
  • S107 Acquire a naming algorithm that matches the coding language according to a programming language used by the user at the current time.
  • the programming language required by the user at the current moment is obtained. For example, detecting other code in the application currently being developed by the user, after analyzing the syntax structure of each code, determining the programming language used by the user at the current time; acquiring the instruction according to the programming language parameter input by the user Users are developing applications
  • the programming language required for the sequence include but are not limited to C language, C++, Python, LinuxC, and Java.
  • S108 Process each of the English words according to the naming algorithm, and recommend the obtained character string to the user.
  • each English word is processed to obtain a character string including each of the above English words.
  • the naming algorithm may be processed by connecting each English word with a preset connector to obtain a character string including each of the above English words.
  • the connector includes but is not limited to an underline, an empty connector, a dash, and the like. After that, the output string is named as a recommendation and displayed.
  • the element usage information input by the user is “acquire student achievement”, the result obtained after the word segmentation is “acquisition/student/score”, and the English words corresponding to each participle are get, student and score respectively.
  • the current programming language of the user is Java, and the corresponding naming algorithm is the hump naming algorithm, the three characters of get, student, and score are processed, and the obtained string is getStudentScore; if the current time user's programming language For Linux C, and its corresponding naming algorithm is an underline combination algorithm, after the three words of get, student and score are connected, the obtained string is get_student_score.
  • the annotations of the respective code elements may not have a high degree of similarity with the element usage information input by the user, and therefore,
  • the English words obtained after the translation are processed, and the string containing the English words is named as a recommendation, so that the user can directly use the string to name the code elements that need to be created, and improve the naming of the code elements.
  • Efficiency because English words have actual semantics, they can be more easily read by others. Therefore, by recommending a string with higher readability to the user and using the string as the naming of the code element, the user needs to be further developed.
  • the code readability of the app since the number of the first code elements stored in the code library is limited, the annotations of the respective code elements may not have a high degree of similarity with the element usage information input by the user, and therefore,
  • the English words obtained after the translation are processed, and the string containing the English words is named as a recommendation, so that the user can directly use the string to name the code elements that need to be created, and improve the naming of the
  • FIG. 5 is a flowchart showing an implementation process of a naming method for a code element according to another embodiment of the present application. As shown in FIG. 5, after the foregoing S104, the method further includes:
  • the recommended naming selected by the user is detected.
  • the number of cumulative selections of the recommended name is increased by one, and the cumulative number of selections indicates the number of times the recommended naming is selected.
  • Each naming recommended to the user matches the element usage information entered by the user. Bind the element usage information to each After the recommended naming, the element usage information, the recommended naming, and the binding relationship between the two are stored in a pre-established information base.
  • each recommended naming bound to the element usage information input by the user at the current moment is directly read from the information base, and the read recommendation naming is displayed to the user.
  • each recommendation naming in the information library Since the cumulative number of selections of each recommendation naming in the information library is different, when the recommended naming is displayed to the user, the naming with a higher cumulative number of times is preferentially displayed. That is, each of the read recommendation names is sequentially displayed in the order of the cumulative number of selections.
  • FIG. 6 is a structural block diagram of the naming device of the code element provided by the embodiment of the present application. For the convenience of description, only the embodiment related to the embodiment of the present application is shown. section.
  • the apparatus includes:
  • the extracting module 601 is configured to extract a naming and an annotation of each first code element from a preset code library, where the preset code library includes a plurality of pieces of code, wherein the first code element includes a variable, a constant, a function, Classes and files.
  • the first obtaining module 602 is configured to acquire element usage information input by the user, where the element usage information is used to describe a function of the second code element that the user needs to create.
  • a calculation module 603 configured to separately calculate a similarity between each of the annotations and the element usage information, obtain the annotation whose similarity with the element usage information is greater than a preset threshold, and determine a corresponding to the annotation The first code element Prime.
  • the recommendation module 604 is configured to recommend the determined naming of the first code element to the user, so that the user selects a naming associated with the second code element from each recommended naming.
  • the calculating module 603 includes:
  • Generating a submodule configured to generate, according to the annotation of the first code element and the element usage information of the user input, a first vector corresponding to the first code element and a second corresponding to the second code element vector.
  • a calculation submodule configured to calculate a cosine similarity between the first vector and the second vector.
  • Obtaining a submodule configured to determine the first vector that has a cosine similarity with the second vector that is greater than a preset threshold, and obtain the first code element corresponding to the determined first vector.
  • the generating submodule is specifically configured to:
  • the word frequency-reverse file frequency TF-IDF information of each participle in the word bag model in the element use information is separately calculated to generate a second vector corresponding to the element use information according to the TF-IDF information.
  • the naming device of the code element further includes:
  • a word segmentation module 605 configured to perform word segmentation processing on the element use information if the annotation with the similarity of the element usage information is greater than a preset threshold in the code library, to obtain a plurality of the words Second participle.
  • the conversion module 606 is configured to convert each of the second word segments into English words, respectively.
  • the second obtaining module 607 is configured to obtain a naming algorithm that matches the encoding language according to a programming language used by the user at the current time.
  • the processing module 608 is configured to process each of the English words based on the naming algorithm, and recommend the processed character string to the user.
  • the naming device of the code element further includes:
  • the storage module 609 is configured to bind the element usage information to each recommended naming.
  • the statistic module 610 is configured to increase the number of cumulative selections of the recommended naming selected by the user at the current time by one, so that when the element usage information input by the user is received again, according to the order of the corresponding cumulative selection times, Each recommended naming of the element usage information binding is recommended to the user in turn.
  • the user can enable the user to Based on the similarly-coded code elements, the recommended naming with higher reference value is obtained, ensuring that the user can quickly determine a naming that best suits his or her own needs from the recommended naming, thereby creating a new code element based on the naming.
  • each recommended naming is the naming used by other developers to ensure that the program is highly readable, so users do not have to spend too much time
  • the embodiment of the present application improves the naming efficiency of code elements.
  • FIG. 9 is a schematic diagram of an electronic device according to an embodiment of the present application.
  • the electronic device 9 of this embodiment includes a processor 90, a memory 91, and computer readable instructions 92 stored in the memory 91 and executable on the processor 90, such as code elements. Name the program.
  • the processor 90 executes the computer readable instructions 92 to implement the steps in the naming method embodiments of the various code elements described above, such as steps 101 through 104 shown in FIG.
  • the processor 90 when executing the computer readable instructions 92, implements the functions of the various modules/units in the various apparatus embodiments described above, such as the functions of the modules 601 through 604 shown in FIG.
  • the computer readable instructions 92 may be partitioned into one or more modules/units that are stored in the memory 91 and executed by the processor 90, To complete this application.
  • the one or more modules/units may be a series of computer readable instruction instruction segments capable of performing a particular function, the instruction segments being used to describe the execution of the computer readable instructions 92 in the electronic device 9.
  • the electronic device 9 can be a computing device such as a desktop computer, a notebook, a palmtop computer, and a cloud server.
  • the electronic device may include, but is not limited to, a processor 90, a memory 91. It will be understood by those skilled in the art that FIG. 9 is merely an example of the electronic device 9, and does not constitute a limitation on the electronic device 9, and may include more or less components than those illustrated, or combine some components, or different components.
  • the electronic device may further include an input and output device, a network access device, a bus, and the like.
  • the processor 90 may be a central processing unit (CPU), or may be other general-purpose processors, a digital signal processor (DSP), an application specific integrated circuit (ASIC), Field-Programmable Gate Array (FPGA) or other programmable logic device, discrete gate or transistor logic device, discrete hardware components, etc.
  • the general purpose processor may be a microprocessor or the processor or any conventional processor or the like.
  • the memory 91 may be an internal storage unit of the electronic device 9, such as a hard disk or memory of the electronic device 9.
  • the memory 91 may also be an external storage device of the electronic device 9, such as a plug-in hard disk equipped on the electronic device 9, a smart memory card (SMC), and a secure digital (SD). Card, flash Flash card, etc. Further, the memory 91 may also include both an internal storage unit of the electronic device 9 and an external storage device.
  • the memory 91 is configured to store the computer readable instructions and other programs and data required by the electronic device.
  • the memory 91 can also be used to temporarily store data that has been output or is about to be output.
  • each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
  • the above integrated unit can be implemented in the form of hardware or in the form of a software functional unit.
  • the integrated unit if implemented in the form of a software functional unit and sold or used as a standalone product, may be stored in a computer readable storage medium.
  • a computer readable storage medium A number of instructions are included to cause a computer device (which may be a personal computer, server, or network device, etc.) to perform all or part of the steps of the methods described in various embodiments of the present application.
  • the foregoing storage medium includes: a U disk, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk, and the like. .

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • General Engineering & Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Computational Linguistics (AREA)
  • Software Systems (AREA)
  • Databases & Information Systems (AREA)
  • Health & Medical Sciences (AREA)
  • Artificial Intelligence (AREA)
  • Audiology, Speech & Language Pathology (AREA)
  • General Health & Medical Sciences (AREA)
  • Data Mining & Analysis (AREA)
  • Machine Translation (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

本方案提供了一种代码元素的命名方法、装置、电子设备及介质,适用于软件开发技术领域,该方法包括:从预设代码库中提取各个第一代码元素的命名及注释,所述预设的代码库中包含多段代码;获取用户输入的元素用途信息;分别计算每一注释与元素用途信息的相似度,获取与元素用途信息的相似度大于预设阈值的注释,并确定出与该注释对应的所述第一代码元素;将确定出的所述第一代码元素的命名推荐至所述用户,以使所述用户从各个推荐命名中,选取出与所述第二代码元素相关的命名。本方案使得用户能够获取到参考价值较高的推荐命名,保证用户能够快速地从推荐命名中确定出最符合自身所需的一个命名,提高了代码元素的命名效率。

Description

代码元素的命名方法、装置、电子设备及介质
本申请要求于2017年08月09日提交中国专利局、申请号为201710674688.5、发明名称为“代码元素的命名方法及终端设备”的中国专利申请的优先权,其全部内容通过引用结合在本申请中。
技术领域
本申请属于软件开发技术领域,尤其涉及一种代码元素的命名方法、装置、电子设备及介质。
背景技术
在软件程序开发过程中,若研发人员想要在程序中创建变量或函数等代码元素,则需要在实际创建这些代码元素之前,预先为其进行命名。
目前,大部分的用户在为代码元素命名时,通常都只能经过一番深思熟虑后,才能输入字符,以对即将要创建的代码元素进行命名,因而现有的代码元素命名方法无法实现代码元素的自动命名。由于好的代码元素命名能够易于他人读懂程序,增强代码可读性,因此,若要为代码元素进行手工命名,则研发人员往往需要耗费较多的时间去思考代码元素的命名,由此使得代码元素的命名效率较为低下。
技术问题
有鉴于此,本申请实施例提供了一种代码元素的命名方法、装置、电子设备及介质,以解决现有技术中代码元素的命名效率较为低下的问题。
技术解决方案
本申请实施例的第一方面,提供了一种代码元素的命名方法,包括:
从预设代码库中提取各个第一代码元素的命名及注释,所述预设的代码库中包含多段代码,其中,所述第一代码元素包括变量、常量、函数、类以及文件;
获取用户输入的元素用途信息,所述元素用途信息用于描述用户所需创建的第二代码元素的功能;
分别计算每一所述注释与所述元素用途信息的相似度,获取与所述元素用途信息的相似度大于预设阈值的所述注释,并确定出与该注释对应的所述第一代码元素;
将确定出的所述第一代码元素的命名推荐至所述用户,以使所述用户从各个推荐命名中,选取出与所述第二代码元素相关的命名。
本申请实施例的第二方面,提供了一种代码元素的命名装置,包括:
提取模块,用于从预设代码库中提取各个第一代码元素的命名及注释,所述预设的代码库中包含多段代码,其中,所述第一代码元素包括变量、常量、函数、类以及文件;
第一获取模块,用于获取用户输入的元素用途信息,所述元素用途信息用于描述用户所需创建的第二代码元素的功能;
计算模块,用于分别计算每一所述注释与所述元素用途信息的相似度,获取与所述元素用途信息的相似度大于预设阈值的所述注释,并确定出与该注释对应的所述第一代码元素;
推荐模块,用于将确定出的所述第一代码元素的命名推荐至所述用户,以使所述用户从各个推荐命名中,选取出与所述第二代码元素相关的命名。
本申请实施例的第三方面,提供了一种电子设备,包括存储器、处理器及存储在所述存储器上并可在所述处理器上运行的计算机可读指令,所述处理器执行所述计算机序时实现如上述第一方面所提供的代码元素的命名方法的步骤。
本申请实施例的第四方面,提供了一种计算机可读存储介质,所述计算机可读存储介质存储有计算机可读指令,所述计算机可读指令被至少一个处理器执行时实现如上述第一方面所提供的代码元素的命名方法的步骤。
有益效果
本申请实施例中,通过计算用户所需创建的代码元素的元素用途信息与收集得到的各个代码元素的注释的相似度,并将相似度较高的代码元素的命名推荐至用户,使得用户能够基于功能相似的代码元素,获取到参考价值较高的推荐命名,保证用户能够快速地从推荐命名中确定出最符合自身所需的一个命名,从而基于该命名来创建新的代码元素。由于推荐命名是代码库中已存在的代码元素的命名,因而各个推荐命名都是其他开发人员在确保程序具有较高可读性的情况下所使用的命名,因此,用户无须耗费过多的时间思考如何提供程序可读性较高的代码元素命名,故本申请实施例提高了代码元素的命名效率。
附图说明
为了更清楚地说明本申请实施例中的技术方案,下面将对实施例或现有技术描述中所需 要使用的附图作简单地介绍,显而易见地,下面描述中的附图仅仅是本申请的一些实施例,对于本领域普通技术人员来讲,在不付出创造性劳动性的前提下,还可以根据这些附图获得其他的附图。
图1是本申请实施例提供的代码元素的命名方法的实现流程图;
图2是本申请实施例提供的代码元素的命名方法S103的具体实现流程图;
图3是本申请实施例提供的代码元素的命名方法S1031的具体实现流程图;
图4是本申请另一实施例提供的代码元素的命名方法的实现流程图;
图5是本申请又一实施例提供的代码元素的命名方法的实现流程图;
图6是本申请实施例提供的代码元素的命名装置的结构框图;
图7是本申请另一实施例提供的代码元素的命名装置的结构框图;
图8是本申请又一实施例提供的代码元素的命名装置的结构框图;
图9是本申请实施例提供的电子设备的示意图。
本发明的实施方式
以下描述中,为了说明而不是为了限定,提出了诸如特定系统结构、技术之类的具体细节,以便透彻理解本申请实施例。然而,本领域的技术人员应当清楚,在没有这些具体细节的其它实施例中也可以实现本申请。在其它情况中,省略对众所周知的系统、装置、电路以及方法的详细说明,以免不必要的细节妨碍本申请的描述。
为了说明本申请所述的技术方案,下面通过具体实施例来进行说明。
图1示出了本申请实施例提供的代码元素的命名方法的实现流程,该方法流程包括步骤S101至S104。各步骤的具体实现原理如下:
S101:从预设代码库中提取各个第一代码元素的命名及注释,所述预设的代码库中包含多段代码,其中,所述第一代码元素包括变量、常量、函数、类以及文件。
代码表示程序开发人员用开发工具所支持的语言所写出来的计算机语言指令。多段代码的集合即为代码库。
代码库中包含有用于实现不同功能的代码。其中,代码的收集过程例如可以是:将程序开发人员自行上传的代码存储至代码库;下载预先存储于多个第三方代码共享平台中的代码,将这些代码存储至代码库。上述第三方代码共享平台例如可以是bitbucket以及gitHub等。
代码库的每一段代码中,存在有各种类型的代码元素,代码元素包括但不限于变量、常量、函数、类以及文件等。在编程过程中,为了提高程序的易读性,程序开发人员都会对 各段代码进行注释,注释通常用于描述代码所能实现的功能或者创建意图。在收集上述代码时,各代码元素的注释也会一并存储至代码库。
本申请实施例中,通过现有的静态分析工具,可对代码库中的各段代码进行静态分析,以获取代码库中各代码元素的命名以及注释。例如,可通过语法分析技术,如clang或llvm等方式,将各段代码转换为语法树后,自动整理出存在于代码库中的各个代码元素的类型、命名以及处于代码行末尾的注释。
S102:获取用户输入的元素用途信息,所述元素用途信息用于描述用户所需创建的第二代码元素的功能。
若用户想要在其所需开发的应用程序中,创建一个新的代码元素,则请求用户输入与该代码元素相关的用途描述。例如,若程序开发人员想要为一个变量进行命名,且该变量主要用于实现累积计数的功能,则获取得到的用户输入的元素用途信息可以是“计数器”。其中,用户输入的元素用途信息包括但不限于中文或英文等不同种类的语言文字。
S103:分别计算每一所述注释与所述元素用途信息的相似度,获取与所述元素用途信息的相似度大于预设阈值的所述注释,并确定出与该注释对应的所述第一代码元素。
本申请实施例中,分别将用户输入的元素用途信息与代码库中各代码元素的注释进行对比,以确定代码库中已有代码元素的功能与用户所需建立的代码元素的功能的相似程度。
具体地,依次获取代码库中每一代码元素的注释,计算该注释与用户输入的元素用途信息的相似度,即,计算注释与元素用途信息的文本相似度。文本相似度的计算方法包括但不限于余弦距离、欧式距离、杰卡德距离以及概率分布距离(K-L距离)等相似性度量方法。
获得代码库中各代码元素的注释与用户输入的元素用途信息的相似度后,筛选出其中相似度大于预设阈值的各个注释。若任一代码元素的注释与用户输入的元素用途信息的相似度大于预设阈值,则表示该代码元素与用户所需建立的代码元素的功能较为接近。因此,在代码库中,获取与筛选出的注释对应的代码元素。
作为本申请的一个实施例,如图2所示,上述S103具体包括:
S1031:根据所述第一代码元素的所述注释以及所述用户输入的元素用途信息,生成所述第一代码元素对应的第一向量以及所述第二代码元素对应的第二向量。
S1032:计算所述第一向量与所述第二向量的余弦相似度。
对于代码库中任一代码元素的注释,基于Word2Vec、Doc2Vec以及向量空间模型VSM等方式,分别将该注释以及用户输入的元素用途信息进行转换处理,以获取与该注释对应的第一向量以及与元素用途信息对应的第二向量。基于转换后所得到的第一向量以及第二向量, 计算代码库中各代码元素的注释与用户输入的元素用途信息的相似度。其中,转换所得到的第一向量以及第二向量的元素总数相同。
本申请实施例中,文本相似度具体为余弦相似度。由于代码库中代码元素的注释已转换为第一向量,用户输入的元素用途信息已转换为第二向量,故计算代码元素的注释与用户输入的元素用途信息的相似度即计算第一向量以及第二向量的余弦相似度。
余弦相似度S的计算公式具体如下:
Figure PCTCN2017104537-appb-000001
其中,xi表示第一向量的第i个元素值;yi表示第二向量的第i个元素值;n表示第一向量或第二向量的元素总数。
S1033:确定出与所述第二向量的余弦相似度大于预设阈值的所述第一向量,并获取确定出的所述第一向量所对应的所述第一代码元素。
获取每一个第一向量与第二向量的余弦相似度后,筛选出其中余弦相似度大于预设阈值的各个第一向量。此时,基于注释以及向量在转换过程中的对应关系,在代码库中,确定出与筛选得到的各个第一向量相对应的代码元素。
本申请实施例中,通过将第一代码元素的注释以及第二代码元素的元素用途信息转换为向量,并计算向量之间的余弦相似度,实现了对以文本形式表示的注释以及元素用途信息的降维处理,由于余弦相似度能够较好地体现文本之间的相似程度,且计算过程较为简单,因此,提高了注释以及元素用途信息之间的相似度的计算效率。
作为本申请的一个实施例,上述S1031的具体实现流程请参见图3。各步骤的实现原理具体如下:
S10311:对所述元素用途信息以及所述注释进行分词处理,以得到分别与所述注释对应的多个第一分词以及与所述元素用途信息对应的多个第二分词。
在获取到代码库中任一代码元素的注释时,去除该注释中的停用词,并对注释中剩余的各个字符进行分词处理,以得到该注释对应的多个分词。与此同时,对于用户所输入的元素用途信息,去除该元素用途信息中的停用词,并对元素用途信息中剩余的各个字符进行分词处理,以得到元素用途信息对应的多个分词。
S10312:对所述多个第一分词以及所述多个第二分词进行合并去重处理后,输入预先建立的词袋模型。
在计算某一注释与用户输入的元素用途信息的相似度时,获取该注释以及元素用途信 息所分别对应的各个分词,并从中筛选出互不重复的分词。将包含这些互不重复的分词的集合输出为一个词袋模型。因此,对于当前时刻所获得的词袋模型,其对应于该注释以及元素用途信息所构成的一个组合。
S10313:分别统计所述词袋模型中每一分词在所述注释中的词频-逆向文件频率TF-IDF信息,以根据该TF-IDF信息,生成所述注释对应的第一向量。
S10314:分别统计所述词袋模型中每一分词在所述元素用途信息中的词频-逆向文件频率TF-IDF信息,以根据该TF-IDF信息,生成所述元素用途信息对应的第二向量。
对于词袋模型中的每一个分词,获取与该分词对应的预设权重值、确定该分词在与词袋模型对应的注释中所出现的频率以及确定该分词在元素用途信息中所出现的频率。上述频率即为词频(TF,term frequency),上述预设权重值即为逆向文件频率(IDF,Inverse Document Frequency)。将分词在注释中所对应的词频以及逆向文件频率的乘积作为该分词在注释中的TF-IDF信息,并将该TF-IDF信息输出为第一向量中的一个元素。将分词在元素用途信息中所对应的词频以及逆向文件频率的乘积作为该分词在元素用途信息中的TF-IDF信息,并将该TF-IDF信息输出为第二向量中的一个元素。因此,当词袋模型中包含N个分词时,在与该词袋模型对应的第一向量以及第二向量中,将包含有N个元素。
作为本申请的一个实施示例,上述S10311至S10314的实现过程如下:对于代码库中某一代码元素所对应的一个注释,若该注释为“函数运行时长:计算时长平均值”,则对其去除停用词并进行分词处理后,得到的多个第一分词为“函数/运行/时长/计算/时长/平均值”;若用户输入的元素用途信息为“运行花费时间”,则对其去除停用词并进行分词处理后,得到的多个第二分词为“运行/花费/时间”;由上述S10312的分析可得,当前时刻与该注释以及元素用途信息对应的词袋模型中,包含有“函数/运行/时长/计算/平均值/花费/时间”这七个互不相同的分词,若其中各个分词在该注释中的TF-IDF信息分别为“1、1、2、1、1、0、0”,在元素用途信息中的TF-IDF信息分别为“0、1、0、0、1、1、0”,则生成的第一向量为
Figure PCTCN2017104537-appb-000002
生成的第二向量为
Figure PCTCN2017104537-appb-000003
本申请实施例中,由于分词在文本中的词频越高、逆向文件频率越高,该分词在文本中的重要程度越大,越能够体现注释或者元素用途信息的主要内容,因此,通过词袋模型的方式来获取各个注释以及元素用途信息所分别对应的向量,以词袋模型中各个分词的TF-IDF信息来表示向量中的每一元素,使得基于向量所计算出的余弦相似度能够与文本的主要内容紧密关联,由此能够准确地体现出注释以及元素用途信息的相似程度。
S104:将确定出的所述第一代码元素的命名推荐至所述用户,以使所述用户从各个推 荐命名中,选取出与所述第二代码元素相关的命名。
若从代码库中确定出的第一代码元素的注释与用户所需创建的第二代码元素的元素用途信息的相似度较高,则第一代码元素与第二代码元素的功能、属性以及使用方式均相同的概率也相对较高,故第一代码元素的命名也比较适用于第二代码元素。因此,将确定出的第一代码元素的命名推荐至用户。
若在上述S103中,与元素用途信息的相似度大于预设阈值的注释的数量为多个,则确定出的第一代码元素的数量也为多个。此时,将其中各个第一代码元素的命名进行展示,以将各个命名推荐至用户,使得用户可以从当前时刻所展示的多个命名中,选取自己所需创建的第二代码元素的命名,或者,选取出其中的一个推荐命名后,为该命名添加额外的字符(如序号),以将最终所得到的命名确定为第二代码元素的命名。
本申请实施例中,通过计算用户所需创建的代码元素的元素用途信息与收集得到的各个代码元素的注释的相似度,并将相似度较高的代码元素的命名推荐至用户,使得用户能够基于功能相似的代码元素,获取到参考价值较高的推荐命名,保证用户能够快速地从推荐命名中确定出最符合自身所需的一个命名,从而基于该命名来创建新的代码元素。由于推荐命名是代码库中已存在的代码元素的命名,因而各个推荐命名都是其他开发人员在确保程序具有较高可读性的情况下所使用的命名,因此,用户无须耗费过多的时间思考如何提供程序可读性较高的代码元素命名,故本申请实施例提高了代码元素的命名效率。
作为本申请的另一实施例,如图4所示,若在上述S103中,不存在与元素用途信息的相似度大于预设阈值的注释,则确定出的第一代码元素的个数为零。此时,执行S105至S108。详述如下:
S105:在所述代码库中,若不存在与所述元素用途信息的相似度大于预设阈值的所述注释,则对所述元素用途信息进行分词处理,得到多个所述第二分词。
S106:分别将各个所述第二分词转换为英文单词。
在用户输入的元素用途信息所对应的分词结果中,若分词中包含有中文分词或者其他非英文分词,则将所述中文分词或者其他非英文分词逐一转换为英文单词。
S107:根据当前时刻所述用户所使用的编程语言,获取与所述编码语言匹配的命名算法。
本申请实施例中,获取用户在当前时刻所需使用的编程语言。例如,对用户当前正在开发的应用程序中的其他代码进行检测,以对各代码的语法结构进行分析后,确定出当前时刻用户所使用的编程语言;根据用户输入的编程语言参数设置指令,获取用户在开发应用程 序过程中所需使用的编程语言。其中,编程语言包括但不限于C语言、C++、Python、LinuxC以及Java等。
对于不同类型的编程语言,在本领域通常都存在有一套推荐的命名标准。因此,本申请实施例中,当用户使用不同的编程语言来创建具有同一功能用途的代码元素时,需要先确定出与编程语言匹配的命名算法。
S108:基于所述命名算法,对各个所述英文单词进行处理,并将处理后所得到的字符串推荐至所述用户。
基于获取得到的与编码语言匹配的命名算法,对各个英文单词进行处理,以得到包含上述各个英文单词的字符串。例如,命名算法的处理方式可以是,以预设的连接符连接各英文单词,从而得到包含上述各个英文单词的字符串。其中,连接符包括但不限于下划线、空连接符以及短横线等。此后,将输出的字符串作为推荐命名并进行展示。
例如,若用户输入的元素用途信息为“获取学生成绩”,则分词后所得到的结果为“获取/学生/成绩”,其中每一分词对应的英文单词分别为get、student以及score。若当前时刻用户的编程语言为Java,且其对应的命名算法为驼峰命名算法,则对get、student以及score这三个单词进行处理后,得到的字符串为getStudentScore;若当前时刻用户的编程语言为Linux C,且其对应的命名算法为下划线组合算法,则对get、student以及score这三个单词进行连接处理后,得到的字符串为get_student_score。
本申请实施例中,由于代码库所存储的第一代码元素的数量是有限的,因而各个代码元素的注释都未必能够会与用户所输入的元素用途信息具有较高的相似度,因此,通过对翻译后所得到的各个英文单词进行处理,并将包含英文单词的字符串作为推荐命名,使得用户能够直接使用该字符串来对其所需创建的代码元素进行命名,提高了代码元素的命名效率;由于英文单词具有实际语义,能够更加易于他人识读,因而通过将可读性较高的字符串推荐至用户,并以该字符串来作为代码元素的命名,进一步加强了用户所需开发的应用程序的代码可读性。
图5示出了本申请又一实施例提供的代码元素的命名方法的实现流程,如图5所示,在上述S104之后,还包括:
S109:将所述元素用途信息与各个推荐命名绑定存储。
将筛选得到的已有代码元素的命名推荐至用户后,检测用户从中选取的推荐命名。将该推荐命名的累积选取次数加一,累积选取次数表示推荐命名被选取的次数。
推荐至用户的各个命名均匹配于用户输入的元素用途信息。将该元素用途信息绑定各 个推荐命名后,将元素用途信息、推荐命名以及二者之间的绑定关系存储至预先建立的信息库中。
S110:将当前时刻用户选取的推荐命名的累积选取次数加一,以在再次接收到用户输入的所述元素用途信息时,根据对应的所述累积选取次数的高低顺序,将与所述元素用途信息绑定的各个推荐命名依次推荐至所述用户。
在任一时刻,若同一用户或其他用户需要再创建一个代码元素,且用户在该时刻输入的元素用途信息与信息库中所存储的元素用途信息相同,则无需再计算该元素用途信息与代码库中已有代码元素的注释的相似度。此时,从信息库中直接读取当前时刻用户输入的元素用途信息所绑定的各个推荐命名,并将读取到的推荐命名展示至用户。
由于信息库中各个推荐命名的累积选取次数不同,故在将读取到的推荐命名展示至用户时,将累积选取次数较高的命名进行优先展示。即,依照累积选取次数的高低顺序,依次展示读取到的各个推荐命名。
本申请实施例中,在确定出与元素用途信息匹配的各个代码元素的推荐命名后,通过将元素用途信息以及各个推荐命名绑定存储,使得下次在检测到用户输入的相同元素用途信息时,无需再重复计算元素用途信息与代码库中已有代码元素的注释的相似度,降低了系统的运算压力,提高了代码元素命名的推荐效率;由于使用频率较高的推荐命名通常都具有较高的代码可读性,故基于累积选取次数的高低顺序来推荐各个命名,使得用户可以从多个推荐命名中,快速地选取出最适合的一个命名,从而提高代码元素的命名效率。
应理解,上述实施例中各步骤的序号的大小并不意味着执行顺序的先后,各过程的执行顺序应以其功能和内在逻辑确定,而不应对本申请实施例的实施过程构成任何限定。
对应于上文实施例所述的代码元素的命名方法,图6示出了本申请实施例提供的代码元素的命名装置的结构框图,为了便于说明,仅示出了与本申请实施例相关的部分。
参照图6,该装置包括:
提取模块601,用于从预设代码库中提取各个第一代码元素的命名及注释,所述预设的代码库中包含多段代码,其中,所述第一代码元素包括变量、常量、函数、类以及文件。
第一获取模块602,用于获取用户输入的元素用途信息,所述元素用途信息用于描述用户所需创建的第二代码元素的功能。
计算模块603,用于分别计算每一所述注释与所述元素用途信息的相似度,获取与所述元素用途信息的相似度大于预设阈值的所述注释,并确定出与该注释对应的所述第一代码元 素。
推荐模块604,用于将确定出的所述第一代码元素的命名推荐至所述用户,以使所述用户从各个推荐命名中,选取出与所述第二代码元素相关的命名。
可选地,所述计算模块603包括:
生成子模块,用于根据所述第一代码元素的所述注释以及所述用户输入的元素用途信息,生成所述第一代码元素对应的第一向量以及所述第二代码元素对应的第二向量。
计算子模块,用于计算所述第一向量与所述第二向量的余弦相似度。
获取子模块,用于确定出与所述第二向量的余弦相似度大于预设阈值的所述第一向量,并获取确定出的所述第一向量所对应的所述第一代码元素。
可选地,所述生成子模块具体用于:
对所述元素用途信息以及所述注释进行分词处理,以得到分别与所述注释对应的多个第一分词以及与所述元素用途信息对应的多个第二分词;
对所述多个第一分词以及所述多个第二分词进行合并去重处理后,输入预先建立的词袋模型;
分别统计所述词袋模型中每一分词在所述注释中的词频-逆向文件频率TF-IDF信息,以根据该TF-IDF信息,生成所述注释对应的第一向量;
分别统计所述词袋模型中每一分词在所述元素用途信息中的词频-逆向文件频率TF-IDF信息,以根据该TF-IDF信息,生成所述元素用途信息对应的第二向量。
可选地,如图7所示,所述代码元素的命名装置还包括:
分词模块605,用于在所述代码库中,若不存在与所述元素用途信息的相似度大于预设阈值的所述注释,则对所述元素用途信息进行分词处理,得到多个所述第二分词。
转换模块606,用于分别将各个所述第二分词转换为英文单词。
第二获取模块607,用于根据当前时刻所述用户所使用的编程语言,获取与所述编码语言匹配的命名算法。
处理模块608,用于基于所述命名算法,对各个所述英文单词进行处理,并将处理后所得到的字符串推荐至所述用户。
可选地,如图8所示,所述代码元素的命名装置还包括:
存储模块609,用于将所述元素用途信息与各个推荐命名绑定存储。
统计模块610,用于将当前时刻用户选取的推荐命名的累积选取次数加一,以在再次接收到用户输入的所述元素用途信息时,根据对应的所述累积选取次数的高低顺序,将与所述元素用途信息绑定的各个推荐命名依次推荐至所述用户。
本申请实施例中,通过计算用户所需创建的代码元素的元素用途信息与收集得到的各个代码元素的注释的相似度,并将相似度较高的代码元素的命名推荐至用户,使得用户能够基于功能相似的代码元素,获取到参考价值较高的推荐命名,保证用户能够快速地从推荐命名中确定出最符合自身所需的一个命名,从而基于该命名来创建新的代码元素。由于推荐命名是代码库中已存在的代码元素的命名,因而各个推荐命名都是其他开发人员在确保程序具有较高可读性的情况下所使用的命名,因此,用户无须耗费过多的时间思考如何提供程序可读性较高的代码元素命名,故本申请实施例提高了代码元素的命名效率。
图9是本申请一实施例提供的电子设备的示意图。如图9所示,该实施例的电子设备9包括:处理器90、存储器91以及存储在所述存储器91中并可在所述处理器90上运行的计算机可读指令92,例如代码元素的命名程序。所述处理器90执行所述计算机可读指令92时实现上述各个代码元素的命名方法实施例中的步骤,例如图1所示的步骤101至104。或者,所述处理器90执行所述计算机可读指令92时实现上述各装置实施例中各模块/单元的功能,例如图6所示模块601至604的功能。
示例性的,所述计算机可读指令92可以被分割成一个或多个模块/单元,所述一个或者多个模块/单元被存储在所述存储器91中,并由所述处理器90执行,以完成本申请。所述一个或多个模块/单元可以是能够完成特定功能的一系列计算机可读指令指令段,该指令段用于描述所述计算机可读指令92在所述电子设备9中的执行过程。
所述电子设备9可以是桌上型计算机、笔记本、掌上电脑及云端服务器等计算设备。所述电子设备可包括,但不仅限于,处理器90、存储器91。本领域技术人员可以理解,图9仅仅是电子设备9的示例,并不构成对电子设备9的限定,可以包括比图示更多或更少的部件,或者组合某些部件,或者不同的部件,例如所述电子设备还可以包括输入输出设备、网络接入设备、总线等。
所称处理器90可以是中央处理单元(Central Processing Unit,CPU),还可以是其他通用处理器、数字信号处理器(Digital Signal Processor,DSP)、专用集成电路(Application Specific Integrated Circuit,ASIC)、现成可编程门阵列(Field-Programmable Gate Array,FPGA)或者其他可编程逻辑器件、分立门或者晶体管逻辑器件、分立硬件组件等。通用处理器可以是微处理器或者该处理器也可以是任何常规的处理器等。
所述存储器91可以是所述电子设备9的内部存储单元,例如电子设备9的硬盘或内存。所述存储器91也可以是所述电子设备9的外部存储设备,例如所述电子设备9上配备的插接式硬盘,智能存储卡(Smart Media Card,SMC),安全数字(Secure Digital,SD)卡,闪 存卡(Flash Card)等。进一步地,所述存储器91还可以既包括所述电子设备9的内部存储单元也包括外部存储设备。所述存储器91用于存储所述计算机可读指令以及所述电子设备所需的其他程序和数据。所述存储器91还可以用于暂时地存储已经输出或者将要输出的数据。
另外,在本申请各个实施例中的各功能单元可以集成在一个处理单元中,也可以是各个单元单独物理存在,也可以两个或两个以上单元集成在一个单元中。上述集成的单元既可以采用硬件的形式实现,也可以采用软件功能单元的形式实现。
所述集成的单元如果以软件功能单元的形式实现并作为独立的产品销售或使用时,可以存储在一个计算机可读取存储介质中。基于这样的理解,本申请的技术方案本质上或者说对现有技术做出贡献的部分或者该技术方案的全部或部分可以以软件产品的形式体现出来,该计算机软件产品存储在一个存储介质中,包括若干指令用以使得一台计算机设备(可以是个人计算机,服务器,或者网络设备等)执行本申请各个实施例所述方法的全部或部分步骤。而前述的存储介质包括:U盘、移动硬盘、只读存储器(ROM,Read-Only Memory)、随机存取存储器(RAM,Random Access Memory)、磁碟或者光盘等各种可以存储程序代码的介质。
以上所述,以上实施例仅用以说明本申请的技术方案,而非对其限制;尽管参照前述实施例对本申请进行了详细的说明,本领域的普通技术人员应当理解:其依然可以对前述各实施例所记载的技术方案进行修改,或者对其中部分技术特征进行等同替换;而这些修改或者替换,并不使相应技术方案的本质脱离本申请各实施例技术方案的精神和范围。

Claims (20)

  1. 一种代码元素的命名方法,其特征在于,包括:
    从预设代码库中提取各个第一代码元素的命名及注释,所述预设的代码库中包含多段代码,其中,所述第一代码元素包括变量、常量、函数、类以及文件;
    获取用户输入的元素用途信息,所述元素用途信息用于描述用户所需创建的第二代码元素的功能;
    分别计算每一所述注释与所述元素用途信息的相似度,获取与所述元素用途信息的相似度大于预设阈值的所述注释,并确定出与该注释对应的所述第一代码元素;
    将确定出的所述第一代码元素的命名推荐至所述用户,以使所述用户从各个推荐命名中,选取出与所述第二代码元素相关的命名。
  2. 如权利要求1所述的代码元素的命名方法,其特征在于,所述分别计算每一所述注释与所述元素用途信息的相似度,获取与所述元素用途信息的相似度大于预设阈值的所述注释,并确定出与该注释对应的所述第一代码元素,包括:
    根据所述第一代码元素的所述注释以及所述用户输入的元素用途信息,生成所述第一代码元素对应的第一向量以及所述第二代码元素对应的第二向量;
    计算所述第一向量与所述第二向量的余弦相似度;
    确定出与所述第二向量的余弦相似度大于预设阈值的所述第一向量,并获取确定出的所述第一向量所对应的所述第一代码元素。
  3. 如权利要求2所述的代码元素的命名方法,其特征在于,所述根据所述第一代码元素的所述注释以及所述用户输入的元素用途信息,生成所述第一代码元素对应的第一向量以及所述第二代码元素对应的第二向量,包括:
    对所述元素用途信息以及所述注释进行分词处理,以得到分别与所述注释对应的多个第一分词以及与所述元素用途信息对应的多个第二分词;
    对所述多个第一分词以及所述多个第二分词进行合并去重处理后,输入预先建立的词袋模型;
    分别统计所述词袋模型中每一分词在所述注释中的词频-逆向文件频率TF-IDF信息,以根据该TF-IDF信息,生成所述注释对应的第一向量;
    分别统计所述词袋模型中每一分词在所述元素用途信息中的词频-逆向文件频率TF-IDF信息,以根据该TF-IDF信息,生成所述元素用途信息对应的第二向量。
  4. 如权利要求1所述的代码元素的命名方法,其特征在于,还包括:
    在所述代码库中,若不存在与所述元素用途信息的相似度大于预设阈值的所述注释,则对所述元素用途信息进行分词处理,得到多个所述第二分词;
    分别将各个所述第二分词转换为英文单词;
    根据当前时刻所述用户所使用的编程语言,获取与所述编码语言匹配的命名算法;
    基于所述命名算法,对各个所述英文单词进行处理,并将处理后所得到的字符串推荐至所述用户。
  5. 如权利要求1所述的代码元素的命名方法,其特征在于,还包括:
    将所述元素用途信息与各个推荐命名绑定存储;
    将当前时刻用户选取的推荐命名的累积选取次数加一,以在再次接收到用户输入的所述元素用途信息时,根据对应的所述累积选取次数的高低顺序,将与所述元素用途信息绑定的各个推荐命名依次推荐至所述用户。
  6. 一种代码元素的命名装置,其特征在于,包括:
    提取模块,用于从预设代码库中提取各个第一代码元素的命名及注释,所述预设的代码库中包含多段代码,其中,所述第一代码元素包括变量、常量、函数、类以及文件;
    第一获取模块,用于获取用户输入的元素用途信息,所述元素用途信息用于描述用户所需创建的第二代码元素的功能;
    计算模块,用于分别计算每一所述注释与所述元素用途信息的相似度,获取与所述元素用途信息的相似度大于预设阈值的所述注释,并确定出与该注释对应的所述第一代码元素;
    推荐模块,用于将确定出的所述第一代码元素的命名推荐至所述用户,以使所述用户从各个推荐命名中,选取出与所述第二代码元素相关的命名。
  7. 根据权利要求6所述的代码元素的命名装置,其特征在于,所述计算模块包括:
    生成子模块,用于根据所述第一代码元素的所述注释以及所述用户输入的元素用途信息,生成所述第一代码元素对应的第一向量以及所述第二代码元素对应的第二向量;
    计算子模块,用于计算所述第一向量与所述第二向量的余弦相似度;
    获取子模块,用于确定出与所述第二向量的余弦相似度大于预设阈值的所述第一向量,并获取确定出的所述第一向量所对应的所述第一代码元素。
  8. 根据权利要求7所述的代码元素的命名装置,其特征在于,所述生成子模块具体用于:
    对所述元素用途信息以及所述注释进行分词处理,以得到分别与所述注释对应的多个第一分词以及与所述元素用途信息对应的多个第二分词;
    对所述多个第一分词以及所述多个第二分词进行合并去重处理后,输入预先建立的词袋模型;
    分别统计所述词袋模型中每一分词在所述注释中的词频-逆向文件频率TF-IDF信息,以根据该TF-IDF信息,生成所述注释对应的第一向量;
    分别统计所述词袋模型中每一分词在所述元素用途信息中的词频-逆向文件频率TF-IDF信息,以根据该TF-IDF信息,生成所述元素用途信息对应的第二向量。
  9. 根据权利要求6所述的代码元素的命名装置,其特征在于,还包括:
    分词模块,用于在所述代码库中,若不存在与所述元素用途信息的相似度 大于预设阈值的所述注释,则对所述元素用途信息进行分词处理,得到多个所述第二分词;
    转换模块,用于分别将各个所述第二分词转换为英文单词。
    第二获取模块,用于根据当前时刻所述用户所使用的编程语言,获取与所述编码语言匹配的命名算法;
    处理模块,用于基于所述命名算法,对各个所述英文单词进行处理,并将处理后所得到的字符串推荐至所述用户。
  10. 根据权利要求6所述的代码元素的命名装置,其特征在于,还包括:
    存储模块,用于将所述元素用途信息与各个推荐命名绑定存储;
    统计模块,用于将当前时刻用户选取的推荐命名的累积选取次数加一,以在再次接收到用户输入的所述元素用途信息时,根据对应的所述累积选取次数的高低顺序,将与所述元素用途信息绑定的各个推荐命名依次推荐至所述用户。
  11. 一种电子设备,其特征在于,包括存储器、处理器,所述存储器上存储有可在所述处理器上运行的计算机可读指令,所述处理器执行所述计算机序时实现如下步骤:
    从预设代码库中提取各个第一代码元素的命名及注释,所述预设的代码库中包含多段代码,其中,所述第一代码元素包括变量、常量、函数、类以及文件;
    获取用户输入的元素用途信息,所述元素用途信息用于描述用户所需创建的第二代码元素的功能;
    分别计算每一所述注释与所述元素用途信息的相似度,获取与所述元素用途信息的相似度大于预设阈值的所述注释,并确定出与该注释对应的所述第一代码元素;
    将确定出的所述第一代码元素的命名推荐至所述用户,以使所述用户从各个推荐命名中,选取出与所述第二代码元素相关的命名。
  12. 根据权利要求11所述的电子设备,其特征在于,所述分别计算每一所 述注释与所述元素用途信息的相似度,获取与所述元素用途信息的相似度大于预设阈值的所述注释,并确定出与该注释对应的所述第一代码元素,包括:
    根据所述第一代码元素的所述注释以及所述用户输入的元素用途信息,生成所述第一代码元素对应的第一向量以及所述第二代码元素对应的第二向量;
    计算所述第一向量与所述第二向量的余弦相似度;
    确定出与所述第二向量的余弦相似度大于预设阈值的所述第一向量,并获取确定出的所述第一向量所对应的所述第一代码元素。
  13. 根据权利要求12所述的电子设备,其特征在于,所述根据所述第一代码元素的所述注释以及所述用户输入的元素用途信息,生成所述第一代码元素对应的第一向量以及所述第二代码元素对应的第二向量,包括:
    对所述元素用途信息以及所述注释进行分词处理,以得到分别与所述注释对应的多个第一分词以及与所述元素用途信息对应的多个第二分词;
    对所述多个第一分词以及所述多个第二分词进行合并去重处理后,输入预先建立的词袋模型;
    分别统计所述词袋模型中每一分词在所述注释中的词频-逆向文件频率TF-IDF信息,以根据该TF-IDF信息,生成所述注释对应的第一向量;
    分别统计所述词袋模型中每一分词在所述元素用途信息中的词频-逆向文件频率TF-IDF信息,以根据该TF-IDF信息,生成所述元素用途信息对应的第二向量。
  14. 根据权利要求11所述的电子设备,其特征在于,所述处理器执行所述计算机序时,还实现如下步骤:
    在所述代码库中,若不存在与所述元素用途信息的相似度大于预设阈值的所述注释,则对所述元素用途信息进行分词处理,得到多个所述第二分词;
    分别将各个所述第二分词转换为英文单词;
    根据当前时刻所述用户所使用的编程语言,获取与所述编码语言匹配的命名算法;
    基于所述命名算法,对各个所述英文单词进行处理,并将处理后所得到的字符串推荐至所述用户。
  15. 根据权利要求11所述的电子设备,其特征在于,所述处理器执行所述计算机序时,还实现如下步骤:
    将所述元素用途信息与各个推荐命名绑定存储;
    将当前时刻用户选取的推荐命名的累积选取次数加一,以在再次接收到用户输入的所述元素用途信息时,根据对应的所述累积选取次数的高低顺序,将与所述元素用途信息绑定的各个推荐命名依次推荐至所述用户。
  16. 一种计算机可读存储介质,所述计算机可读存储介质存储有计算机可读指令,其特征在于,所述计算机可读指令被至少一个处理器执行时实现如下步骤:
    从预设代码库中提取各个第一代码元素的命名及注释,所述预设的代码库中包含多段代码,其中,所述第一代码元素包括变量、常量、函数、类以及文件;
    获取用户输入的元素用途信息,所述元素用途信息用于描述用户所需创建的第二代码元素的功能;
    分别计算每一所述注释与所述元素用途信息的相似度,获取与所述元素用途信息的相似度大于预设阈值的所述注释,并确定出与该注释对应的所述第一代码元素;
    将确定出的所述第一代码元素的命名推荐至所述用户,以使所述用户从各个推荐命名中,选取出与所述第二代码元素相关的命名。
  17. 根据权利要求16所述的计算机可读存储介质,其特征在于,所述分别计算每一所述注释与所述元素用途信息的相似度,获取与所述元素用途信息的相似度大于预设阈值的所述注释,并确定出与该注释对应的所述第一代码元素,包括:
    根据所述第一代码元素的所述注释以及所述用户输入的元素用途信息,生 成所述第一代码元素对应的第一向量以及所述第二代码元素对应的第二向量;
    计算所述第一向量与所述第二向量的余弦相似度;
    确定出与所述第二向量的余弦相似度大于预设阈值的所述第一向量,并获取确定出的所述第一向量所对应的所述第一代码元素。
  18. 根据权利要求17所述的计算机可读存储介质,其特征在于,所述根据所述第一代码元素的所述注释以及所述用户输入的元素用途信息,生成所述第一代码元素对应的第一向量以及所述第二代码元素对应的第二向量,包括:
    对所述元素用途信息以及所述注释进行分词处理,以得到分别与所述注释对应的多个第一分词以及与所述元素用途信息对应的多个第二分词;
    对所述多个第一分词以及所述多个第二分词进行合并去重处理后,输入预先建立的词袋模型;
    分别统计所述词袋模型中每一分词在所述注释中的词频-逆向文件频率TF-IDF信息,以根据该TF-IDF信息,生成所述注释对应的第一向量;
    分别统计所述词袋模型中每一分词在所述元素用途信息中的词频-逆向文件频率TF-IDF信息,以根据该TF-IDF信息,生成所述元素用途信息对应的第二向量。
  19. 根据权利要求16所述的计算机可读存储介质,其特征在于,所述计算机可读指令被至少一个处理器执行时,还实现如下步骤:
    在所述代码库中,若不存在与所述元素用途信息的相似度大于预设阈值的所述注释,则对所述元素用途信息进行分词处理,得到多个所述第二分词;
    分别将各个所述第二分词转换为英文单词;
    根据当前时刻所述用户所使用的编程语言,获取与所述编码语言匹配的命名算法;
    基于所述命名算法,对各个所述英文单词进行处理,并将处理后所得到的字符串推荐至所述用户。
  20. 根据权利要求16所述的计算机可读存储介质,其特征在于,所述计算 机可读指令被至少一个处理器执行时,还实现如下步骤:
    将所述元素用途信息与各个推荐命名绑定存储;
    将当前时刻用户选取的推荐命名的累积选取次数加一,以在再次接收到用户输入的所述元素用途信息时,根据对应的所述累积选取次数的高低顺序,将与所述元素用途信息绑定的各个推荐命名依次推荐至所述用户。
PCT/CN2017/104537 2017-08-09 2017-09-29 代码元素的命名方法、装置、电子设备及介质 WO2019028990A1 (zh)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN201710674688.5 2017-08-09
CN201710674688.5A CN107463683B (zh) 2017-08-09 2017-08-09 代码元素的命名方法及终端设备

Publications (1)

Publication Number Publication Date
WO2019028990A1 true WO2019028990A1 (zh) 2019-02-14

Family

ID=60548738

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2017/104537 WO2019028990A1 (zh) 2017-08-09 2017-09-29 代码元素的命名方法、装置、电子设备及介质

Country Status (2)

Country Link
CN (1) CN107463683B (zh)
WO (1) WO2019028990A1 (zh)

Families Citing this family (10)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN109918058B (zh) * 2017-12-13 2022-08-12 富士通株式会社 信息处理装置和方法以及在编程环境中推荐代码的方法
CN108170679B (zh) * 2017-12-28 2021-09-03 中国联合网络通信集团有限公司 基于计算机可识别自然语言描述的语义匹配方法及系统
CN108427580B (zh) * 2018-01-08 2020-01-10 平安科技(深圳)有限公司 配置对命名重复的检测方法、存储介质和智能设备
CN108664237B (zh) * 2018-05-14 2019-04-12 北京理工大学 一种基于启发式和神经网络的非api成员推荐方法
CN108717470B (zh) * 2018-06-14 2020-10-23 南京航空航天大学 一种具有高准确度的代码片段推荐方法
CN109828748A (zh) * 2018-12-15 2019-05-31 深圳壹账通智能科技有限公司 代码命名方法、系统、计算机装置及计算机可读存储介质
CN111061688B (zh) * 2019-12-13 2023-06-09 深圳前海环融联易信息科技服务有限公司 统计变量命名方式的方法、装置、计算机设备及存储介质
CN111291208B (zh) * 2020-05-09 2020-11-10 支付宝(杭州)信息技术有限公司 前端页面元素的命名方法、装置及电子设备
CN112463162B (zh) * 2020-12-11 2022-12-20 苏州浪潮智能科技有限公司 一种代码命名的推荐方法、系统、存储介质及设备
CN112579098B (zh) * 2020-12-25 2024-02-06 平安银行股份有限公司 软件发布方法、装置、电子设备及可读存储介质

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20100131925A1 (en) * 2008-11-25 2010-05-27 Sap Ag Dynamic naming conventions in a source code editor
CN102819575A (zh) * 2012-07-20 2012-12-12 南京大学 一种用于Web服务推荐的个性化搜索方法
CN103914296A (zh) * 2013-01-03 2014-07-09 国际商业机器公司 用于本机语言ide代码帮助的方法和系统
CN104809139A (zh) * 2014-01-29 2015-07-29 日本电气株式会社 代码文件查询方法和装置

Family Cites Families (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
EP2145265A4 (en) * 2007-03-30 2011-09-14 Amazon Tech Inc CLUSTER-BASED ASSESSMENT OF USER INTERESTS
CN106462399B (zh) * 2014-06-30 2019-10-18 微软技术许可有限责任公司 代码推荐

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20100131925A1 (en) * 2008-11-25 2010-05-27 Sap Ag Dynamic naming conventions in a source code editor
CN102819575A (zh) * 2012-07-20 2012-12-12 南京大学 一种用于Web服务推荐的个性化搜索方法
CN103914296A (zh) * 2013-01-03 2014-07-09 国际商业机器公司 用于本机语言ide代码帮助的方法和系统
CN104809139A (zh) * 2014-01-29 2015-07-29 日本电气株式会社 代码文件查询方法和装置

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
GAO, YUAN ET AL.: "Method Name Recommendation Based on Source Code Repository and Feature Matching", JOURNAL OF SOFTWARE, vol. 26, no. 12, 31 December 2015 (2015-12-31), pages 3062 - 3074, ISSN: 1000-9825 *

Also Published As

Publication number Publication date
CN107463683B (zh) 2018-07-24
CN107463683A (zh) 2017-12-12

Similar Documents

Publication Publication Date Title
WO2019028990A1 (zh) 代码元素的命名方法、装置、电子设备及介质
CN107463605B (zh) 低质新闻资源的识别方法及装置、计算机设备及可读介质
CN110390044B (zh) 一种相似网络页面的搜索方法及设备
CN110210038B (zh) 核心实体确定方法及其系统、服务器和计算机可读介质
US20200004817A1 (en) Method, device, and program for text classification
CN110427453B (zh) 数据的相似度计算方法、装置、计算机设备及存储介质
CN110895961A (zh) 医疗数据中的文本匹配方法及装置
CN111767713A (zh) 关键词的提取方法、装置、电子设备及存储介质
CN111651674B (zh) 双向搜索方法、装置及电子设备
CN108052509A (zh) 一种文本相似度计算方法、装置及服务器
CN107273546B (zh) 仿冒应用检测方法以及系统
CN113986950A (zh) 一种sql语句处理方法、装置、设备及存储介质
KR20210089340A (ko) 문서 내 텍스트를 분류하는 방법 및 장치
CN112328655B (zh) 文本标签挖掘方法、装置、设备及存储介质
CN109753646B (zh) 一种文章属性识别方法以及电子设备
Rexha et al. Towards Authorship Attribution for Bibliometrics using Stylometric Features.
CN111160445B (zh) 投标文件相似度计算方法及装置
CN112307235B (zh) 前端页面元素的命名方法、装置及电子设备
US11347928B2 (en) Detecting and processing sections spanning processed document partitions
KR20210146832A (ko) 토픽 키워드의 추출 장치 및 방법
CN116029280A (zh) 一种文档关键信息抽取方法、装置、计算设备和存储介质
CN105279172B (zh) 视频匹配方法和装置
Selivanov et al. Package ‘text2vec’
CN113935387A (zh) 文本相似度的确定方法、装置和计算机可读存储介质
CN114743012B (zh) 一种文本识别方法及装置

Legal Events

Date Code Title Description
NENP Non-entry into the national phase

Ref country code: DE

32PN Ep: public notification in the ep bulletin as address of the adressee cannot be established

Free format text: NOTING OF LOSS OF RIGHTS PURSUANT TO RULE 112(1) EPC (EPO FORM 1205A DATED 10/07/2020)

32PN Ep: public notification in the ep bulletin as address of the adressee cannot be established

Free format text: NOTING OF LOSS OF RIGHTS PURSUANT TO RULE 112(1) EPC (EPO FORM 1205A DATED 12-02-2021)

121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 17921404

Country of ref document: EP

Kind code of ref document: A1

122 Ep: pct application non-entry in european phase

Ref document number: 17921404

Country of ref document: EP

Kind code of ref document: A1