A method for determining text similarity and related equipment
By extracting subject words in short texts and calculating the cosine similarity of their word vectors, combined with the number of subject words, the problem of low efficiency and accuracy in calculating short text similarity in the existing technology is solved, and a more efficient and accurate similarity is achieved. Degree measurement.
Patent Information
- Application Number
- CN201910960734.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2019-10-10
- Publication Date
- 2024-10-29
- Estimated Expiration
- 2039-10-10
AI Technical Summary
When calculating the similarity of short texts in the existing technology, the method usually treats all words equally, resulting in a large amount of calculation and low accuracy, making it difficult to accurately and efficiently measure the similarity between short texts.
By calling the topic word recognition model, the topic words in the text are extracted, and the cosine similarity of the word vectors of the topic words is calculated. Combined with the number of topic words, the preset text similarity algorithm is used for calculation to determine the similarity between the texts.
It reduces the computational complexity of similarity calculation and improves calculation efficiency and accuracy. Especially when processing short texts, it can more accurately measure the similarity between texts.
Smart Images

Figure CN110866095B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of data processing, and in particular to a method for determining text similarity and related equipment. Background Art
[0002] In Natural Language Processing (NLP), the problem of how to measure the similarity between two texts is often involved. In problems such as dialogue systems and information retrieval, it is particularly important to measure the similarity between short texts (such as sentences or phrases). The current method for calculating the similarity of short texts is usually to calculate the word vector of each word, and then calculate the cosine similarity between the word vectors to obtain the similarity between the texts. This method "treats all words equally", which significantly increases the amount of calculation and reduces the accuracy at the same time. Therefore, how to accurately and efficiently calculate the similarity between short texts has become a problem that needs to be solved urgently. Summary of the invention
[0003] The embodiment of the present invention provides a method for determining text similarity and related devices, which can improve the accuracy of determining text similarity.
[0004] In a first aspect, an embodiment of the present invention provides a method for determining text similarity, the method being applied to a server, the method comprising:
[0005] Invoking a subject word recognition model to determine at least one first subject word corresponding to the first text and at least one second subject word corresponding to the second text;
[0006] Obtain a first word vector for each first subject word in the at least one first subject word and a second word vector for each second subject word in the at least one second subject word, wherein the first word vector and the second word vector include static word vectors or dynamic word vectors;
[0007] Calculate the cosine similarity between each of the first word vectors and each of the second word vectors, the number M of the first subject words, and the number N of the second subject words, where both M and N are integers greater than 0;
[0008] The cosine similarities, the number M of the first subject words, and the number N of the second subject words are calculated based on a preset text similarity algorithm to determine the similarity between the first text and the second text.
[0009] In one embodiment, the specific implementation of calculating the cosine similarity, the number M of the first subject words, and the number N of the second subject words based on a preset text similarity algorithm to determine the similarity between the first text and the second text is:
[0010] The cosine similarities are summed up based on a preset text similarity algorithm, and the summed result is divided by the sum of log M and log N to obtain the similarity between the first text and the second text.
[0011] In one embodiment, before calling the subject word recognition model to determine at least one first subject word corresponding to the first text and at least one second subject word corresponding to the second text, the following steps may be performed:
[0012] Acquire an initial topic word recognition model, wherein the initial topic word recognition model includes a feature function;
[0013] Adjusting the feature function in the initial topic word recognition model based on a preset feature function;
[0014] A training short text including subject word annotations is obtained, and the initial subject word recognition model adjusted by the feature function is trained and optimized according to the training short text to obtain a subject word recognition model.
[0015] In one embodiment, the specific implementation of obtaining the first word vector of each first topic word in the at least one first topic word and the second word vector of each second topic word in the at least one second topic word is:
[0016] The language representation model is called to perform vector representation on each first subject word in the at least one first subject word and each second subject word in the at least one second subject word to obtain a first word vector for each first subject word and a second word vector for each second subject word, wherein both the first word vector and the second word vector are dynamic word vectors.
[0017] In one embodiment, the specific implementation of obtaining the first word vector of each first topic word in the at least one first topic word and the second word vector of each second topic word in the at least one second topic word is:
[0018] Query and obtain the first word vector of each first topic word in the at least one first topic word and the second word vector of each second topic word in the at least one second topic word from the open source word vector data, wherein the first word vector and the second word vector are both static word vectors.
[0019] In one embodiment, after querying the first word vector of each first topic word in the at least one first topic word from the open source word vector data, the following method may be further performed:
[0020] If the first word vector of any first theme word in the at least one first theme word is not found in the open source word vector data, an all-0 vector is constructed, and the all-0 vector is determined as the first word vector of any first theme word.
[0021] In one embodiment, before calling the subject word recognition model to determine at least one first subject word corresponding to the first text and at least one second subject word corresponding to the second text, the following steps may be performed:
[0022] Detecting whether the number of characters corresponding to the first text and the second text is less than or equal to a preset character number threshold;
[0023] If it is detected that the number of characters corresponding to the first text is less than or equal to the preset character number threshold, triggering the step of calling the subject word recognition model to determine at least one first subject word corresponding to the first text;
[0024] If it is detected that the number of characters corresponding to the second text is less than or equal to the preset character number threshold, the step of calling the subject word recognition model to determine at least one second subject word corresponding to the second text is triggered.
[0025] In a second aspect, an embodiment of the present invention provides a device for determining text similarity, wherein the device for determining text similarity includes a module for executing the method of the first aspect.
[0026] In a third aspect, an embodiment of the present invention provides a server, comprising a processor, a network interface and a memory, wherein the processor, the network interface and the memory are interconnected, wherein the network interface is controlled by the processor for sending and receiving messages, and the memory is used to store a computer program that supports the server to execute the above method, wherein the computer program comprises program instructions, and the processor is configured to call the program instructions to execute the method of the above first aspect.
[0027] 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, wherein the computer program includes program instructions, and when the program instructions are executed by a processor, the processor executes the method of the first aspect.
[0028] In an embodiment of the present application, the server can call a subject word recognition model to determine at least one first subject word corresponding to the first text and at least one second subject word corresponding to the second text, and obtain the first word vector of each first subject word in the at least one first subject word and the second word vector of each second subject word in the at least one second subject word. Further, the server can calculate the cosine similarity between each first word vector and each second word vector, the number M of first subject words, and the number N of second subject words, and then calculate each cosine similarity, the number M of first subject words, and the number N of second subject words based on a preset text similarity algorithm to determine the similarity between the first text and the second text. By using the present application, extracting subject words from the text can not only reduce the amount of calculation of subsequent similarity calculations, improve the calculation efficiency, but also improve the accuracy of the similarity. BRIEF DESCRIPTION OF THE DRAWINGS
[0029] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the accompanying drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other accompanying drawings can be obtained based on these accompanying drawings without paying any creative work.
[0030] Figure 1 It is a flowchart of a method for determining text similarity provided by an embodiment of the present invention;
[0031] Figure 2 It is a flowchart of another method for determining text similarity provided by an embodiment of the present invention;
[0032] Figure 3 is a schematic diagram of a preset characteristic function provided by an embodiment of the present invention;
[0033] Figure 4 is a schematic block diagram of a device for determining text similarity provided by an embodiment of the present invention;
[0034] Figure 5 It is a schematic block diagram of a server provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0035] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0036] See also Figure 1 , Figure 11 is a flow chart of a method for determining text similarity provided by an embodiment of the present invention. The method is applied to a server and can be executed by the server. As shown in the figure, the method for determining text similarity may include:
[0037] S101: Calling a subject word recognition model to determine at least one first subject word corresponding to a first text and at least one second subject word corresponding to a second text.
[0038] The first text and the second text mainly refer to short texts, such as microblogs, chat messages, news topics, opinion comments, question texts, mobile phone text messages, document abstracts, etc.
[0039] In one embodiment, before executing step S101, it can be detected whether the number of characters corresponding to the first text and the second text is less than or equal to a preset character number threshold. If it is detected that the number of characters corresponding to the first text is less than or equal to the preset character number threshold, the step of calling the subject word recognition model to determine at least one first subject word corresponding to the first text is triggered; if it is detected that the number of characters corresponding to the second text is less than or equal to the preset character number threshold, the step of calling the subject word recognition model to determine at least one second subject word corresponding to the second text is triggered. The preset character number is pre-set by the developer based on experimental data, for example, it can be 160 characters.
[0040] S102: Obtain a first word vector of each first topic word in at least one first topic word and a second word vector of each second topic word in at least one second topic word.
[0041] The first word vector and the second word vector include a static word vector or a dynamic word vector. The static word vector can be obtained based on open source word vector data, and the dynamic word vector can be obtained from a language representation model, which can be, for example, a word vector representation (Embeddings from Language Model, ELMo) model or a bidirectional encoder pre-trained representation (Bidirectional Encoder Representations from Transformers, BERT) model.
[0042] The above language representation model means that the word vectors obtained for the same word in different sentences are different, that is, the word vectors obtained are dynamic. Therefore, the word vectors obtained for the same word in different sentences after passing through the language representation model are different, and the sets of synonym candidate obtained are also different, that is, the dynamic word vector is not fixed, but changes at any time according to the context, which can well reflect the meaning of the word in different contexts.
[0043] For example, for a polysemous word "apple", in the static word vector representation, the word vectors of the word "apple" obtained in sentence 1: "I ate an apple" and sentence 2: "Apples are very useful" are the same, but in fact, the synonym candidate sets of these two words are completely different. The words that are close to the word vector of the word "apple" in sentence 1 are fruits such as "orange" and "banana", and the words that are close to the word vector of the word "apple" in sentence 2 are words such as "mobile phone" and "Huawei". It can be seen that the use of a language representation model to obtain dynamic word vectors corresponding to the first text and the second text, and determining the similarity between the first text and the second text based on the dynamic word vector can improve the accuracy of the similarity.
[0044] S103: Calculate the cosine similarity between each first word vector and each second word vector, the number M of first topic words, and the number N of second topic words, where M and N are both integers greater than 0.
[0045] S104: Calculate the cosine similarities, the number M of the first subject words, and the number N of the second subject words based on a preset text similarity algorithm to determine the similarity between the first text and the second text.
[0046] In one embodiment, the server may calculate the sum of the cosine similarities based on a preset text similarity algorithm, and divide the sum calculation result by the sum of log M and log N to obtain the similarity between the first text and the second text.
[0047] Exemplarily, the calculation method corresponding to the preset text similarity algorithm is shown in Formula 1-1:
[0048]
[0049] Among them, w u is the first word vector set corresponding to all the first subject words identified in the first text, and i is the first word vector set w u The first word vector in t is the second word vector set corresponding to all the second subject words identified in the second text, j is the second word vector set w t The second word vector in, d is the cosine similarity value between the first word vector and the second word vector; M and N are the number of topic words in the first text and the second text respectively, and S is the similarity between the first text and the second text.
[0050] It can be seen that since both the first text and the second text refer to short texts, Formula 1-1 iteratively calculates the cosine similarity value between the first word vector and the second word vector, and finally takes the log average to obtain the similarity between the two short texts. In this way, the arithmetic advantage of relatively long texts in the similarity calculation process can be suppressed, thereby improving the accuracy of the similarity between short texts.
[0051] In an embodiment of the present application, the server can call a subject word recognition model to determine at least one first subject word corresponding to the first text and at least one second subject word corresponding to the second text, and obtain the first word vector of each first subject word in the at least one first subject word and the second word vector of each second subject word in the at least one second subject word. Further, the server can calculate the cosine similarity between each first word vector and each second word vector, the number M of first subject words, and the number N of second subject words, and then calculate each cosine similarity, the number M of first subject words, and the number N of second subject words based on a preset text similarity algorithm to determine the similarity between the first text and the second text. By using the present application, extracting subject words from the text can not only reduce the amount of computation of subsequent similarity calculations, but also improve the accuracy of the similarity.
[0052] See also Figure 2 , Figure 2 is a flow chart of another method for determining text similarity provided by an embodiment of the present invention. The method is applied to a server and can be executed by the server. As shown in the figure, the method for determining text similarity may include:
[0053] S201: Acquire an initial topic word recognition model, where the initial topic word recognition model includes a feature function.
[0054] S202: adjusting the feature function in the initial topic word recognition model based on a preset feature function.
[0055] S203: Obtain a training short text including subject word annotations, and train and optimize the initial subject word recognition model after the feature function is adjusted according to the training short text to obtain a subject word recognition model.
[0056] S204: Calling a subject word recognition model to determine at least one first subject word corresponding to the first text and at least one second subject word corresponding to the second text.
[0057] In one embodiment, the initial keyword recognition model can be, for example, a conditional random field algorithm (CRF), which is characterized by flexible feature design, can accommodate more context information, and the final recognition result is globally optimal. For short texts with relatively little information, using this algorithm to extract keywords can improve the accuracy of keyword extraction.
[0058] Exemplarily, the calculation formula corresponding to the CRF algorithm is shown in Formula 2-1:
[0059]
[0060] Where P is the conditional probability, Z(x) is the normalization factor, which is the sum of all possible output sequences, and t k and l is the characteristic function, λ k and μ l Yes k and l The corresponding weights are respectively, x represents the input observation sequence, y represents the output tag sequence corresponding to the input observation sequence, in this scheme, the input observation sequence is the input first text or the second text, when the input observation sequence is the first text, the output tag sequence corresponding to the input observation sequence is the at least one first theme word corresponding to the output of the first text; when the input observation sequence is the second text, the output tag sequence corresponding to the input observation sequence is the at least one second theme word corresponding to the output of the second text.
[0061] In one embodiment, the server may perform the above-mentioned feature function t in the initial topic word recognition model based on a preset feature function. k and l Adjustments are made, and several training short texts including subject word annotations are obtained. Further, the initial subject word recognition algorithm after the feature function is adjusted can be optimized based on the several training short texts to obtain a subject word body recognition algorithm.
[0062] Among them, the preset feature function is set for the detection of short text keywords. The preset feature function includes multiple ones, which increases the number of feature functions in the initial keyword recognition algorithm and the field interval detected during the detection process, which can improve the accuracy of the keyword body recognition algorithm in identifying short text keywords.
[0063] Exemplarily, the above-mentioned multiple preset characteristic functions can be as follows: Figure 3As shown, U00~U12 are all preset feature functions, which represent the specific detection method and the field interval of the detection process in the process of detecting short text keywords. For example, U00[-2,0], where 0 represents the first position corresponding to the current word, and -2 represents the target position of two words pushed forward based on the current word. In this case, based on the preset feature function of U00, any word in the notification text can be first determined as the current word, and the current word can be identified as a keyword for the post; further, the current word can be used as a reference to push two words forward to obtain the target word corresponding to the target position, and the target word can be identified as a keyword for the post. Among them, the maximum field interval detected in U00 is 2, and correspondingly, the other U01~U12 are also understood accordingly.
[0064] It is understandable that, since short texts generally have fewer words, the maximum range of the feature function (ie, the maximum field interval detected) cannot be greater than the preset interval threshold. Figure 3 The corresponding preset interval threshold is 2, that is, Figure 2 The “2” and “-2” in the image.
[0065] Furthermore, after the keyword recognition model is obtained, the first text and the second text can be used as inputs of the keyword model, and the keyword model will output at least one first keyword corresponding to the text and at least one second keyword corresponding to the second text.
[0066] S205: Obtain a first word vector of each first topic word in at least one first topic word and a second word vector of each second topic word in at least one second topic word.
[0067] The first word vector and the second word vector include a static word vector or a dynamic word vector. The static word vector can be obtained based on open source word vector data, and the dynamic word vector can be obtained from a language representation model.
[0068] In one embodiment, the server can call a language representation model to perform vector representation on each first subject word in at least one first subject word and each second subject word in at least one second subject word, and obtain a first word vector for each first subject word and a second word vector for each second subject word, wherein both the first word vector and the second word vector are dynamic word vectors.
[0069] In one embodiment, the server may first train the language representation model using the training corpus, and call the trained language representation model to perform data processing on the short text corpus to be expanded to obtain the dynamic word vector of each word in the corpus to be expanded. The short text corpus to be expanded may be one or more short sentences (such as a question), and the training corpus may include a large amount of open source network corpus.
[0070] The above language representation model means that the word vectors obtained by the same word in different sentences are different, that is, the word vectors obtained are dynamic. Therefore, the word vectors obtained by the language representation model for the same word in different sentences are different, and the candidate sets of synonyms obtained are also different, that is, the dynamic word vector is not fixed, but changes at any time according to the context.
[0071] In one embodiment, the above-mentioned language representation model may include a word vector representation (Embeddings from Language Model, ELMo) model or a pre-trained representation (Bidirectional Encoder Representations from Transformers, BERT) model obtained by a language model. Among them, the ELMo model is learned from the internal state of a deep bidirectional language model. The ELMo model can learn the complexity of vocabulary usage, such as grammar and semantics, and can also learn the lexical ambiguity in different contexts, and the dynamic word vector obtained can be flexibly applied to a variety of tasks. The BERT model is a method for pre-training language representation, which can be understood as a general "language understanding" model trained on a large text corpus (such as Wikipedia), and then this model is used for downstream natural language processing (Natural Language Processing, NLP) tasks (such as question answering). BERT is superior to previous methods because it is the first unsupervised, deep bidirectional system for pre-training NLP.
[0072] In one embodiment, the first word vector and the second word vector are static word vectors. The server can query and obtain the first word vector of each first subject word in at least one first subject word and the second word vector of each second subject word in at least one second subject word from the open source word vector data, and the first word vector and the second word vector are both static word vectors.
[0073] In one embodiment, after the server queries the first word vector of each first topic word in at least one first topic word from the open source word vector data, if the first word vector of any first topic word in at least one first topic word is not found in the open source word vector data, an all-0 vector is constructed and the all-0 vector is determined as the first word vector of any first topic word.
[0074] It can be seen that the use of open source word vector data to determine the first word vector or the second word vector can not only save the time of training the model by yourself while ensuring the quality of the vector, but also avoid tedious steps such as data collection, cleaning and parameter adjustment.
[0075] In one embodiment, the above-mentioned open source word vector data includes multiple words and word vectors corresponding to each word. In response to this situation, the server can query the first word vector corresponding to each first subject word and the second word vector corresponding to each second subject word from the open source word vector data. If the first word vector and the second word vector are not found in the open source word vector data, an all-0 vector can be constructed and the all-0 vector can be used as the corresponding first word vector or second word vector. For example, if the server does not query the first word vector corresponding to the first subject word in the open source word vector data, an all-0 vector can be constructed and the all-0 vector can be determined as the first word vector corresponding to the first subject word.
[0076] S206: Calculate the cosine similarity between each first word vector and each second word vector, the number of first subject words M, and the number of second subject words N, and calculate each cosine similarity, the number of first subject words M, and the number of second subject words N based on a preset text similarity algorithm to determine the similarity between the first text and the second text. The specific implementation of step 206 can refer to the relevant description of steps S103 to S104 in the above embodiment, which will not be repeated here.
[0077] In an embodiment of the present application, the server can obtain an initial subject word recognition model, which includes a feature function, and adjusts the feature function in the initial subject word recognition model based on a preset feature function. Further, a training short text including subject word annotations is obtained, and the initial subject word recognition model after the feature function is adjusted is trained and optimized according to the training short text, thereby obtaining a subject word recognition model. Further, the server calls the subject word recognition model to determine at least one first subject word corresponding to the first text and at least one second subject word corresponding to the second text, and obtains the first word vector of each first subject word in the at least one first subject word and the second word vector of each second subject word in the at least one second subject word. Further, the server can calculate the cosine similarity between each first word vector and each second word vector, the number of first subject words M, and the number of second subject words N, and then calculate each cosine similarity, the number of first subject words M, and the number of second subject words N based on a preset text similarity algorithm to determine the similarity between the first text and the second text. By adopting the present application, the accuracy of the determined first subject words and second subject words can be improved, thereby facilitating the improvement of the accuracy of calculating the similarity between the first text and the second text.
[0078] The embodiment of the present invention also provides a device for determining text similarity. The device includes a method for executing the above Figure 1 or Figure 2 The module of the method is configured on the server. Figure 4, is a schematic block diagram of a device for determining text similarity provided by an embodiment of the present invention. The device for determining text similarity of this embodiment includes:
[0079] A processing module 40 is used to call a subject word recognition model to determine at least one first subject word corresponding to the first text and at least one second subject word corresponding to the second text;
[0080] An acquisition module 41 is used to acquire a first word vector of each first subject word in the at least one first subject word and a second word vector of each second subject word in the at least one second subject word, wherein the first word vector and the second word vector include a static word vector or a dynamic word vector;
[0081] The processing module 40 is further used to calculate the cosine similarity between each of the first word vectors and each of the second word vectors, the number M of the first subject words, and the number N of the second subject words, where both M and N are integers greater than 0;
[0082] The processing module 40 is further configured to calculate the cosine similarities, the number M of the first subject words, and the number N of the second subject words based on a preset text similarity algorithm to determine the similarity between the first text and the second text.
[0083] In one embodiment, the cosine similarity is calculated based on a preset text similarity algorithm, and the processing module 40 is specifically used to: sum up the cosine similarities based on a preset text similarity algorithm, and divide the summation result by the sum of log M and log N to obtain the similarity between the first text and the second text.
[0084] In one embodiment, the acquisition module 41 is also used to obtain an initial subject word recognition model and a training short text including subject word annotations, wherein the initial subject word recognition model includes a feature function; the processing module 40 is also used to adjust the feature function in the initial subject word recognition model based on a preset feature function, and train and optimize the initial subject word recognition model after the feature function is adjusted according to the training short text to obtain a subject word recognition model.
[0085] In one embodiment, the acquisition module 41 is further specifically used to call a language representation model to perform vector representation on each first subject word in the at least one first subject word and each second subject word in the at least one second subject word, to obtain a first word vector for each first subject word and a second word vector for each second subject word, wherein both the first word vector and the second word vector are dynamic word vectors.
[0086] In one embodiment, the acquisition module 41 is also specifically used to query and obtain the first word vector of each first topic word in the at least one first topic word and the second word vector of each second topic word in the at least one second topic word from open source word vector data, and the first word vector and the second word vector are both static word vectors.
[0087] In one embodiment, the acquisition module 41 is also used to: if the first word vector of any first subject word in the at least one first subject word is not found in the open source word vector data, then construct an all-0 vector and determine the all-0 vector as the first word vector of any first subject word.
[0088] In one embodiment, the processing module 40 is further configured to:
[0089] Detecting whether the number of characters corresponding to the first text and the second text is less than or equal to a preset character number threshold;
[0090] If it is detected that the number of characters corresponding to the first text is less than or equal to the preset character number threshold, triggering the step of calling the subject word recognition model to determine at least one first subject word corresponding to the first text;
[0091] If it is detected that the number of characters corresponding to the second text is less than or equal to the preset character number threshold, the subject word recognition model is called to determine at least one second subject word corresponding to the second text.
[0092] It should be noted that the functions of the functional modules of the apparatus for determining text similarity described in the embodiment of the present invention can be based on Figure 1 or Figure 2 The specific implementation process of the method in the method embodiment can be referred to Figure 1 or Figure 2 The relevant description of the method embodiment will not be repeated here.
[0093] See also Figure 5 , Figure 5 is a schematic block diagram of a server provided by an embodiment of the present invention, such as Figure 5 As shown, the server includes a processor 501, a memory 502 and a network interface 503. The processor 501, the memory 502 and the network interface 503 can be connected via a bus or other means. Figure 5In the example, the connection through the bus is taken. Wherein, the network interface 503 is controlled by the processor to send and receive messages, the memory 502 is used to store a computer program, the computer program includes program instructions, and the processor 501 is used to execute the program instructions stored in the memory 502. Wherein, the processor 501 is configured to call the program instructions to execute: call the subject word recognition model to determine at least one first subject word corresponding to the first text and at least one second subject word corresponding to the second text; obtain the first word vector of each first subject word in the at least one first subject word and the second word vector of each second subject word in the at least one second subject word, the first word vector and the second word vector include static word vectors or dynamic word vectors; calculate the cosine similarity between each first word vector and each second word vector, the number of the first subject words M, and the number of the second subject words N, both of which are integers greater than 0; calculate each of the cosine similarities, the number of the first subject words M, and the number of the second subject words N based on the preset text similarity algorithm to determine the similarity between the first text and the second text.
[0094] In one embodiment, the processor 501 is specifically configured to: sum the cosine similarities based on a preset text similarity algorithm, and divide the summation result by the sum of log M and log N to obtain the similarity between the first text and the second text.
[0095] In one embodiment, the processor 501 is also used to obtain an initial subject word recognition model and a training short text including subject word annotations, wherein the initial subject word recognition model includes a feature function; the processor 501 is also used to adjust the feature function in the initial subject word recognition model based on a preset feature function, and train and optimize the initial subject word recognition model after the feature function is adjusted according to the training short text to obtain a subject word recognition model.
[0096] In one embodiment, the processor 501 is further specifically used to call a language representation model to perform vector representation on each first subject word in the at least one first subject word and each second subject word in the at least one second subject word, to obtain a first word vector for each first subject word and a second word vector for each second subject word, wherein both the first word vector and the second word vector are dynamic word vectors.
[0097] In one embodiment, the processor 501 is further specifically used to query and obtain the first word vector of each first topic word in the at least one first topic word and the second word vector of each second topic word in the at least one second topic word from open source word vector data, and the first word vector and the second word vector are both static word vectors.
[0098] In one embodiment, the processor 501 is further used to: if the first word vector of any first topic word in the at least one first topic word is not found in the open source word vector data, then construct an all-0 vector and determine the all-0 vector as the first word vector of any first topic word.
[0099] In one embodiment, the processor 501 is further configured to:
[0100] Detecting whether the number of characters corresponding to the first text and the second text is less than or equal to a preset character number threshold;
[0101] If it is detected that the number of characters corresponding to the first text is less than or equal to the preset character number threshold, triggering the step of calling the subject word recognition model to determine at least one first subject word corresponding to the first text;
[0102] If it is detected that the number of characters corresponding to the second text is less than or equal to the preset character number threshold, the subject word recognition model is called to determine at least one second subject word corresponding to the second text.
[0103] It should be understood that in the embodiment of the present invention, the processor 501 may be a central processing unit (CPU), and the processor 501 may also be other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (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 the processor may also be any conventional processor, etc.
[0104] The memory 502 may include a read-only memory and a random access memory, and provide instructions and data to the processor 501. A portion of the memory 502 may also include a non-volatile random access memory. For example, the memory 502 may also store information about the device type.
[0105] In a specific implementation, the processor 501, the memory 502 and the network interface 503 described in the embodiment of the present invention can execute the Figure 1 or Figure 2 The implementation described in the method embodiment can also implement the implementation of the device for determining text similarity described in the embodiment of the present invention, which will not be described in detail here.
[0106] In another embodiment of the present invention, a computer-readable storage medium is provided, the computer-readable storage medium stores a computer program, the computer program includes program instructions, and when the program instructions are executed by a processor, the following are implemented: calling a subject word recognition model to determine at least one first subject word corresponding to a first text and at least one second subject word corresponding to a second text; obtaining a first word vector of each first subject word in the at least one first subject word and a second word vector of each second subject word in the at least one second subject word, the first word vector and the second word vector include static word vectors or dynamic word vectors; calculating the cosine similarity between each first word vector and each second word vector, the number M of the first subject words, and the number N of the second subject words, wherein M and N are both integers greater than 0; calculating each of the cosine similarities, the number M of the first subject words, and the number N of the second subject words based on a preset text similarity algorithm to determine the similarity between the first text and the second text.
[0107] The computer-readable storage medium may be an internal storage unit of the server described in any of the foregoing embodiments, such as a hard disk or memory of the server. The computer-readable storage medium may also be an external storage device of the server, such as a plug-in hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (Secure Digital, SD) card, a flash card (Flash Card), etc. equipped on the server. Furthermore, the computer-readable storage medium may also include both an internal storage unit of the server and an external storage device. The computer-readable storage medium is used to store the computer program and other programs and data required by the server. The computer-readable storage medium may also be used to temporarily store data that has been output or is to be output.
[0108] A person skilled in the art can understand that all or part of the processes in the above-mentioned embodiments can be implemented by instructing the relevant hardware through a computer program, and the program can be stored in a computer-readable storage medium, and when the program is executed, it can include the processes of the embodiments of the above-mentioned methods. The storage medium can be a disk, an optical disk, a read-only memory (ROM) or a random access memory (RAM), etc.
[0109] The above disclosure is only part of the embodiments of the present invention, which certainly cannot be used to limit the scope of the rights of the present invention. Ordinary technicians in this field can understand that all or part of the processes of the above embodiments and equivalent changes made according to the claims of the present invention are still within the scope of the invention.
Claims
1. A method for determining text similarity, characterized in that: The method comprises: First, it is detected whether the number of characters corresponding to the first text and the second text is less than or equal to a preset character number threshold; if it is detected that the number of characters corresponding to the first text is less than or equal to the preset character number threshold, then the step of calling the subject word recognition model to determine at least one first subject word corresponding to the first text is triggered; if it is detected that the number of characters corresponding to the second text is less than or equal to the preset character number threshold, then the step of calling the subject word recognition model to determine at least one second subject word corresponding to the second text is triggered; Acquire an initial subject word recognition model, the initial subject word recognition model includes a feature function; adjust the feature function in the initial subject word recognition model based on a preset feature function; acquire a training short text including subject word annotations, and train and optimize the initial subject word recognition model after the feature function is adjusted according to the training short text to obtain a subject word recognition model; the initial subject word recognition model uses a conditional random field algorithm; Invoking a subject word recognition model to determine at least one first subject word corresponding to the first text and at least one second subject word corresponding to the second text; Obtain a first word vector for each first topic word in the at least one first topic word and a second word vector for each second topic word in the at least one second topic word, wherein the first word vector and the second word vector include static word vectors or dynamic word vectors; the static word vector is obtained from open source word vector data, and the dynamic word vector is obtained from a language representation model; the language representation model is an ELMO model or a BERT model; Calculate the cosine similarity between each of the first word vectors and each of the second word vectors, the number M of the first subject words, and the number N of the second subject words, where both M and N are integers greater than 0; The cosine similarities, the number M of the first subject words, and the number N of the second subject words are calculated based on a preset text similarity algorithm to determine the similarity between the first text and the second text. The calculation method corresponding to the preset text similarity algorithm is shown in the following formula: Among them, wu is the set of first word vectors corresponding to all the identified first subject words of the first text, i is the first word vector in the first word vector set wu; wt is the set of second word vectors corresponding to all the identified second subject words of the second text, j is the second word vector in the second word vector set wt, d is the cosine similarity value between the first word vector and the second word vector; M and N are the number of subject words in the first text and the second text respectively, and S is the similarity between the first text and the second text.
2. The method according to claim 1, characterized in that: The calculating the cosine similarity, the number M of the first subject words, and the number N of the second subject words based on a preset text similarity algorithm to determine the similarity between the first text and the second text includes: The cosine similarities are summed up based on a preset text similarity algorithm, and the summed result is divided by the sum of log M and log N to obtain the similarity between the first text and the second text.
3. The method according to claim 1, characterized in that: The obtaining of a first word vector of each first subject word in the at least one first subject word and a second word vector of each second subject word in the at least one second subject word comprises: Query and obtain the first word vector of each first topic word in the at least one first topic word and the second word vector of each second topic word in the at least one second topic word from the open source word vector data, wherein the first word vector and the second word vector are both static word vectors.
4. The method according to claim 1, characterized in that: After querying the first word vector of each first topic word in the at least one first topic word from the open source word vector data, the method further includes: If the first word vector of any first theme word in the at least one first theme word is not found in the open source word vector data, an all-0 vector is constructed, and the all-0 vector is determined as the first word vector of any first theme word.
5. A device for determining text similarity, characterized in that: The device comprises: A processing module, used for calling a subject word recognition model to determine at least one first subject word corresponding to the first text and at least one second subject word corresponding to the second text; An acquisition module, used to acquire a first word vector of each first topic word in the at least one first topic word and a second word vector of each second topic word in the at least one second topic word, wherein the first word vector and the second word vector include static word vectors or dynamic word vectors; the static word vectors are obtained from open source word vector data, and the dynamic word vectors are obtained from a language representation model; the language representation model is an ELMO model or a BERT model; The processing module is further used to calculate the cosine similarity between each of the first word vectors and each of the second word vectors, the number M of the first subject words, and the number N of the second subject words, where both M and N are integers greater than 0; The processing module is further used to calculate each of the cosine similarities, the number M of the first subject words, and the number N of the second subject words based on a preset text similarity algorithm to determine the similarity between the first text and the second text. The calculation method corresponding to the preset text similarity algorithm is shown in the following formula: Wherein, wu is the first word vector set corresponding to all the first subject words identified in the first text, i is the first word vector in the first word vector set wu; wt is the second word vector set corresponding to all the second subject words identified in the second text, j is the second word vector in the second word vector set wt, d is the cosine similarity value between the first word vector and the second word vector; M and N are the number of subject words in the first text and the second text respectively, and S is the similarity between the first text and the second text; Before calling the subject word recognition model to determine at least one first subject word corresponding to the first text and at least one second subject word corresponding to the second text, the method includes: First, it is detected whether the number of characters corresponding to the first text and the second text is less than or equal to a preset character number threshold; if it is detected that the number of characters corresponding to the first text is less than or equal to the preset character number threshold, then the step of calling the subject word recognition model to determine at least one first subject word corresponding to the first text is triggered; if it is detected that the number of characters corresponding to the second text is less than or equal to the preset character number threshold, then the step of calling the subject word recognition model to determine at least one second subject word corresponding to the second text is triggered; An initial subject word recognition model is obtained, wherein the initial subject word recognition model includes a feature function; the feature function in the initial subject word recognition model is adjusted based on a preset feature function; a training short text including subject word annotations is obtained, and the initial subject word recognition model after the feature function is adjusted is trained and optimized according to the training short text to obtain a subject word recognition model; the initial subject word recognition model uses a conditional random field algorithm.
6. A server, characterized in that: The method comprises a processor and a memory, wherein the processor and the memory are connected to each other, wherein the memory is used to store a computer program, the computer program comprises program instructions, and the processor is configured to call the program instructions to execute the method according to any one of claims 1 to 5.
7. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and the computer program is executed by a processor to implement the method according to any one of claims 1 to 5.
Citation Information
Patent Citations
Intelligent questioning and answering method and system
CN108256056A
Forum post recommendation method, system and device and storage medium
CN109871491A
