Short text matching method based on matching model, storage medium and electronic device
By introducing normalization of keyword vectors and weight vectors and optimizing the first attention mechanism layer in the short text matching model, the problems of low training complexity and low accuracy of existing models are solved, and the accuracy of user intent recognition in smart home scenarios is improved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-06-29
- Publication Date
- 2026-03-20
AI Technical Summary
Existing short text matching models are complex to train and have low matching accuracy, making it difficult to effectively utilize information such as the importance of keywords in short texts, synonyms, similar-looking words, and homophones.
By obtaining the keyword vectors, part-of-speech vectors, and weight vectors of the text, and performing normalization processing, the keyword relevance and similarity matrices are calculated. Prior keyword knowledge is introduced into the first attention mechanism layer of the matching model to optimize the scoring function matrix and simplify the training process.
It simplifies the training process of short text matching models and improves matching accuracy, especially in smart home scenarios, enhancing the accuracy of user intent recognition.
Smart Images

Figure CN115292446B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the field of natural language processing, in particular to a short text matching method based on a matching model, a storage medium and an electronic device. BACKGROUND
[0002] In the field of natural language processing, short text matching tasks are often involved, such as similar question judgment, question and answer systems, etc. There are two difficulties in the task of short text matching. One is short, and there is less information that can be used, such as context, which needs to rely on some external knowledge to help the machine understand. The second difficulty is the diversity of language expression forms, which makes the problem sensitive to key word information. The existing recognition algorithm can perform attention mechanism operation on the key word information between two texts to enhance their mutual information. However, in the training process of the matching model, new training tasks usually need to be introduced to realize the integration of prior knowledge and improve the mutual information, resulting in a complex training process and low matching accuracy.
[0003] There is a need for a short text matching method based on a matching model to simplify the training process of the short text matching model and improve the accuracy of short text matching. SUMMARY
[0004] The present application provides a short text matching method based on a matching model, a storage medium and an electronic device to simplify the training process of the short text matching model and improve the accuracy of short text matching
[0005] In a first aspect, the present application provides a short text matching method based on a matching model, comprising:
[0006] obtaining a first key word vector, a first part of speech vector and a first weight vector of a first text; and obtaining a second key word vector, a second part of speech vector and a second weight vector of a second text;
[0007] normalizing the first weight vector and the second weight vector respectively, and calculating the correlation degree of the normalized first weight vector and the second weight vector to obtain a key word correlation matrix; and calculating the similarity of the first key word vector and the second key word vector to obtain a key word similarity matrix;
[0008] point-multiplying the key word correlation matrix and the key word similarity matrix to obtain a weighted key word similarity matrix;
[0009] inputting the first text and the second text into a matching model, and obtaining a short text matching result of the first text and the second text output by the matching model based on the weighted key word similarity matrix.
[0010] Optionally, the matching model is a keyword-based transformer-based bidirectional encoding representation Keyword-BERT network model; an embedding layer of the matching model is configured to: obtain a third keyword vector in numerical form according to the first keyword vector and the second keyword vector;
[0011] A first attention mechanism layer of the matching model is configured to:
[0012] According to the third keyword vector, obtain a query matrix, a key matrix and a value matrix; and generate an original scoring function matrix according to the query matrix and the key matrix;
[0013] An optimized scoring function matrix is obtained by calculating the dot product of the original scoring function matrix and the weighted keyword similarity matrix;
[0014] The optimized scoring function matrix and a mask matrix are normalized to output the product of the normalized calculation result and the value matrix.
[0015] Optionally, the keyword relevance matrix is obtained by calculating the correlation between the first weight vector and the second weight vector after normalization, including:
[0016] The first weight vector after normalization or the second weight vector after normalization is transposed based on a transposition strategy; wherein the transposition strategy includes: if the first weight vector after normalization and the second weight vector after normalization are both row vectors, then the first weight vector after normalization is transposed; if the first weight vector after normalization and the second weight vector after normalization are both column vectors, then the second weight vector after normalization is transposed.
[0017] The first weight vector and the second weight vector obtained by the transposition are multiplied to obtain the keyword relevance matrix.
[0018] Optionally, the keyword similarity matrix is obtained by calculating the similarity between the first keyword vector and the second keyword vector, including:
[0019] If each element in the first keyword vector and the second keyword vector is Chinese, then the similarity between each keyword in the first keyword vector and each keyword in the second keyword vector is calculated using a word2vec tool to obtain the keyword similarity matrix.
[0020] If each element in the first keyword vector and the second keyword vector is English, the similarity between each keyword in the first keyword vector and each keyword in the second keyword vector is calculated by using a Hownet tool to obtain the keyword similarity matrix.
[0021] Optionally, the first similarity calculation module is further configured to:
[0022] If the keywords in the first keyword vector and the keywords in the second keyword vector are synonyms or homophones, the similarity of the elements corresponding to the synonyms or homophones is set to 1.
[0023] In a second aspect, the present application provides a short text matching device based on a matching model, comprising:
[0024] A word segmentation module is configured to obtain a first keyword vector, a first part-of-speech vector and a first weight vector of a first text, and obtain a second keyword vector, a second part-of-speech vector and a second weight vector of a second text.
[0025] A first similarity calculation module is configured to normalize the first weight vector and the second weight vector respectively, and calculate the correlation of the normalized first weight vector and the second weight vector to obtain a keyword relevance matrix, and calculate the similarity of the first keyword vector and the second keyword vector to obtain a keyword similarity matrix.
[0026] A second similarity calculation module is configured to perform point multiplication calculation on the keyword relevance matrix and the keyword similarity matrix to obtain a weighted keyword similarity matrix.
[0027] A matching module is configured to input the first text and the second text into a matching model, and obtain a short text matching result of the first text and the second text output by the matching model based on the weighted keyword similarity matrix.
[0028] Optionally, the matching model is a Keyword-BERT network model based on a keyword converter, and an embedding layer of the matching model is configured to obtain a third keyword vector in numerical form according to the first keyword vector and the second keyword vector.
[0029] A first attention mechanism layer of the matching model is configured to:
[0030] According to the third keyword vector, a query matrix, a key matrix and a value matrix are obtained, and an original scoring function matrix is generated according to the query matrix and the key matrix.
[0031] obtaining an optimized scoring function matrix by calculating a dot product result of the original scoring function matrix and the weighted keyword similarity matrix;
[0032] performing a normalization calculation on the optimized scoring function matrix and the mask matrix, and outputting a multiplication result of the normalization calculation result and the value matrix.
[0033] Optionally, the first similarity calculation module is configured to:
[0034] performing transpose processing on the normalized first weight vector or the normalized second weight vector based on a transpose strategy, wherein the transpose strategy comprises: if the normalized first weight vector and the normalized second weight vector are both row vectors, transposing the normalized first weight vector; or if the normalized first weight vector and the normalized second weight vector are both column vectors, transposing the normalized second weight vector.
[0035] performing matrix multiplication on the first weight vector and the second weight vector obtained through the transpose processing, to obtain the keyword relevance matrix.
[0036] Optionally, the first similarity calculation module is configured to:
[0037] if each element in the first keyword vector and the second keyword vector is Chinese, calculating the similarity between each keyword in the first keyword vector and each keyword in the second keyword vector by using a word2vec tool, to obtain the keyword similarity matrix.
[0038] if each element in the first keyword vector and the second keyword vector is English, calculating the similarity between each keyword in the first keyword vector and each keyword in the second keyword vector by using a Hownet tool, to obtain the keyword similarity matrix.
[0039] Optionally, the first similarity calculation module is further configured to:
[0040] if a keyword in the first keyword vector and a keyword in the second keyword vector are synonyms or homophones, setting the similarity of the elements corresponding to the synonyms or homophones to 1.
[0041] In a third aspect, the present application provides a computer readable storage medium, which comprises a stored program, wherein the program performs the method of the first aspect when running.
[0042] In a fourth aspect, the present application provides an electronic device comprising a memory and a processor, wherein the memory stores a computer program, and the processor is configured to execute the method according to the first aspect by using the computer program.
[0043] The present application provides a short text matching method based on a matching model, a storage medium and an electronic device, comprising: obtaining a first keyword vector, a first part-of-speech vector and a first weight vector of a first text; obtaining a second keyword vector, a second part-of-speech vector and a second weight vector of a second text; performing normalization processing on the first weight vector and the second weight vector respectively, and calculating the correlation degree of the normalized first weight vector and the second weight vector to obtain a keyword correlation matrix; calculating the similarity of the first keyword vector and the second keyword vector to obtain a keyword similarity matrix; performing point multiplication calculation on the keyword correlation matrix and the keyword similarity matrix to obtain a weighted keyword similarity matrix; inputting the first text and the second text into a matching model, and obtaining a short text matching result of the first text and the second text output by the matching model based on the weighted keyword similarity matrix. By performing word segmentation on the text, the keyword vector and the weight vector of the two texts are extracted respectively, and then the weighted keyword similarity matrix is calculated, the scoring result of the first attention mechanism layer of the original Keyword-BERT network model is optimized, the importance of the keyword in the short text, the synonym, the homonym and other information are directly introduced to guide the model, the addition of other training processes is avoided, the training process of the model is simplified, and the accuracy of the short text matching is improved. BRIEF DESCRIPTION OF DRAWINGS
[0044] The accompanying drawings, which are incorporated herein and form part of the specification, illustrate embodiments consistent with the present application and, together with the description, further serve to explain the principles of the application.
[0045] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the prior art, the accompanying drawings needed to be used in the embodiments or prior art description will be briefly introduced. Obviously, for those skilled in the art, other drawings can also be obtained based on these drawings without creative labor.
[0046] Figure 1 The application scenario diagram provided for the examples of the present application;
[0047] Figure 2 The short text matching process diagram provided for the first embodiment of the present application based on a matching model;
[0048] Figure 3A short text matching flowchart based on a matching model provided for Embodiment Two of the present application is shown in FIG. 2;
[0049] Figure 4 A short text matching flowchart based on a matching model provided for Embodiment Three of the present application is shown in FIG. 3;
[0050] Figure 5 A short text matching flowchart based on a matching model provided for Embodiment Four of the present application is shown in FIG. 4;
[0051] Figure 6 A short text matching flowchart based on a matching model provided for Embodiment Four of the present application is shown in FIG. 4;
[0052] Figure 7 A short text matching flowchart based on a matching model provided for Embodiment Five of the present application is shown in FIG. 5;
[0053] Figure 8 A short text matching flowchart based on a matching model provided for Embodiment Six of the present application is shown in FIG. 6. DETAILED DESCRIPTION
[0054] In order to make the personnel in the art better understand the present application, the technical solutions in the embodiments of the present application will be described clearly and completely below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative work should fall within the scope of protection of the present application.
[0055] It should be noted that the terms "first", "second", and the like in the specification and claims of the present application and the above-described drawings are used to distinguish similar objects, and do not necessarily indicate a specific order or a chronological sequence. It should be understood that the data thus used can be interchanged under appropriate circumstances, so that the embodiments of the present application described herein can be implemented in an order other than that illustrated or described herein. In addition, the terms "include" and "have" and any variations thereof are intended to cover non-exclusive inclusion, for example, a process, method, system, product or device that includes a series of steps or units does not necessarily have to be limited to those steps or units clearly listed, but can include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0056] Short text matching tasks are often involved in the field of natural language processing, such as similar question judgment, question and answer system, etc. There are two difficulties in the task of short text matching. One is short, and there is less information that can be used, such as context, which needs to rely on some external knowledge to help the machine understand. The second difficulty is the diversity of language expression form, which makes the problem sensitive to key word information. In the field of smart home, since the text information input by the user is usually not long, if the device wants to accurately identify the user's intention, it needs to accurately match the short text.
[0057] A commonly used short text matching model is a bidirectional encoder representation from transformers (BERT) based on a converter. The input text is converted into a numerical form vector through multiple (usually three) embedding layers, and then multiple serial Transformer layers are used to complete recognition and matching, and finally output. The Transformer layer includes an attention mechanism layer and a full connection layer connected in series to realize transformation and output. The existing recognition algorithm can perform attention mechanism operation on the key word information between two texts to enhance their mutual information. However, in the training process of the matching model, new training tasks need to be introduced to integrate prior knowledge and improve mutual information, resulting in a complex training process and low matching accuracy.
[0058] Figure 1 The application scenario diagram provided for the examples of the present application is used to illustrate the technical concept of the present application. The integration of prior knowledge is usually in the training process, and a keyword layer is introduced in the attention mechanism layer of the Transformer layer. The model shown is an improved model of an existing BERT, keyword bidirectional encoder representation from transformers (Keyword-BERT). An additional keyword prior knowledge layer is introduced in the last Transformer layer of BERT. Through attention mechanism calculation and random mask covering, the keyword information between the two texts is operated by attention mechanism between each other, the mutual information is enhanced, the representations of the two output texts are fused, and the fusion result and the output label CLS of the first embedding layer (flag embedding layer) are output to the classification layer. Through such modification, the Keyword-BERT is superior to the original BERT in various indicators at different layers.
[0059] However, the effect of the keyword priori knowledge layer in different positions in the model is usually different. The closer to the embedding layer, the greater the impact on the training process, and the closer to the output layer, the greatest impact on the output. The existing improvement technology is shown in the figure, which is modified at the output layer, but other training processes are also integrated into the training process to ensure sufficient priori knowledge, otherwise, the accuracy of the modification of only the last layer is usually not enough in the smart home scene short text matching task. Complex features such as part of speech, character shape, word meaning, and word sound need to be used, in other words, the importance of keywords in short texts, the relevance of synonyms, and information such as "login" and "landing" in the training of Keyword-BERT are less applied, resulting in insufficient influence of these information in the training process. Therefore, the keyword priori knowledge layer can be introduced into the first Transformer layer of the BERT model as shown in the figure to directly inject the keyword similarity knowledge into the model to directly guide the entire training process and simplify the complexity of model training.
[0060] The technical solutions of the present application and the technical solutions of the present application will be 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 described again in some embodiments. In the description of the present application, unless otherwise explicitly specified and limited, each term should be understood in a broad sense within the art. The embodiments of the present application will be described below with reference to the accompanying drawings.
[0061] Embodiment one
[0062] Figure 2 A short text matching method based on a matching model provided by Embodiment One of the present application is shown in the flowchart as Figure 2 The method comprises:
[0063] S101: obtaining a first keyword vector, a first part-of-speech vector, and a first weight vector of a first text; and obtaining a second keyword vector, a second part-of-speech vector, and a second weight vector of a second text;
[0064] S102: normalizing the first weight vector and the second weight vector, respectively, and calculating the correlation degree of the normalized first weight vector and the second weight vector to obtain a keyword correlation matrix;
[0065] S103: obtaining a keyword similarity matrix by calculating the similarity of the first keyword vector and the second keyword vector;
[0066] S104: point-multiplying the keyword correlation matrix and the keyword similarity matrix to obtain a weighted keyword similarity matrix;
[0067] S105: input the first text and the second text into a matching model, obtain a short text matching result of the first text and the second text output by the matching model based on the weighted keyword similarity matrix.
[0068] In the field of natural language processing, it is often necessary to convert input text information into numerical or vector form for processing. This process is mainly completed by multiple embedding layers in the BERT series model. The embedding layers usually have three, including a token embedding layer that converts input text into a vector form, usually a 768-dimensional vector, for word and sentence vector representation; a sentence embedding layer or division embedding layer that marks the front and back two sentences with different marks of 0 and 1, and marks all as 0 if there is only one sentence to distinguish the vector representation of two different sentences; and a position embedding layer that distinguishes the position of different words in a sentence and marks the order of each word, i.e., the order attribute of the input. Finally, the three embedding layers are added element by element to obtain a composite representation as the input of the BERT series model.
[0069] Therefore, the input text needs to be segmented to obtain the first keyword vector, the first part-of-speech vector, and the first weight vector of the first text; and the second keyword vector, the second part-of-speech vector, and the second weight vector of the second text. The keyword vector is mainly used to calculate the keyword similarity matrix, which can be completed by the BERT model. At the same time, the weight vector can be used to modify the model and introduce the weight vector to incorporate prior knowledge to guide the training of the BERT model. Specifically, the first weight vector and the second weight vector are normalized, and the correlation between the normalized first weight vector and the second weight vector is calculated to obtain a keyword relevance matrix; and the similarity between the first keyword vector and the second keyword vector is calculated to obtain a keyword similarity matrix. Normalization is usually max-min normalization, for example, the first weight vector is (1, 3, 2, 3), which can be normalized to (0, 1, 0.5, 1), and the second weight vector is (1, 2, 2, 3, 1), which can be normalized to (0, 0.5, 0.5, 1, 0). The maximum value in the vector is set to 1 and the minimum value is set to 0. Normalize the vector to convert all elements in the vector to a value between 0 and 1. The part-of-speech vector can be used to adjust the weight. Through specific dictionaries or specific algorithms in the field, the weight can be adjusted, for example, if both are nouns, the weight is increased by 0.2.
[0070] After calculating the two similarity matrices, the information can be integrated into the model. Specifically, the first similarity matrix and the second similarity matrix are point multiplied to obtain a weighted keyword similarity matrix; the first text and the second text are input into a matching model, and based on the weighted keyword similarity matrix, the short text matching result of the first text and the second text output by the matching model is obtained; wherein the matching model is a Keyword-BERT network model, and the first attention mechanism layer of the matching model is used to obtain an original scoring function matrix QK T , a value matrix V and a mask matrix mask, and performing normalization calculation on the optimized scoring function matrix and the mask matrix, and calculating the product result of the normalization calculation result and the value matrix. The optimized scoring function matrix is the point multiplication result of the original scoring function matrix and the weighted keyword similarity matrix. In each application of the pre-trained Keyword-BERT network model, the current weighted keyword similarity matrix needs to be calculated, and the model is improved adaptively to meet the current application scenario.
[0071] The embodiment provides a short text matching method based on a matching model, including: obtaining a first keyword vector, a first part-of-speech vector and a first weight vector of a first text; and obtaining a second keyword vector, a second part-of-speech vector and a second weight vector of a second text; performing normalization processing on the first weight vector and the second weight vector respectively, and calculating the correlation degree of the normalized first weight vector and the second weight vector to obtain a keyword relevance matrix; and calculating the similarity of the first keyword vector and the second keyword vector to obtain a keyword similarity matrix; performing point multiplication calculation on the keyword relevance matrix and the keyword similarity matrix to obtain a weighted keyword similarity matrix; inputting the first text and the second text into a matching model, and based on the weighted keyword similarity matrix, obtaining a short text matching result of the first text and the second text output by the matching model. By performing word segmentation on the text, the keyword vector and the weight vector of the two texts are extracted, and then the weighted keyword similarity matrix is calculated, the scoring result of the first attention mechanism layer of the original Keyword-BERT network model is optimized, the importance of keywords in short texts, synonymous words, similar words, homophonic words and other information are directly introduced to guide the model, avoiding the addition of other training processes, simplifying the model training process, and improving the accuracy of short text matching.
[0072] Embodiment two
[0073] Figure 3A short text matching method based on a matching model provided for Embodiment Two of the present application is shown in a flowchart as Figure 3 As shown in the flowchart, on the basis of any other embodiment, the matching model is a Keyword-BERT network model based on a converter for bidirectional encoding of keywords; the embedding layer of the matching model is configured to obtain a third keyword vector in numerical form according to the first keyword vector and the second keyword vector;
[0074] The first attention mechanism layer of the matching model is configured to:
[0075] S201: Obtain a query matrix, a key matrix and a value matrix according to the third keyword vector; and generate an original scoring function matrix according to the query matrix and the key matrix;
[0076] S202: Obtain an optimized scoring function matrix by calculating the dot product of the original scoring function matrix and the weighted keyword similarity matrix;
[0077] S203: Perform normalization calculation on the optimized scoring function matrix and the mask matrix, and output the product of the result of the normalization calculation and the value matrix.
[0078] The present embodiment is exemplarily described in combination with a specific application scenario: the matching model is improved based on the BERT model, which is an optimized Keyword-BERT model, but the difference from the existing Keyword-BERT model is that the keyword priori knowledge is introduced into the first Transformer layer instead of the last one. Therefore, the matching model also needs an embedding layer for word-to-vector, i.e., the embedding layer of the matching model is configured to obtain a third keyword vector in numerical form according to the first keyword vector and the second keyword vector. For the improved first attention mechanism layer, the first attention mechanism layer of the matching model is configured to obtain a query matrix Q, a key matrix K and a value matrix V according to the third keyword vector; and generate an original scoring function matrix according to the query matrix and the key matrix; obtain an optimized scoring function matrix by calculating the dot product of the original scoring function matrix and the weighted keyword similarity matrix; and perform normalization calculation on the optimized scoring function matrix and the mask matrix, and output the product of the result of the normalization calculation and the value matrix. That is, according to each input, an optimized scoring function matrix under the current scenario is obtained, and the original scoring function matrix is replaced with the optimized scoring function matrix to realize the optimization of model training.
[0079] The embodiment provides a short text matching method based on a matching model. An embedding layer of the matching model is used to: according to the first keyword vector and the second keyword vector, a third keyword vector in a numerical form is obtained; a first attention mechanism layer of the matching model is used to: according to the third keyword vector, a query matrix, a key matrix and a value matrix are obtained; and according to the query matrix and the key matrix, an original scoring function matrix is generated. By calculating the dot product of the original scoring function matrix and the keyword similarity matrix with weights, an optimized scoring function matrix is obtained; the optimized scoring function matrix and a mask matrix are subjected to normalization calculation, and a product result of the normalization calculation result and the value matrix is output. By optimizing the scoring function of the first attention mechanism layer of the matching model, the guiding information of the keyword priori knowledge is integrated into the model, and the accuracy of the matching model is improved.
[0080] Embodiment three
[0081] Figure 4 A short text matching method based on a matching model provided for the embodiment three of the application is shown in the flowchart as shown in the figure, Figure 4 The S102 specifically includes the following steps on the basis of any other embodiment:
[0082] S301: based on a transposition strategy, the first weight vector after normalization or the second weight vector after normalization is subjected to transposition processing;
[0083] S302: the first weight vector and the second weight vector obtained by the transposition processing are subjected to matrix multiplication, and the keyword relevance matrix is obtained.
[0084] The embodiment is exemplarily described in combination with a specific application scenario: in order to generate the keyword relevance matrix, since the first weight vector after normalization and the second weight vector after normalization have the same row and column nature, the first weight vector after normalization or the second weight vector after normalization is subjected to transposition processing through the transposition strategy, so as to ensure the mathematical feasibility, that is, to ensure that the final calculation mode is the vector multiplication of the column vector and the row vector, but the actual sequence of the first weight vector or the second weight vector is not limited. Specifically, based on the transposition strategy, the first weight vector after normalization or the second weight vector after normalization is subjected to transposition processing; wherein the transposition strategy includes: if the first weight vector after normalization and the second weight vector after normalization are both row vectors, the first weight vector after normalization is subjected to transposition; if the first weight vector after normalization and the second weight vector after normalization are both column vectors, the second weight vector after normalization is subjected to transposition; the first weight vector and the second weight vector obtained by the transposition processing are subjected to matrix multiplication, and the keyword relevance matrix is obtained.
[0085] This embodiment provides a short text matching method based on a matching model. The step of calculating the correlation between a normalized first weight vector and a second weight vector to obtain a keyword relevance matrix includes: transposing either the normalized first weight vector or the normalized second weight vector using a transpose strategy; and performing matrix multiplication between the transposed first weight vector and the second weight vector to obtain the keyword relevance matrix. By using a transpose strategy to transpose either the normalized first weight vector or the normalized second weight vector, the mathematical feasibility of generating the keyword relevance matrix is ensured.
[0086] Example 4
[0087] Figure 5 This is a flowchart illustrating a short text matching method based on a matching model, as provided in Embodiment 4 of this application. Figure 5 As shown, based on any other embodiment, S103 specifically includes:
[0088] S401: If all elements in the first keyword vector and the second keyword vector are Chinese characters, then use the word2vec tool to calculate the similarity between each keyword in the first keyword vector and each keyword in the second keyword vector to obtain the keyword similarity matrix;
[0089] S402: If all elements in the first keyword vector and the second keyword vector are in English, then use the Hownet tool to calculate the similarity between each keyword in the first keyword vector and each keyword in the second keyword vector to obtain the keyword similarity matrix.
[0090] This embodiment will be illustrated with specific application scenarios: the methods for calculating keyword similarity differ between Chinese and English application scenarios. Generally, in Chinese contexts, the word-to-text tool word2vec performs better, while in English and similar language contexts, the Hownet tool from CNKI performs better. However, it should be noted that word2vec and Hownet can also be applied to other contexts; each tool has its preferred scenario. word2vec performs better in Chinese or predominantly Chinese texts, while Hownet performs better in alphabetic languages such as English and French.
[0091] For text, it can be pre-classified first to determine the model category that should be selected. If each element in the first keyword vector and the second keyword vector is Chinese, the similarity between each keyword in the first keyword vector and each keyword in the second keyword vector is calculated using the word2vec tool to obtain the keyword similarity matrix; if each element in the first keyword vector and the second keyword vector is English, the similarity between each keyword in the first keyword vector and each keyword in the second keyword vector is calculated using the Hownet tool to obtain the keyword similarity matrix. In fact, for specific text, sometimes not all words belong to the same language. Each element can be identified by the language it belongs to, or the language of the key word to be highlighted or the specific context can be identified, for example, a mixed Chinese-English sentence usually highlights the meaning expressed by English words or English abbreviations in the sentence, so even if the Chinese proportion in the text is high at this time, the Hownet tool can be used to obtain the keyword similarity matrix.
[0092] An example, Figure 6 Another short text matching method based on a matching model is provided for Embodiment Four of the present application, and a flowchart is shown in FIG. 10. Figure 6 As shown in FIG. 10, after S103, the method further includes:
[0093] S403: If the keyword in the first keyword vector and the keyword in the second keyword vector are synonyms or homophones, the similarity of the corresponding elements of the synonyms or homophones is set to 1.
[0094] For synonyms or homophones, in the application scenario of smart home, they usually have a high degree of relevance. A general keyword similarity setting method is to set the relative elements of synonyms or homophones to 1. Specifically, if the keyword in the first keyword vector and the keyword in the second keyword vector are synonyms or homophones, the similarity of the corresponding elements of the synonyms or homophones is set to 1. By adaptively adjusting the corresponding elements of synonyms and homophones in the keyword similarity matrix, the relevance of keyword prior knowledge and actual application scenarios is improved, and the accuracy of the matching model is further improved.
[0095] The embodiment provides a short text matching method based on a matching model, and the method comprises the following steps: obtaining a keyword similarity matrix by calculating the similarity of a first keyword vector and a second keyword vector, wherein if each element in the first keyword vector and the second keyword vector is Chinese, the similarity between each keyword in the first keyword vector and each keyword in the second keyword vector is calculated by using a word2vec tool to obtain the keyword similarity matrix; if each element in the first keyword vector and the second keyword vector is English, the similarity between each keyword in the first keyword vector and each keyword in the second keyword vector is calculated by using a Hownet tool to obtain the keyword similarity matrix. The similarity of keywords is calculated by using different tools in different contexts to obtain the keyword similarity matrix, important words in the text are accurately divided, the applicability of the keyword similarity matrix is improved, and the effect of the matching model is improved.
[0096] Embodiment five
[0097] The embodiment five also provides a short text matching device based on a matching model to realize the foregoing method. Figure 7 A structural schematic diagram of the short text matching device based on the matching model provided by the embodiment five is shown in the figure. Figure 7 The device comprises the following components on the basis of any one of the foregoing embodiments:
[0098] The word segmentation module 51 is configured to obtain a first keyword vector, a first part-of-speech vector and a first weight vector of a first text, and obtain a second keyword vector, a second part-of-speech vector and a second weight vector of a second text.
[0099] The first similarity calculation module 52 is configured to perform normalization processing on the first weight vector and the second weight vector respectively, calculate the correlation degree of the normalized first weight vector and the second weight vector to obtain a keyword relevance matrix, and calculate the similarity of the first keyword vector and the second keyword vector to obtain a keyword similarity matrix.
[0100] The second similarity calculation module 53 is configured to perform point multiplication calculation on the keyword relevance matrix and the keyword similarity matrix to obtain a weighted keyword similarity matrix.
[0101] The matching module 54 is configured to input the first text and the second text into a matching model, obtain a short text matching result of the first text and the second text output by the matching model based on the weighted keyword similarity matrix.
[0102] An example, the matching model is a keyword based on converter bidirectional encoding representation Keyword-BERT network model; the embedding layer of the matching model is used for: according to the first keyword vector and the second keyword vector, obtain the third keyword vector in numerical form;
[0103] The first attention mechanism layer of the matching model is used for:
[0104] According to the third keyword vector, obtain query matrix, key matrix and value matrix;And according to the query matrix and the key matrix, generate the original scoring function matrix;
[0105] By calculating the dot product of the original scoring function matrix and the weighted keyword similarity matrix, an optimized scoring function matrix is obtained;
[0106] The normalized calculation result of the optimized scoring function matrix and the mask matrix is output, and the product result of the value matrix is output.
[0107] By calculating the dot product of the original scoring function matrix and the weighted keyword similarity matrix, an optimized scoring function matrix is obtained;The normalized calculation result of the optimized scoring function matrix and the mask matrix is output, and the product result of the value matrix is output.By optimizing the scoring function of the first attention mechanism layer of the matching model, the guiding information of the keyword priori knowledge is integrated into the model, and the accuracy of the matching model is improved.
[0108] An example, the first similarity calculation module 52 is used for:
[0109] Based on the transposition strategy, the first weight vector after normalization or the second weight vector after normalization is transposed;Wherein the transposition strategy includes if the first weight vector after normalization and the second weight vector after normalization are both row vectors, then the first weight vector after normalization is transposed;If the first weight vector after normalization and the second weight vector after normalization are both column vectors, then the second weight vector after normalization is transposed;
[0110] The first weight vector and the second weight vector obtained by the transposition processing are multiplied by matrix to obtain the keyword relevance matrix.
[0111] By transposition strategy, one of the first weight vector after normalization or the second weight vector after normalization is transposed, to ensure the mathematical feasibility of generating keyword relevance matrix process.
[0112] An example, the first similarity calculation module 52 is also used for:
[0113] If each element in the first keyword vector and the second keyword vector is Chinese, the similarity between each keyword in the first keyword vector and each keyword in the second keyword vector is calculated using the word2vec tool to obtain the keyword similarity matrix.
[0114] If each element in the first keyword vector and the second keyword vector is English, the similarity between each keyword in the first keyword vector and each keyword in the second keyword vector is calculated using the Hownet tool to obtain the keyword similarity matrix.
[0115] The method of calculating keyword similarity is different for Chinese application scenarios and English application scenarios. Generally, in the Chinese context, the word-to-vector tool word2vec has good results, while in the context of English and similar languages, the Hownet tool has better results. By using different tools to calculate the similarity of keywords in different contexts to obtain the keyword similarity matrix, it ensures that important words in the text are accurately divided, improves the applicability of the keyword similarity matrix, and further improves the effect of the matching model.
[0116] In an example, the first similarity calculation module 52 is further configured to:
[0117] If the keyword in the first keyword vector and the keyword in the second keyword vector are synonyms or homophones, the similarity of the elements corresponding to the synonyms or homophones is set to 1.
[0118] For synonyms or homophones, in the application scenario of smart home, they usually have a high degree of relevance. A general keyword similarity setting method is to set the relative elements of synonyms or homophones to 1. Specifically, if the keyword in the first keyword vector and the keyword in the second keyword vector are synonyms or homophones, the similarity of the elements corresponding to the synonyms or homophones is set to 1. By adaptively adjusting the corresponding elements of synonyms and homophones in the keyword similarity matrix, the relevance of keyword prior knowledge and actual application scenarios is improved, and the accuracy of the matching model is further improved.
[0119] The embodiment provides a short text matching device based on a matching model, which comprises: a word segmentation module, which is used for obtaining a first keyword vector, a first part-of-speech vector and a first weight vector of a first text; and obtaining a second keyword vector, a second part-of-speech vector and a second weight vector of a second text; a first similarity calculation module, which is used for performing normalization processing on the first weight vector and the second weight vector respectively, and calculating the correlation degree of the normalized first weight vector and the second weight vector to obtain a keyword correlation matrix; and calculating the similarity of the first keyword vector and the second keyword vector to obtain a keyword similarity matrix; a second similarity calculation module, which is used for performing point multiplication calculation on the first similarity matrix and the second similarity matrix to obtain a weighted keyword similarity matrix; a matching module, which is used for inputting the first text and the second text into a matching model, obtaining a short text matching result of the first text and the second text output by the matching model based on the weighted keyword similarity matrix; the keyword vector and the weight vector of two texts are extracted by performing word segmentation on the texts, and then the weighted keyword similarity matrix is calculated, the scoring result of the first attention mechanism layer of the original Keyword-BERT network model is optimized, the importance of keywords in short texts, synonym information, homonym information and the like are directly introduced to guide the model, the addition of other training processes is avoided, the training process of the model is simplified, and the accuracy of short text matching is improved.
[0120] Embodiment six
[0121] Figure 8 A structural schematic diagram of an electronic device provided by the embodiment six of the application is shown in the figure, and the electronic device comprises: Figure 8
[0122] A processor 291, and the electronic device further comprises a memory 292; and can further comprise a communication interface 293 and a bus 294. The processor 291, the memory 292 and the communication interface 293 can complete mutual communication through the bus 294. The communication interface 293 can be used for information transmission. The memory 292 stores a computer program, and the processor 291 is configured to execute the method of the above-mentioned embodiments through the computer program.
[0123] In addition, the logic instructions in the memory 292 can be implemented in the form of a software functional unit and sold or used as an independent product, and can be stored in a computer readable storage medium.
[0124] The memory 292 can be configured to store software programs, computer executable programs, such as program instructions / modules corresponding to the method in the embodiments of the present application. The processor 291 can execute the functions of the application and data processing, that is, implement the method in the method embodiments described above, by running the software programs, instructions and modules stored in the memory 292.
[0125] The memory 292 can include a program storage area and a data storage area. The program storage area can store an operating system and at least one application required by a function; the data storage area can store data created during use of the terminal device, and the like. In addition, the memory 292 can include a high-speed random access memory, and can also include a nonvolatile memory.
[0126] The embodiments of the present application also provide a computer readable storage medium, which includes a stored program. The program, when executed by a processor, is configured to implement the method in any of the embodiments.
[0127] The embodiments of the present application also provide a computer program product, which includes a computer program. The computer program, when executed by a processor, implements the method provided in the above embodiments.
[0128] The above only describes the preferred embodiments of the present application. It should be noted that, for those skilled in the art, without departing from the principles of the present application, a number of improvements and refinements can be made, which should also be considered as the protection scope of the present application.
Claims
1. A short text matching method based on a matching model, characterized in that, include: Obtain the first keyword vector, the first part-of-speech vector, and the first weight vector of the first text; In addition, obtain the second keyword vector, the second part-of-speech vector, and the second weight vector of the second text; The first weight vector and the second weight vector are normalized respectively, and the correlation between the normalized first weight vector and the second weight vector is calculated to obtain the keyword relevance matrix. Furthermore, a keyword similarity matrix is obtained by calculating the similarity between the first keyword vector and the second keyword vector; The keyword relevance matrix and the keyword similarity matrix are multiplied by a dot product to obtain a weighted keyword similarity matrix. Input the first text and the second text into the matching model, and obtain the short text matching results of the first text and the second text output by the matching model based on the weighted keyword similarity matrix; The matching model is a Keyword-BERT network model, which is a bidirectional encoding representation of keywords based on a converter. The embedding layer of the matching model is used to obtain a third keyword vector in numerical form based on the first keyword vector and the second keyword vector. The first attention mechanism layer of the matching model is used for: Based on the third keyword vector, obtain the query matrix, key matrix, and value matrix; and generate the original scoring function matrix based on the query matrix and the key matrix. The optimized scoring function matrix is obtained by calculating the dot product of the original scoring function matrix and the weighted keyword similarity matrix. The optimized scoring function matrix and mask matrix are normalized, and the product of the normalized result and the value matrix is output.
2. The method according to claim 1, characterized in that, The process of calculating the correlation between the normalized first and second weight vectors to obtain the keyword relevance matrix includes: Based on the transpose strategy, the normalized first weight vector or the normalized second weight vector is transposed; wherein the transpose strategy includes transposing the normalized first weight vector if both the normalized first weight vector and the normalized second weight vector are row vectors; and transposing the normalized second weight vector if both the normalized first weight vector and the normalized second weight vector are column vectors. The keyword relevance matrix is obtained by performing matrix multiplication between the first weight vector and the second weight vector obtained by the transpose process.
3. The method according to any one of claims 1-2, characterized in that, The step of obtaining a keyword similarity matrix by calculating the similarity between the first keyword vector and the second keyword vector includes: If all elements in the first keyword vector and the second keyword vector are Chinese characters, then the word2vec tool is used to calculate the similarity between each keyword in the first keyword vector and each keyword in the second keyword vector to obtain the keyword similarity matrix. If all elements in the first keyword vector and the second keyword vector are in English, then the similarity between each keyword in the first keyword vector and each keyword in the second keyword vector is calculated using the Hownet tool to obtain the keyword similarity matrix.
4. The method according to claim 3, characterized in that, After obtaining the keyword similarity matrix by calculating the similarity between the first keyword vector and the second keyword vector, the method further includes: If a keyword in the first keyword vector is a synonym or homophone of a keyword in the second keyword vector, then the similarity between the elements corresponding to the synonym or homophone is set to 1.
5. A short text matching device based on a matching model, characterized in that, include: The word segmentation module is used to obtain the first keyword vector, the first part-of-speech vector, and the first weight vector of the first text. In addition, obtain the second keyword vector, the second part-of-speech vector, and the second weight vector of the second text; The first similarity calculation module is used to normalize the first weight vector and the second weight vector respectively, and calculate the correlation between the normalized first weight vector and the second weight vector to obtain the keyword relevance matrix. Furthermore, a keyword similarity matrix is obtained by calculating the similarity between the first keyword vector and the second keyword vector; The second similarity calculation module is used to perform a dot product calculation on the keyword relevance matrix and the keyword similarity matrix to obtain a weighted keyword similarity matrix. A matching module is used to input the first text and the second text into a matching model, and obtain the short text matching results of the first text and the second text output by the matching model based on the weighted keyword similarity matrix. The matching model is a Keyword-BERT network model with bidirectional encoding representation based on a keyword converter. The embedding layer of the matching model is used to: obtain a third keyword vector in numerical form based on the first keyword vector and the second keyword vector; the first attention mechanism layer of the matching model is used to: obtain a query matrix, a key matrix, and a value matrix based on the third keyword vector; and generate an original scoring function matrix based on the query matrix and the key matrix; and obtain an optimized scoring function matrix by calculating the dot product of the original scoring function matrix and the weighted keyword similarity matrix. The optimized scoring function matrix and mask matrix are normalized, and the product of the normalized result and the value matrix is output.
6. A computer-readable storage medium, characterized in that, The computer-readable storage medium includes a stored program, wherein the program, when executed, performs the method of any one of claims 1 to 4.
7. An electronic device comprising a memory and a processor, characterized in that, The memory stores a computer program, and the processor is configured to execute the method of any one of claims 1 to 4 through the computer program.
8. A computer program product, characterized in that, The computer program product includes a computer program that, when executed by a processor, implements the method of any one of claims 1 to 4.
Citation Information
Patent Citations
Fault detection method and device, storage medium and electronic device
CN112033066A
Text classification method and device, equipment and storage medium
CN113886577A