Data query method, device, medium and product based on relational database
By receiving natural language query statements and generating SQL, the problem of insufficient SQL writing and expression capabilities is solved, and efficient data query and full expression on the mobile terminal is realized.
Patent Information
- Application Number
- CN202111471020.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-03
- Publication Date
- 2024-12-20
- Estimated Expiration
- 2041-12-03
AI Technical Summary
In the prior art, users need to have strong SQL writing ability and be familiar with the correspondence between each field in a database table, or although the drag and drop fields reduces the user's usage threshold, there is a problem of insufficient expression ability and inability to use it on the mobile terminal. .
By receiving natural language query statements input by users, a pre-constructed database dictionary is used for word slicing and grammar analysis, a target syntax tree is generated, and a structured query language SQL is automatically generated by traversing the syntax tree.
It effectively lowers the threshold for user usage, so that users can query data without having SQL writing capabilities, and can be used on mobile, improving SQL generation efficiency and achieving full expression of search data.
Smart Images

Figure CN114138817B_ABST
Abstract
Description
Technical Field
[0001] Embodiments of the present invention relate to the field of intelligent search technology, and in particular, to a data query method, device, medium and product based on a relational database. Background Art
[0002] With the continuous development of artificial intelligence, search technology has also made continuous progress. Search based on relational databases has been widely used.
[0003] At present, in the data search of relational databases, users are generally required to use structured query language (SQL) to search for data. Therefore, SQL needs to be provided before searching. Currently, one way to provide SQL is for users to write SQL. Another way to provide SQL is to provide users with a SQL generation interface, and users can generate SQL by dragging fields in the SQL generation interface.
[0004] Therefore, the current SQL language method requires users to have strong SQL writing skills and to be familiar with the correspondence between the information to be queried and the fields in the database table. Alternatively, although the drag-and-drop method lowers the user threshold, it has the problem of insufficient expression ability and cannot be used on mobile terminals. Summary of the invention
[0005] The embodiment of the present invention provides a data query method, device, medium and product based on a relational database, which is used to solve the problem that users need to have strong SQL writing ability and need to be familiar with the corresponding relationship between the information to be queried and the fields in the data table. Although the method of dragging fields reduces the user's usage threshold, it has insufficient expression ability and cannot be used on mobile terminals.
[0006] In a first aspect, an embodiment of the present invention provides a data query method based on a relational database, comprising:
[0007] Receive a natural language query statement input by a user;
[0008] The natural language query statement is segmented according to a dictionary corresponding to a pre-built database to obtain a target segmentation result; the dictionary stores a mapping relationship between words and fields in a table of the database;
[0009] Performing grammatical analysis on the target word segmentation result to obtain a grammatical analysis result;
[0010] Generate a target syntax tree corresponding to the natural language query statement according to the syntax analysis result;
[0011] The target syntax tree is traversed to generate a target structured query language SQL corresponding to a natural language query statement.
[0012] In a second aspect, an embodiment of the present invention provides a data query device based on a relational database, comprising:
[0013] A receiving module, used for receiving a natural language query statement input by a user;
[0014] A word segmentation module is used to perform word segmentation processing on the natural language query statement according to a dictionary corresponding to a pre-built database to obtain a target word segmentation result; the dictionary stores a mapping relationship between words and fields in a table of the database;
[0015] An analysis module is used to perform grammatical analysis on the target word segmentation result to obtain a grammatical analysis result;
[0016] A generation module, used to generate a target syntax tree corresponding to the natural language query statement according to the syntax analysis result;
[0017] The traversal module is used to traverse the target syntax tree to generate a target structured query language SQL corresponding to the natural language query statement.
[0018] In a third aspect, an embodiment of the present invention provides an electronic device, including: at least one processor and a memory;
[0019] The memory stores computer-executable instructions;
[0020] The at least one processor executes the computer-executable instructions stored in the memory, so that the at least one processor performs the method as described in any one of the first aspects.
[0021] In a fourth aspect, an embodiment of the present invention provides a computer-readable storage medium, wherein the computer-readable storage medium stores computer-executable instructions, and when the computer-executable instructions are executed by a processor, they are used to implement the method as described in any one of the first aspects.
[0022] In a fifth aspect, an embodiment of the present invention provides a computer program product, including a computer program, which implements any method described in the first aspect when executed by a processor.
[0023] The data query method, device, equipment, storage medium and product based on a relational database provided by the embodiment of the present invention receive a natural language query statement input by a user; perform word segmentation processing on the natural language query statement according to a dictionary corresponding to a pre-constructed database to obtain a target word segmentation result; the dictionary stores a mapping relationship between words and fields in a table of the database; performs syntax analysis on the target word segmentation result to obtain a syntax analysis result; generates a target syntax tree corresponding to the natural language query statement according to the syntax analysis result; and traverses the target syntax tree to generate a target structured query language SQL corresponding to the natural language query statement. Since the user only needs to input a natural language query statement, the natural language query statement can be segmented and analyzed according to the dictionary corresponding to the pre-constructed database to generate a corresponding target syntax tree, and the SQL language can be automatically generated by traversing the target syntax tree, so the user's use threshold can be effectively lowered. It can also be used on a mobile terminal, and the search data can be fully expressed. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present disclosure and, together with the description, serve to explain the principles of the present disclosure.
[0025] Figure 1 It is a network architecture diagram that can implement the data query method based on a relational database according to an embodiment of the present invention;
[0026] Figure 2 It is a flowchart of a data query method based on a relational database provided by an embodiment of the present invention;
[0027] Figure 3 is a flow chart of a data query method based on a relational database provided by another embodiment of the present invention;
[0028] Figure 4 is a flow chart of a data query method based on a relational database provided by another embodiment of the present invention;
[0029] Figure 5 is a schematic diagram of a syntax tree in a data query method based on a relational database provided in an embodiment of the present invention;
[0030] Figure 6 It is a structural schematic diagram of a data query device based on a relational database provided by an embodiment of the present invention;
[0031] Figure 7 The block diagram is a block diagram of an electronic device for implementing the data query method based on a relational database according to an embodiment of the present invention.
[0032] The above drawings show clear embodiments of the present disclosure, which will be described in more detail below. These drawings and text descriptions are not intended to limit the scope of the present disclosure in any way, but to illustrate the concepts of the present disclosure to those skilled in the art by referring to specific embodiments. DETAILED DESCRIPTION
[0033] Exemplary embodiments will be described in detail herein, examples of which are shown in the accompanying drawings. When the following description refers to the drawings, the same numbers in different drawings represent the same or similar elements unless otherwise indicated. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present disclosure. Instead, they are merely examples of devices and methods consistent with some aspects of the present disclosure as detailed in the appended claims.
[0034] In order to clearly understand the technical solution of the present invention, the solution of the prior art is introduced in detail.
[0035] There are two main ways to provide SQL. The first is for the user to write SQL. The other way to provide SQL is to provide the user with a SQL generation interface, where the user generates SQL by dragging fields.
[0036] Specifically, in the first method of providing SQL, users can write SQL in a custom SQL interface. However, due to capability limitations, some users only know how to use SQL for traditional relational databases such as Oracle. However, due to certain differences between the SQL syntax of relational databases such as Hive and Spark and Oracle, the user's SQL may not run or have poor performance. Therefore, the first method of providing SQL requires users to have strong SQL writing capabilities. In addition, when writing SQL, users need to be familiar with the correspondence between the information to be queried and the fields in the database table before they can write SQL, which makes the generation of SQL cumbersome and inefficient.
[0037] In the second method of providing SQL, a SQL generation interface is provided to the user. The fields in the database table are displayed in the SQL generation interface. The user drags the fields in the SQL generation interface, and the backend automatically generates SQL based on the fields dragged by the user. Although this method lowers the user's usage threshold, it has the problem of insufficient expression ability. In addition, since the drag-and-drop method requires a large workspace, the space on the mobile terminal is very limited, so this method of providing SQL cannot be used on the mobile terminal.
[0038] Therefore, in view of the technical problems in the prior art, the inventors found through research that in order to lower the user's usage threshold, allowing users to directly input the most accustomed natural language search statement is the lowest usage threshold. Then, after obtaining the natural language search statement, in order to generate SQL, it is necessary to pre-build a dictionary corresponding to the database, and store the mapping relationship between words and fields in the database table in the dictionary. The dictionary can be used to perform word segmentation on the natural language query statement to obtain the target word segmentation result. Then the target word segmentation result is subjected to grammatical analysis to obtain the grammatical analysis result; and the target syntax tree corresponding to the natural language query statement is generated according to the grammatical analysis result. The target syntax tree is an abstract syntax tree format that can be converted to and from SQL, so by traversing the target syntax tree, the target structured query language SQL corresponding to the natural language query statement is generated. Since the user inputs a natural language search statement, the user's usage threshold can be effectively lowered. It can also be used on the mobile terminal, and the search data can be fully expressed. Since the process from the natural language search statement to the generation of SQL does not require user operation, the generation efficiency of SQL can be effectively improved.
[0039] Therefore, based on the above creative findings, the inventors have proposed a technical solution of the embodiment of the present invention. The network structure and application scenarios of the data query method based on a relational database provided by the embodiment of the present invention are introduced below.
[0040] like Figure 1 As shown, the network architecture corresponding to the data query method based on the relational database provided by the embodiment of the present invention includes: an electronic device 1 and multiple relational databases. Figure 1 It may include three relational databases. They are Oracle database 2, MySQL database 3 and Hive database 4. Each relational database can be stored in a server. Electronic device 1 can access each relational database. A client corresponding to a data query method based on a relational database is provided on the electronic device 1, or a web page corresponding to a data query method based on a relational database can be searched through a browser. The user can input a natural language query statement through the client or the web page. After the electronic device 1 receives the natural language query statement input by the user and clicks on the search component, the data query method based on a relational database provided in an embodiment of the present invention generates a target structured query language SQL corresponding to the natural language query statement. SQL is used to search for data in the target database corresponding to the target table identifier. For example, in Figure 1 In the example, the target database is the hive database 4. The data search results are obtained from the hive database 4 and can also be displayed in the form of a pre-configured chart.
[0041] The technical solution of the present invention and how the technical solution of the present invention solves the above-mentioned technical problems are described in detail below with specific embodiments. The following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments. The embodiments of the present invention will be described below in conjunction with the accompanying drawings.
[0042] Embodiment 1
[0043] Figure 2 FIG. 1 is a flow chart of a data query method based on a relational database provided by an embodiment of the present invention. Figure 2 As shown, the execution subject of the data query method based on a relational database provided in the embodiment of the present invention is a data query device based on a relational database. The data query device based on a relational database is located in an electronic device. Then the data query method based on a relational database provided in this embodiment includes the following steps:
[0044] Step 201: receiving a natural language query statement input by a user.
[0045] In this embodiment, the electronic device may be loaded with a client or browser of the data query method based on a relational database provided in this embodiment. The web page corresponding to the data query method based on a relational database provided in this embodiment may be accessed through the browser. The user opens the client or web page. And enters a natural language query statement in the search box of the client or web page. After clicking the confirmation component, the electronic device receives the natural language query statement entered by the user.
[0046] The natural language query statement is a colloquial statement that conforms to human expression habits. And the natural language query statement is a statement for data query. For example, the input natural language query statement may be: how many visits are there in each region of a certain application.
[0047] Step 202, segmenting the natural language query statement according to the pre-built dictionary corresponding to the database to obtain a target segmentation result; the dictionary stores the mapping relationship between words and fields in the table of the database.
[0048] In this embodiment, metadata is stored in at least one database in the form of a table. The metadata has various fields and values corresponding to the various fields. In this embodiment, a corresponding dictionary is first constructed according to the metadata stored in each database table.
[0049] For example, the metadata of a table stores the number of visits to a certain application in different regions. As shown in Table 1:
[0050] Table 1: Example table of metadata
[0051] area uv(visit volume) Beijing 123 Sichuan 456
[0052] Then, in constructing the dictionary, the mapping relationship from words to fields in the table can be stored in the form of key-value pairs (referred to as: key-value). The fields in the metadata can be determined as fields in the dictionary. The values of the fields in the metadata are determined as words in the dictionary. For example, for the metadata in Table 1, the mapping relationship from words to fields stored in the dictionary may include: Beijing->a.area, Sichuan->a.area, visits->a.uv. Among them, "Beijing", "Sichuan", and "visits" are words, and "a.area" and "a.uv" are fields.
[0053] Specifically, in this embodiment, a preset word segmentation algorithm can be used to perform word segmentation processing on the natural language query statement to obtain an initial word segmentation result. The initial word segmentation result is matched with the fields in the dictionary corresponding to the pre-constructed database. If it is determined that the initial word segmentation result matches the fields in the dictionary, the initial word segmentation result is determined as the target word segmentation result. If it is determined that the initial word segmentation result does not match the fields in the dictionary, other word segmentation algorithms can be used to perform word segmentation processing on the natural language query statement. Similarly, the word segmentation result is also matched with the fields in the dictionary. Then the target word segmentation result is determined based on the matching result.
[0054] It is understandable that in this embodiment, the implementation method of performing word segmentation processing on the natural language query statement according to the dictionary corresponding to the pre-built database to obtain the target word segmentation result can also be other implementation methods, which are not limited in this embodiment.
[0055] Step 203: Perform grammatical analysis on the target word segmentation result to obtain a grammatical analysis result.
[0056] In this embodiment, a syntax analysis rule may be pre-constructed, and the pre-constructed syntax analysis rule may be used to perform syntax analysis on the target word segmentation result to obtain a syntax analysis result.
[0057] The grammatical analysis result includes whether there is a grammatical segment matching the grammatical analysis rule. If there is a grammatical segment matching the grammatical analysis rule, the words matching the grammatical analysis rule in the target word segmentation result can be combined to form a grammatical segment. If there is no grammatical segment matching the grammatical analysis rule, there is no need to combine the words in the target word segmentation result.
[0058] Step 204: Generate a target syntax tree corresponding to the natural language query statement according to the syntax analysis result.
[0059] The target syntax tree is in an abstract syntax tree format that matches SQL, and is composed of at least a root node, multiple child nodes, and leaf nodes of the child nodes.
[0060] In this embodiment, according to whether there is a grammatical fragment matching the grammatical analysis rule in the grammatical analysis result, the operator and operand when searching the target database are first determined. Then, according to the fields in the dictionary matched in the target word segmentation result, the target table and target field in the database to be searched are determined. According to the target table, target field, operator and operand, a target grammar tree corresponding to the natural language query statement is generated.
[0061] Step 205: traverse the target syntax tree to generate a target structured query language SQL corresponding to the natural language query statement.
[0062] In this embodiment, the target syntax tree includes the target table to be searched, the target field to be searched, the operator and operand required for the search. Therefore, after traversing the target syntax tree, it is converted into the target structured query language SQL corresponding to the natural language query statement.
[0063] The data query method based on a relational database provided in this embodiment receives a natural language query statement input by a user; performs word segmentation processing on the natural language query statement according to a dictionary corresponding to a pre-built database to obtain a target word segmentation result; the dictionary stores a mapping relationship between words and fields in a table of the database; performs syntax analysis on the target word segmentation result to obtain a syntax analysis result; generates a target syntax tree corresponding to the natural language query statement according to the syntax analysis result; and traverses the target syntax tree to generate a target structured query language SQL corresponding to the natural language query statement. Since the user only needs to input a natural language query statement, the natural language query statement can be segmented and analyzed according to the dictionary corresponding to the pre-built database, and the corresponding target syntax tree can be generated, and the SQL language can be automatically generated by traversing the target syntax tree, so the user threshold can be effectively lowered. It can also be used on a mobile terminal, and the search data can be fully expressed. Since the process from natural language search statements to SQL generation does not require user operation, the SQL generation efficiency can be effectively improved.
[0064] Embodiment 2
[0065] Figure 3 FIG. 1 is a flow chart of a data query method based on a relational database provided by another embodiment of the present invention. Figure 3 As shown, the data query method based on a relational database provided in this embodiment is based on the data query method based on a relational database provided in the first embodiment, and further refines step 202, specifically including steps 301 to 308. The data query method based on a relational database provided in this embodiment includes the following steps:
[0066] Step 301: Use a preset word segmentation algorithm to perform word segmentation processing on a natural language query sentence to obtain an initial word segmentation result.
[0067] The preset word segmentation algorithm is the word segmentation algorithm with the highest priority. For example, the preset word segmentation algorithm may be a maximum matching rule word segmentation algorithm.
[0068] In this embodiment, the natural language query sentence is input into a preset word segmentation algorithm, and the preset word segmentation algorithm performs word segmentation processing on the natural language query sentence to obtain an initial word segmentation result, and outputs the initial word segmentation result.
[0069] There may be at least one initial word segmentation result.
[0070] Step 302, matching the initial word segmentation result with words in the dictionary.
[0071] Step 303, determine whether the initial segmentation result matches the field in the word. If yes, execute step 304, otherwise, execute step 306.
[0072] In this embodiment, for each initial word segmentation result, it is queried whether the words segmented in the initial word segmentation result have matching words in the dictionary. If it is determined that there are matching words, it is further determined whether the number of initial word segmentation results is one or more. If it is determined that there are no matching words, the backup word segmentation algorithm is used to perform word segmentation processing on the natural language query sentence to obtain the backup word segmentation result.
[0073] The priority of the backup word segmentation algorithm is lower than the priority of the preset word segmentation algorithm. For example, the backup word segmentation algorithm may be a word segmentation algorithm based on the minimum number of words.
[0074] Step 304, determine whether the initial word segmentation result is one, if so, execute step 305, otherwise execute step 306.
[0075] Step 305: determine the initial word segmentation result as the target word segmentation result.
[0076] In this embodiment, if it is determined that there is one initial word segmentation result and the initial word segmentation result matches a word in the dictionary, the initial word segmentation result is determined as the target word segmentation result.
[0077] Specifically, in this embodiment, if it is determined that there is one initial segmentation result, it means that after the natural language search sentence is segmented using the preset segmentation algorithm with the highest priority, the initial segmentation result not only matches the words in the dictionary, but also has only one initial segmentation result, so that the initial segmentation result is unambiguous. Therefore, the initial segmentation result is determined as the target segmentation result.
[0078] Step 306: Use a backup word segmentation algorithm to perform word segmentation processing on the natural language query sentence to obtain a backup word segmentation result.
[0079] In this embodiment, if it is determined that there are multiple initial word segmentation results and the initial word segmentation results all match the words in the dictionary, a backup word segmentation algorithm is used to perform word segmentation processing on the natural language query sentence to obtain a backup word segmentation result.
[0080] Specifically, in this embodiment, if it is determined that there are multiple initial segmentation results, it means that after the natural language search sentence is segmented using the preset segmentation algorithm with the highest priority, although the initial segmentation results all match the words in the dictionary, the initial segmentation results are multiple, making the initial segmentation results ambiguous. Therefore, a backup segmentation algorithm with a lower priority is further used to segment the natural language query sentence to obtain a backup segmentation result.
[0081] It is understandable that there may be multiple backup word segmentation algorithms, and the priority levels are different.
[0082] Step 307: Match the standby word segmentation result with the words in the dictionary.
[0083] In this embodiment, when the standby word segmentation result is used to match the words in the dictionary, it is still queried whether the words cut out in the standby word segmentation result have matching words in the dictionary. If it is determined that there are, it is further determined whether the number of standby word segmentation results is one or more. If it is determined that there are not, the next lower priority standby word segmentation algorithm is used to perform word segmentation processing on the natural language query sentence to obtain the standby word segmentation result. This is done until there is one standby word segmentation result.
[0084] Step 308: If it is determined that there is only one backup word segmentation result and the backup word segmentation result matches the words in the dictionary, the backup word segmentation result is determined as the target word segmentation result.
[0085] In this embodiment, if it is determined that there is only one backup word segmentation result, it means that after the natural language search sentence is segmented using the backup word segmentation algorithm with a lower priority, the backup word segmentation result not only matches the words in the dictionary, but also has only one backup word segmentation result, so that the backup word segmentation result is unambiguous. Therefore, the backup word segmentation result is determined as the target word segmentation result.
[0086] The data query method based on a relational database provided in the present embodiment performs word segmentation processing on a natural language query statement according to a dictionary corresponding to a pre-constructed database to obtain a target word segmentation result, and then uses a preset word segmentation algorithm to perform word segmentation processing on the natural language query statement to obtain an initial word segmentation result, and matches the initial word segmentation result with words in the dictionary; if it is determined that the initial word segmentation result is one and the initial word segmentation result matches the words in the dictionary, the initial word segmentation result is determined as the target word segmentation result; if it is determined that the initial word segmentation result is multiple and the initial word segmentation result matches the words in the dictionary, then a backup word segmentation algorithm is used to perform word segmentation processing on the natural language query statement to obtain a backup word segmentation result, and the backup word segmentation result is matched with the words in the dictionary; if it is determined that the backup word segmentation result is one and the backup word segmentation result matches the words in the dictionary, then the backup word segmentation result is determined as the target word segmentation result. Since the initial segmentation results obtained by the segmentation algorithm with a higher priority are multiple, the segmentation results will be ambiguous. Therefore, in order to obtain accurate segmentation results, when the segmentation results obtained by the segmentation algorithm with a higher priority are not unique, the segmentation algorithm with a lower priority is used to continue the segmentation process until a unique segmentation result is obtained, and the unique segmentation result is determined as the target segmentation result. This can effectively improve the accuracy of segmentation.
[0087] Embodiment 3
[0088] Figure 4 FIG. 1 is a flow chart of a data query method based on a relational database provided by another embodiment of the present invention. Figure 4 As shown, the data query method based on a relational database provided in this embodiment further includes other steps on the basis of the data query method based on a relational database provided in any of the above embodiments. The data query method based on a relational database provided in this embodiment includes the following steps:
[0089] Step 401: receiving a natural language query statement input by a user.
[0090] The implementation of step 401 provided in this embodiment is similar to the implementation of step 201 in the first embodiment, and will not be described in detail here.
[0091] Step 402, segmenting the natural language query statement according to the pre-built dictionary corresponding to the database to obtain a target segmentation result; the dictionary stores the mapping relationship between words and fields in the table of the database.
[0092] In this embodiment, the implementation of step 402 is similar to the implementation of step 202 in embodiment 1 or steps 301-308 in embodiment 2, and will not be described in detail here.
[0093] Step 403: determine the target field that matches the target word segmentation result in the dictionary and the identifier of the target table where the target field is located.
[0094] Specifically, in this embodiment, since the target word segmentation result is obtained by using a certain word segmentation algorithm to perform word segmentation processing on a natural language query statement, and then matching the word segmentation result with the words in the dictionary, the words that match the target word segmentation result in the dictionary are obtained, and the fields that have a mapping relationship with the matched words are obtained, and the fields that have a mapping relationship are determined as the target fields. The dictionary also includes the table to which each field belongs. Therefore, the identifier of the target table is determined according to the target field.
[0095] Step 404: perform grammatical analysis on the target word segmentation result to obtain a grammatical analysis result.
[0096] As an optional implementation, in this embodiment, step 404 includes the following steps:
[0097] Step 4041, matching the words in the target word segmentation result with the keywords of each grammatical analysis rule in the pre-constructed grammatical analysis rule set.
[0098] The preset syntax analysis rule set includes a variety of syntax analysis rules. For example, one syntax analysis rule is {date}{to}{date}, which represents a time range.
[0099] The keywords in each grammatical analysis rule may be words at specified positions in the grammatical analysis rule. For example, in the grammatical analysis rule in the above example, the words at three positions are all keywords.
[0100] In this embodiment, the words in the target word segmentation result are matched with the keywords of each grammatical analysis rule to determine whether they match the keywords of one or more grammatical analysis rules.
[0101] For example, the target segmentation result is {visits from {June 1st}{to}{June 8th}. Then {June 1st}{to}{June 8th} in the target segmentation result matches the three keywords {date}{to}{date} of the grammatical analysis rule.
[0102] Step 4042: If it is determined that the words in the target word segmentation result match the keywords in a syntax analysis rule, it is determined that there are grammatical fragments in the syntax analysis result.
[0103] In this embodiment, the words in the target word segmentation result are matched with the keywords of each grammatical analysis rule. If it is determined that the words in the target word segmentation result match the keywords in a grammatical analysis rule, it means that the matched words in the target word segmentation result satisfy this grammatical analysis rule, which means that there are grammatical fragments in the grammatical analysis result that match the keywords in this grammatical analysis rule.
[0104] Continuing with the above example, {June 1st}{to}{June 8th} matches the three keywords {date}{to}{date} of the grammatical analysis rule, so {June 1st}{to}{June 8th} is combined into a grammatical fragment.
[0105] Step 4043: If it is determined that the words in the target word segmentation result do not match the keywords in any grammatical analysis rule, it is determined that there is no grammatical fragment in the grammatical analysis result.
[0106] In this embodiment, the words in the target word segmentation result are matched with the keywords of each grammatical analysis rule. If it is determined that the words in the target word segmentation result do not match the keywords in any grammatical analysis rule, it means that the target word segmentation result does not have several consecutive words that satisfy any grammatical analysis rule, which means that there is no grammatical fragment in the grammatical analysis result that matches the keywords in this grammatical analysis rule, and there is no need to combine the words in the target word segmentation result in any form.
[0107] Step 405 , generating a target syntax tree corresponding to the natural language query statement according to the syntax analysis result, the target table and the target field corresponding to the target word segmentation result.
[0108] In this embodiment, step 405 is an optional implementation of step 204.
[0109] As an optional implementation, step 405 includes the following steps:
[0110] Step 4051, identify the target table as the root node of the target syntax tree.
[0111] In this embodiment, since the identifier of the target table where the target field matching the target word segmentation result in the dictionary is located is determined in step 403, the target table identifier is determined as the root node of the target syntax tree to represent the identifier of the table to be searched by the natural language search statement.
[0112] The identifier of the target table may be the name of the target table, a serial number in the target database, and other information.
[0113] For example, Figure 5As shown, when the natural language search statement is "What is the traffic volume in Sichuan?", and the target table is Table A, the root node of the target syntax tree is "Table A".
[0114] Step 4052: Determine the target field as the child node of the root node.
[0115] In this embodiment, the target field indicates the field that should be searched in the corresponding target table when searching the natural language search statement. To facilitate the smooth conversion of the natural language search statement into SQL, in the target syntax tree, the target field is used as the child node of the root node.
[0116] Exemplarily, as Figure 5 shown, the child nodes of the root node are the target fields "a.area" and "a.uv".
[0117] Step 4053: Determine the target word or the grammar fragment in the target word segmentation result that matches the target word segmentation result as the leaf node of the child node.
[0118] In this embodiment, after determining the target field, it is necessary to determine the operand and the operator. Among them, the operand is at least one target word or at least one word in the grammar fragment in the target word segmentation result that matches the target word segmentation result. The operator is at least one word in the target word or the grammar fragment in the target word segmentation result that matches the target word segmentation result, or is transformed from at least one word in the target word or the grammar fragment in the target word segmentation result that matches the target word segmentation result.
[0119] Exemplarily, continuing with the above example. Since there is no grammar fragment in the above example, the operand is "Sichuan", and the operator is transformed from "of", which is "=".
[0120] Exemplarily, for the case including grammar fragments, such as {June 1st}{to}{June 8th}, the operands are "June 1st" and "June 8th", and the operator is "to".
[0121] In this embodiment, the target word segmentation result includes words that match the dictionary and target fields that have a mapping relationship with the matching words. Therefore, after giving the target field in the target syntax tree, the target word having a mapping relationship with the target field is given.
[0122] Step 4054: Generate the target syntax tree corresponding to the natural language query statement according to the root node, the child nodes of the root node, and the leaf nodes of the child nodes.
[0123] In this embodiment, the content of each node is added in the order of the root node, the child nodes of the root node, and the leaf nodes of the child nodes, and the relationships between the nodes are established through connections to generate a target syntax tree corresponding to the natural language query statement.
[0124] Exemplarily, in Figure 5 the target syntax tree, the root node is "Table a". The child nodes of the root node are "a.area" and "a.uv". The leaf nodes of "a.area" are "operand" and "operator". The operand is "Sichuan", and the operator is "=".
[0125] Step 406: Traverse the target syntax tree to generate a target structured query language SQL corresponding to the natural language query statement.
[0126] In this embodiment, since the target syntax tree is in the format of an abstract syntax tree. The abstract syntax tree and SQL are converted according to fixed rules. The node information in the target syntax tree is used during the conversion. Therefore, after traversing the target syntax tree, the node information in the target syntax tree is obtained and added to the preset positions in the SQL, and then the SQL can be generated.
[0127] As in the above exemplary description, the corresponding SQL can be expressed as: select a.area,a.uv from a where a.area = 'Sichuan'.
[0128] Step 407: Use the SQL to search for data in the target database corresponding to the target table identifier.
[0129] In this embodiment, since the SQL includes the target table identifier, the target database where the target table is located is obtained, and by accessing the target database, the SQL is used to search for the answer corresponding to the natural language search statement to obtain the data search result. After the result is found in the target database, the data search result is fed back to the electronic device.
[0130] Step 408: Obtain the data search result and display it in a pre-configured chart form.
[0131] In this embodiment, the user can pre-configure the display form of the data search result in the client or web page of the data query method based on the relational database. After obtaining the data search result, the data search result is converted into a pre-configured chart form and displayed.
[0132] Among them, the pre-configured display form can be in chart format. For example, it can be a bar chart, a line chart, a pie chart, a funnel chart, a map, etc.
[0133] The data query method based on a relational database provided in this embodiment, when generating a target syntax tree corresponding to a natural language query statement according to the syntax analysis result, the target table and the target field corresponding to the target word segmentation result, the target table identifier is determined as the root node of the target syntax tree; the target field is determined as the child node of the root node; the target word matching the target word segmentation result or the syntax fragment in the target word segmentation result is determined as the leaf node of the child node; the target syntax tree corresponding to the natural language query statement is generated according to the root node, the child node of the root node and the leaf node of the child node. The data required for the search can be fully displayed in the target syntax tree, and the target syntax tree is in the form of an abstract syntax tree, so after traversing the target syntax tree, the corresponding SQL can be automatically generated. The generation efficiency of SQL is further improved.
[0134] The data query method based on a relational database provided in this embodiment, after traversing the target syntax tree to generate the target structured query language SQL corresponding to the natural language query statement, uses SQL to search for data in the target database corresponding to the target table identifier; obtains the data search results, and displays them in a pre-configured chart form. Since the data search results can be displayed in the pre-configured chart form, the display method of the data search results can be made more in line with the needs of users.
[0135] Optionally, before the user uses the data query method based on a relational database provided by the implementation of the present invention to query data, the access rights of the database can also be configured. When the user enters a natural language query statement, the user identification information is obtained, and after the natural language query statement is processed, the target database is determined. It is determined whether the user has access rights to the target database based on the pre-configured permission information, and after the user has access rights to the target database, the data search results are displayed. If the user does not have access rights to the target database, a notification of lack of access rights is fed back, which can effectively ensure the security of the data in the database.
[0136] Embodiment 4
[0137] Figure 6 FIG. 1 is a schematic diagram of a data query device based on a relational database according to an embodiment of the present invention. Figure 6 As shown, the data query device based on relational database provided in this embodiment is located in the electronic device. Then the data query device based on relational database provided in this embodiment 60 includes: a receiving module 61, a word segmentation module 62, an analysis module 63, a generation module 64, and a traversal module 65.
[0138] Among them, the receiving module 61 is used to receive the natural language query statement input by the user. The word segmentation module 62 is used to perform word segmentation processing on the natural language query statement according to the dictionary corresponding to the pre-constructed database to obtain the target word segmentation result; the dictionary stores the mapping relationship between words and fields in the table of the database. The analysis module 63 is used to perform syntax analysis on the target word segmentation result to obtain the syntax analysis result. The generation module 64 is used to generate the target syntax tree corresponding to the natural language query statement according to the syntax analysis result. The traversal module 65 is used to traverse the target syntax tree to generate the target structured query language SQL corresponding to the natural language query statement.
[0139] The data query device based on the relational database provided in this embodiment can be executed Figure 2 The technical solution of the method embodiment shown in the figure has the same implementation principle and technical effect as Figure 2 The method embodiments shown are similar and will not be described in detail here.
[0140] Optionally, the word segmentation module 62 is specifically used for:
[0141] A preset word segmentation algorithm is used to segment the natural language query sentence to obtain an initial word segmentation result; the initial word segmentation result is matched with words in a dictionary; if it is determined that the initial word segmentation result is one and the initial word segmentation result matches the words in the dictionary, the initial word segmentation result is determined as the target word segmentation result.
[0142] Optionally, the word segmentation module 62 is further used for:
[0143] If it is determined that there are multiple initial word segmentation results and the initial word segmentation results match the words in the dictionary, a backup word segmentation algorithm is used to segment the natural language query statement to obtain a backup word segmentation result; the backup word segmentation result is matched with the words in the dictionary; if it is determined that there is one backup word segmentation result and the backup word segmentation result matches the words in the dictionary, the backup word segmentation result is determined as the target word segmentation result; the priority of the backup word segmentation algorithm is lower than the priority of the preset word segmentation algorithm.
[0144] Optionally, the word segmentation module 62 is further used for:
[0145] If it is determined that the initial word segmentation result does not match the words in the dictionary, the step of using a backup word segmentation algorithm to perform word segmentation processing on the natural language query sentence to obtain a backup word segmentation result is executed.
[0146] Optionally, the data query device based on the relational database provided in this embodiment further includes:
[0147] The determination module is used to determine the target field that matches the target word segmentation result in the dictionary and the identifier of the target table where the target field is located.
[0148] Optionally, the analysis module 63 is specifically configured to:
[0149] The words in the target word segmentation result are matched with the keywords of each grammatical analysis rule in the pre-constructed grammatical analysis rule set; if it is determined that the words in the target word segmentation result match the keywords in a grammatical analysis rule, it is determined that there are grammatical fragments in the grammatical analysis result; if it is determined that the words in the target word segmentation result do not match the keywords in any grammatical analysis rule, it is determined that there are no grammatical fragments in the grammatical analysis result.
[0150] Optionally, the generating module 64 is specifically configured to:
[0151] Generate a target syntax tree corresponding to the natural language query statement based on the syntax analysis results, the target table and the target field corresponding to the target word segmentation results.
[0152] Optionally, when the generating module 64 generates a target syntax tree corresponding to the natural language query statement according to the syntax analysis result and the target table and target field corresponding to the target word segmentation result, it is specifically used to:
[0153] The target table identifier is determined as the root node of the target syntax tree; the target field is determined as the child node of the root node; the target word matching the target word segmentation result or the syntax fragment in the target word segmentation result is determined as the leaf node of the child node; and the target syntax tree corresponding to the natural language query statement is generated according to the root node, the child nodes of the root node and the leaf nodes of the child nodes.
[0154] Optionally, the data query device based on the relational database provided in this embodiment further includes:
[0155] The search module is used to search for data in the target database corresponding to the target table identifier using SQL. The display module is used to obtain the data search result and display it in the form of a pre-configured chart.
[0156] The data query device based on the relational database provided in this embodiment can be executed Figure 3-Figure 4 The technical solution of the method embodiment shown in the figure has the same implementation principle and technical effect as Figure 3-Figure 4 The method embodiments shown are similar and will not be described in detail here.
[0157] Embodiment 5
[0158] Embodiment 8 of the present invention provides an electronic device, at least one processor and a memory.
[0159] Specifically, the memory stores computer-executable instructions; at least one processor executes the computer-executable instructions stored in the memory, so that the at least one processor executes the data query method based on the relational database provided in any one of the above embodiments.
[0160] Embodiment 6
[0161] Figure 7 is a block diagram of an electronic device for implementing the data query method based on a relational database according to an embodiment of the present invention, such as Figure 7 As shown, the electronic device provided in this embodiment can be a digital computer in various forms, such as a laptop computer, a desktop computer, a workbench, a personal digital assistant, a server, a blade server, a mainframe computer, and other suitable computers. It can also be a mobile device, such as a mobile phone, a computer, a digital broadcast terminal, a message transceiver device, a game console, a tablet device, a medical device, a fitness device, a personal digital assistant, etc.
[0162] The electronic device 700 may include one or more of the following components: a processing component 702 , a memory 704 , a power component 706 , a multimedia component 708 , an audio component 710 , an input / output (I / O) interface 712 , a sensor component 714 , and a communication component 716 .
[0163] The processing component 702 generally controls the overall operation of the electronic device 700, such as operations associated with display, phone calls, data communications, camera operations, and recording operations. The processing component 702 may include one or more processors 720 to execute instructions to complete all or part of the steps of the above-mentioned method. In addition, the processing component 702 may include one or more modules to facilitate the interaction between the processing component 702 and other components. For example, the processing component 702 may include a multimedia module to facilitate the interaction between the multimedia component 708 and the processing component 702.
[0164] The memory 704 is configured to store various types of data to support operations on the electronic device 700. Examples of such data include instructions for any application or method operating on the electronic device 700, contact data, phone book data, messages, pictures, videos, etc. The memory 704 can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk or optical disk.
[0165] The power supply component 706 provides power to the various components of the electronic device 700. The power supply component 706 may include a power management system, one or more power supplies, and other components associated with generating, managing, and distributing power to the electronic device 700.
[0166] The multimedia component 708 includes a screen that provides an output interface between the electronic device 700 and the user. In some embodiments, the screen may include a liquid crystal display (LCD) and a touch panel (TP). If the screen includes a touch panel, the screen may be implemented as a touch screen to receive input signals from the user. The touch panel includes one or more touch sensors to sense touch, slide, and gestures on the touch panel. The touch sensor may not only sense the boundaries of the touch or slide action, but also detect the duration and pressure associated with the touch or slide operation. In some embodiments, the multimedia component 708 includes a front camera and / or a rear camera. When the electronic device 700 is in an operating mode, such as a shooting mode or a video mode, the front camera and / or the rear camera may receive external multimedia data. Each front camera and rear camera may be a fixed optical lens system or have a focal length and optical zoom capability.
[0167] The audio component 710 is configured to output and / or input audio signals. For example, the audio component 710 includes a microphone (MIC), and when the electronic device 700 is in an operating mode, such as a call mode, a recording mode, and a voice recognition mode, the microphone is configured to receive an external audio signal. The received audio signal can be further stored in the memory 704 or sent via the communication component 716. In some embodiments, the audio component 710 also includes a speaker for outputting audio signals.
[0168] I / O interface 712 provides an interface between processing component 702 and peripheral interface modules, such as keyboards, click wheels, buttons, etc. These buttons may include but are not limited to: a home button, a volume button, a start button, and a lock button.
[0169] The sensor assembly 714 includes one or more sensors for providing various aspects of status assessment for the electronic device 700. For example, the sensor assembly 714 can detect the open / closed state of the electronic device 700, the relative positioning of components, such as the display and keypad of the electronic device 700, and the sensor assembly 714 can also detect the position change of the electronic device 700 or a component of the electronic device 700, the presence or absence of user contact with the electronic device 700, the orientation or acceleration / deceleration of the electronic device 700, and the temperature change of the electronic device 700. The sensor assembly 714 may include a proximity sensor configured to detect the presence of nearby objects without any physical contact. The sensor assembly 714 may also include an optical sensor, such as a CMOS or CCD image sensor, for use in imaging applications. In some embodiments, the sensor assembly 714 may also include an acceleration sensor, a gyroscope sensor, a magnetic sensor, a pressure sensor, or a temperature sensor.
[0170] The communication component 716 is configured to facilitate wired or wireless communication between the electronic device 700 and other devices. The electronic device 700 can access a wireless network based on a communication standard, such as WiFi, 2G or 3G, or a combination thereof. In an exemplary embodiment, the communication component 716 receives a broadcast signal or broadcast-related information from an external broadcast management system via a broadcast channel. In an exemplary embodiment, the communication component 716 also includes a near field communication (NFC) module to facilitate short-range communication. For example, the NFC module can be implemented based on radio frequency identification (RFID) technology, infrared data association (IrDA) technology, ultra-wideband (UWB) technology, Bluetooth (BT) technology and other technologies.
[0171] In an exemplary embodiment, the electronic device 700 may be implemented by one or more application-specific integrated circuits (ASICs), digital signal processors (DSPs), digital signal processing devices (DSPDs), programmable logic devices (PLDs), field programmable gate arrays (FPGAs), controllers, microcontrollers, microprocessors, or other electronic components to perform the above methods.
[0172] In an exemplary embodiment, a non-transitory computer-readable storage medium including instructions is also provided, such as a memory 704 including instructions, and the instructions can be executed by a processor 720 of an electronic device 700 to perform the above method. For example, the non-transitory computer-readable storage medium can be a ROM, a random access memory (RAM), a CD-ROM, a magnetic tape, a floppy disk, an optical data storage device, etc.
[0173] A non-temporary computer-readable storage medium, when instructions in the storage medium are executed by a processor of an electronic device, enables the electronic device to execute the data query method based on a relational database of the electronic device.
[0174] In an exemplary embodiment, a computer program product is further provided, including a computer program, wherein a processor executes the data query method based on a relational database in any one of the above embodiments.
[0175] Those skilled in the art will readily appreciate other embodiments of the present disclosure after considering the specification and practicing the invention disclosed herein. The present invention is intended to cover any variations, uses or adaptations of the present disclosure that follow the general principles of the present disclosure and include common knowledge or customary techniques in the art that are not disclosed in the present disclosure. The description and examples are to be regarded as exemplary only, and the true scope and spirit of the present disclosure are indicated by the following claims.
[0176] It should be understood that the present disclosure is not limited to the exact structures that have been described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present disclosure is limited only by the appended claims.
Claims
1. A data query method based on a relational database, characterized in that: include: Receive a natural language query statement input by a user; Using a preset word segmentation algorithm to perform word segmentation processing on the natural language query sentence to obtain an initial word segmentation result; Matching the initial word segmentation result with words in a dictionary corresponding to a pre-constructed database to obtain a matching result corresponding to the initial word segmentation result; the dictionary stores a mapping relationship between words and fields in a table of the database; If it is determined that the initial word segmentation result is one and the matching result corresponding to the initial word segmentation result is matched, the initial word segmentation result is determined as the target word segmentation result; If it is determined that there are multiple initial word segmentation results and the matching results corresponding to each initial word segmentation result are all matched, or if it is determined that the matching result corresponding to the initial word segmentation result is not matched, then one of the multiple backup word segmentation algorithms is used to re-segment the natural language query statement to obtain a backup word segmentation result; the priority of each of the multiple backup word segmentation algorithms is lower than the priority of the preset word segmentation algorithm, and the priorities of the multiple backup word segmentation algorithms are different from each other; Matching the standby word segmentation result with words in the dictionary to obtain a matching result corresponding to the standby word segmentation result; According to the number of the standby word segmentation results and the matching results corresponding to the standby word segmentation results, determining whether to return to the step of re-segmenting the natural language query statement using a standby word segmentation algorithm with a next lower priority among the multiple standby word segmentation algorithms; Until it is determined that there is one backup word segmentation result and the matching result corresponding to the backup word segmentation result matches, the current backup word segmentation result is determined as the target word segmentation result; Determine a target field that matches the target word segmentation result in the dictionary and a target table identifier of a target table where the target field is located; Performing grammatical analysis on the target word segmentation result to obtain a grammatical analysis result; Determine the target table identifier as a root node, determine the target field as a child node of the root node, and determine the target word matching the target word segmentation result or the grammatical fragment in the target word segmentation result as a leaf node of the child node; Generate a target syntax tree corresponding to the natural language query statement according to the root node, the child nodes of the root node and the leaf nodes of the child nodes; The target syntax tree is traversed to generate a target structured query language SQL corresponding to a natural language query statement; wherein the SQL is used to search for data in a target database, the target database has pre-configured permission information, and the permission information is used to determine whether the user has access rights to the target database.
2. The method according to claim 1, characterized in that The step of performing grammatical analysis on the target word segmentation result to obtain a grammatical analysis result includes: Matching the words in the target word segmentation result with the keywords of each grammatical analysis rule in the pre-built grammatical analysis rule set; If it is determined that a word in the target word segmentation result matches a keyword in a grammatical analysis rule, it is determined that a grammatical fragment exists in the grammatical analysis result; If it is determined that the words in the target word segmentation result do not match the keywords in any of the grammatical analysis rules, it is determined that there is no grammatical fragment in the grammatical analysis result.
3. The method according to claim 1 or 2, characterized in that: After traversing the target syntax tree to generate a target structured query language SQL corresponding to a natural language query statement, the method further includes: Use SQL to search for data in the target database corresponding to the target table identifier; Get data search results and display them in pre-configured charts.
4. A data query device based on a relational database, characterized in that: include: A receiving module, used for receiving a natural language query statement input by a user; A word segmentation module, used to perform word segmentation processing on the natural language query sentence using a preset word segmentation algorithm to obtain an initial word segmentation result; The initial word segmentation result is matched with words in a dictionary corresponding to a pre-constructed database to obtain a matching result corresponding to the initial word segmentation result; the dictionary stores a mapping relationship between words and fields in a table of the database; if it is determined that the initial word segmentation result is one and the matching result corresponding to the initial word segmentation result is matched, the initial word segmentation result is determined as the target word segmentation result; if it is determined that the initial word segmentation result is multiple and the matching result corresponding to each initial word segmentation result is matched, or if it is determined that the matching result corresponding to the initial word segmentation result is not matched, one of multiple backup word segmentation algorithms is used to segment the natural word segmentation result. The language query statement is re-segmented to obtain a backup word segmentation result; the priority of each backup word segmentation algorithm in the multiple backup word segmentation algorithms is lower than the priority of the preset word segmentation algorithm, and the priorities of the backup word segmentation algorithms in the multiple backup word segmentation algorithms are different from each other; the backup word segmentation result is matched with the words in the dictionary to obtain a matching result corresponding to the backup word segmentation result; according to the number of the backup word segmentation results and the matching result corresponding to the backup word segmentation result, it is determined whether to return to the step of re-segmenting the natural language query statement using the next backup word segmentation algorithm with a lower priority among the multiple backup word segmentation algorithms; Until it is determined that there is one backup word segmentation result and the matching result corresponding to the backup word segmentation result matches, the current backup word segmentation result is determined as the target word segmentation result; A determination module, used to determine a target field that matches the target word segmentation result in a dictionary and a target table identifier of a target table where the target field is located; An analysis module is used to perform grammatical analysis on the target word segmentation result to obtain a grammatical analysis result; A generating module, configured to determine the target table identifier as a root node, determine the target field as a child node of the root node, and determine the target word matching the target word segmentation result or the grammatical fragment in the target word segmentation result as a leaf node of the child node; Generate a target syntax tree corresponding to the natural language query statement according to the root node, the child nodes of the root node and the leaf nodes of the child nodes; A traversal module is used to traverse the target syntax tree to generate a target structured query language SQL corresponding to a natural language query statement; wherein the SQL is used to search for data in a target database, the target database has pre-configured permission information, and the permission information is used to determine whether the user has access rights to the target database.
5. An electronic device, characterized in that: include: at least one processor and memory; The memory stores computer-executable instructions; The at least one processor executes the computer-executable instructions stored in the memory, so that the at least one processor performs the method according to any one of claims 1 to 3.
6. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-executable instructions, which are used to implement the method according to any one of claims 1 to 3 when executed by a processor.
7. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the method according to any one of claims 1 to 3 is implemented.
