Text classification method, device, terminal equipment and medium based on artificial intelligence
By splitting the text into subtexts and calculating similarity and weights using semantic models, the problem of high calculation time-consuming and low accuracy in deep text matching is solved, achieving more efficient and accurate text classification.
Patent Information
- Application Number
- CN202210573891.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-05-25
- Publication Date
- 2025-01-07
- Estimated Expiration
- 2042-05-25
AI Technical Summary
Existing deep text matching models have high computational time and low text matching accuracy.
Split the text to be classified into subtexts, use the trained semantic model to obtain the token feature vector, calculate the weight based on the similarity between subtexts, calculate the target semantic expression based on the number of subtexts, and perform the classification text Semantic expression matches to determine classification results.
While reducing the calculation time, it improves the accuracy of text classification. Subtext weights and eigenvectors are analyzed through semantic models to obtain more accurate semantic expressions and classification results.
Smart Images

Figure CN114996448B_ABST
Abstract
Description
Technical Field
[0001] The present invention is applicable to the field of artificial intelligence, and in particular relates to a text classification method, device, terminal equipment and medium based on artificial intelligence. Background Art
[0002] Text matching is an important basic problem in natural language processing. Many natural language processing tasks can be abstracted as text matching problems to a large extent, such as information retrieval, question-answering systems, paraphrase questions, dialogue systems, machine translation, etc.
[0003] There are two main types of current deep text matching models: representation models and interactive models.
[0004] Among them, the representation model refers to converting the text into a unique overall representation vector at the representation layer before matching. This representation model can greatly reduce the time consumption of online computing, but it lacks interactive information between texts and is prone to losing semantic focus, thereby reducing the accuracy of text matching; the interactive model first matches words at the input layer and uses the matching results as grayscale images for subsequent modeling, which better grasps the semantic focus and can better model the importance of context. However, the computational time of this interactive model is relatively long, and it cannot express the fine-grained semantic information of the text, thereby reducing the accuracy of text matching.
[0005] Therefore, in the scenario of deep text matching, how to improve the accuracy of text matching while reducing computational time becomes an urgent problem to be solved. Summary of the invention
[0006] In view of this, the embodiments of the present invention provide a text classification method, apparatus, terminal device and medium based on artificial intelligence to solve the problems of high computational time consumption and low text matching accuracy.
[0007] In a first aspect, an embodiment of the present invention provides a text classification method based on artificial intelligence, the text classification method comprising:
[0008] Split the text to be classified into N sub-texts, encode each sub-text according to the trained semantic model, and obtain the token feature vector of the corresponding sub-text, where N is a positive integer;
[0009] Calculate the similarity between the token feature vectors of any sub-text and all other sub-texts, and determine the weight of the corresponding sub-text according to the sum of the similarities between the token feature vectors of each sub-text and all other sub-texts;
[0010] Determine the target semantic expression of the text to be classified according to the token feature vector of each sub-text and the weight of the corresponding sub-text, combined with the number of all sub-texts;
[0011] The target semantic expression is matched with known semantic expressions of classified texts in a database to obtain a known semantic expression that matches the target semantic expression, and the category to which the corresponding classified text belongs is determined to be a classification result of the text to be classified.
[0012] In a second aspect, an embodiment of the present invention provides a text classification device based on artificial intelligence, the text classification device comprising:
[0013] The token feature acquisition module is used to split the text to be classified into N sub-texts, encode each sub-text according to the trained semantic model, and obtain the token feature vector of the corresponding sub-text, where N is a positive integer;
[0014] A weight calculation module is used to calculate the similarity between the token feature vectors of any sub-text and all other sub-texts, and determine the weight of the corresponding sub-text according to the sum of the similarities between the token feature vectors of each sub-text and all other sub-texts;
[0015] A semantic expression module, used to determine the target semantic expression of the text to be classified according to the token feature vector of each sub-text and the weight of the corresponding sub-text, combined with the number of all sub-texts;
[0016] The text classification module is used to match the target semantic expression with the known semantic expression of the classified text in the database, obtain the known semantic expression matching the target semantic expression, and determine that the category to which the corresponding classified text belongs is the classification result of the text to be classified.
[0017] In a third aspect, an embodiment of the present invention provides a terminal device, comprising a processor, a memory, and a computer program stored in the memory and executable on the processor, wherein the processor implements the text classification method as described in the first aspect when executing the computer program.
[0018] In a fourth aspect, an embodiment of the present invention provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the text classification method as described in the first aspect is implemented.
[0019] Compared with the prior art, the embodiments of the present invention have the following beneficial effects: the present invention splits the text to be classified into N sub-texts, obtains the token feature vector of each sub-text according to the trained semantic model, and determines the weight corresponding to each sub-text according to the sum of the similarities between each sub-text and the token feature vectors of all other sub-texts, thereby multiplying the token feature vector of each sub-text with the corresponding weight, calculating the mean of the token feature vector according to the multiplication result and the number of sub-texts, and determining the mean as the target semantic expression of the text to be classified, thereby matching the target semantic expression with the known semantic expression of the classified text in the database, obtaining the known semantic expression matching the target semantic expression, and determining the classification result that the classification to which the corresponding classified text belongs is the text to be classified. By using the semantic model to determine the token feature vector, the corresponding weight of the sub-text is analyzed, and a relatively accurate semantic expression is obtained together with the token feature vector, and matched with the existing classification result, the classification result can be determined relatively quickly, and the accuracy of text classification is improved while reducing the computational time. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.
[0021] Figure 1 This is a schematic diagram of an application environment of an artificial intelligence-based text classification method provided in Embodiment 1 of the present invention;
[0022] Figure 2 This is a flowchart of a text classification method based on artificial intelligence provided in Embodiment 1 of the present invention;
[0023] Figure 3 It is a structural diagram of a text classification device based on artificial intelligence provided in Embodiment 2 of the present invention;
[0024] Figure 4 It is a structural diagram of a terminal device provided in Embodiment 3 of the present invention. DETAILED DESCRIPTION
[0025] In the following description, specific details such as specific system structures, technologies, etc. are provided for the purpose of illustration rather than limitation, so as to provide a thorough understanding of the embodiments of the present invention. However, it should be clear to those skilled in the art that the present invention may be implemented in other embodiments without these specific details. In other cases, detailed descriptions of well-known systems, devices, circuits, and methods are omitted to prevent unnecessary details from obstructing the description of the present invention.
[0026] It should be understood that when used in the present specification and the appended claims, the term "comprising" indicates the presence of described features, integers, steps, operations, elements and / or components, but does not exclude the presence or addition of one or more other features, integers, steps, operations, elements, components and / or combinations thereof.
[0027] It should also be understood that the term "and / or" used in the present description and the appended claims refers to and includes any and all possible combinations of one or more of the associated listed items.
[0028] As used in the present specification and the appended claims, the term "if" may be interpreted as "when" or "upon" or "in response to determining" or "in response to detecting", depending on the context. Similarly, the phrase "if it is determined" or "if [described condition or event] is detected" may be interpreted as meaning "upon determination" or "in response to determining" or "upon detection of [described condition or event]" or "in response to detecting [described condition or event]", depending on the context.
[0029] In addition, in the description of the present specification and the appended claims, the terms "first", "second", "third", etc. are only used to distinguish the descriptions and cannot be understood as indicating or implying relative importance.
[0030] References to "one embodiment" or "some embodiments" etc. described in the present specification mean that one or more embodiments of the present invention include specific features, structures or characteristics described in conjunction with the embodiment. Therefore, the statements "in one embodiment", "in some embodiments", "in some other embodiments", "in some other embodiments", etc. that appear in different places in this specification do not necessarily refer to the same embodiment, but mean "one or more but not all embodiments", unless otherwise specifically emphasized in other ways. The terms "including", "comprising", "having" and their variations all mean "including but not limited to", unless otherwise specifically emphasized in other ways.
[0031] The embodiments of the present invention can acquire and process relevant data based on artificial intelligence technology. Artificial Intelligence (AI) is the theory, method, technology and application system that uses digital computers or machines controlled by digital computers to simulate, extend and expand human intelligence, perceive the environment, acquire knowledge and use knowledge to obtain the best results.
[0032] AI basic technologies generally include sensors, dedicated AI chips, cloud computing, distributed storage, big data processing technology, operation / interaction systems, mechatronics, etc. AI software technologies mainly include computer vision technology, robotics technology, biometrics technology, speech processing technology, natural language processing technology, and machine learning / deep learning.
[0033] It should be understood that the order of execution of the steps in the following embodiments does not imply a precedence 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.
[0034] In order to illustrate the technical solution of the present invention, specific embodiments are provided below for illustration.
[0035] The text classification method based on artificial intelligence provided by the first embodiment of the present invention can be applied in the following aspects: Figure 1 In an application environment, a client communicates with a server. The client includes but is not limited to a PDA, a desktop computer, a laptop computer, an ultra-mobile personal computer (UMPC), a netbook, a cloud terminal device, a personal digital assistant (PDA), and other terminal devices. The server can be implemented by an independent server or a server cluster consisting of multiple servers.
[0036] See also Figure 2 , is a flowchart of a text classification method based on artificial intelligence provided by the first embodiment of the present invention. The above text classification method can be applied to Figure 1 In the client, the text classification method may include the following steps:
[0037] Step S201, split the text to be classified into N sub-texts, encode each sub-text according to the trained semantic model, and obtain the token feature vector of the corresponding sub-text.
[0038] Wherein, N is a positive integer, and the text to be classified may refer to a long or short sentence composed of different words or phrases. Accordingly, the text to be classified may be a text in any language. The semantic model may refer to a semantic extractor, a feature encoder, etc. The trained semantic model can perform semantic extraction and analysis on words or phrases to obtain the encoding features of the corresponding analysis object.
[0039] The split text to be classified has a smaller granularity, which facilitates semantic extraction and analysis using a trained semantic model, so that the corresponding encoding features can be obtained accurately and efficiently.
[0040] After the text to be classified is split into N sub-texts, for any sub-text, it is input into the trained semantic model to extract semantic features and obtain the token feature vector corresponding to the sub-text. Among them, the trained semantic model uses an autoencoder network to output the original input text through the encoder-decoder network structure, so that the encoder can complete the extraction of text semantic features. And the encoder structure in the autoencoder network is used as the trained semantic model in this embodiment to extract the semantic features of the input sub-text.
[0041] Specifically, the autoencoder network consists of two parts: an encoder and a decoder. The encoder converts the input text into an internal feature representation, and the decoder converts the internal feature representation into an output text, and ensures that the output text is consistent with the input text.
[0042] In the training process of the autoencoder, the data set is a large amount of text. For any text, the text is input into the encoder, and any characters in the text are set to 0. The encoder extracts features from the text after setting it to 0, and the decoder decodes the extracted features and outputs the original input text. A loss function is constructed based on the difference between the input text and the output text. The parameters in the encoder and decoder are updated by making the loss function converge to the minimum value, thereby improving the accuracy of the autoencoder network in extracting text features.
[0043] When constructing a loss function based on the difference between the input text and the output text, the input text and the output text are first represented as an X-dimensional word vector through the word vector technology, and the loss function is constructed through the cosine similarity between the input text word vector and the output text word vector. The larger the cosine similarity, the smaller the difference between the input text word vector and the output text word vector, and the smaller the loss function. The dimension X of the word vector can be set according to the actual situation.
[0044] For example, the input text and output text are represented as an X-dimensional word vector through word2vec technology (an existing word vector technology), the word vector of the input text is recorded as R, and the word vector of the output text is recorded as C. Then the loss function of the autoencoder network is Loss1:
[0045]
[0046] In the formula, R is the word vector of the input text, and C is the word vector of the output text.
[0047] Since the smaller the difference between the input text and the output text, the smaller the loss function Loss1, the parameters in the encoder and decoder are updated by making the loss function Loss1 converge to the minimum value, thereby completing the training of the autoencoder network.
[0048] Since the encoder in the autoencoder network is used to extract features from the input text, the encoder in the autoencoder network is used as a trained semantic model in this embodiment to extract semantic features of the input sub-text, and the extracted semantic features are used as token feature vectors of the corresponding sub-text.
[0049] Optionally, the text to be classified is split into N sub-texts, including:
[0050] According to the word segmentation algorithm, the text to be classified is matched with the entries in the known machine dictionary, and the text matching the entry is determined to be a sub-text, and N sub-texts are obtained.
[0051] Among them, the word segmentation algorithm includes the word segmentation method based on string matching, the word segmentation method based on understanding and the word segmentation method based on statistics. Among them, the word segmentation method based on string matching includes forward maximum matching, reverse maximum matching and bidirectional maximum matching.
[0052] Different types of text to be classified can be split using different splitting methods. At the same time, the parameters in the splitting method can be set according to the requirements such as the length of the split text during use.
[0053] In one embodiment, first, a word segmentation method based on string matching is used. In accordance with the forward maximum matching method, a symbol string of fixed length Chinese characters is selected as the maximum symbol string, and the maximum symbol string is matched with a word entry in a "sufficiently large" machine dictionary. If the match fails, one Chinese character in the maximum symbol string is removed and the matching with the word entry in the machine dictionary is continued until the word corresponding to the maximum symbol string is found in the machine dictionary, and finally the text to be classified is split into N sub-texts. The direction of matching the maximum symbol string with the word entry in the machine dictionary is from left to right, and the direction of reducing the maximum symbol string is from right to left.
[0054] In one embodiment, the word segmentation method based on string matching can also be used to match the text to be classified with the entries in a "sufficiently large" machine dictionary in a reverse maximum matching manner to split the text to be classified into N sub-texts. The direction of matching the maximum symbol string with the word entries in the machine dictionary is from right to left, and the direction of reducing the maximum symbol string is from left to right.
[0055] Step S202, calculating the similarity between the token feature vectors of any subtext and all other subtexts, and determining the weight of the corresponding subtext according to the sum of the similarities between the token feature vectors of each subtext and all other subtexts.
[0056] The dimensions of the token feature vectors of each sub-text are consistent. For any sub-text, the similarity between the token feature vectors of the sub-text and other sub-texts can be calculated according to a similarity algorithm. The similarity algorithms include Pearson correlation coefficient, Euclidean distance, Manhattan distance and cosine similarity. For token feature vectors of different dimensions, different similarity algorithms can be used to calculate the similarity between the token feature vectors of the text.
[0057] Then, the similarity between the token feature vectors of the subtext and all other subtexts is summed, and the sum of the similarity corresponding to each subtext is calculated, and the sum of the subtexts is normalized. Since the greater the similarity between the token feature vectors of each subtext and other subtexts, the deeper the connection between the subtext and other subtexts in the text to be classified, the deeper the connection between the subtext and the text to be classified. Therefore, when obtaining the semantic features of the text to be classified, the greater the sum of the corresponding similarities of the subtexts, the greater the weight of the subtext in obtaining the semantic features of the text to be classified.
[0058] Therefore, the normalized value of the sum of the similarities of each sub-text is determined as the weight of the sub-text, which is used to combine with the token feature vector of each sub-text to obtain the semantic features of the text to be classified.
[0059] Optionally, the weight of the corresponding subtext is determined according to the sum of similarities between each subtext and the token feature vectors of all other subtexts, including:
[0060] For any sub-text, calculate the similarity between the token feature vectors of the sub-text and other sub-texts;
[0061] The similarities between the subtext and the token feature vectors of all other subtexts are summed up, and the sum result is determined as the weight of the corresponding subtext.
[0062] The number of subtexts is recorded as N, and for the i-th (i=1, 2, ..., N) subtext, its token feature vector is recorded as X i , calculate the token feature vector X of the i-th subtext i and the token feature vector X of the jth subtext j The cosine similarity Y ij :
[0063]
[0064] Where, X i is the token feature vector of the i-th subtext, X j is the token feature vector of the jth subtext.
[0065] The cosine similarity Y ijThe larger the value of , the greater the semantic similarity between the i-th sub-text and the j-th sub-text.
[0066] Calculate the token feature vector X of the i-th subtext respectively i The cosine similarity Y with the token feature vectors of the 1st, 2nd, ...i-1, i+1, ..., Nth subtext i1 , Y i2 , ..., Y i(i-1) , Y i(i+1) , ..., Y iN , then we can obtain the cosine similarity and Y of the token feature vectors of the i-th sub-text and all other sub-texts i :
[0067]
[0068] Where N is the number of subtexts, Y ij is the token feature vector X of the i-th subtext i and the token feature vector X of the jth subtext j The cosine similarity of .
[0069] Calculate the cosine similarity of the token feature vectors of the 1st, 2nd, ..., Nth sub-text and all other sub-texts and Y1, Y2, ..., Y N , and then determine the sum result as the weight of the corresponding sub-text.
[0070] Optionally, determining the sum result as the weight of the corresponding subtext includes:
[0071] The summation results of all sub-texts are normalized, and the normalized value of each sub-text is determined as the weight of the corresponding sub-text.
[0072] The sum of the cosine similarities is recorded as Y, and the cosine similarities corresponding to each sub-text are normalized by calculating the ratio of the cosine similarities corresponding to each sub-text to the sum of the cosine similarities Y, and each normalized value is used as the weight of the corresponding sub-text.
[0073] Specifically, the sum of the cosine similarity is Y:
[0074]
[0075] Where N is the number of subtexts, Y i is the cosine similarity sum of the token feature vectors of the i-th subtext.
[0076] The weight of the i-th subtext is Q i :
[0077]
[0078] Where Y i is the cosine similarity sum of the token feature vectors of the i-th subtext and all other subtexts, and Y is the sum of N cosine similarities.
[0079] Then the weights Q1, Q2, ..., Q of the 1st, 2nd, ..., Nth paragraphs of subtext can be calculated respectively. N .
[0080] Step S203, determining the target semantic expression of the text to be classified according to the token feature vector of each sub-text and the weight of the corresponding sub-text, combined with the number of all sub-texts.
[0081] Among them, the target semantic expression is a vector with the same dimension as the token feature vector of the sub-text. The token feature vector of each sub-text is used to characterize the semantic features of each sub-text. The weight of each sub-text is used to characterize the contribution of the semantic features of the sub-text to the overall semantic features of the text to be classified. Therefore, according to the token feature vector of each sub-text and the weight of the corresponding sub-text, combined with the number of all sub-texts, the target semantic expression of the text to be classified is determined to characterize the overall semantic features of the text to be classified.
[0082] Optionally, the target semantic expression of the text to be classified is determined based on the token feature vector of each sub-text and the weight of the corresponding sub-text, combined with the number of all sub-texts, including:
[0083] For any sub-text, multiply the token feature vector of the sub-text by the corresponding weight to obtain the feature expression result of the corresponding sub-text;
[0084] The feature expression results of all sub-texts are added together, and the added result is divided by the number of all sub-texts, and the division result is determined to be the target semantic expression of the text to be classified.
[0085] Among them, for any sub-text, the feature expression results of each sub-text are obtained by weighting the feature vectors of each sub-text token in the text to be classified, and the target semantic expression of the text to be classified is obtained by averaging the feature expression results of the sub-texts, which is used to characterize the overall semantic characteristics of the text to be classified.
[0086] For example, for the i-th subtext, its token feature vector is X i , the corresponding weight is Q i , the token feature vector X of this subtext i And the corresponding weight Q i Multiply them together to get the characteristic expression result of the i-th sub-text as G i :
[0087] G i =Qi X i
[0088] Where, X i is the token feature vector of the i-th subtext, Q i is the weight corresponding to the i-th sub-text.
[0089] Calculate and obtain the feature expression results G1, G2, ..., G of the 1st, 2nd, ..., Nth paragraphs of sub-text respectively. N , add up the feature expression results of all sub-texts and divide them by the number of all sub-texts to determine the target semantic expression of the text to be classified as M i :
[0090]
[0091] Where N is the number of subtexts, G i is the feature expression result of the i-th sub-text.
[0092] Step S204, matching the target semantic expression with the known semantic expression of the classified text in the database, obtaining the known semantic expression matching the target semantic expression, and determining the classification result that the classification to which the corresponding classified text belongs is the text to be classified.
[0093] Among them, the classified text is a text whose category is known. For any classified text, it is treated as a text to be classified and the corresponding operations of the above steps S201-S203 are performed to determine the known semantic expression of the classified text, and the category and known semantic expression of the classified text are stored in the database.
[0094] By respectively determining the known semantic expressions of all the classified texts, the categories and known semantic expressions of all the classified texts are stored in the database. When the text to be classified is classified, the known semantic expressions of each classified text stored in the database are read.
[0095] The target semantic expression of the text to be classified is matched with the known semantic expression of the classified text in the database. The more similar the semantic features of the classified text and the text to be classified are, the greater the corresponding matching degree is. Therefore, the classified text that is most similar to the text to be classified can be determined by the known semantic expression that best matches the target semantic expression, thereby determining that the category to which the classified text belongs is the classification result of the text to be classified.
[0096] Optionally, matching the target semantic expression with known semantic expressions of classified texts in a database to obtain a known semantic expression matching the target semantic expression, and determining that the classification to which the corresponding classified text belongs is a classification result of the text to be classified, includes:
[0097] Calculate the similarity between the target semantic expression and the known semantic expressions of the classified texts in the database to obtain the similarity between the target semantic expression and each known semantic expression;
[0098] The known semantic expression corresponding to the maximum value of the similarity degree is taken as the known semantic expression matching the target semantic expression;
[0099] The category to which the classified text corresponds to the known semantic expression that matches the target semantic expression is determined as the classification result of the text to be classified.
[0100] The known semantic expressions of each classified text are determined according to the corresponding operations of steps S201-S203, and the categories and known semantic expressions of each classified text are stored in the database. When classifying the text to be classified, the known semantic expressions of each classified text stored in the database are read.
[0101] The similarity with the target semantic expression is calculated respectively, and the maximum value of the similarity is determined. The known semantic expression corresponding to the maximum value of the similarity is the known semantic expression matching the target semantic expression, and the classification result of the text to be classified is finally determined to be the category to which the classified text belongs corresponding to the known semantic expression matching the target semantic expression.
[0102] For example, the target semantic expression of the text to be classified is recorded as D0, the number of known semantic expressions in the database is recorded as M, and the kth (k = 1, 2, ..., M) known semantic expression is recorded as D k Since the closer the known semantic expression is to the target semantic expression, the higher the similarity between the known semantic expression and the target semantic expression is, the present embodiment calculates the similarity between the target semantic expression D0 and the kth known semantic expression D k The cosine similarity of the known semantic expression and the target semantic expression is determined by T k :
[0103]
[0104] Where D0 is the target semantic expression of the text to be classified, D k is the kth known semantic expression in the database.
[0105] Calculate the 1st, 2nd, ..., Mth known semantic expressions D1, D2, ..., D M The similarity between the target semantic expression and the target semantic expression is T1, T2, ..., T M , determine the known semantic expression corresponding to the maximum value of the similarity, and determine the classified text corresponding to the known semantic expression. Then, the category to which the classified text belongs can be used as the classification result of the text to be classified, thereby completing the text classification of the text to be classified.
[0106] Optionally, matching the target semantic expression with known semantic expressions of classified texts in a database to obtain a known semantic expression matching the target semantic expression, and determining that the classification to which the corresponding classified text belongs is a classification result of the text to be classified, includes:
[0107] The target semantic expression of the text to be classified and the known semantic expressions of each classified text in the database are input into the trained twin network model for matching, and the similarity between the target semantic expression and each known semantic expression is output;
[0108] The known semantic expression corresponding to the maximum value of the similarity degree is taken as the known semantic expression matching the target semantic expression;
[0109] The category to which the classified text corresponds to the known semantic expression that matches the target semantic expression is determined as the classification result of the text to be classified.
[0110] Among them, since the twin network is used to deal with situations where two inputs are relatively similar, it is based on a coupled framework established by two artificial neural networks. It takes two samples as input, and the two sub-networks each receive an input and output its representation embedded in a high-dimensional space. The similarity of the two samples is compared by calculating the distance between the two representations, for example, the Euclidean distance.
[0111] Therefore, this embodiment uses a twin network to calculate the similarity between the target semantic expression and the known semantic expressions of the classified texts in the database to obtain the degree of similarity between the text to be classified and each known semantic expression.
[0112] Specifically, in the training process of the twin network, the data set is a large number of text semantic expressions, and whether each text matches is marked. If the two texts match, the corresponding mark is 1, and if the two samples do not match, the corresponding mark is 0. And the contrast loss function is used to construct the loss function of the twin network according to the matching relationship of the samples and the similarity between the samples, so that in the contrast loss function, when the two input samples match, the larger the Euclidean distance between the two sample representations, the larger the contrast loss function, indicating that the effect of the twin network model is worse; when the two input samples do not match, the similarity between the two samples is small. Under normal circumstances, the Euclidean distance between the two sample representations should be larger. Therefore, if the Euclidean distance between the two sample representations obtained in the twin network is smaller, the contrast loss function is larger, indicating that the effect of the twin network model is worse. Therefore, in the training process of the twin network, the shared parameters in the twin network are updated according to the contrast loss function until the contrast loss function converges to the minimum value, which is used to improve the matching accuracy of the twin network model.
[0113] For example, during the training process of the twin network, the text semantic expressions in the data set are divided into multiple batches, each batch containing P text semantic expressions.
[0114] For each batch of text semantic expressions, the ath (a=1, 2, ...P)th text semantic expression and the bth (b=1, 2, ...P, b≠a)th text semantic expression are selected as the input of the two sub-networks of the twin network. The two sub-networks represent the two text semantic expressions in a high-dimensional space through shared parameters to obtain the corresponding two text semantic representations L a and L b , where the text semantic representation L a and L b are all vectors, and their vector dimension is recorded as w, then the text semantic representation L a L a =[l a1 , l a2 , ..., l aw ], representing the text semantics L b L b =[l b1 , l b2 , ..., l bw ].
[0115] Then, the semantic representation L of the two texts is calculated a and L b The Euclidean distance d between ab :
[0116]
[0117] Where w is the text semantic representation L a and L b The vector dimension, l ar is the text semantic representation L a The r-th dimension data, l br is the text semantic representation L b The r-th dimension data in .
[0118] The maximum value of the Euclidean distance in the twin network model is recorded as maxd, and the text semantic representation L can be represented according to the maximum value of the Euclidean distance. a and L b The Euclidean distance between them is normalized to obtain the corresponding normalized value d ab ′:
[0119]
[0120] Where, d ab is the text semantic representation L aand L b The Euclidean distance between them, maxd is the maximum value of the Euclidean distance in the twin network model.
[0121] Therefore, the contrast loss function of the twin network is Loss2:
[0122]
[0123] Where P is the number of text semantic expressions in each batch, d ab ′ is the text semantic representation L corresponding to the a-th text semantic expression and the b-th text semantic expression a and L b , y is the normalized value of the Euclidean distance between the a-th text semantic expression and the b-th text semantic expression, and d0 is the preset Euclidean distance threshold, which can be set according to actual conditions.
[0124] Among them, when the semantic expression of the ath text matches the semantic expression of the bth text, y = 1, and the contrast loss function Correspondingly, the more similar the semantic expressions of the two texts are, the better the two text representations L a and L b The smaller the normalized value of the Euclidean distance between them, the smaller the corresponding loss function Loss2. When the semantic expression of the a-th text does not match the semantic expression of the b-th text, y =0, contrast loss function Correspondingly, since the semantic expressions of the two texts are not similar, when the two text representations L a and L b The smaller the normalized value of the Euclidean distance between them, the lower the accuracy of the twin network, and the larger the corresponding loss function Loss2.
[0125] By training the semantic expression of the text in the data set, the shared parameters in the twin network are continuously updated until convergence to obtain a trained twin network model.
[0126] Then, when classifying the text to be classified, the target semantic expression D0 of the text to be classified and the known semantic expression D of the kth classified text stored in the database are compared. k They are used as the input of the two sub-networks of the trained twin network, and the similarity between the target semantic expression and the k-th known semantic expression stored in the database is output, denoted as T k Then we can obtain the 1st, 2nd, ..., Mth known semantic expressions D1, D2, ..., D M The similarity between the target semantic expression D0 and T1, T2, ..., T M, determine the known semantic expression corresponding to the maximum value of the similarity, and determine the classified text corresponding to the known semantic expression. Then, the category to which the classified text belongs can be used as the classification result of the text to be classified, thereby completing the text classification of the text to be classified.
[0127] The embodiment of the present invention divides the text to be classified into N sub-texts, obtains the token feature vector of each sub-text according to the trained semantic model, and determines the weight corresponding to each sub-text according to the sum of the similarities between each sub-text and the token feature vectors of all other sub-texts, thereby multiplying the token feature vector of each sub-text with the corresponding weight, and calculating the mean of the token feature vector according to the multiplication result and the number of sub-texts, and determining the mean as the target semantic expression of the text to be classified, thereby matching the target semantic expression with the known semantic expression of the classified text in the database, obtaining the known semantic expression matching the target semantic expression, and determining the classification result that the classification to which the corresponding classified text belongs is the text to be classified. By using the semantic model to determine the token feature vector, the corresponding weight of the sub-text is analyzed, and a relatively accurate semantic expression is obtained together with the token feature vector, and matched with the existing classification result, the classification result can be determined more quickly, while reducing the time consumption and improving the accuracy of text classification.
[0128] Corresponding to the text classification method of the above embodiment, Figure 3 A structural block diagram of an artificial intelligence-based text classification device provided in Embodiment 2 of the present invention is given. For ease of explanation, only the parts related to the embodiment of the present invention are shown.
[0129] See also Figure 3 , the text classification device comprises:
[0130] The token feature acquisition module 31 is used to split the text to be classified into N sub-texts, encode each sub-text according to the trained semantic model, and obtain the token feature vector of the corresponding sub-text, where N is a positive integer;
[0131] A weight calculation module 32 is used to calculate the similarity between the token feature vectors of any sub-text and all other sub-texts, and determine the weight of the corresponding sub-text according to the sum of the similarities between the token feature vectors of each sub-text and all other sub-texts;
[0132] The semantic expression module 33 is used to determine the target semantic expression of the text to be classified according to the token feature vector of each sub-text and the weight of the corresponding sub-text, combined with the number of all sub-texts;
[0133] The text classification module 34 is used to match the target semantic expression with the known semantic expression of the classified text in the database, obtain the known semantic expression matching the target semantic expression, and determine the classification result that the category to which the corresponding classified text belongs is the text to be classified.
[0134] Optionally, the token feature acquisition module 31 includes:
[0135] A text splitting unit is used to match the text to be classified with the entries in the known machine dictionary according to the word segmentation algorithm, and split the text to be classified into N sub-texts;
[0136] Optionally, the weight calculation module 32 includes:
[0137] A similarity calculation unit, used for calculating the similarity between a sub-text and a token feature vector of other sub-texts for any sub-text;
[0138] The weight determination unit is used to sum the similarities between the subtext and the token feature vectors of all other subtexts, and determine the summation result as the weight of the corresponding subtext.
[0139] Optionally, the weight determination unit includes:
[0140] The weight determination subunit is used to normalize the summed results of all sub-texts and determine the normalized value of each sub-text as the weight of the corresponding sub-text.
[0141] Optionally, the semantic expression module 33 includes:
[0142] A feature expression unit is used for multiplying the token feature vector of any sub-text by the corresponding weight to obtain a feature expression result of the corresponding sub-text;
[0143] The semantic determination unit is used to add the feature expression results of all sub-texts, and divide the addition result by the number of all sub-texts to determine the division result as the target semantic expression of the text to be classified.
[0144] Optionally, the text classification module 34 includes:
[0145] A text matching unit, used to obtain a maximum value of the similarity degree, and determine that the known semantic expression corresponding to the maximum value of the similarity degree is a known semantic expression that matches the target semantic expression;
[0146] The text classification unit is used to determine the category to which the classified text corresponding to the known semantic expression matching the target semantic expression belongs as the classification result of the text to be classified.
[0147] It should be noted that the information interaction, execution process and other contents between the above modules are based on the same concept as the embodiment of the method of the present invention. Their specific functions and technical effects can be found in the method embodiment part and will not be repeated here.
[0148] Figure 4This is a schematic diagram of the structure of a terminal device provided in Embodiment 3 of the present invention. Figure 4 As shown, the terminal device of this embodiment includes: at least one processor ( Figure 4 Only one is shown in the figure), a memory, and a computer program stored in the memory and executable on at least one processor, wherein when the processor executes the computer program, the steps in any of the above-mentioned text classification method embodiments are implemented.
[0149] The terminal device may include, but is not limited to, a processor and a memory. Those skilled in the art will appreciate that Figure 4 It is only an example of a terminal device and does not constitute a limitation on the terminal device. The terminal device may include more or fewer components than shown in the figure, or a combination of certain components, or different components. For example, it may also include a network interface, a display screen, and an input device.
[0150] The processor may be a CPU, or other general-purpose processors, a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or any conventional processor, etc.
[0151] The memory includes a readable storage medium, an internal memory, etc., wherein the internal memory may be the memory of the terminal device, and the internal memory provides an environment for the operation of the operating system and the computer-readable instructions in the readable storage medium. The readable storage medium may be the hard disk of the terminal device, and in other embodiments, it may also be an external storage device of the terminal device, for example, a plug-in hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (SecureDigital, SD) card, a flash card (Flash Card), etc. equipped on the terminal device. Further, the memory may also include both an internal storage unit of the terminal device and an external storage device. The memory is used to store an operating system, an application program, a boot loader (BootLoader), data, and other programs, such as the program code of a computer program, etc. The memory may also be used to temporarily store data that has been output or is to be output.
[0152] The technicians in the relevant field can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional units and modules is used as an example. In practical applications, the above-mentioned function allocation can be completed by different functional units and modules as needed, that is, the internal structure of the device is divided into different functional units or modules to complete all or part of the functions described above. The functional units and modules in the embodiment can be integrated into a processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The above-mentioned integrated unit can be implemented in the form of hardware or in the form of software functional units. In addition, the specific names of the functional units and modules are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of the present invention. The specific working process of the units and modules in the above-mentioned device can refer to the corresponding process in the above-mentioned method embodiment, which will not be repeated here. If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on such understanding, the present invention implements all or part of the processes in the above-mentioned embodiment method, which can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a computer-readable storage medium. When the computer program is executed by the processor, the steps of the above-mentioned method embodiment can be implemented. Among them, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form. The computer-readable medium may at least include: any entity or device capable of carrying computer program code, recording medium, computer memory, read-only memory (ROM), random access memory (RAM), electric carrier signal, telecommunication signal and software distribution medium. For example, a USB flash drive, a mobile hard disk, a magnetic disk or an optical disk. In some jurisdictions, according to legislation and patent practice, computer-readable media cannot be electric carrier signals and telecommunication signals.
[0153] The present invention implements all or part of the processes in the above-mentioned embodiment method, and can also be completed by a computer program product. When the computer program product runs on a terminal device, the terminal device can implement the steps in the above-mentioned method embodiment when executing it.
[0154] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described or recorded in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0155] Those skilled in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the present invention.
[0156] In the embodiments provided by the present invention, it should be understood that the disclosed devices / terminal equipment and methods can be implemented in other ways. For example, the device / terminal equipment embodiments described above are only schematic, for example, the division of modules or units is only a logical function division, and there may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0157] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0158] The above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features may be replaced by equivalents. Such modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention, and should be included in the protection scope of the present invention.
Claims
1. A text classification method based on artificial intelligence, characterized in that: The text classification method comprises: Split the text to be classified into N sub-texts, encode each sub-text according to the trained semantic model, and obtain the token feature vector of the corresponding sub-text, where N is a positive integer; Calculate the similarity between the token feature vectors of any sub-text and all other sub-texts, and determine the weight of the corresponding sub-text according to the sum of the similarities between the token feature vectors of each sub-text and all other sub-texts; Determine the target semantic expression of the text to be classified according to the token feature vector of each sub-text and the weight of the corresponding sub-text, combined with the number of all sub-texts; Matching the target semantic expression with known semantic expressions of classified texts in a database to obtain a known semantic expression that matches the target semantic expression, and determining that the classification to which the corresponding classified text belongs is a classification result of the text to be classified; Wherein, determining the target semantic expression of the text to be classified includes: For any sub-text, multiply the token feature vector of the sub-text by the corresponding weight to obtain the feature expression result of the corresponding sub-text; The feature expression results of all sub-texts are added together, and the added result is divided by the number of all sub-texts, and the division result is determined to be the target semantic expression of the text to be classified.
2. The text classification method according to claim 1, characterized in that: The step of splitting the text to be classified into N sub-texts includes: According to the word segmentation algorithm, the text to be classified is matched with the entries in the known machine dictionary, and the text matching the entry is determined to be a sub-text, and N sub-texts are obtained.
3. The text classification method according to claim 1, characterized in that: The step of determining the weight of the corresponding subtext according to the sum of similarities between the token feature vectors of each subtext and all other subtexts includes: For any sub-text, calculate the similarity between the token feature vectors of the sub-text and other sub-texts; The similarities between the subtext and the token feature vectors of all other subtexts are summed up, and the sum result is determined as the weight of the corresponding subtext.
4. The text classification method according to claim 3, characterized in that: Determine the summation result as the weight of the corresponding subtext, including: The summation results of all sub-texts are normalized, and the normalized value of each sub-text is determined as the weight of the corresponding sub-text.
5. The text classification method according to any one of claims 1 to 4, characterized in that: The step of matching the target semantic expression with known semantic expressions of classified texts in a database to obtain a known semantic expression matching the target semantic expression, and determining that the classification to which the corresponding classified text belongs is a classification result of the text to be classified, includes: Calculating the similarity between the target semantic expression and the known semantic expressions of the classified texts in the database to obtain the similarity between the target semantic expression and each known semantic expression; Taking the known semantic expression corresponding to the maximum value of the similarity as the known semantic expression matching the target semantic expression; The category to which the classified text corresponding to the known semantic expression matching the target semantic expression belongs is determined as the classification result of the text to be classified.
6. A text classification device based on artificial intelligence, characterized in that: The text classification device comprises: The token feature acquisition module is used to split the text to be classified into N sub-texts, encode each sub-text according to the trained semantic model, and obtain the token feature vector of the corresponding sub-text, where N is a positive integer; A weight calculation module is used to calculate the similarity between the token feature vectors of any sub-text and all other sub-texts, and determine the weight of the corresponding sub-text according to the sum of the similarities between the token feature vectors of each sub-text and all other sub-texts; A semantic expression module, used to determine the target semantic expression of the text to be classified according to the token feature vector of each sub-text and the weight of the corresponding sub-text, combined with the number of all sub-texts; A text classification module is used to match the target semantic expression with known semantic expressions of classified texts in a database to obtain a known semantic expression that matches the target semantic expression, and determine that the category to which the corresponding classified text belongs is a classification result of the text to be classified; Wherein, determining the target semantic expression of the text to be classified includes: For any sub-text, multiply the token feature vector of the sub-text by the corresponding weight to obtain the feature expression result of the corresponding sub-text; The feature expression results of all sub-texts are added together, and the added result is divided by the number of all sub-texts, and the division result is determined to be the target semantic expression of the text to be classified.
7. The text classification device according to claim 6, characterized in that: The weight calculation module comprises: A similarity calculation unit, used for calculating the similarity between a sub-text and a token feature vector of other sub-texts for any sub-text; The weight determination unit is used to sum the similarities between the subtext and the token feature vectors of all other subtexts, and determine the summation result as the weight of the corresponding subtext.
8. A terminal device, characterized in that: The terminal device includes a processor, a memory, and a computer program stored in the memory and executable on the processor, and the processor implements the text classification method according to any one of claims 1 to 5 when executing the computer program.
9. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the text classification method according to any one of claims 1 to 5 is implemented.
Citation Information
Patent Citations
Semantic matching method and device based on knowledge distillation, computer equipment and medium
CN111859960A
Text matching method and device, electronic equipment and storage medium
CN112182166A