Question and answer matching method and device based on K-means clustering algorithm, equipment and medium
By employing K-means clustering and attention mechanisms, the problem of information loss in traditional question-answering matching is solved, improving the accuracy and matching efficiency of answer vectors and enhancing the user experience.
Patent Information
- Application Number
- CN202111182232.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-10-11
- Publication Date
- 2025-12-05
- Estimated Expiration
- 2041-10-11
AI Technical Summary
Traditional question-and-answer matching methods cannot balance matching effectiveness and efficiency. The fixed-length answer vector obtained by semi-encoder compression may lose a lot of information, resulting in inaccurate matching results.
The candidate answer vectors are clustered using the K-means clustering algorithm to generate K adaptive semantic vectors. The matching value between the candidate answer vector and the question vector is calculated through an attention mechanism to determine the correct answer.
It improves the accuracy of candidate answer vectors and matching results, reduces information loss, increases encoding speed and matching efficiency, and enhances user experience.
Smart Images

Figure CN113918696B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of artificial intelligence, and in particular to a question-answering matching method, apparatus, device, and medium based on the K-means clustering algorithm. Background Technology
[0002] Question answering systems are an advanced form of information retrieval systems that can answer user questions accurately and concisely using natural language. Question answering systems typically use a matching model based on the BERT network for question-answering matching. This matching model is pre-trained on the BERT model using a massive corpus of general-domain data, and then the encoder built on top of the BERT model is fine-tuned to improve the matching performance, thereby ensuring the retrieval accuracy of the question answering system.
[0003] In traditional question-answering matching methods, to improve the speed of question-answering matching, the encoder built on top of the BERT model is generally a semi-encoder. During question-answering matching, the semi-encoder compresses the candidate answer vector to obtain a fixed-length answer vector (usually the first k words of the candidate answer) to represent the candidate answer. Then, the question is encoded into a question vector, and a series of processes are performed on the question and the fixed-length answer vector to determine the matching degree between them. However, the fixed-length answer vector obtained by the semi-encoder is much shorter than the length of the candidate answer itself. This fixed-length answer vector may lose a significant amount of answer information, resulting in an inaccurate answer vector and reducing the accuracy of the matching results. Summary of the Invention
[0004] This invention provides a question-answering matching method, apparatus, device, and medium based on the K-means clustering algorithm to solve the problem that traditional question-answering matching methods cannot balance matching effect and matching efficiency.
[0005] A question-answering matching method based on the K-means clustering algorithm is provided, including:
[0006] Obtain the user's input question and perform vector transformation on the user question to obtain the question vector;
[0007] Multiple candidate answers to the user's question are determined according to a preset matching method, and K semantic vectors for each candidate answer are determined. The K semantic vectors are semantic vectors obtained by performing vector clustering on the candidate answers based on the K-means clustering algorithm.
[0008] Based on the question vector, perform attention-based vector computation on the semantic vectors of K answers to obtain candidate answer vectors for the candidate answers;
[0009] Based on the matching value between the candidate answer vector and the question vector, the correct answer to the user's question is determined from multiple candidate answers.
[0010] A question-answering matching device based on the K-means clustering algorithm is provided, comprising:
[0011] The transformation module is used to obtain the user's input question and perform vector transformation on the user's question to obtain the question vector of the user's question;
[0012] The first determining module is used to determine multiple candidate answers to the user's question according to a preset matching method, and to determine K answer semantic vectors for each candidate answer. The K answer semantic vectors are semantic vectors obtained by performing vector clustering on the candidate answers based on the K-means clustering algorithm.
[0013] The computation module is used to perform attention-based vector computation on the semantic vectors of K answers based on the question vector to obtain the candidate answer vector of the candidate answer;
[0014] The second determination module is used to determine the correct answer to the user's question from multiple candidate answers based on the matching value between the candidate answer vector and the question vector.
[0015] A computer device is provided, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the steps of the question-answering matching method based on the K-means clustering algorithm described above.
[0016] A computer-readable storage medium is provided, the computer-readable storage medium storing a computer program, which, when executed by a processor, implements the steps of the question-answering matching method based on the K-means clustering algorithm described above.
[0017] In one of the solutions provided by the question-answering matching method, apparatus, device, and medium based on the K-means clustering algorithm, the user input question is obtained, and the user question is vectorized to obtain a question vector. Then, multiple candidate answers to the user question are determined according to a preset matching method, and K answer semantic vectors are determined for each candidate answer. The K answer semantic vectors are semantic vectors obtained after vector clustering of candidate answers based on the K-means clustering algorithm. Then, vector calculation based on an attention mechanism is performed on the K answer semantic vectors according to the question vector to obtain candidate answer vectors. Finally, the correct answer to the user question is determined from multiple candidate answers based on the matching value between the candidate answer vectors and the question vector. In this invention, the K-means clustering algorithm is used to retrieve all global features that can represent more semantics in the candidate answers, generating K adaptive semantic vectors for the candidate answers. This results in faster encoding speed and reduces the possibility of losing candidate answer information. Then, attention calculation is performed on the K adaptive semantic vectors using the question vector, which improves the accuracy of the candidate answer vectors, thereby improving the accuracy of the matching results. Attached Figure Description
[0018] To more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings used in the description of the embodiments of the present invention will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0019] Figure 1 This is a schematic diagram of an application environment for a question-answering matching method based on the K-means clustering algorithm in one embodiment of the present invention;
[0020] Figure 2 This is a flowchart illustrating a question-answering matching method based on the K-means clustering algorithm in one embodiment of the present invention;
[0021] Figure 3 yes Figure 2 A schematic diagram of the implementation process of step S10;
[0022] Figure 4 yes Figure 2 A schematic diagram of the implementation process of step S30;
[0023] Figure 5 yes Figure 2 A schematic diagram of the implementation process of step S40;
[0024] Figure 6 yes Figure 2 Another implementation flow diagram of step S40;
[0025] Figure 7 yes Figure 2 Another implementation flow diagram of step S20;
[0026] Figure 8 This is a schematic diagram illustrating the process of obtaining the semantic vectors of the K subsequent answers in one embodiment of the present invention;
[0027] Figure 9 This is a schematic diagram of a question-answering matching device based on the K-means clustering algorithm in one embodiment of the present invention;
[0028] Figure 10 This is a schematic diagram of the structure of a computer device according to an embodiment of the present invention. Detailed Implementation
[0029] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0030] The question-answering matching method based on the K-means clustering algorithm provided in this invention can be applied to, for example... Figure 1 In this application environment, the terminal device communicates with the server via a network. The server receives the user's question input through the terminal device, performs vector transformation on the question to obtain a question vector, and then determines multiple candidate answers based on a preset matching method. For each candidate answer, it determines K semantic vectors, which are semantic vectors obtained by clustering the candidate answers using the K-means clustering algorithm. Then, based on the question vector, it performs attention-based vector calculation on the K answer semantic vectors to obtain candidate answer vectors. Finally, based on the matching value between the candidate answer vectors and the question vector, it determines the correct answer from among the multiple candidate answers. The K-means clustering algorithm is used to retrieve all global features that can represent more semantic meaning from the candidate answers, generating K adaptive semantic vectors for each candidate answer. This method has a fast encoding speed and reduces the possibility of information loss in candidate answers. Then, attention calculation is performed on the K adaptive semantic vectors using the question vector, improving the accuracy of the candidate answer vectors and thus improving the accuracy of the matching results. Ultimately, this further enhances the artificial intelligence of the question-answering system and improves the user experience.
[0031] The user question, multiple candidate answers, and the semantic vectors of the K candidate answers are stored in the server's database. When it is necessary to match answers to a user question, the relevant data is directly retrieved from the server's database, thus improving the efficiency of question-and-answer matching.
[0032] In this embodiment, the database is stored in a blockchain network to store data used and generated in the question-and-answer matching method based on the K-means clustering algorithm, such as user questions, multiple candidate answers, and semantic vectors of the K candidate answers. The blockchain referred to in this application is a novel application model of computer technologies such as distributed data storage, peer-to-peer transmission, consensus mechanisms, and encryption algorithms. Essentially, a blockchain is a decentralized database, a chain of data blocks linked using cryptographic methods. Each data block contains information about a batch of network transactions, used to verify the validity of the information (anti-counterfeiting) and generate the next block. A blockchain can include a blockchain underlying platform, a platform product service layer, and an application service layer. Deploying the database on a blockchain can improve the security of data storage.
[0033] The terminal devices can be, but are not limited to, various personal computers, laptops, smartphones, tablets, and portable wearable devices. Servers can be implemented using standalone servers or server clusters consisting of multiple servers.
[0034] In one embodiment, such as Figure 2 As shown, a question-answering matching method based on the K-means clustering algorithm is provided, and this method is applied to... Figure 1 Taking the server in the example, the following steps are included:
[0035] S10: Obtain the user's input question and perform vector transformation on the user's question to obtain the question vector of the user's question.
[0036] Understandably, a user question is a question input by the user through a terminal device in a question-and-answer system, which the system then uses to retrieve and push answers. The question-and-answer system's server receives the user question input through the terminal device and then extracts its speech feature vector to obtain a question vector representing the user question.
[0037] S20: Determine multiple candidate answers to the user's question according to a preset matching method, and obtain K answer semantic vectors for each candidate answer.
[0038] After receiving the user's input question, the question-answering system's server retrieves multiple standard answers stored in the database. Then, it determines multiple candidate answers to the user's question based on a preset matching method, such as keyword matching between the standard answers and the user's question. After determining the multiple candidate answers, it needs to determine K semantic vectors for each candidate answer so that the matching result between each candidate answer and the user's question can be determined subsequently based on these K semantic vectors. Here, K is an integer greater than 2.
[0039] Among them, the semantic vectors of the K candidate answers are semantic vectors obtained by performing vector clustering on the candidate answers based on the K-means clustering algorithm.
[0040] The semantic vectors of the K candidate answers can be obtained in the following way:
[0041] The first method involves retrieving a candidate answer from the database and inputting it into the BERT model. This yields multiple hidden-state answer vectors output by the BERT model. These hidden-state answer vectors are then clustered using a K-means clustering algorithm to obtain K semantic vectors for each candidate answer. After obtaining the user's question vector, the K semantic vectors of the candidate answers are used to determine if the candidate answer matches the user's question. If not, another candidate answer is retrieved from the database, and this process is repeated to determine if it matches the user's question. This process continues until all matching answers are obtained, and the correct answer is selected from among all matching answers.
[0042] The second approach involves, after identifying multiple standard answers, performing offline conversion to obtain K semantic vectors corresponding to each standard answer. These standard answers and their corresponding K semantic vectors are then stored in a database in a one-to-one correspondence. After obtaining the user's input question, the standard answer corresponding to the user's question is determined in the database as a candidate answer. The K semantic vectors corresponding to the candidate answer are then directly retrieved from the database, eliminating the need for online conversion and reducing computational load while improving question-answering matching efficiency. In the offline conversion, the standard answers are pre-input into the BERT model to obtain multiple hidden-state answer vectors output by the BERT model. These hidden-state answer vectors are then clustered using the K-means clustering algorithm to obtain the K semantic vectors of the standard answers, thus obtaining the K semantic vectors of all standard answers offline.
[0043] S30: Perform attention-based vector computation on the semantic vectors of K answers based on the question vector to obtain the candidate answer vector.
[0044] After determining the K semantic vectors of candidate answers, a weighted sum of these K semantic vectors is performed based on the question vector to obtain a candidate answer vector. The calculation of the candidate answer vector is based on an attention mechanism. First, an attention weight matrix is determined based on the question vector and the K semantic vectors. Then, the K semantic vectors are weighted and summed based on the K attention weights in the attention weight matrix to obtain the candidate answer vector. This attention mechanism-based interaction and fusion of the question vector and the K semantic vectors to obtain the candidate answer vector improves the relevance between the candidate answer vector and the question vector, facilitating matching.
[0045] S40: Determine the correct answer to the user's question from among multiple candidate answers based on the matching value between the candidate answer vector and the question vector.
[0046] After obtaining the candidate answer vectors, the correct answer to the user's question is determined from among the multiple candidate answers based on the matching value between the candidate answer vectors and the question vector. The candidate answer corresponding to the candidate answer vector with the largest matching value is selected as the correct answer to the user's question.
[0047] The matching value (matching score) between the candidate answer vector and the question vector is calculated using the following formula:
[0048]
[0049] Where q represents the user question, and e q Let d be the question vector of the user's question. i For the i-th candidate answer, Let s(q,d) be the answer vector for the i-th candidate answer. i ) represents the matching value between the candidate answer vector and the question vector of the i-th candidate answer.
[0050] In this embodiment, the user's input question is obtained and vectorized to obtain a question vector. Then, multiple candidate answers are determined according to a preset matching method, and K semantic vectors for each candidate answer are determined. These K semantic vectors are semantic vectors obtained by clustering candidate answers using the K-means clustering algorithm. Next, attention-based vector calculation is performed on the K semantic vectors based on the question vector to obtain candidate answer vectors. Finally, the correct answer to the user's question is determined from among the multiple candidate answers based on the matching value between the candidate answer vectors and the question vector. In this invention, the K-means clustering algorithm is used to retrieve all global features that can represent more semantics from the candidate answers, generating K adaptive semantic vectors for the candidate answers. This method has a fast encoding speed and reduces the possibility of losing candidate answer information. Then, attention calculation is performed on the K adaptive semantic vectors using the question vector. The answer vector is added to the calculation of the user's question vector before matching, improving the accuracy of the candidate answer vectors, thereby improving the accuracy of subsequent matching results and ultimately enhancing the user experience of the question-answering system. Furthermore, this embodiment, while ensuring the accuracy of the matching results, can also improve the speed of vector calculation, thereby improving matching efficiency and response speed to user questions, and further enhancing the user experience.
[0051] In one embodiment, such as Figure 3 As shown, step S10, which involves vector transformation of the user question to obtain the question vector, specifically includes the following steps:
[0052] S11: Segment the user question into single characters to obtain the character vector sequence of the user question.
[0053] After obtaining the user's input question, the question is segmented into individual characters to obtain a sequence of character vectors. Using character vectors as input to the BERT model is more accurate than traditional word vector segmentation, resulting in more precise question vectors.
[0054] S12: Input the word vector sequence of the user's question into the BERT model to obtain multiple hidden state question vectors output by the BERT model.
[0055] After obtaining the word vector sequence of the user question, this sequence is input into the BERT model for encoding to obtain the hidden states of the BERT model. These hidden state question vectors serve as the output of the BERT model after encoding the user question. The hidden state question vectors represent the hidden states of the last layer of the transformer in the BERT model. Using the hidden states of the last layer of the transformer in the BERT model to represent the user question ensures a higher correlation between the vectors and the user question information, facilitating subsequent feature extraction based on the hidden state question vectors and guaranteeing the accuracy of the extracted feature vectors.
[0056] For example, if the user question is q, the word vector sequence of the user question is represented as: The word vector sequence of the user question The input is encoded into the BERT model to obtain the hidden state of the last layer of the BERT model's transformer, which serves as the representation vector h of the user's question. j This means obtaining multiple hidden state question vectors output by the BERT model. The hidden state question vectors are represented by the following formula:
[0057]
[0058] Where, N x The length of the word vector sequence in the user's question. For the Nth user question x Each word vector, h j Let j be the hidden state question vector of the j-th word in the user question, where j∈[0,N]. x ].
[0059] S13: Aggregate multiple hidden state question vectors based on a preset aggregation method to obtain the question vector of the user's question.
[0060] After obtaining multiple hidden state question vectors output by the BERT model, the multiple hidden state question vectors are aggregated based on a preset aggregation method to obtain the question vector of the user's question.
[0061] Wherein, the word vector sequence of user question q is The formula for calculating the question vector of a user's question is:
[0062]
[0063] Among them, e q The question vector represents the user's question; the function of the BERT model is represented by Concat, which indicates the aggregation function (preset aggregation method). For the Nth user question x N word vectors;x The length of the word vector sequence in the user's question.
[0064] In one embodiment, the preset aggregation method may be: among multiple hidden state question vectors, determine the first hidden state question vector output by the BERT model, and use the first hidden state question vector output by the BERT model as the question vector of the user question.
[0065] After obtaining multiple hidden state question vectors output by the BERT model, the first hidden state question vector output by the BERT model is selected. This first hidden state question vector is the vector corresponding to [CLS] in the BERT model. Then, this first hidden state question vector is used as the user's question vector. In the BERT model, the vector corresponding to [CLS] can more fairly integrate the semantic information of each character / word in the text, representing the user's question better than other characters / words. Furthermore, directly using the first hidden state question vector output by the BERT model as the user's question vector reduces computation and increases the speed of obtaining candidate vectors, thereby improving question-answering matching efficiency.
[0066] In other embodiments, the preset aggregation method may also be: determining the mean of multiple hidden state question vectors and using the mean of multiple hidden state question vectors as the question vector of the user's question to improve the accuracy of the question vector; or, determining the top m hidden state question vectors among the multiple hidden state question vectors output by the BERT model, determining the mean of the top m hidden state question vectors, and using the mean of the top m hidden state question vectors as the question vector to reduce meaningless vector processing and reduce the amount of computation.
[0067] In this embodiment, the user question is segmented into individual characters to obtain a sequence of character vectors for the user question. This sequence is then input into the BERT model to obtain multiple hidden state question vectors output by the BERT model. Finally, the multiple hidden state question vectors are aggregated based on a preset aggregation method to obtain the question vector of the user question. This clarifies the specific steps for vector transformation of the user question to obtain the question vector. By using the hidden states of the BERT model as a representation of the user question and aggregating multiple hidden state question vectors based on a preset aggregation method, the accuracy of the question vector is ensured.
[0068] In one embodiment, such as Figure 4 As shown, step S30, which involves performing attention-based vector computation on the semantic vectors of the K answers based on the question vector to obtain the candidate answer vector, specifically includes the following steps:
[0069] S31: Determine the initial weights of each answer semantic vector to obtain the initial weight matrix.
[0070] After determining the K semantic vectors of the candidate answers, an initial weight is assigned to each semantic vector to obtain an initial weight matrix for the K semantic vectors. The expression for the initial weight matrix is (a1..a2...a3...a4...a5...a6...a7...a8...a9...a1...a9...a1...a1...a1...a1...a1...a1...a1...a2...a1...a2...a1...a2...a1...a2...a2...a3...a1...a2...a2...a3...a2...a3...a2...a3...a3...a4...a5...a2...a3...a3...a4...a5...a6...a7...a1...a2...a3...a3...a4...a5...a6...a7...a1...a2...a3...a3...a4...a5...a6...a7...a1...a2...a3...a3...a4...a5...a6...a7...a3...a3...a4...a5...a6...a7...a3...a3...a3...a4...a5...a6...a7...a3...a3...a3...a4...a5...a3...a3...a3...a4...a5...a3...a3...a3...a4...a3 ... k ), where a k The initial weights are the semantic vectors of the Kth answer.
[0071] S32: Determine the first weight matrix based on the initial weight matrix and the semantic vectors of the K answers.
[0072] After obtaining the initial weight matrix, the product of each answer semantic vector and its initial weight is taken as the first weight, resulting in k first weights. These k first weights form the first weight matrix. That is, the first weight matrix includes K first weights, where each first weight is the product of an answer semantic vector and its initial weight.
[0073] Where, let (c1..c k Let be a sequence of K semantic vectors for each answer, and c k Let the semantic vector be the Kth answer. Then the expression for the first weight matrix is (a1·c1,…,a…). k ·c k ).
[0074] S33: Normalize the first weight matrix to obtain the target weight of each answer semantic vector.
[0075] After determining the first weight matrix based on the initial weight matrix and the K answer semantic vectors, the first weight matrix is normalized to obtain the target weight of each answer semantic vector.
[0076] The first weight matrix can be normalized using a normalized exponential function (softmax) to obtain the target weight for each answer semantic vector. The calculation formula is as follows:
[0077] (w1,…,w k = softmax(a1·c1,…,a) k ·c k )
[0078] Among them, c k Let a be the semantic vector of the Kth answer. k w is the initial weight of the semantic vector of the Kth answer. k The target weight is the semantic vector of the Kth answer.
[0079] S34: Based on the target weights of the answer semantic vectors, perform a weighted summation of the K answer semantic vectors to obtain the candidate answer vectors of the candidate answers.
[0080] After obtaining the target weight of each answer semantic vector, the K answer semantic vectors are weighted and summed according to the target weight of the answer semantic vectors to obtain the candidate answer vector of the candidate answer.
[0081] The candidate answer vector is calculated using the following formula:
[0082] e d =∑ i w i c i ;
[0083] Among them, e d Let c be the candidate answer vector of the candidate answers. i Let w be the semantic vector of the i-th answer. i Let be the target weight of the semantic vector of the i-th answer, i∈[1,k].
[0084] In this embodiment, an initial weight matrix is obtained by determining the initial weight of each answer semantic vector. Then, a first weight matrix is determined based on the initial weight matrix and K answer semantic vectors. The first weight matrix is then normalized to obtain the target weight of each answer semantic vector. Finally, the K answer semantic vectors are weighted and summed based on their target weights to obtain the candidate answer vector of the candidate answer. This clarifies the specific process of performing attention-based vector calculation on K answer semantic vectors based on the question vector to obtain the candidate answer vector of the candidate answer. Introducing the question vector into the calculation of the candidate answer vector based on the attention mechanism increases the interaction between the user's question and the candidate answer, improves the accuracy of the candidate answer vector, and thus improves the accuracy of the candidate matching value, which is beneficial to improving the matching effect.
[0085] In one embodiment, such as Figure 5 As shown, step S40, which involves determining the correct answer to the user's question from multiple candidate answers based on the matching value between the candidate answer vector and the question vector, specifically includes the following steps:
[0086] S41: Determine whether the matching value between the candidate answer vector and the question vector is less than the preset matching threshold.
[0087] After obtaining the candidate answer vector and the user question vector, it is necessary to determine whether the matching value between the candidate answer vector and the question vector is less than a preset matching threshold. Based on this determination, it is determined whether the candidate answer and the user question match. A higher matching value indicates a better match between the two vectors. If the matching value is less than the preset matching threshold, it means the candidate answer and the user question do not match; otherwise, it means they match.
[0088] Among them, the preset matching threshold is a threshold set in advance according to the question and answer scenario type. Different preset matching thresholds are determined for different question and answer scenario types, which can quickly determine the candidate answers that meet the expectations and improve the user experience.
[0089] After obtaining the candidate answer vector and the user's question vector, the matching value between the candidate answer vector and the question vector is calculated using a dot product operation. The matching value is calculated using the following formula:
[0090]
[0091] Where q represents the user question, and e q Let d be the question vector of the user's question. i For the i-th candidate answer, Let s(q,d) be the answer vector for the i-th candidate answer. i ) represents the matching value between the candidate answer vector and the question vector of the i-th candidate answer, i.e., the matching value of the candidate answer.
[0092] S42: If the matching value is greater than or equal to the preset matching threshold, then the candidate answer corresponding to the matching value is taken as the target answer.
[0093] After determining whether the matching value between the candidate answer vector and the question vector is less than a preset matching threshold, if the matching value is greater than or equal to the preset matching threshold, it means that the candidate answer matches the user's question, and the candidate answer corresponding to the matching value is taken as the target answer.
[0094] S43: Determine whether the number of target answers among multiple candidate answers is greater than 1.
[0095] Based on steps S41 and S42, all candidate answers are traversed to determine all target answers among the multiple candidate answers. Multiple target answers may exist among the multiple candidate answers, meaning there may be multiple candidate answers that closely match the user's question. To ensure that the answer pushed to the user is the optimal answer, it is necessary to determine whether the number of target answers among the multiple candidate answers is greater than 1, and to implement different answer push strategies based on the determination result.
[0096] S44: If the number of target answers is equal to 1, then the target answer is taken as the correct answer to the user's question.
[0097] After determining whether the number of target answers among multiple candidate answers is greater than 1, if it is determined that the number of target answers among multiple candidate answers is equal to 1, that is, there is only 1 target answer among multiple candidate answers, then the target answer is taken as the correct answer to the user's question and pushed to the user.
[0098] In this embodiment, by determining whether the matching value between the candidate answer vector and the question vector is less than a preset matching threshold, if the matching value is greater than or equal to the preset matching threshold, the candidate answer corresponding to the matching value is taken as the target answer. Then, it is determined whether the number of target answers among multiple candidate answers is greater than 1. If the number of target answers is equal to 1, the target answer is taken as the correct answer to the user question. This clarifies the specific process of determining the correct answer to the user question among multiple candidate answers based on the matching value between the candidate answer vector and the question vector, providing a basis for the correct answer to the user question.
[0099] In one embodiment, such as Figure 6 As shown, after step S43, i.e., after determining whether the number of target answers among multiple candidate answers is greater than 1, the method further includes the following steps:
[0100] S45: If the number of target answers is greater than 1, sort the multiple target answers in ascending order according to the size of the matching value to obtain a list of target answers.
[0101] After determining whether the number of target answers among multiple candidate answers is greater than 1, if the number of target answers among multiple candidate answers is greater than 1 (i.e., multiple target answers exist among multiple candidate answers), then the multiple target answers need to be sorted in ascending order according to the size of their matching values to obtain a list of target answers. The larger the matching value corresponding to a target answer, the higher the ranking of that target answer in the list of target answers.
[0102] S46: Select the first-ranked candidate answer in the target answer list as the correct answer to the user's question.
[0103] After obtaining the target answer list, the top-ranked candidate answer in the target answer list is selected as the correct answer to the user's question, and the top-ranked candidate answer is pushed to the user to ensure that the user receives the optimal answer, thereby improving the user experience.
[0104] In this embodiment, after determining whether the number of target answers among multiple candidate answers is greater than 1, if the number of target answers is greater than 1, the multiple target answers are sorted in ascending order according to the size of the matching value to obtain a list of target answers. The candidate answer ranked first in the list of target answers is taken as the correct answer to the user's question, ensuring that the answer received by the user is the optimal answer, which can improve the user experience.
[0105] In one embodiment, such as Figure 7 As shown, step S20, which involves determining multiple candidate answers to the user's question based on a preset matching method and determining K semantic vectors for each candidate answer, specifically includes the following steps:
[0106] S21: Retrieve multiple standard answers stored in the database and identify named entities in the user's question.
[0107] After obtaining the user's input question, it is necessary to retrieve multiple standard answers stored in the database. Simultaneously, it is also necessary to identify the named entities within the user's question.
[0108] S22: Match multiple standard answers based on named entities to select the standard answers containing named entities as candidate answers to the user's question, thereby obtaining multiple candidate answers.
[0109] After retrieving multiple standard answers stored in the database, the system matches these answers against named entities to identify multiple candidate answers. Specifically, keyword (named entity) matching is used to identify candidate answers from the database's standard answers.
[0110] First, it's necessary to determine whether each standard answer contains a named entity from the user's question. If a standard answer does, it's selected as a candidate answer. This process is repeated for all standard answers to obtain multiple candidate answers. By identifying standard answers with the same named entities as candidate answers from among multiple standard answers, the computational cost of candidate matching is reduced, thereby improving question-and-answer matching efficiency, enabling faster responses to users, and enhancing user experience.
[0111] S23: Extract K semantic vectors for each candidate answer from the database.
[0112] In the database, since the standard answer is stored in a one-to-one correspondence with the K answer semantic vectors of the standard answer, after the candidate answers are determined, the K answer semantic vectors of each candidate answer can be directly extracted from the database.
[0113] In this embodiment, the database stores the standard answer and its corresponding answer vector, with a one-to-one correspondence between the standard answer and its K semantic vectors. After determining the candidate answers to the user's question, the database retrieves the K semantic vectors corresponding to each candidate answer. The standard answer is pre-converted into K semantic vectors and stored offline, allowing the server to quickly determine the K semantic vectors of the candidate answers based on the actual user question. This eliminates the need for online computation of the K semantic vectors, reducing server load and improving question-and-answer matching efficiency and server response speed.
[0114] In this embodiment, multiple standard answers stored in the database are obtained, and named entities in the user question are identified. Then, the multiple standard answers are matched according to the named entities to select the standard answers containing named entities as candidate answers to the user question, thereby obtaining multiple candidate answers. Finally, K answer semantic vectors of each candidate answer are extracted from the database. This clarifies the specific process of determining multiple candidate answers to the user question and determining the K answer semantic vectors of each candidate answer according to a preset matching method. It eliminates the need for online calculation of the K answer semantic vectors of the candidate answers, reducing the computational load on the server and achieving the effects of reducing server load, improving question-and-answer matching efficiency, and improving server response speed.
[0115] In one embodiment, such as Figure 8 As shown, in step S10, the semantic vectors of the K candidate answers are obtained in the following way:
[0116] S01: Segment the candidate answers into individual characters to obtain the character vector sequence of the candidate answers.
[0117] After obtaining candidate answers, each candidate answer is segmented into individual characters to obtain a sequence of character vectors. Using this sequence as input to the BERT model yields more accurate and precise character vectors compared to traditional word vector segmentation.
[0118] S02: Input the word vector sequence of the candidate answer into the BERT model for encoding to obtain multiple hidden state answer vectors output by the BERT model.
[0119] After obtaining the word vector sequence of candidate answers, the word vector sequence of candidate answers is input into the BERT model for encoding to obtain the hidden state of the last layer of the transformer in the BERT model, which serves as multiple hidden state answer vectors of the candidate answers output by the BERT model.
[0120] S03: Perform vector clustering on multiple hidden state answer vectors based on the K-means clustering algorithm to obtain K semantic vectors of candidate answers.
[0121] After obtaining multiple hidden state answer vectors output by the BERT model, the multiple hidden state answer vectors are clustered using the K-means clustering algorithm to obtain K semantic vectors of candidate answers.
[0122] The K-means clustering algorithm is used to perform vector clustering on multiple hidden state answer vectors. This involves two steps: first, assigning values to K cluster centers based on the multiple hidden state answer vectors to obtain K clusters; then, updating the K clusters based on the multiple hidden state answer vectors. These two steps are performed alternately until the convergence condition is met. The K final clusters that meet the convergence condition are then used as the K answer semantic vectors of the candidate answers.
[0123] After obtaining multiple hidden state answer vectors from the BERT model output, these vectors are divided into K groups. The mean of all hidden state answer vectors in each group is then determined, and this mean is used as the cluster center for that group, resulting in K cluster centers. The Euclidean distance between the hidden state answer vectors and the cluster centers is determined. Based on this Euclidean distance, the hidden state answer vectors are further divided to assign each vector to the cluster center with the smallest Euclidean distance, forming K clusters. Each cluster includes a cluster center and the hidden state answer vector with the smallest Euclidean distance to that cluster center. The assignment is then complete.
[0124] Then, the K clusters are updated as follows: the mean of all hidden state answer vectors in each cluster is determined as the new cluster center; the Euclidean distance between the hidden state answer vector and the cluster center is determined, and each hidden state answer vector is assigned to the cluster center with the smallest Euclidean distance, forming a new K clusters. Each new cluster includes a new cluster center and the hidden state answer vector with the smallest Euclidean distance to the cluster center. This process is repeated until the convergence condition is met. The K final clusters that meet the convergence condition are then used as the K answer semantic vectors of the candidate answers.
[0125] The convergence condition can be any one of the following conditions:
[0126] 1) No (or minimum number) hidden state answer vectors are reassigned to different clusters.
[0127] 2) No (or minimum number) cluster centers change.
[0128] 3) The sum of squared errors is locally minimized.
[0129] In this embodiment, candidate answers are segmented into individual characters to obtain a sequence of character vectors. This sequence is then input into the BERT model for encoding, resulting in multiple hidden-state answer vectors output by the BERT model. Finally, K-means clustering is used to cluster these hidden-state answer vectors, yielding K semantic vectors for each candidate answer. This clarifies the process of obtaining the K semantic vectors for each candidate answer. The K-means clustering algorithm is used to retrieve all global features from the candidate answers that can represent more semantic meaning, generating K adaptive semantic vectors for each candidate answer. This method offers faster encoding speed and reduces the possibility of information loss in candidate answers, ensuring the accuracy of the subsequently obtained candidate answer vectors and thus improving the accuracy of subsequent matching results.
[0130] It should be understood that the sequence number of each step in the above embodiments does not imply the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present invention.
[0131] In one embodiment, a question-answering matching device based on the K-means clustering algorithm is provided, which corresponds one-to-one with the question-answering matching method based on the K-means clustering algorithm in the above embodiments. For example... Figure 9 As shown, the question-answering matching device based on the K-means clustering algorithm includes a conversion module 901, a first determination module 902, a calculation module 903, and a second determination module 904. Detailed descriptions of each functional module are as follows:
[0132] The conversion module 901 is used to obtain the user's input question and perform vector conversion on the user's question to obtain the question vector of the user's question;
[0133] The first determining module 902 is used to determine multiple candidate answers to the user's question according to a preset matching method, and to determine K answer semantic vectors for each candidate answer. The K answer semantic vectors are semantic vectors obtained by performing vector clustering on the candidate answers based on the K-means clustering algorithm.
[0134] The calculation module 903 is used to perform attention-based vector calculation on the semantic vectors of K answers based on the question vector to obtain the candidate answer vector of the candidate answer;
[0135] The second determining module 904 is used to determine the correct answer to the user's question from multiple candidate answers based on the matching value between the candidate answer vector and the question vector.
[0136] Furthermore, the conversion module 901 is specifically used for:
[0137] The user question is segmented into individual characters to obtain a character vector sequence of the user question;
[0138] Input the word vector sequence of the user's question into the BERT model to obtain multiple hidden state question vectors output by the BERT model;
[0139] Multiple hidden state question vectors are aggregated based on a preset aggregation method to obtain the question vector of the user's question.
[0140] Furthermore, the computing module 903 is specifically used for:
[0141] Determine the initial weights for each answer's semantic vector to obtain the initial weight matrix;
[0142] The first weight matrix is determined based on the initial weight matrix and the semantic vectors of the K answers;
[0143] The first weight matrix is normalized to obtain the target weight of each answer semantic vector;
[0144] Based on the target weights of the answer semantic vectors, the K answer semantic vectors are weighted and summed to obtain the candidate answer vector of the candidate answer.
[0145] Furthermore, the second determining module 904 is specifically used for:
[0146] Determine whether the matching value between the candidate answer vector and the question vector is less than a preset matching threshold;
[0147] If the matching value is greater than or equal to the preset matching threshold, the candidate answer corresponding to the matching value will be taken as the target answer.
[0148] Determine whether the number of target answers among multiple candidate answers is greater than 1;
[0149] If the number of target answers is equal to 1, then the target answer is taken as the correct answer to the user's question.
[0150] Furthermore, after determining whether the number of target answers among multiple candidate answers is greater than 1, the second determining module 904 is specifically used for:
[0151] If the number of target answers is greater than 1, then the multiple target answers are sorted in ascending order according to the size of the matching value to obtain a list of target answers;
[0152] The first candidate answer in the target answer list is selected as the correct answer to the user's question.
[0153] Furthermore, the first determining module 902 is specifically used for:
[0154] Retrieve multiple standard answers stored in the database and identify named entities in the user's question;
[0155] Multiple standard answers are matched based on named entities to select the standard answers containing named entities as candidate answers to the user's question, thereby obtaining multiple candidate answers.
[0156] Extract K semantic vectors for each candidate answer from the database.
[0157] Furthermore, the first determining module 902 is specifically used to obtain the semantic vectors of the K candidate answers in the following manner:
[0158] The candidate answers are segmented into individual characters to obtain the character vector sequence of the candidate answers;
[0159] The word vector sequence of candidate answers is input into the BERT model for encoding to obtain multiple hidden state answer vectors output by the BERT model;
[0160] The K-means clustering algorithm is used to perform vector clustering on multiple hidden state answer vectors to obtain K semantic vectors of candidate answers.
[0161] Specific limitations regarding the question-answering matching device based on the K-means clustering algorithm can be found in the limitations of the question-answering matching method based on the K-means clustering algorithm mentioned above, and will not be repeated here. Each module in the aforementioned question-answering matching device based on the K-means clustering algorithm can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in the processor of a computer device in hardware form or independent of it, or stored in the memory of a computer device in software form, so that the processor can call and execute the corresponding operations of each module.
[0162] In one embodiment, a computer device is provided, which may be a server, and its internal structure diagram may be as follows: Figure 10 As shown, the computer device includes a processor, memory, network interface, and database connected via a system bus. The processor provides computational and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system, computer programs, and database. The internal memory provides an environment for the operation of the operating system and computer programs stored in the non-volatile storage media. The database stores relevant data used and generated by a question-answering matching method based on the K-means clustering algorithm. The network interface communicates with external terminals via a network connection. When executed by the processor, the computer program implements a question-answering matching method based on the K-means clustering algorithm.
[0163] In one embodiment, a computer device is provided, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to perform the following steps:
[0164] Obtain the user's input question and perform vector transformation on the user question to obtain the question vector;
[0165] Multiple candidate answers to the user's question are determined according to a preset matching method, and K semantic vectors for each candidate answer are determined. The K semantic vectors are semantic vectors obtained by performing vector clustering on the candidate answers based on the K-means clustering algorithm.
[0166] Based on the question vector, perform attention-based vector computation on the semantic vectors of K answers to obtain candidate answer vectors for the candidate answers;
[0167] Based on the matching value between the candidate answer vector and the question vector, the correct answer to the user's question is determined from multiple candidate answers.
[0168] In one embodiment, a computer-readable storage medium is provided having a computer program stored thereon, the computer program performing the following steps when executed by a processor:
[0169] Obtain the user's input question and perform vector transformation on the user question to obtain the question vector;
[0170] Multiple candidate answers to the user's question are determined according to a preset matching method, and K semantic vectors for each candidate answer are determined. The K semantic vectors are semantic vectors obtained by performing vector clustering on the candidate answers based on the K-means clustering algorithm.
[0171] Based on the question vector, perform attention-based vector computation on the semantic vectors of K answers to obtain candidate answer vectors for the candidate answers;
[0172] Based on the matching value between the candidate answer vector and the question vector, the correct answer to the user's question is determined from multiple candidate answers.
[0173] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium, and when executed, it can include the processes of the embodiments of the above methods. Any references to memory, storage, databases, or other media used in the embodiments provided in this application can include non-volatile and / or volatile memory. Non-volatile memory can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM), or flash memory. Volatile memory can include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in various forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), dual data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link DRAM (SLDRAM), Rambus direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM), etc.
[0174] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the above-described division of functional units and modules is used as an example. In practical applications, the above functions can be assigned to different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above.
[0175] The above-described embodiments are only used to illustrate the technical solutions of the present invention, and are not intended to limit it. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention, and should all be included within the protection scope of the present invention.
Claims
1. A question and answer matching method based on a K-means clustering algorithm, characterized by, The method comprises the following steps: obtaining a user question input by a user, and performing vector conversion on the user question to obtain a question vector of the user question; determining a plurality of candidate answers of the user question according to a preset matching method, and determining K answer semantic vectors of each of the candidate answers, wherein the K answer semantic vectors are semantic vectors obtained by performing vector clustering on the candidate answers based on a K-means clustering algorithm; performing vector calculation on the K answer semantic vectors based on an attention mechanism according to the question vector to obtain a candidate answer vector of the candidate answer; determining a correct answer of the user question from the plurality of candidate answers according to a matching value of the candidate answer vector and the question vector; the step of performing vector calculation on the K answer semantic vectors based on an attention mechanism according to the question vector to obtain a candidate answer vector of the candidate answer comprises the following steps: determining an initial weight of each of the answer semantic vectors to obtain an initial weight matrix; determining a first weight matrix according to the initial weight matrix and the K answer semantic vectors, wherein a product of each answer semantic vector and the initial weight of the answer semantic vector is taken as a first weight; performing normalization processing on the first weight matrix to obtain a target weight of each of the answer semantic vectors; performing weighted summation on the K answer semantic vectors according to the target weight of the answer semantic vector to obtain the candidate answer vector of the candidate answer; the K answer semantic vectors of the candidate answer are obtained by the following steps: performing single-character segmentation on the candidate answer to obtain a word vector sequence of the candidate answer; inputting the word vector sequence of the candidate answer into a BERT model for encoding to obtain a plurality of hidden state answer vectors output by the BERT model; performing vector clustering on the plurality of hidden state answer vectors based on the K-means clustering algorithm to obtain the K answer semantic vectors of the candidate answer.
2. The method of claim 1, wherein the K-means clustering algorithm is based on, the step of performing vector conversion on the user question to obtain a question vector of the user question comprises the following steps: performing single-character segmentation on the user question to obtain a word vector sequence of the user question; inputting the word vector sequence of the user question into a BERT model to obtain a plurality of hidden state question vectors output by the BERT model; performing aggregation on the plurality of hidden state question vectors based on a preset aggregation manner to obtain the question vector of the user question.
3. The method of claim 1, wherein the K-means clustering algorithm is based on, the step of determining a correct answer of the user question from the plurality of candidate answers according to a matching value of the candidate answer vector and the question vector comprises the following steps: determining whether the matching value of the candidate answer vector and the question vector is less than a preset matching threshold value; if the matching value is greater than or equal to the preset matching threshold value, taking the candidate answer corresponding to the matching value as a target answer; determining whether the number of target answers in the plurality of candidate answers is greater than 1; if the number of target answers is equal to 1, taking the target answer as the correct answer of the user question.
4. The method of claim 3, wherein the K-means clustering algorithm is based on, After the determining whether the number of the target answers in the plurality of candidate answers is greater than 1, the method further comprises: If the number of the target answers is greater than 1, sorting the plurality of target answers in ascending order according to the size of the matching value to obtain a target answer list; Taking the candidate answer ranked first in the target answer list as the correct answer of the user question.
5. The method of claim 1-4, wherein, The method further comprises: obtaining a plurality of standard answers stored in a database, and determining a named entity in the user question; matching the plurality of standard answers according to the named entity, taking the standard answer containing the named entity as the candidate answer of the user question, and obtaining a plurality of candidate answers; extracting K answer semantic vectors of each candidate answer in the database.
6. A question and answer matching device based on a K-means clustering algorithm, characterized by, The method further comprises: a conversion module configured to obtain a user question input by a user, and perform vector conversion on the user question to obtain a question vector of the user question; a first determination module configured to determine a plurality of candidate answers of the user question according to a preset matching method, and determine K answer semantic vectors of each candidate answer, wherein the K answer semantic vectors are semantic vectors obtained by performing vector clustering on the candidate answer based on a K-means clustering algorithm; a calculation module configured to perform vector calculation on the K answer semantic vectors based on an attention mechanism according to the question vector to obtain a candidate answer vector of the candidate answer; a second determination module configured to determine a correct answer of the user question from the plurality of candidate answers according to a matching value between the candidate answer vector and the question vector; The calculation module is specifically configured to: determine an initial weight of each answer semantic vector to obtain an initial weight matrix; determine a first weight matrix according to the initial weight matrix and the K answer semantic vectors, wherein a product between each answer semantic vector and the initial weight of the answer semantic vector is taken as a first weight; perform normalization processing on the first weight matrix to obtain a target weight of each answer semantic vector; perform weighted summation on the K answer semantic vectors according to the target weight of the answer semantic vector to obtain the candidate answer vector of the candidate answer; The first determination module is specifically configured to obtain the K answer semantic vectors of the candidate answer by the following manner: perform single-character segmentation on the candidate answer to obtain a character vector sequence of the candidate answer; input the character vector sequence of the candidate answer into a BERT model for encoding to obtain a plurality of hidden state answer vectors output by the BERT model; perform vector clustering on the plurality of hidden state answer vectors based on the K-means clustering algorithm to obtain the K answer semantic vectors of the candidate answer.
7. A computer device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, The processor executes the computer program to implement the steps of the question and answer matching method based on the K-means clustering algorithm according to any one of claims 1 to 5.
8. A computer-readable storage medium storing a computer program, the computer-readable storage medium comprising: The computer program is executed by the processor to implement the steps of the question and answer matching method based on the K-means clustering algorithm according to any one of claims 1 to 5.
Citation Information
Patent Citations
Semantic matching method and device for question and answer text, medium and electronic equipment
CN109726396A
Question answering method and device, computer equipment and storage medium
CN112632248A