A natural language data query method based on basic-level governance
The natural language data query method using trie and sliding window algorithms solves the cumbersome problem of multi-condition combination queries in grassroots governance systems and achieves a convenient data search experience.
Patent Information
- Application Number
- CN202310861375.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-07-13
- Publication Date
- 2026-02-17
- Estimated Expiration
- 2043-07-13
AI Technical Summary
Traditional conditional query methods are cumbersome to operate in grassroots governance systems, and the combination of multiple conditions is difficult, resulting in inconvenient and time-consuming queries and a poor user experience.
A natural language data query method based on trie and sliding window algorithm is adopted. The semantic trie is used to verify the semantic correctness of user input in real time, generate a list of associative completion text, and realize structured parsing and SQL statement generation.
Users can input their query intent using natural language and flexibly combine conditional logic to reduce the difficulty of learning and operation, thus achieving convenient data search.
Smart Images

Figure CN116795870B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The application relates to a natural language data query method, which is applied to the fields of grassroots social governance, intelligent digital office, big data processing and the like. BACKGROUND
[0002] A traditional conditional query mode needs a technician to pre-set a condition component supporting query on a page, and with the increase of conditions, the difficulty of condition combination also increases, so that it is difficult to realize accurate combined query, for example, the logical management problem between conditions, whether to use the "and" or "or" mode to process. In order to realize general conditional query, a complex and tedious operation function needs to be designed, so that the learning and operation threshold is increased for user experience. For example, when a grassroots worker needs to query the information of high-age pension of high-age old people in each month, he needs to filter out the residents who are 80 years old and first apply for the pension in the last month on the system. When the existing query mode is used, first, the age of 80 needs to be input in the search condition, and then the pension application state of first application needs to be selected. If classification according to gender or jurisdiction is also needed, the gender and jurisdiction also need to be searched. With the increase of search conditions, the operation becomes more and more inconvenient. In addition, fixed columns such as name, age, gender, ID card, home address, application state, etc. also need to be returned. In the traditional data query system, conditions and returned columns cannot be flexibly assembled. SUMMARY
[0003] The technical problem to be solved by the application is that multi-condition combined query is not convenient, data is difficult to retrieve, and time-consuming is long.
[0004] In order to solve the above technical problems, the technical scheme of the application provides a natural language data query method based on grassroots governance, characterized in that it comprises the following steps:
[0005] Step 1: The front end initializes corresponding data fields, data conditions, shortcut conditions, condition logical relationships, aggregation words, grouping words and mood words according to different query requirements.
[0006] Step 2: The front end combines the data fields and data conditions to form word entries and puts them into a dictionary tree, combines the data fields and aggregation words to form word entries and puts them into a semantic dictionary tree, combines the data fields and grouping words to form word entries and puts them into a semantic dictionary tree, and directly puts the shortcut conditions, logical relationships and mood words into the semantic dictionary tree, finally forming a semantic dictionary tree, each leaf node in the semantic dictionary tree storing a type value for representing a word entry type and an ext value for representing word entry information.
[0007] Step 3, while the user is inputting text, the front-end input box uses the tree structure of the semantic dictionary tree to verify the correctness of the input text semantics in real time through a sliding window algorithm; after the user inputs an intent statement in the input box, the front-end uses the characteristics of the semantic dictionary tree to generate an associated completion text list by traversing all leaf nodes under the currently matched tree node in real time, and the user selects or inputs by clicking the mouse on the prompted associated text until the user inputs a complete statement expressing the query intent;
[0008] Step 4, after the front-end checks the correctness of the user input semantics, the input text is structurally parsed, the type value and ext value matched for each keyword are obtained, and the parameters of the structured representation of the query intent are classified through these two attributes, which are passed to the back-end interface as input parameters;
[0009] Step 5, the back-end obtains the query intent parameters and generates a query SQL, which is divided into four arrays according to the type value and ext value: query field / query aggregation, query table, query condition, and query grouping;
[0010] Step 6, the back-end splices the four arrays obtained in step 5 according to the SQL syntax format to splice the complete SQL statement;
[0011] Step 7, the back-end obtains the MySQL connection, executes the SQL, obtains the ResultSet result set, and generates interface output parameters by sequentially traversing the result set, and the output parameter structure is assembled by rows, with the first row being the field title and the second row being the data row, and the number of columns being dynamically generated according to the query intent;
[0012] Step 8, after the front-end obtains the interface output parameters, it dynamically renders the first row to generate a list title, and renders the second row and subsequent rows as data content.
[0013] Preferably, in step 2, the word type includes data field, data condition, aggregation word, condition logic, grouping word, mood word, and shortcut condition.
[0014] Preferably, in step 2, the leaf node structure of the word type of data condition, grouping word, and aggregation word is: {name: word name, field: field name, value: word information};
[0015] The leaf node structure of the word type of data field, logic relationship, mood word, and shortcut condition is: {name: word name, value: word information}.
[0016] Preferably, in step 3, the sliding window algorithm includes the following steps:
[0017] Step 301, create a variable left and a variable right, wherein left is 0 by default, pointing to the index subscript 0 of the input text; right is 1 by default, pointing to the index subscript 1 of the input text;
[0018] Step 302, match the text characters between left and right in the semantic dictionary tree: if not matched, increase the text index subscript pointed by right by 1, continue to match the text characters between left and right in the semantic dictionary tree, until a correct leaf node in the semantic dictionary tree is matched; if matched, set the text index subscript pointed by left as the text index subscript pointed by right, increase the text index subscript pointed by right by 1, and return to step 302 until all the leaf nodes that can be matched are found by traversing the leaf nodes of the semantic dictionary tree.
[0019] Preferably, in step 3, when the input text semantics are verified in real time by the sliding window algorithm, the node in the semantic dictionary tree corresponding to the character at the text index subscript pointed by the right variable in each sliding process is recorded by the currentNode variable, which is the current matched tree node, and then each leaf node in the semantic dictionary tree is found by continuing to traverse from the current matched tree node, and the character content from the current matched tree node to the leaf node is the semantic text list that needs to be associated and prompted.
[0020] The application can issue instructions to the machine through human language, the machine can understand semantics and semantic segmentation to identify the intention, and convert it into SQL language to query the database, and finally complete the operation of natural language query data. In the use process, the combination between conditions does not need to be paid attention to, and the association mode can complete the semantic writing of the query with the user inputting one by one. After the technical scheme disclosed by the application is adopted, the grassroots staff can use natural language to query, for example, inputting the statement "query the oldest old people who first apply for subsidies, return name, age, gender, ID card, home address, and application status", and the required data can be obtained, avoiding the cumbersome operation of the traditional system.
[0021] The application uses the dictionary tree, the sliding window algorithm and the SQL query syntax tree to construct, realizes the recognition and conversion of the input query intention into the query SQL search data. The user can input the query intention through the association type natural language, and flexibly combine the logic of each query condition. Only the input query intention is needed, and the required data can be obtained. Compared with the traditional multi-condition combination query mode, the application does not need the cumbersome condition combination operation, reduces the learning threshold and operation difficulty, and realizes the convenient data search effect. BRIEF DESCRIPTION OF DRAWINGS
[0022] Figure 1 Figure 1 is a schematic diagram of the principles of the present application;
[0023] Figure 2 Figure 2 is a schematic diagram of a dictionary tree structure;
[0024] Figure 3 Figure 3 is a schematic diagram of a semantic analysis description;
[0025] Figure 4 Figure 4 is a schematic diagram of SQL generation. DETAILED DESCRIPTION
[0026] The present application will be further described below with reference to specific embodiments. It should be understood that these embodiments are intended to illustrate the present application and are not intended to limit the scope of the present application. Furthermore, it should be understood that those skilled in the art can make various modifications or changes to the present application after reading the content of the present application, and these equivalent forms also fall within the scope of the appended claims.
[0027] The present application specifically applies the following aspects of technology:
[0028] 1. Dictionary tree (prefix tree) structure and algorithm, an associative string list is formed by combining fields, conditions, grouping, and aggregation, and the string list is split by characters to build a dictionary tree.
[0029] 2. Sliding window, the characters in the window are input for semantic verification, and the input string satisfies the dictionary tree matching, indicating that the semantics is correct.
[0030] 3. SQL query syntax tree, a syntax tree is built according to the SQL structure, select, from, where, group, each node contains N child nodes, and the child nodes store specific values, such as field names under select, table names under from, conditions under where, and grouping fields under group.
[0031] Based on the above technology, the natural language data query method based on grassroots governance disclosed by the present application includes the following steps:
[0032] Step 1, initialize data fields, data conditions, shortcut conditions, condition logical relationships (and and or), aggregation words, grouping words, and mood words in the front end.
[0033] The code example for realizing step 1 is as follows:
[0034]
[0035]
[0036]
[0037] Step 2, the front end combines the data field with the data condition to form a word entry and puts it into the dictionary tree, combines the data field with the aggregation word to form a word entry and puts it into the dictionary tree, combines the data field with the grouping word to form a word entry and puts it into the dictionary tree, and directly puts the shortcut condition, logical relationship and modal word into the dictionary tree.
[0038] In this embodiment, the code for implementing step 2 is as follows:
[0039]
[0040]
[0041]
[0042] A semantic dictionary tree (dictionary tree) is formed, and the type value is stored in the leaf node of the dictionary tree, the type value = 1, 2, 3, 4, 5, 6, 7, wherein 1 represents a field, 2 represents a condition, 3 represents aggregation, 4 represents a condition logic, 5 represents grouping, 6 represents a modal word, and 7 represents a shortcut condition. In this embodiment, the code is as follows:
[0043] const TYPE_FIELD = 1; / / field word
[0044] const TYPE WHERE = 2; / / condition word
[0045] const TYPE AGGRE = 3; / / aggregation word
[0046] const TYPE LOGIC = 4; / / logical relationship word
[0047] const TYPE GROUP = 5; / / grouping word
[0048] const TYPE MODAL = 6; / / modal word
[0049] const TYPE SHORTCUT = 7; / / shortcut condition word
[0050] In addition to the type value, all leaf nodes also store an ext value, the ext value represents word entry information, and the structure is as follows:
[0051] The structure of the data condition, grouping word and aggregation word is: {name: word entry name, field: field name, value: word entry information, corresponding to the variable in the front end initialization};
[0052] The structure of the data field, logical relationship, particles, and shortcut condition entries is: {name: entry name, value: entry information, corresponding to the variable in the front-end initialization}.
[0053] The type value represents the entry type, and the ext value represents the entry information.
[0054] Step 3: While the user is inputting, the front-end input box uses the tree structure specific to the dictionary tree to verify the correctness of the input semantics in real time using the sliding window algorithm, which includes the following steps:
[0055] Step 301: Create two variables left and right, where left is set to 0 by default, pointing to the index subscript 0 of the input text, and right is set to 1 by default, pointing to the index subscript 1 of the input text.
[0056] Step 302: Get the text characters between left and right and match them in the dictionary tree: if they do not match, increase the text index subscript pointed to by right by 1 and continue to match the text characters between left and right in the dictionary tree until a correct syntax can be matched in the dictionary tree; if they match, set the text index subscript pointed to by left to the text index subscript pointed to by right, increase the text index subscript pointed to by right by 1, and return to step 302 until all matching syntax entries are found by traversing the dictionary tree nodes.
[0057] Step 4: After the user inputs the intent statement in the input box, the front-end will use the characteristics of the dictionary tree to generate an associated completion text list by traversing all the child nodes under the current matching tree node in real time. The user selects or inputs the suggested associated text by mouse click until the user inputs the complete expression of the query intent statement.
[0058] In the semantic analysis described in step 3, we use a sliding window to traverse the dictionary tree nodes. To record the characters corresponding to the text index subscript pointed to by the right variable in the dictionary tree during each sliding process, we create a currentNode variable. When the front-end is inputting, if the complete semantics is not inputted, we will continue to traverse from the currentNode node to find each leaf node in the dictionary tree. The character content from the currentNode to the leaf node is the semantic text list that needs to be associated with the prompt. For example, when the front-end inputs "query first", it will suggest inputting "old people applying for subsidies", and after selecting the prompt, a complete semantic statement "query first old people applying for subsidies" is formed.
[0059] Step 5, after the front-end checks the correctness of the user input semantics, the input text is structured and parsed, and the type value and ext value obtained by matching each keyword are classified by these two attributes to form the structured representation of the query intent parameters. These parameters are passed to the back-end interface as input parameters.
[0060] The following is the input parameter structure of the user input "query the oldest old people who first apply for subsidies" after semantic analysis and transmission to the back-end interface:
[0061]
[0062]
[0063] Step 6, the back-end obtains the query intent parameters and generates the query SQL. The type value and ext value are divided into four arrays: query field / query aggregation (select), query table (from), query condition (where), and query grouping (group).
[0064] Step 7, the back-end splices the four arrays obtained in step 6 according to the SQL syntax format to splice the complete SQL statement.
[0065] Step 8, after the back-end obtains the MySQL connection, the SQL is executed to obtain the ResultSet result set, and the interface output parameters are generated by iterating the result set in turn. The output parameter structure is assembled by row, with the first row as the field title and the second row as the data row. The number of columns is dynamically generated according to the query intent.
[0066] Step 9, after the front-end obtains the interface output parameters, it dynamically renders the first row as the list title and the second row as the data content.
Claims
1. A method for natural language data query based on grassroots governance, characterized in that, The method comprises the following steps: Step 1, the front end initializes corresponding data fields, data conditions, shortcut conditions, condition logical relationships, aggregation words, grouping words and mood words according to different query requirements; Step 2, the front end combines the data fields and the data conditions to form a word entry and put it into a dictionary tree, combines the data fields and the aggregation words to form a word entry and put it into a semantic dictionary tree, combines the data fields and the grouping words to form a word entry and put it into a semantic dictionary tree, and directly puts the shortcut conditions, the condition logical relationships, the mood words into the semantic dictionary tree, and finally forms a semantic dictionary tree, wherein each leaf node in the semantic dictionary tree stores a type value for representing a word entry type and an ext value for representing word entry information; Step 3, the front end uses a tree structure unique to the semantic dictionary tree to verify the correctness of the input text semantics in real time through a sliding window algorithm while the user inputs the text; after the user inputs an intent statement in the input box, the front end uses the characteristics of the semantic dictionary tree to generate an associated completion text list by traversing all the leaf nodes under the current matching tree node in real time, and the user selects or inputs the associated text by mouse clicking until the user inputs a complete statement expressing the query intent; Step 4, after the front end checks the correctness of the user input semantics, the input text is structurally parsed, the type value and the ext value matched for each keyword are obtained, and the two attributes are classified to form a parameter of the structured query intent, and the parameter is transmitted to the back end interface as an input parameter; Step 5, the back end obtains the query intent parameter, generates a query SQL, and divides the query fields / aggregate fields, the query table, the query condition and the query grouping into four arrays according to the type value and the ext value; Step 6, the back end splices the four arrays obtained in step 5 according to the SQL syntax format to splice the query fields, the aggregation fields, the conditions and the grouping, and obtains a complete SQL statement through a SQL syntax tree; Step 7, the back end obtains a MySQL connection, executes the SQL, obtains a ResultSet result set, and generates an interface output parameter by sequentially and circularly traversing the result set, wherein the output parameter structure is spliced by rows, the first row is a field title, the second row is a data row, and the number of columns is dynamically generated according to the query intent; Step 8, after the front end obtains the interface output parameter, the first row is rendered as a list title, and the second row and the subsequent rows are rendered as data content.
2. The natural language data query method based on grassroots governance according to claim 1, wherein, In step 2, the word entry types include data fields, data conditions, aggregation words, condition logical relationships, grouping words, mood words and shortcut conditions.
3. The natural language data query method based on grassroots governance according to claim 2, characterized in that, In step 2, the leaf node structure of the word entry types of the data conditions, the grouping words and the aggregation words is {name: word entry name, field: field name, value: word entry information}, and the leaf node structure of the word entry types of the data fields, the condition logical relationships, the mood words and the shortcut conditions is {name: word entry name, value: word entry information}.
4. The natural language data query method based on grassroots governance according to claim 1, characterized in that, In step 3, the sliding window algorithm comprises the following steps: step 301, creating a variable left and a variable right, wherein left is 0 by default, pointing to the index 0 of the input text; right is 1 by default, pointing to the index 1 of the input text; step 302, obtaining the text characters between left and right in the input text to match in the semantic dictionary tree: if no match is found, the text index pointed to by right is increased by 1, and the matching of the text characters between left and right in the semantic dictionary tree is continued, until a correct leaf node that can be matched in the semantic dictionary tree is found; if a match is found, the text index pointed to by left is set to the text index pointed to by right, the text index pointed to by right is increased by 1, and step 302 is returned, until all leaf nodes that can be matched are found by traversing the leaf nodes of the semantic dictionary tree.
5. The natural language data query method based on grassroots governance according to claim 4, characterized in that, In step 3, when verifying the correctness of the input text semantics in real time by the sliding window algorithm, the node in the semantic dictionary tree corresponding to the character at the text index pointed to by the right variable in each sliding process is recorded by the currentNode variable, which is the current matched tree node. Then, according to the current matched tree node, each leaf node in the semantic dictionary tree is found by further traversing, and the character content from the current matched tree node to the leaf node is the semantic text list that needs to be associated and prompted.
Citation Information
Patent Citations
Semantic requirement analysis method and device for query string
CN107203526A
Similar article retrieval method and system
CN112527971A