A method, device, equipment and readable storage medium for obtaining text similarity
By obtaining multiple numerical features of text pairs, constructing a sample feature matrix and conducting model training, combined with semantic and syntactic structures, the problem of unified definition and efficiency of text similarity calculation in the existing technology is solved, and fast and accurate text similarity is achieved. Computing, improving user experience.
Patent Information
- Application Number
- CN201810827262.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2018-07-25
- Publication Date
- 2024-09-24
- Estimated Expiration
- 2038-12-28
AI Technical Summary
Existing text similarity calculation technology lacks a unified definition and effective methods, making it difficult to quickly and accurately obtain text similarity in different application scenarios.
By obtaining multiple numerical features of text pairs, taking into account semantic and syntactic structures, constructing a sample feature matrix and conducting model training, a prediction model is obtained to calculate text similarity, and a prediction model based on ordered and unordered edit distance, word meaning distance and dependency relationship is used. Features, using neural networks for feature extraction.
It achieves fast, accurate and easy-to-implement text similarity calculation, improves user experience, and has the advantages of trainable weights and less manual intervention.
Smart Images

Figure CN110852056B
Abstract
Description
Technical Field
[0001] The present invention relates to the field of communication technology, and in particular to a method, device, equipment and readable storage medium for obtaining text similarity. Background Art
[0002] In the era of information explosion, people's demand for quickly and accurately obtaining the required content from massive information is increasing day by day. To meet this demand, many applications have emerged, such as information retrieval, intelligent question and answer, document duplicate checking, personalized recommendation, etc. Behind these applications, text similarity calculation technology is one of the key core technologies.
[0003] Text similarity has been widely discussed in different fields. Due to different application scenarios, its connotation varies, so there is no unified and recognized definition. From the perspective of information theory, text similarity is related to the commonalities and differences between texts. The greater the commonality and the smaller the difference, the higher the similarity between texts; conversely, the smaller the commonality and the greater the difference, the lower the similarity between texts. Summary of the invention
[0004] This article aims to provide a method, device, equipment and readable storage medium for obtaining text similarity. By obtaining multiple numerical features of text pairs, the text similarity is judged taking into account semantic and syntactic structures. It has the advantages of trainable weights, less manual intervention, simple and fast, easy to implement, high accuracy, etc., which improves the user experience.
[0005] The technical solutions adopted in this paper to solve the above technical problems are as follows:
[0006] According to one aspect of this document, a method for obtaining text similarity is provided, comprising:
[0007] Obtaining numerical features of the text pairs according to the text pair dataset;
[0008] Constructing a sample feature matrix through the numerical features of the text pairs;
[0009] Performing model training according to the sample feature matrix and the prediction vector to obtain a prediction model;
[0010] A target text pair is obtained, and a similarity score of the target text pair is obtained according to the sample feature matrix and the prediction model.
[0011] Optionally, the numerical features include: semantic features based on ordered edit distance, semantic features based on unordered edit distance, semantic features based on word meaning distance, and syntactic features based on dependency relationship.
[0012] Optionally, obtaining the numerical features of the text pair according to the text pair dataset includes:
[0013] Obtaining a training corpus file, wherein the training corpus file includes a plurality of groups of text pairs and a similarity score of each group of text pairs;
[0014] Obtaining a training data set according to the training corpus file;
[0015] Obtaining a word vector matrix from the training data set;
[0016] According to the word vector matrix and the edit distance, obtaining a first improved edit distance between the text pairs as a semantic feature based on the ordered edit distance;
[0017] According to the edit distance and the bag-of-words model, obtaining a second improved edit distance between the text pairs as a semantic feature based on the unordered edit distance;
[0018] According to the word vector matrix, obtaining the word meaning distance between the text pairs as a semantic feature based on the word meaning distance;
[0019] Perform dependency syntactic analysis on text pairs and obtain the syntactic distance between text pairs as a syntactic feature based on dependency relations.
[0020] Optionally, the acquiring the target text pair, and acquiring the similarity score of the target text pair according to the sample feature matrix and the prediction model includes:
[0021] Obtaining a target text pair, obtaining numerical features of the target text pair, and forming a feature vector of the target text pair;
[0022] Substitute the feature vector of the target text pair into the prediction model to obtain a similarity score of the target text pair.
[0023] According to another aspect of the present invention, a device for obtaining text similarity is provided, comprising:
[0024] A training module, used for obtaining numerical features of the text pairs according to the text pair data set;
[0025] A matrix construction module, used for constructing a sample feature matrix through the numerical features of the text pairs;
[0026] A prediction module, used for performing model training according to the sample feature matrix and the prediction vector to obtain a prediction model;
[0027] The online acquisition module is used to acquire a target text pair and acquire a similarity score of the target text pair according to the sample feature matrix and the prediction model.
[0028] Optionally, the numerical features include: semantic features based on ordered edit distance, semantic features based on unordered edit distance, semantic features based on word meaning distance, and syntactic features based on dependency relationship.
[0029] Optionally, the training module includes:
[0030] An acquisition unit, used for acquiring a training corpus file, wherein the training corpus file includes a plurality of groups of text pairs and a similarity score of each group of text pairs;
[0031] An extraction unit, used to obtain a training data set according to the training corpus file;
[0032] A word vector acquisition unit, used to obtain a word vector matrix from the training data set;
[0033] An ordered edit distance acquisition unit, used to acquire a first improved edit distance between text pairs according to the word vector matrix and the edit distance as a semantic feature based on the ordered edit distance;
[0034] An unordered edit distance acquisition unit, used to acquire a second improved edit distance between text pairs according to the edit distance and the bag-of-words model as a semantic feature based on the unordered edit distance;
[0035] A word meaning distance acquisition unit, used to acquire the word meaning distance between text pairs according to the word vector matrix as a semantic feature based on the word meaning distance;
[0036] The syntactic distance acquisition unit is used to perform dependency syntactic analysis on the text pairs and acquire the syntactic distance between the text pairs as a syntactic feature based on the dependency relationship.
[0037] Optionally, the online acquisition module includes:
[0038] A feature vector acquisition unit, used to acquire a target text pair, acquire numerical features of the target text pair, and form a feature vector of the target text pair;
[0039] The similarity acquisition unit is used to substitute the feature vector of the target text pair into the prediction model to obtain the similarity score of the target text pair.
[0040] According to another aspect of the present invention, an electronic device is provided, comprising a memory, a processor, and at least one application stored in the memory and configured to be executed by the processor, wherein the application is configured to execute the above-mentioned method for obtaining text similarity.
[0041] According to another aspect of the present invention, a readable storage medium is provided, on which a computer program is stored, and when the program is executed by a processor, the method for obtaining text similarity described above is implemented.
[0042] The embodiments of the present invention provide a method, device, equipment and readable storage medium for obtaining text similarity. The method comprises: obtaining numerical features of text pairs according to a text pair data set; constructing a sample feature matrix according to the numerical features of the text pairs; performing model training according to the sample feature matrix and a prediction vector to obtain a prediction model; obtaining a target text pair, and obtaining a similarity score of the target text pair according to the sample feature matrix and the prediction model; and determining text similarity by obtaining multiple numerical features of the text pairs, taking into account semantic and syntactic structures. The method has the advantages of trainable weights, less manual intervention, simplicity and speed, easy implementation, high accuracy, etc., thereby improving user experience. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] Figure 1 A flow chart of a method for obtaining text similarity provided in Embodiment 1 of the present invention;
[0044] Figure 2 for Figure 1 A method flow chart of step S10;
[0045] Figure 3 for Figure 1 A method flow chart of step S40;
[0046] Figure 4 An exemplary structural block diagram of a device for obtaining text similarity provided in Embodiment 2 of the present invention;
[0047] Figure 5 for Figure 4 An exemplary structural block diagram of the training module in FIG.
[0048] Figure 6 for Figure 4 An exemplary structural block diagram of the online acquisition module.
[0049] The realization of the purpose, functional features and advantages of this article will be further explained in conjunction with embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION
[0050] In order to make the technical problems, technical solutions and beneficial effects to be solved by this article clearer and more understandable, this article is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described here are only used to explain this article and are not used to limit this article.
[0051] Embodiment 1
[0052] like Figure 1 As shown, in this embodiment, a method for obtaining text similarity includes:
[0053] S10, obtaining numerical features of the text pair according to the text pair data set;
[0054] S20, constructing a sample feature matrix through the numerical features of the text pairs;
[0055] S30, performing model training according to the sample feature matrix and the prediction vector to obtain a prediction model;
[0056] S40: Obtain a target text pair, and obtain a similarity score of the target text pair according to the sample feature matrix and the prediction model.
[0057] In this embodiment, the text similarity is judged by obtaining multiple numerical features of the text pair, taking into account the semantic and syntactic structures. The semantic similarity between the texts, including features such as word meaning, edit distance, and bag-of-words model, is considered. The grammatical similarity including syntactic structure is also considered, and the semantics and syntax are combined. A neural network is used to perform feature extraction at a higher level, which has the advantages of trainable weights, less manual intervention, simple and fast, easy implementation, and high accuracy, thereby improving the user experience.
[0058] In step S10, first, a large amount of annotated text pair data is prepared as training corpus; each sample in the training corpus is a set of text pairs and corresponding annotated similarity scores, which can be formally expressed as [text1; text2; score], where text1 and text2 are text pairs for similarity acquisition, and score is the annotated similarity score. The annotation score can come from manual annotation or other prior information, such as the user's satisfaction with the system's answer in the question-and-answer system, the user's browsing of the system's arrangement information in the retrieval system, etc. All samples are saved in the file originalData.txt, each line is a training sample, and text1, text2, and score in each training sample can be separated by a tab. Among them, the annotated similarity score score is a real number between 0 and 1, and the larger the number, the higher the similarity between the text pairs, and vice versa; in particular, a score of 0 indicates that the text pairs are completely unrelated, and a score of 1 indicates that the text pairs are exactly the same. The accuracy of the score is not fixed depending on the source. For example, if it comes from manual annotation, it may be a single-digit decimal such as 0.3 or 0.6. If it comes from other application systems, it may be a multi-digit decimal such as 0.563 or 0.8192.
[0059] In this embodiment, the format of the file originalData.txt is as follows:
[0060] I would like to ask where I can buy ZTE mobile phones, where can I buy ZTE mobile phones, 0.769
[0061] ZTE Corporation in Nanjing Yuhuatai District, Nanjing Yuhuatai District ZTE Corporation, 0.816
[0062] The intelligent question-answering system team has made new achievements, the field of intelligent question-answering is changing with each passing day, 0.324
[0063] What are the channels for applying for credit cards, how to apply for debit cards, 0.814.
[0064] In this embodiment, assuming that the training corpus file has M lines of text pairs, and N numerical features are obtained for each text pair in the training corpus, the sample feature matrix extracted from the training corpus can be expressed as X∈R M×N For each text pair in the training corpus, the annotation similarity score is used as the prediction value of the sample, and the prediction vector y∈R can be extracted from the training corpus. M×1 Therefore, the training data set can be expressed as D = [X, y].
[0065] In this embodiment, the numerical features include: semantic features based on ordered edit distance, semantic features based on unordered edit distance, semantic features based on word meaning distance, and syntactic features based on dependency relationship.
[0066] In this embodiment, in addition to the ordered edit distance, the moving distance of disordered words is also considered, which has stronger adaptability to texts with simple word order reversal and can greatly improve the system recall rate. Moreover, this embodiment also obtains syntactic similarity based on the number of valid dependency pairs in the sentence, which can better measure the number of core words in the sentence and the words with dependency relationships therewith.
[0067] like Figure 2 As shown, in this embodiment, step S10 includes:
[0068] S11, obtaining a training corpus file, wherein the training corpus file includes a plurality of text pairs and a similarity score of each text pair;
[0069] S12, obtaining a training data set according to the training corpus file;
[0070] S13, obtaining a word vector matrix from the training data set;
[0071] In this embodiment, the word vector training method can adopt the common method in the industry. Here, the Word2Vec method is adopted. The specific steps are as follows:
[0072] S131. Generate a new training corpus file originalDataForWord2Vec.txt from the file originalData.txt: obtain only text1 and text2 for each line of samples in the file originalData.txt, and then divide text1 and text2 into two lines for storage.
[0073] The corpus file originalDataForWord2Vec.txt is in the following format:
[0074] I would like to ask where I can buy a ZTE mobile phone
[0075] Where to buy ZTE mobile phones
[0076] ZTE Corporation in Yuhuatai District, Nanjing
[0077] ZTE Corporation in Yuhuatai District, Nanjing
[0078] The intelligent question-answering system team has made new achievements
[0079] The field of intelligent question answering is changing with each passing day
[0080] What are the channels for applying for credit cards?
[0081] How to apply for a debit card
[0082] S132. Use word2vec to train word vectors. The vector length is d. w (For example, d w =400).
[0083] S133, record the trained wordv2ec model as a matrix Where V is the vocabulary consisting of all words in the corpus file, |V| is the number of words in the vocabulary, Indicates |V| row d w A real matrix with columns.
[0084] S134, the word vector obtained by the model can be expressed as in, Indicates that the word vector is 1 row d w A matrix of columns, where w is a variable that can refer to any word, such as "ZTE".
[0085] S14, obtaining a first improved edit distance between text pairs according to the word vector matrix and the edit distance as a semantic feature based on the ordered edit distance;
[0086] In this embodiment, the first improved edit distance c AThe editing operations defined in include: match (Mat), insert (Ins), delete (Del), and replace (Sub), and the corresponding operation costs are c Mat 、c Ins 、c Del 、c Sub The specific calculation steps are as follows:
[0087] S141. After performing word segmentation and stop word removal operations on the texts text1 and text2, word sequences t1 and t2 are obtained.
[0088] For example, text1 is "I want to apply for in-app purchase of ZTE mobile phone", after word segmentation, it is [I|want|apply|in-app purchase|ZTE|mobile phone|], after removing stop words, the word sequence t1 is [apply|in-app purchase|ZTE|mobile phone]; text2 is "How to apply for in-app purchase of ZTE products", after word segmentation, it is [how to|apply|for|ZTE|product|of|in-app purchase|], after removing stop words, the word sequence t2 is [how to|apply|ZTE|product|in-app purchase]. Among them, "I", "want", "have", "a while", "of", "are" are all stop words.
[0089] S142, using a general method (such as a method based on dynamic programming) to calculate the editing path Path from word sequence t1 to word sequence t2 A And the corresponding editing element sequence Elements A .
[0090] For example, the general method can be used to calculate the editing path from t1 = [apply | in-app purchase | ZTE | mobile phone] to t2 = [how to | apply | ZTE | product | in-app purchase] A [Ins,Mat,Sub,Sub,Sub], corresponding to the edit element sequence Elements A It is [How to, Apply, In-App Purchase→ZTE, ZTE→Product, Mobile Phone→In-App Purchase]. Among them, those without arrows represent Mat, Ins, Del operations, and those with arrows represent Sub operations.
[0091] S143, edit path Path A Get the corresponding editing operation cost vector Action A Specifically, all editing operations are replaced with corresponding operation costs to form an editing operation cost vector.
[0092] For example, edit the path A is [Ins,Mat,Sub,Sub,Sub], and the corresponding editing operation cost vector is [c Ins ,c Mat ,c Sub ,c Sub ,cSub ].
[0093] S144. Edit element sequence Elements A Calculate the edit element distance for each element in to obtain the edit element distance vector Dis A Specifically, the edit element distance for Mat, Ins, and Del operations is 1, and the edit element distance for Sub operations is sim cos (w1,w2). Among them, sim cos (w1,w2) is the cosine similarity between word w1 and word w2, which can be expressed as
[0094] For example, to edit the element sequence Elements A For [how, apply, in-app purchase → ZTE, ZTE → product, mobile phone → in-app purchase], the corresponding edit element distance vector Dis A is [1,1,0.218,0.294,0.511].
[0095] S145, according to the edit operation cost vector Action A and the corresponding edit element distance vector Dis A , calculate the improved edit distance between two texts As a semantic feature based on ordered edit distance.
[0096] For example, the edit operation cost vector is [c Ins ,c Mat ,c Sub ,c Sub ,c Sub ], the corresponding edit element distance vector is [1,1,0.218,0.294,0.511], then:
[0097] c A =1*c Ins +1*c Mat +1*c Ins +0.218*c Sub +0.294*c Sub +0.511*c Sub .
[0098] S15, calculating a second improved edit distance between text pairs according to the edit distance and the bag-of-words model as a semantic feature based on unordered edit distance;
[0099] In this embodiment, the second improved edit distance c B The editing operations defined in include: match (Mat), insert (Ins), delete (Del), and the corresponding operation cost is cMat 、c Ins 、c Del The specific calculation steps are as follows:
[0100] S151, word sequences t1 and t2 are obtained after word segmentation and stop word removal operations on texts text1 and text2.
[0101] S152, add all non-repeating words in word sequences t1 and t2 to the set to form a bag of words BOW.
[0102] For example, for t1 = [apply|in-app purchase|ZTE|mobile phone] and t2 = [how to|apply|ZTE|product|in-app purchase], the obtained bag of words BOW is [how to|apply|in-app purchase|ZTE|mobile phone|product].
[0103] S153. Calculate the edit distance from t1 to t2 based on the bag of words BOW and t1 and t2. The calculation method is: for a word w in the bag of words BOW, if the word or its synonym exists in t1, and if the word or its synonym exists in t2, then record the operation Mat; if the word or its synonym exists in t1, and if the word or its synonym does not exist in t2, then record the operation Del; if the word or its synonym does not exist in t1, and if the word or its synonym exists in t2, then record the operation Ins. After performing the above operations on all words in the bag of words BOW in sequence, the edit path Path can be obtained. B , and then get the corresponding editing operation cost vector Action B .
[0104] For example, the editing path from t1 = [Apply | In-app Purchase | ZTE | Mobile Phone] to t2 = [How to | Apply | ZTE | Product | In-app Purchase] B is [Ins,Mat,Mat,Mat,Del,Ins], then the edit operation cost vector Action B For [c Ins ,c Mat ,c Mat ,c Mat ,c Del ,c Ins ].
[0105] S154, edit the cost vector Action B Add up all the elements in to get the second improved edit distance c between the two texts B , as a semantic feature based on unordered edit distance.
[0106] For example, for the edit operation cost vector Action B =[c Ins ,c Mat ,c Mat ,cMat ,c Del ,c Ins ], c B =c Ins +c Mat +c Mat +c Mat +c Del +c Ins .
[0107] S16, calculating the word meaning distance between the text pairs according to the word vector matrix as a semantic feature based on the word meaning distance;
[0108] In this step, first, the texts text1 and text2 are segmented and stop words are removed to obtain word sequences t1 and t2. Assume that the words contained in t1 are The words contained in t2 are Secondly, the calculation word With words Semantic distance Among them, For example, the subscript m represents the total number of t1 word sequences, and the superscript 1 represents that the word belongs to t1, sequence. Similarly, The subscript n represents the total number of word sequences in t2, and the superscript 2 represents that the word belongs to t2, thus defining the word in t1 The semantic distance from t2 is T2 Chinese words The semantic distance from t1 is Finally, calculate the semantic similarity between the two texts As a semantic feature based on word meaning distance.
[0109] S17. Perform dependency syntactic analysis on the text pairs and calculate the syntactic distance between the text pairs as a syntactic feature based on the dependency relationship.
[0110] In this step, first, the word sequences t1 and t2 are obtained after the word segmentation and stop word removal operations on the texts text1 and text2. Secondly, use general methods (such as StanfordNLP, FNLP and other tools) to perform dependency syntactic analysis on t1 and t2 respectively, and calculate the number of valid word collocation pairs in t1 and t2 respectively, denoted as p1 and p2. Among them, the valid collocation pair refers to the collocation pair consisting of the core word in the sentence and the valid words that directly depend on it. The core word is the only core vocabulary in the whole sentence obtained after the sentence is analyzed by dependency syntax; the valid word is the noun, verb and adjective after the sentence is analyzed by dependency syntax.
[0111] For example, for t1 = [apply | in-app purchase | ZTE | mobile phone], after dependency syntactic analysis, the core word is "in-app purchase", and the words that are directly dependent on it are "apply" and "mobile phone", and these two words are both valid words. Therefore, the number of valid collocation pairs of t1 is 2. According to the syntactic structure distance c between the two texts p1 and p2, D =|p1-p2|, as a syntactic feature based on dependency relations.
[0112] like Figure 3 As shown, in this embodiment, step S40 includes:
[0113] S41, obtaining a target text pair, obtaining numerical features of the target text pair, and forming a feature vector of the target text pair;
[0114] S42: Substitute the feature vector of the target text pair into the prediction model to obtain a similarity score of the target text pair.
[0115] In this embodiment, a network structure for training is first built, and then the model is trained according to the sample feature matrix X and the prediction vector y obtained in the previous section, and finally the model is saved for subsequent online acquisition.
[0116] The network structure adopts a multi-layer perceptron (MLP), and uses the sample feature matrix X and the prediction vector y to perform model training on the above network structure using a general method.
[0117] After training, the obtained model parameters are recorded as W 1* , b 1* , W 2* , b 2* , where W 1* represents the connection weight of the first layer in MLP, b 1* is the bias of the second layer in MLP, W 2* is the connection weight of the second layer in MLP, b 2* is the bias of the second layer in MLP, then the prediction model can be expressed as where x T is the feature vector of the sample to be predicted, g 1 is the nonlinear activation function of the first layer in MLP, g 2 is the nonlinear activation function of the second layer in MLP, x T is the feature vector of the target text pair.
[0118] In this embodiment, for the target text pair t1 and t2 input into the system, according to the above numerical feature calculation steps, four numerical features c of the text pair are calculated in sequence. A 、c B 、cC 、c D , which constitutes the feature vector x of the target text pair T =[c A ,c B ,c C ,c D ].
[0119] Substituting the feature vector of the above target text pair into the prediction model, we can get the similarity score of the target text pair t1 and t2:
[0120]
[0121] Embodiment 2
[0122] like Figure 4 As shown, in this embodiment, a device for obtaining text similarity includes:
[0123] A training module 10, used to obtain numerical features of the text pairs according to the text pair data set;
[0124] A matrix construction module 20, used to construct a sample feature matrix through the numerical features of the text pairs;
[0125] A prediction module 30, configured to perform model training according to the sample feature matrix and the prediction vector to obtain a prediction model;
[0126] The online acquisition module 40 is used to acquire a target text pair and acquire a similarity score of the target text pair according to the sample feature matrix and the prediction model.
[0127] In this embodiment, the text similarity is judged by obtaining multiple numerical features of the text pair, taking into account the semantic and syntactic structures. The semantic similarity between the texts, including features such as word meaning, edit distance, and bag-of-words model, is considered. The grammatical similarity including syntactic structure is also considered, and the semantics and syntax are combined. A neural network is used to perform feature extraction at a higher level, which has the advantages of trainable weights, less manual intervention, simple and fast, easy implementation, and high accuracy, thereby improving the user experience.
[0128] In this embodiment, a large amount of annotated text pair data is first prepared as training corpus; each sample in the training corpus is a set of text pairs and corresponding annotated similarity scores, which can be formally expressed as [text1; text2; score], where text1 and text2 are text pairs for similarity calculation, and score is the annotated similarity score. The annotated score can come from manual annotation or other prior information, such as the user's satisfaction with the system's answer in the question-and-answer system, the user's browsing of the system's arrangement information in the retrieval system, etc. All samples are saved in the file originalData.txt, each line is a training sample, and text1, text2, and score in each training sample can be separated by a tab. Among them, the annotated similarity score score is a real number between 0 and 1, and the larger the number, the higher the similarity between the text pairs, and vice versa; in particular, a score of 0 indicates that the text pairs are completely unrelated, and a score of 1 indicates that the text pairs are exactly the same. The accuracy of the score is not fixed depending on the source. For example, if it comes from manual annotation, it may be a single-digit decimal such as 0.3 or 0.6. If it comes from other application systems, it may be a multi-digit decimal such as 0.563 or 0.8192.
[0129] In this embodiment, the format of the file originalData.txt is as follows:
[0130] I would like to ask where I can buy ZTE mobile phones, where can I buy ZTE mobile phones, 0.769
[0131] ZTE Corporation in Nanjing Yuhuatai District, Nanjing Yuhuatai District ZTE Corporation, 0.816
[0132] The intelligent question-answering system team has made new achievements, the field of intelligent question-answering is changing with each passing day, 0.324
[0133] What are the channels for applying for credit cards, how to apply for debit cards, 0.814.
[0134] In this embodiment, assuming that the training corpus file has M lines of text pairs, and N numerical features are obtained for each text pair in the training corpus, the sample feature matrix extracted from the training corpus can be expressed as X∈R M×N For each text pair in the training corpus, the annotation similarity score is used as the prediction value of the sample, and the prediction vector y∈R can be extracted from the training corpus. M×1 Therefore, the training data set can be expressed as D = [X, y].
[0135] In this embodiment, the numerical features include: semantic features based on ordered edit distance, semantic features based on unordered edit distance, semantic features based on word meaning distance, and syntactic features based on dependency relationship.
[0136] In this embodiment, in addition to the ordered edit distance, the moving distance of disordered words is also considered, which has stronger adaptability to texts with simple word order reversal and can greatly improve the system recall rate. Moreover, this embodiment also calculates the syntactic similarity based on the number of valid dependency pairs in the sentence, which can better measure the number of core words in the sentence and the words that have dependency relationships with them.
[0137] like Figure 5 As shown, in this embodiment, the training module includes:
[0138] An acquisition unit 11 is used to acquire a training corpus file, wherein the training corpus file includes a plurality of text pairs and a similarity score of each text pair;
[0139] An extraction unit 12, used to obtain a training data set according to the training corpus file;
[0140] A word vector acquisition unit 13, used to obtain a word vector matrix from the training data set;
[0141] In this embodiment, the word vector training method can adopt the common method in the industry. Here, the Word2Vec method is adopted. The specific steps are as follows:
[0142] S131. Generate a new training corpus file originalDataForWord2Vec.txt from the file originalData.txt: obtain only text1 and text2 for each line of samples in the file originalData.txt, and then divide text1 and text2 into two lines for storage.
[0143] The corpus file originalDataForWord2Vec.txt is in the following format:
[0144] I would like to ask where I can buy a ZTE mobile phone
[0145] Where to buy ZTE mobile phones
[0146] ZTE Corporation in Yuhuatai District, Nanjing
[0147] ZTE Corporation in Nanjing Yuhuatai District
[0148] The intelligent question-answering system team has made new achievements
[0149] The field of intelligent question answering is changing with each passing day
[0150] What are the channels for applying for credit cards?
[0151] How to apply for a debit card
[0152] S132. Use word2vec to train word vectors. The vector length is d. w (For example, d w =400).
[0153] S133, record the trained wordv2ec model as a matrix Where V is the vocabulary consisting of all words in the corpus file, |V| is the number of words in the vocabulary, Indicates |V| row d w A real matrix with columns.
[0154] S134, the word vector obtained by the model can be expressed as in, Indicates that the word vector is 1 row d w A matrix of columns, where w is a variable that can refer to any word, such as "ZTE".
[0155] An ordered edit distance acquisition unit 14, configured to acquire a first improved edit distance between text pairs according to the word vector matrix and the edit distance as a semantic feature based on the ordered edit distance;
[0156] In this embodiment, the first improved edit distance c A The editing operations defined in include: match (Mat), insert (Ins), delete (Del), and replace (Sub), and the corresponding operation costs are c Mat 、c Ins 、c Del 、c Sub The specific calculation steps are as follows:
[0157] S141. After performing word segmentation and stop word removal operations on the texts text1 and text2, word sequences t1 and t2 are obtained.
[0158] For example, text1 is "I want to apply for in-app purchase of ZTE mobile phone", after word segmentation, it is [I|want|apply|in-app purchase|ZTE|mobile phone|], after removing stop words, the word sequence t1 is [apply|in-app purchase|ZTE|mobile phone]; text2 is "How to apply for in-app purchase of ZTE products", after word segmentation, it is [how to|apply|for|ZTE|product|of|in-app purchase|], after removing stop words, the word sequence t2 is [how to|apply|ZTE|product|in-app purchase]. Among them, "I", "want", "have", "a while", "of", "are" are all stop words.
[0159] S142, using a general method (such as a method based on dynamic programming) to calculate the editing path Path from word sequence t1 to word sequence t2 A And the corresponding editing element sequence Elements A .
[0160] For example, the general method can be used to calculate the editing path from t1 = [apply | in-app purchase | ZTE | mobile phone] to t2 = [how to | apply | ZTE | product | in-app purchase] A [Ins,Mat,Sub,Sub,Sub], corresponding to the edit element sequence Elements A It is [How to, Apply, In-App Purchase→ZTE, ZTE→Product, Mobile Phone→In-App Purchase]. Among them, those without arrows represent Mat, Ins, Del operations, and those with arrows represent Sub operations.
[0161] S143, edit the path Path A Get the corresponding editing operation cost vector Action A Specifically, all editing operations are replaced with corresponding operation costs to form an editing operation cost vector.
[0162] For example, edit the path A is [Ins,Mat,Sub,Sub,Sub], and the corresponding editing operation cost vector is [c Ins ,c Mat ,c Sub ,c Sub ,c Sub ].
[0163] S144. Edit element sequence Elements A Calculate the edit element distance for each element in to obtain the edit element distance vector Dis A Specifically, the edit element distance for Mat, Ins, and Del operations is 1, and the edit element distance for Sub operations is sim cos (w1,w2). Among them, sim cos (w1,w2) is the cosine similarity between word w1 and word w2, which can be expressed as
[0164] For example, to edit the element sequence Elements A For [how, apply, in-app purchase → ZTE, ZTE → product, mobile phone → in-app purchase], the corresponding edit element distance vector Dis A is [1,1,0.218,0.294,0.511].
[0165] S145, according to the edit operation cost vector ActionA and the corresponding edit element distance vector Dis A , calculate the improved edit distance between two texts As a semantic feature based on ordered edit distance.
[0166] For example, the edit operation cost vector is [c Ins ,c Mat ,c Sub ,c Sub ,c Sub ], the corresponding edit element distance vector is [1,1,0.218,0.294,0.511], then:
[0167] c A =1*c Ins +1*c Mat +1*c Ins +0.218*c Sub +0.294*c Sub +0.511*c Sub .
[0168] An unordered edit distance acquisition unit 15, configured to acquire a second improved edit distance between text pairs according to the edit distance and the bag-of-words model as a semantic feature based on the unordered edit distance;
[0169] In this embodiment, the second improved edit distance c B The editing operations defined in include: match (Mat), insert (Ins), delete (Del), and the corresponding operation cost is c Mat 、c Ins 、c Del The specific calculation steps are as follows:
[0170] S151. Word sequences t1 and t2 are obtained after word segmentation and stop word removal operations on texts text1 and text2.
[0171] S152, add all non-repeating words in word sequences t1 and t2 to the set to form a bag of words BOW.
[0172] For example, for t1 = [apply|in-app purchase|ZTE|mobile phone] and t2 = [how to|apply|ZTE|product|in-app purchase], the obtained bag of words BOW is [how to|apply|in-app purchase|ZTE|mobile phone|product].
[0173] S153. Calculate the edit distance from t1 to t2 based on the bag of words BOW and t1 and t2. The calculation method is: for a word w in the bag of words BOW, if the word or its synonym exists in t1, and if the word or its synonym exists in t2, then record the operation Mat; if the word or its synonym exists in t1, and if the word or its synonym does not exist in t2, then record the operation Del; if the word or its synonym does not exist in t1, and if the word or its synonym exists in t2, then record the operation Ins. After performing the above operations on all words in the bag of words BOW in sequence, the edit path Path can be obtained. B , and then get the corresponding editing operation cost vector Action B .
[0174] For example, the editing path from t1 = [Apply | In-app Purchase | ZTE | Mobile Phone] to t2 = [How to | Apply | ZTE | Product | In-app Purchase] B is [Ins,Mat,Mat,Mat,Del,Ins], then the edit operation cost vector Action B For [c Ins ,c Mat ,c Mat ,c Mat ,c Del ,c Ins ].
[0175] S154, edit the cost vector Action B Add up all the elements in to get the second improved edit distance c between the two texts B , as a semantic feature based on unordered edit distance.
[0176] For example, for the edit operation cost vector Action B =[c Ins ,c Mat ,c Mat ,c Mat ,c Del ,c Ins ], c B =c Ins +c Mat +c Mat +c Mat +c Del +c Ins .
[0177] A word meaning distance acquisition unit 16, used to acquire the word meaning distance between text pairs according to the word vector matrix as a semantic feature based on the word meaning distance;
[0178] In this embodiment, first, the word sequences t1 and t2 are obtained after the texts text1 and text2 are segmented and stop words are removed. Assume that the words contained in t1 are The words contained in t2 are Secondly, the calculation word With words Semantic distance Among them, For example, the subscript m represents the total number of t1 word sequences, and the superscript 1 represents that the word belongs to t1, sequence. Similarly, The subscript n represents the total number of word sequences in t2, and the superscript 2 represents that the word belongs to t2, thus defining the word in t1 The semantic distance from t2 is T2 Chinese words The semantic distance from t1 is Finally, calculate the semantic similarity between the two texts As a semantic feature based on word meaning distance.
[0179] The syntactic distance acquisition unit 17 is used to perform dependency syntactic analysis on the text pairs and acquire the syntactic distance between the text pairs as a syntactic feature based on the dependency relationship.
[0180] In this embodiment, first, the word sequences t1 and t2 are obtained after the word segmentation and stop word removal operations on the texts text1 and text2. Secondly, a general method (such as StanfordNLP, FNLP and other tools) is used to perform dependency syntactic analysis on t1 and t2 respectively, and the number of valid word collocation pairs in t1 and t2 is calculated respectively, which are recorded as p1 and p2. Among them, a valid collocation pair refers to a collocation pair consisting of a core word in a sentence and a valid word that directly depends on it. The core word is the only core vocabulary in the whole sentence obtained after the sentence is analyzed by dependency syntactic analysis; the valid word is the noun, verb and adjective after the sentence is analyzed by dependency syntactic analysis.
[0181] For example, for t1 = [apply | in-app purchase | ZTE | mobile phone], after dependency syntactic analysis, the core word is "in-app purchase", and the words that are directly dependent on it are "apply" and "mobile phone", and these two words are both valid words. Therefore, the number of valid collocation pairs of t1 is 2. According to the syntactic structure distance c between the two texts p1 and p2, D =|p1-p2|, as a syntactic feature based on dependency relations.
[0182] like Figure 6 As shown, in this embodiment, the online acquisition module includes:
[0183] A feature vector acquisition unit 41 is used to acquire a target text pair, calculate the numerical features of the target text pair, and form a feature vector of the target text pair;
[0184] The similarity acquisition unit 42 is used to substitute the feature vector of the target text pair into the prediction model to obtain the similarity score of the target text pair.
[0185] In this embodiment, a network structure for training is first built, and then the model is trained according to the sample feature matrix X and the prediction vector y obtained in the previous section, and finally the model is saved for subsequent online calculations.
[0186] The network structure adopts a multi-layer perceptron (MLP), and uses the sample feature matrix X and the prediction vector y to perform model training on the above network structure using a general method.
[0187] After training, the obtained model parameters are recorded as W 1* , b 1* , W 2* , b 2* , then the prediction model can be expressed as where x T is the feature vector of the sample to be predicted.
[0188] In this embodiment, for the target text pair t1 and t2 input into the system, according to the above numerical feature calculation steps, four numerical features c of the text pair are calculated in sequence. A 、c B 、c C 、c D , which constitutes the feature vector x of the target text pair T =[c A ,c B ,c C ,c D ].
[0189] Substituting the feature vector of the above target text pair into the prediction model, we can get the target text pair t1 and
[0190] Similarity score of t2:
[0191]
[0192] Embodiment 3
[0193] In this embodiment, an electronic device includes a memory, a processor, and at least one application stored in the memory and configured to be executed by the processor, wherein the application is configured to execute the method for obtaining text similarity described in Embodiment 1.
[0194] Embodiment 4
[0195] An embodiment of the present invention provides a readable storage medium having a computer program stored thereon. When the program is executed by a processor, the program implements any method embodiment described in the above method embodiments for obtaining text similarity.
[0196] It should be noted that the above-mentioned device, equipment and readable storage medium embodiments belong to the same concept as the method embodiments. The specific implementation process is detailed in the method embodiments, and the technical features in the method embodiments are correspondingly applicable in the device embodiments, which will not be repeated here.
[0197] The embodiments of the present invention provide a method, device, equipment and readable storage medium for obtaining text similarity. The method comprises: obtaining numerical features of text pairs according to a text pair data set; constructing a sample feature matrix according to the numerical features of the text pairs; performing model training according to the sample feature matrix and a prediction vector to obtain a prediction model; obtaining a target text pair, and obtaining a similarity score of the target text pair according to the sample feature matrix and the prediction model; and determining text similarity by obtaining multiple numerical features of the text pairs, taking into account semantic and syntactic structures. The method has the advantages of trainable weights, less manual intervention, simplicity and speed, easy implementation, high accuracy, etc., thereby improving user experience.
[0198] Those skilled in the art will appreciate that all or some of the steps in the methods disclosed above, and the functional modules / units in the systems and devices may be implemented as software, firmware, hardware, or a suitable combination thereof.
[0199] In hardware implementation, the division between the functional modules / units mentioned in the above description does not necessarily correspond to the division of physical components; for example, a physical component may have multiple functions, or a function or step may be performed by several physical components in cooperation. Some physical components or all physical components may be implemented as software executed by a processor, such as a central processing unit, a digital signal processor or a microprocessor, or implemented as hardware, or implemented as an integrated circuit, such as an application-specific integrated circuit. Such software may be distributed on a computer-readable medium, which may include a computer storage medium (or non-transitory medium) and a communication medium (or temporary medium). As known to those of ordinary skill in the art, the term computer storage medium includes volatile and non-volatile, removable and non-removable media implemented in any method or technology for storing information (such as computer-readable instructions, data structures, program modules or other data). Computer storage media include, but are not limited to, RAM, ROM, EEPROM, flash memory or other memory technology, CD-ROM, digital versatile disk (DVD) or other optical disk storage, magnetic cassettes, magnetic tapes, disk storage or other magnetic storage devices, or any other medium that can be used to store desired information and can be accessed by a computer. Furthermore, it is well known to those skilled in the art that communication media typically embodies computer readable instructions, data structures, program modules, or other data in a modulated data signal such as a carrier wave or other transport mechanism, and may include any information delivery media.
[0200] The preferred embodiments of the present invention are described above with reference to the accompanying drawings, but the scope of the present invention is not limited thereby. Any modification, equivalent substitution and improvement made by those skilled in the art without departing from the scope and essence of the present invention shall be within the scope of the present invention.
Claims
1. A method for obtaining text similarity, characterized in that: include: Obtaining numerical features of the text pairs according to the text pair data set, the numerical features including: semantic features based on ordered edit distance, semantic features based on unordered edit distance, semantic features based on word meaning distance, and syntactic features based on dependency relationship; Constructing a sample feature matrix through the numerical features of the text pairs; Performing model training according to the sample feature matrix and the prediction vector to obtain a prediction model; Obtaining a target text pair, and obtaining a similarity score of the target text pair according to the sample feature matrix and the prediction model; Among them, the semantic feature based on the ordered edit distance is the first improved edit distance between text pairs obtained according to the word vector matrix and the edit distance of the text pair data set, the semantic feature based on the unordered edit distance is the second improved edit distance between text pairs obtained according to the edit distance and the bag-of-words model, the semantic feature based on the word meaning distance is the word meaning distance between text pairs obtained according to the word vector matrix, and the syntactic feature based on the dependency relationship is the syntactic distance between the text pairs obtained by performing dependency syntactic analysis on the text pairs.
2. A method for obtaining text similarity according to claim 1, characterized in that: The step of obtaining the numerical features of the text pair according to the text pair dataset includes: Obtaining a training corpus file, wherein the training corpus file includes a plurality of groups of text pairs and a similarity score of each group of text pairs; Obtaining a training data set according to the training corpus file; Obtaining a word vector matrix from the training data set; According to the word vector matrix and the edit distance, obtaining a first improved edit distance between the text pairs as a semantic feature based on the ordered edit distance; According to the edit distance and the bag-of-words model, obtaining a second improved edit distance between the text pairs as a semantic feature based on the unordered edit distance; According to the word vector matrix, obtaining the word meaning distance between the text pairs as a semantic feature based on the word meaning distance; Perform dependency syntactic analysis on text pairs and obtain the syntactic distance between text pairs as a syntactic feature based on dependency relations.
3. A method for obtaining text similarity according to claim 2, characterized in that: The step of obtaining a target text pair and obtaining a similarity score of the target text pair according to the sample feature matrix and the prediction model includes: Obtaining a target text pair, obtaining numerical features of the target text pair, and forming a feature vector of the target text pair; Substitute the feature vector of the target text pair into the prediction model to obtain a similarity score of the target text pair.
4. A device for obtaining text similarity, characterized in that: include: A training module, used for obtaining numerical features of the text pairs according to the text pair data set, wherein the numerical features include: semantic features based on ordered edit distance, semantic features based on unordered edit distance, semantic features based on word meaning distance, and syntactic features based on dependency relationship; A matrix construction module, used for constructing a sample feature matrix through the numerical features of the text pairs; A prediction module, used for performing model training according to the sample feature matrix and the prediction vector to obtain a prediction model; An online acquisition module, used to acquire a target text pair, and acquire a similarity score of the target text pair according to the sample feature matrix and the prediction model; Among them, the semantic feature based on the ordered edit distance is the first improved edit distance between text pairs obtained according to the word vector matrix and the edit distance of the text pair data set, the semantic feature based on the unordered edit distance is the second improved edit distance between text pairs obtained according to the edit distance and the bag-of-words model, the semantic feature based on the word meaning distance is the word meaning distance between text pairs obtained according to the word vector matrix, and the syntactic feature based on the dependency relationship is the syntactic distance between the text pairs obtained by performing dependency syntactic analysis on the text pairs.
5. The device for obtaining text similarity according to claim 4, characterized in that: The training module includes: An acquisition unit, used for acquiring a training corpus file, wherein the training corpus file includes a plurality of groups of text pairs and a similarity score of each group of text pairs; An extraction unit, used to obtain a training data set according to the training corpus file; A word vector acquisition unit, used to obtain a word vector matrix from the training data set; An ordered edit distance acquisition unit, used to acquire a first improved edit distance between text pairs according to the word vector matrix and the edit distance as a semantic feature based on the ordered edit distance; An unordered edit distance acquisition unit, used to acquire a second improved edit distance between text pairs according to the edit distance and the bag-of-words model as a semantic feature based on the unordered edit distance; A word meaning distance acquisition unit, used to acquire the word meaning distance between text pairs according to the word vector matrix as a semantic feature based on the word meaning distance; The syntactic distance acquisition unit is used to perform dependency syntactic analysis on the text pairs and acquire the syntactic distance between the text pairs as a syntactic feature based on the dependency relationship.
6. The device for obtaining text similarity according to claim 5, characterized in that: The online acquisition module comprises: A feature vector acquisition unit, used to acquire a target text pair, acquire numerical features of the target text pair, and form a feature vector of the target text pair; The similarity acquisition unit is used to substitute the feature vector of the target text pair into the prediction model to obtain the similarity score of the target text pair.
7. An electronic device comprising a memory, a processor, and at least one application stored in the memory and configured to be executed by the processor, characterized in that: The application is configured to execute the method for obtaining text similarity according to any one of claims 1 to 3.
8. A readable storage medium, characterized in that: A computer program is stored thereon, and when the program is executed by a processor, the method for obtaining text similarity as described in any one of claims 1 to 3 is implemented.
