A Chinese writing phrase recommendation method based on Bert language model
By combining the self-attention mechanism and vectorization representation method on the Bert language model, the problem of high resource consumption and lack of phrase prediction model in the existing technology is solved, efficient Chinese phrase recommendation is achieved, and writing training and language are improved. learning effect.
Patent Information
- Application Number
- CN202210565638.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-05-23
- Publication Date
- 2024-09-13
- Estimated Expiration
- 2042-05-23
AI Technical Summary
Existing language models consume a lot of resources and lack effective phrase prediction models, making it difficult to effectively handle Chinese phrase-level language prediction in downstream tasks.
Using a Bert-based language model, combined with the self-attention mechanism and word embedding method, by constructing a data set for the phrase masked text prediction task, the vectorization representation method is used to fuse contextual semantics and grammatical structure to perform phrase recommendation.
It improves the accuracy of phrase prediction, reduces the consumption of computing resources and time, provides an effective Chinese writing phrase recommendation method, and assists language learning and writing training.
Smart Images

Figure CN114996546B
Abstract
Description
Technical Field
[0001] The present invention relates to the field of natural language processing, and in particular to a phrase recommendation method based on vectorized representation and fusion of contextual semantics and syntactic information based on Bert. Background Art
[0002] In the Internet era, a large amount of text information is generated. For example, a large amount of effective text information is contained in various industries such as the new media industry and the news industry every day. These data are obtained for mining and learned through semi-supervised and supervised learning, thus deriving various language models. Bert, as a bidirectional Transformer language model, learns the overall model through two self-supervised tasks, NSP and MLM. With the progress of China's education and the continuous consolidation of its international status, Chinese has become the basic learning language for many people. Compared with other languages, Chinese not only has a variety of combination patterns, but also has specific meanings in terms of glyphs and pronunciations. In addition, Chinese words and phrases are subdivided into many categories. It is also worth thinking about what words to use for semantic expression in different contexts. Therefore, many beginners find it difficult to learn Chinese at the beginning. Making sentences or writing is a more effective way to help language learners understand semantics and grammar. This can be done with the help of language model technology in natural language understanding to model the entire sentence and train for different tasks. With the development of deep learning technology, text representation models can use distributed representation language models such as word2vec and glove, and use neural networks for information extraction and classification. In recent years, the introduction and widespread use of attention mechanisms have improved the effectiveness of language models. But there are still some shortcomings:
[0003] 1) Language model consumes a lot of resources
[0004] In order to build language models based on large-scale corpora, existing models usually require a large number of parameters, which makes the model parameter volume huge and consumes a lot of computing resources. When adapting to downstream tasks, additional fine-tuning is required, and a large number of parameters need to be optimized, which places high demands on resources and time.
[0005] 2) No effective phrase prediction model
[0006] Currently, most language models focus on classification problems when performing downstream tasks, but there is no method that focuses on phrase-level language prediction methods. Phrases are composed of multiple words and require special processing methods. How to determine the phrase at a position in a language sequence requires consideration from many aspects. Summary of the invention
[0007] The purpose of the present invention is to provide a Chinese writing phrase recommendation method based on the Bert language model in response to the deficiencies in the prior art. The method combines the self-attention mechanism, uses a phrase representation method based on word embedding, combines the semantic context of the context, and pays attention to the information of the grammatical structure to improve the prediction accuracy of the phrase, thereby obtaining a representation that combines semantics and integrates grammatical knowledge for writing phrase recommendation.
[0008] The specific technical solution for achieving the purpose of the present invention is:
[0009] A Chinese writing phrase recommendation method based on the Bert language model, the method comprising the following specific steps:
[0010] Step 1: Obtain modern long and short text corpus from the Internet
[0011] Crawl long and short fiction texts and news texts on the Internet. The crawled texts are based on paragraph and chapter-level corpora. The crawled texts are divided into sentences and basic data cleaning is performed to organize them into neat and standardized sentence-level text data.
[0012] Step 2: Get common phrases and idioms
[0013] At the same time, commonly used Chinese phrases and idioms are collected on the Internet, crawled according to the part-of-speech categories, saved and archived according to the part-of-speech categories, and a common phrase collection is obtained;
[0014] Step 3: Build a dataset for phrase masked text prediction task
[0015] Match the phrase set obtained in step 2 with the sentence-level text data processed in step 1 to extract sentences containing phrases in the phrase set; construct a dataset for the phrase masking text prediction task based on the sentences extracted by matching, and divide it into a training set, a validation set, and a test set in a ratio of 8:1:1;
[0016] Step 4: Vectorize the phrase dictionary and category dictionary
[0017] For the set of phrases filed in step 2, a vectorized dictionary is constructed; each phrase corresponds to a vectorized representation; for each category in the set of phrases filed in step 2, a vectorized dictionary is constructed, each category also corresponds to a vectorized representation, and the two vectorized dictionaries are randomly initialized;
[0018] Step 5: Train a BERT-based masked phrase prediction model
[0019] Build a masked phrase prediction model based on Bert, use the training set built in step 3 to train the model, update the model parameters through the Adam optimizer back propagation, calculate the prediction loss of the model on the validation set in step 3 after each parameter update, repeat the above process until the prediction loss of the model on the validation set no longer decreases, and save the model at this time;
[0020] Step 6: Use the phrase prediction model to recommend phrases to users
[0021] In the model saved in step 5, add the function of accepting user input and formatting the output of the model prediction results, obtain the category probability predicted by the model and the predicted probability of each phrase under the categories with the top k probability values, and sort them according to the phrase prediction probability values, and take the top o phrases as the predicted phrases under this category and recommend them to the user.
[0022] Step 1 describes the sentence segmentation of the crawled text, which specifically includes:
[0023] 2-1: Use the sentence segmentation method based on punctuation marks to divide the paragraph-level text in step 1 into sentences;
[0024] 2-2: For the sentences segmented by punctuation marks in 2-1, if their length exceeds the preset value len, they are segmented to ensure that the length of each sentence does not exceed len;
[0025] 2-3: All sentences processed by 2-2 are archived to obtain neat sentence-level text data.
[0026] The filing according to the part of speech category described in step 2 specifically includes:
[0027] 3-1: Divide phrases into ten categories according to their parts of speech, including pronouns, verbs, adverbs, prepositions, conjunctions, nouns, quantifiers, adjectives, question words and auxiliary words.
[0028] Step 3 constructs a dataset for the phrase masked text prediction task, specifically including:
[0029] 4-1: For the sentence obtained after matching with the phrase set, mask the target phrase contained in the sentence and replace it with "[MASK]";
[0030] 4-2: Perform label conversion on the part-of-speech categories, map the categories to labels represented by numbers, and record the corresponding numbers for the categories corresponding to the target phrases contained in each sentence in 4-1 as the phrase part-of-speech category labels;
[0031] 4-3: Perform label conversion on all phrases in the phrase set in step 2, map the phrases to labels represented by numbers, and form a mapping from phrases to numbers;
[0032] 4-4: For each sentence in 4-1, randomly select m-1 phrase lists that do not contain the phrases originally contained in the sentence from the phrase set, and together with the phrases originally contained in the sentence, form a phrase candidate list of size m, and map the phrases to the phrase-to-digital labels described in 4-3;
[0033] 4-5: Divide the dataset consisting of sentences and labels generated above into training set, validation set and test set in a ratio of 8:1:1.
[0034] The step 4 is specifically as follows:
[0035] 5-1: For the phrase set archived in step 2, build a vectorized dictionary, each phrase corresponds to a vectorized representation, the dimension is d w Dimension, and randomly initialize each representation;
[0036] 5-2: For each category in the phrase set archived in step 2, build a vectorized dictionary, each category also corresponds to a vectorized representation, dimension d c dimensional, and each representation is randomly initialized.
[0037] In step 5, a Bert-based masked phrase prediction model is constructed, which includes: a Bert text encoder, a phrase vectorization dictionary and a category vectorization dictionary constructed in step 3, a context-based information integration unit, and a category classifier and a candidate phrase classifier based on a forward connection network; the specific construction steps are as follows:
[0038] 6-1: The Bert text encoder in the model is composed of a Transformer sequence encoder based on the self-attention mechanism. The encoder includes a Multi-Head Attention module and a Feed-Forward Networks module; Multi-Head Attention module: Let the input be X = (x1, x2, ..., x n ), the output is expressed as MultiHead(X), and the formula is as follows:
[0039]
[0040] MultiHead(X)=Concat(head1,head2,…head h )W o
[0041] is the weight matrix, d model , d x, respectively, the input vector dimension and the subspace dimension; Atterltion, represents the self-Attention mechanism, head i represents the i-th subspace, Concat represents the merging operation; the self-Attention mechanism is formulated as follows:
[0042]
[0043] Among them, d k is the input dimension, is the scale factor, Q, K, and V are the query, key, and value of the Attention mechanism respectively. In the Multi-Head Attention module, the values of Q, K, and V are same;
[0044] Feed-Forward Networks module: The input of this module is the output of the Multi-Head Attention part plus the input original input X, set to x, and the output is FFN(x). The formula is as follows:
[0045] x=MultHead(X)+X
[0046] FFN(x)=max(0,xW1+b1)W2+b2
[0047] The above W i , b i (i=1, 2) represent the weight term and bias term of the i-th layer neural network respectively;
[0048] Based on the above encoder, a hierarchical neural network is constructed. The hierarchical level refers to the word level and sentence level. The construction steps are as follows:
[0049] Suppose the length of the sequence of words in the i-th sentence is n, and X i =[x i1 , x i2 , …, x in ] means that X i As the input of the above Encoder, we get X′ i =[x i1 ′,x i2 ′,…,x in ′];
[0050] Use soft Attention to reduce the dimension of X′ and get the sentence vector s i , the formula is as follows:
[0051] u ij =tanh(W w xij ′+b w )
[0052]
[0053] Among them, W w , b w为 The weight matrix and offset term, tanh is the activation function, u ij To input x ij ′After the output of a layer of neural network, u w is the weight vector, α ij For x ij ′ weight, x ij ′ is X i The jth vector in ′, u ij T for u ij Transpose;
[0054] 6-2: The Bert model adds "[CLS]" and "[SEP]" characters at the beginning and end of the sentence respectively. At this time, the input representation sequence {x CLS , x0, x1, ..., x T , x SEP After the above Bert model, the hidden state sequence {h CLS ,h0,h1,...,h n-1 ,h SEP}, the hidden state sequence corresponding to the position of “[MASK]” is h m ;
[0055] 6-3: The information integration unit is a forward propagation network. For the hidden state sequence at the "[MASK]" position, the input h of the information integration unit is constructed. i , after inputting the information integration unit, the corresponding output h is obtained o :
[0056]
[0057] FFN aggr (h i )=max(0,h i W1+b1)W2+b2
[0058] h o =FFN aggr (h i )
[0059] The above W i , b i (i=1, 2) represents the information integration unit FFN aggr The weight and bias terms in ;
[0060] 6-4: Output h of the category information integration unit o , input it into the category classifier to obtain the vectorized representation corresponding to the category with the largest category probability:
[0061] FFN cls (h o )=max(0,h o W1+b1)W2+b2
[0062] E c =argmax(softmax(FFN cls (h o )))
[0063] The above W i , b i (i=1, 2) represents the category classifier FFN cls The weight and bias terms in ;
[0064] 6-5: Based on the category representation obtained by the maximum judgment probability obtained in 6-4, it is concatenated with the output of the original information integration unit, input into the phrase classifier, and dot product is performed with the vectorized representation of each phrase in the candidate set G, and the probability value of each candidate input phrase is probabilistically calculated:
[0065]
[0066] FFN w (h c )=max(0,h v W1+b1)W2+b2
[0067]
[0068] Among them, W i , b i (i=1, 2) represents the phrase classifier FFN w The weight term and offset term in , v represents a phrase in the candidate set G.
[0069] In step 6, the function of accepting user input and formatting the output model prediction results is added. The specific implementation method is as follows:
[0070] 7-1: The model converts the “*” in the input sentence into “[MASK]” and inputs it into the phrase prediction model;
[0071] 7-2: The model calculates and processes the input sentence, predicts the category and probability of the corresponding position in turn, sorts the probability values of the part-of-speech categories of the vacant position, selects the top k categories for guided phrase prediction, predicts the phrases under the corresponding category distribution, and sorts them by the phrase prediction probability value, and takes the top o phrases as the recommended phrases under this category;
[0072] 7-3: The model sorts the category probabilities in 7-2 and the corresponding phrases under the category according to the probability values, and outputs them to the user as the model's phrase recommendation results for the missing positions in the sentence input.
[0073] While introducing a Bert-based language model, the present invention embeds phrases and categories through a vectorization method to represent them. For an input sentence, its context is integrated and the grammatical structure is considered, so as to predict suitable phrase candidates for the position. Meanwhile, the computing time and computing resources of the model are considerable. While recommending appropriate phrases for users' writing, it can also assist scholars in writing training and language learning. BRIEF DESCRIPTION OF THE DRAWINGS
[0074] Figure 1 It is a structural schematic diagram of a phrase prediction model based on the Bert language model of the present invention;
[0075] Figure 2 It is a flow chart of the present invention. DETAILED DESCRIPTION
[0076] The present invention uses text corpus obtained from the Internet to construct a data set. The obtained segment-level text is segmented by commas or other punctuation marks based on punctuation marks to obtain sentence-level text. At the same time, it is matched with a set of regular phrases obtained from the Internet to construct a data set with phrase category labels, phrase labels, candidate phrases, etc. The model predicts phrases for vacant positions by combining the features of contextual semantics and sentence syntactic information, calculates classification losses based on the labels constructed from the data set, and performs gradient backpropagation, thereby optimizing the parameters of the entire model.
[0077] Example 1
[0078] See also Figure 2 , follow the steps below to train the Bert-based Chinese phrase prediction model:
[0079] S1: Collect text corpus from target websites and platforms, perform text preprocessing on the collected text, and divide it into training set, validation set and test set; among them, text preprocessing includes data cleaning and long sentence segmentation, and the sentence length len is selected as 256. At the same time, common phrases under different categories are collected and archived according to categories. The preprocessed text is matched with the phrase set to obtain matching sentence-level data, for example:
[0080] Original text:
[0081] Having said that, in order to be recognized by fans and coaches, Gijs still needs to rely on game practice to earn space to stand out. In the last round of the league, Kx was injured, and Wen temporarily used Joe to fill in, and not letting Gijs on the bench play can prove some problems. But in today's game, Gijs seized the opportunity and played well throughout the game. In the first half, he didn't know the strength of the opponent, so he played a little conservatively and was content with defense. In the second half, Gijs was a real little genius with both strong defense and assists. In the second half, Gijs contributed two shots in a row. One volley was saved by the goalkeeper, and one header was slightly higher at the back point, which reflected his good offensive desire and quality, but the most important thing was his assists. In the 50th minute, his beautiful long pass from the backcourt just found Bei, and the Mexican stopped the ball and passed the opponent, and then scored a wonderful lob shot. Gijs's good overall view and accuracy of the backcourt kick were undoubtedly shown in this shot...
[0082] The sentence text obtained after preprocessing and phrase matching:
[0083] Having said that, in order to be recognized by fans and coaches, Gijs still needs to rely on game practice to earn space to stand out. In the last round of the league, Khedira was injured, and Wenger temporarily used Joe to replace him, and did not let Gijs on the bench play. This can prove some problems. But in today's game, Gijs seized the opportunity and played well throughout the game. In the first half, he did not know the strength of the opponent, so he played a little conservatively and was content with defense. In the second half, Gijs was a real little genius who was good at both defense and assists. Phrase_label="know"
[0084] S2: Construct a mapping from labels to numbers, that is, each phrase corresponds to a number, and each category corresponds to a number, which is convenient for model input. For each sentence of data, m = 7 candidate phrase sets containing real phrases are randomly enumerated without duplication. Then the data in S1 can further obtain a training data as follows:
[0085] Having said that, in order to be recognized by fans and coaches, Gijs still needs to rely on game practice to earn space to stand out. In the last round of the league, when Xrich was injured, Wenger temporarily used Joe to replace him, and did not let Gijs on the bench play. This can prove some problems. But in today's game, Gijs seized the opportunity and played well throughout the game. In the first half, he did not "[MASK]" the opponent's strength, so he played a little conservatively and was content with defense. In the second half, Gijs was a real little genius who was good at both defense and assists. Phrase_label=645, class_label=3, candidate=[231,442,645,84,665,433,235]
[0086] S3: Build and initialize the phrase prediction model. First, build two vectorized dictionaries, one is the phrase vectorized dictionary and the other is the type vectorized dictionary. Set their dimensions d w =d c =768, the dimensions of the two dictionaries are [total number of phrases, 768] and [total number of categories, 768], and they are randomly initialized.
[0087] S4: Build a Bert-based phrase prediction model to predict the phrase type of the input sentence;
[0088] The model mainly includes Bert text encoder, vectorized representation dictionary for phrase representation through vectorization, vectorized representation dictionary for category representation through vectorization, context-based information integration unit, and category classifier and phrase classifier based on forward connection network, as shown below: Figure 1 shown.
[0089] The Bert encoder in the model is mainly composed of a Transformer sequence encoder based on the self-attention mechanism. The encoder includes a Multi-Head Attention module and a Feed-Forward Networks module. The Multi-Head Attention module: Let the input be X = (x1, x2, ..., x n ), the output is expressed as MultiHead(X), and the formula is as follows:
[0090]
[0091] MultiHead(X)=Conca(head1, head2,…head h )W o
[0092] is the weight matrix, d model , dX , respectively, the input vector dimension and the subspace dimension; Attention, represents the self-Attention mechanism, head i represents the i-th subspace, Concat represents the merging operation; the self-Attention mechanism is formulated as follows:
[0093]
[0094] Among them, d k is the input dimension, is the scale factor, Q, K, and V are the query, key, and value of the Attention mechanism respectively. In the Multi-Head Attention module, the values of Q, K, and V are same;
[0095] Feed-Forward Networks module: The input of this module is the output of the Multi-Head Attention part plus the input original input X, set to x, and the output is FFN(x). The formula is as follows:
[0096] x=MultiHead(X)+X
[0097] FFN(x)=max(0,xW1+b1)W2+b2
[0098] The above W i , b i (i=1, 2) represent the weight term and bias term of the i-th layer neural network respectively;
[0099] Based on the above encoder, a hierarchical neural network is constructed. The hierarchical level refers to the word level and sentence level. The construction steps are as follows:
[0100] Suppose the length of the sequence of words in the i-th sentence is n, and use X i =[x i1 , x i2 , …, x in ] means that X i As the input of the above Encoder, we get X i ′=[x i1 ′,x i2 ′,…,x in ′];
[0101] Use soft Attention to reduce the dimension of X′ and get the sentence vector s i , the formula is as follows:
[0102] u ij =tanh(Ww x ij ′+b w )
[0103]
[0104] Among them, W w , b w is the weight matrix and offset term, tanh is the activation function, u ij To input x ij ′After the output of a layer of neural network, u w is the weight vector, α ij For x ij ′ weight, x ij ′ is X i The jth vector in ′, u ij T for u ij Transpose;
[0105] Since the Bert model input needs to be added with "[CLS]" and "[SEP]" at the beginning and end of the sentence respectively, the input representation sequence {x CLS , x0, x1, ..., x T , x SEP After the above Bert model, the hidden state sequence {h CLS ,h0,h1,...,h n-1 ,h SEP}, the hidden state sequence corresponding to the position of “[MASK]” is h m ;
[0106] For the hidden state sequence at the "[MASK]" position, construct the input of the information integration unit The information integration unit is a forward propagation network, which obtains the corresponding output h0;
[0107] The output h of the category information integration unit o , input it into the category classifier to obtain the vectorized representation corresponding to the category with the largest category probability:
[0108] FFN(h o )=max(0,h o W1+b1)W2+b2
[0109] p = softma(FFN(h o ))
[0110] For the input in S2, we can get the model's calculated probabilities for all categories for the phrase position: [0.0639, 0.0789, 0.0629, 0.4023, 0.0631, 0.0681, 0.0636, 0.0713, 0.0631, 0.0628] S5: Based on the probability calculated in S4, select the category vector with the largest probability in the vectorized category dictionary
[0111] E c =argmax(softmax((FFN(h o ))
[0112] It is concatenated with the output of the original information integration unit, input into the phrase classifier, dot-producted with the vectorized representations of the seven phrases in the candidate set G, and the probability value of each candidate input phrase is probabilistically calculated:
[0113]
[0114] FFN(h c )=max(0,h v W1+b1)W2+b2
[0115]
[0116] Based on the data in S2, we can calculate the probability value of each phrase based on category 3. The phrase with the highest score argmaxp(v) is the phrase predicted by the model. The cross entropy loss function is used to calculate the loss of the result.
[0117] in y When i (i = 1, 2, ..., c) is equal to 1, it means that the phrase belongs to the i-th category. Predict the probability that a phrase belongs to the i-th class for the classifier.
[0118] The above is only an implementation example of the present invention, and the protection scope of the present invention is not limited to the above example. All technical solutions under the concept of the present invention belong to the protection scope of the present invention. It should be pointed out that for ordinary technicians in this technical field, some improvements without departing from the principle of the present invention should be regarded as the protection scope of the present invention.
Claims
1. A Chinese writing phrase recommendation method based on the Bert language model, characterized in that: The method comprises the following specific steps: Step 1: Obtain modern long and short text corpus from the Internet Crawl long and short fiction texts and news texts on the Internet, segment the crawled texts into sentences, and perform basic data cleaning to organize them into neat and standardized sentence-level text data; Step 2: Get common phrases and idioms At the same time, commonly used Chinese phrases and idioms are collected on the Internet, crawled according to the part-of-speech categories, saved and archived according to the part-of-speech categories, and a common phrase collection is obtained; Step 3: Build a dataset for phrase masked text prediction task Match the phrase set obtained in step 2 with the sentence-level text data processed in step 1 to extract sentences containing phrases in the phrase set; construct a dataset for the phrase masking text prediction task based on the sentences extracted by matching, and divide it into a training set, a validation set, and a test set in a ratio of 8:1:1; Step 4: Vectorize the phrase dictionary and category dictionary For the phrase set in step 2, a vectorized dictionary is constructed; each phrase corresponds to a vectorized representation; for each category in the phrase set in step 2, a vectorized dictionary is constructed, each category corresponds to a vectorized representation at the same time, and the two vectorized dictionaries are randomly initialized; Step 5: Train a BERT-based masked phrase prediction model Build a masked phrase prediction model based on Bert, use the training set built in step 3 to train the model, update the model parameters through the Adam optimizer back propagation, calculate the prediction loss of the model on the validation set in step 3 after each parameter update, repeat the above process until the prediction loss of the model on the validation set no longer decreases, and save the model at this time; Step 6: Use the phrase prediction model to recommend phrases to users In the model saved in step 5, add the function of accepting user input and formatting the output of the model prediction results, obtain the category probability predicted by the model and the prediction probability of each phrase under the categories with the top k probability values, sort them according to the phrase prediction probability values, and take the top o phrases as the predicted phrases under the category and recommend them to the user; where: In step 5, a Bert-based masked phrase prediction model is constructed, which includes: a Bert text encoder, a phrase vectorization dictionary and a category vectorization dictionary constructed in step 3, a context-based information integration unit, and a category classifier and a candidate phrase classifier based on a forward connection network; the specific construction steps are as follows: 6-1: The Bert text encoder in the model is composed of a Transformer sequence encoder based on the self-attention mechanism. The encoder includes a Multi-Head Attention module and a Feed-Forward Networks module; Multi-Head Attention module: Let the input be X = (x1, x2, ..., x n ), the output is expressed as MultiHead(X), and the formula is as follows: MultiHead(X)=Concat(head1,head2,…,head h )W o is the weight matrix, d ,o#-. ,d X , respectively, the input vector dimension and the subspace dimension; Attention, represents the self-Attention mechanism, head i represents the i-th subspace, and Concat represents the merging operation; The self-Attention mechanism is formulated as follows: Among them, d8 is the input dimension, is the scale factor, Q, K, and V are the query, key, and value of the Attention mechanism respectively. In the Multi-Head Attention module, the values of Q, K, and V are same; Feed-Forward Networks module: The input of this module is the output of the Multi-Head Attention part plus the original input X, set to x, and the output is FFN(x). The formula is as follows: x=NultiHead(X)+X FFN(x)=max(0,xW1+b1)W2+b2 The above W i ,b i (i=1,2) represent the weight term and bias term of the i-th layer neural network respectively; Based on the above encoder, a hierarchical neural network is constructed. The hierarchical level refers to the word level and sentence level. The construction steps are as follows: Suppose the length of the sequence of words in the i-th sentence is n, and use X i =[x i1 ,x i2 ,…,x in ] means that X is used as the input of the above Encoder to obtain X i ′=[x i1 ′,x i2 ′,…,x in ′]; Use soft Attention to reduce the dimension of X′ and get the sentence vector s i , the formula is as follows: u ij =tanh(W w x ij ′+b w ) Among them, W K , b K is the weight matrix and offset term, tanh is the activation function, u ij To input x ij ′After the output of a layer of neural network, u w is the weight vector, α ij For x ij ′ weight, x ij ′ is X i The jth vector in ′, u ij T for u ij Transpose; 6-2: The Bert model adds "[CLS]" and "[SEP]" characters at the beginning and end of the sentence respectively. At this time, the input representation sequence {x CLS ,x0,x1,…,x T ,x SEP After the above Bert model, the hidden state sequence {h CLS ,h0,h1,…,h n-1 ,h SEP }, the hidden state sequence corresponding to the "[MASK]" position is h m ; 6-3: The information integration unit is a forward propagation network. For the hidden state sequence at the "[MASK]" position, the input h of the information integration unit is constructed. i , after inputting the information integration unit, the corresponding output h is obtained o : FFN aggr (h i )=max(0,h i W1+b1)W2+b2 h i =FFN aggr (h i ) The above W i ,b i (i=1,2) represents the information integration unit FFN aggr The weight and bias terms in ; 6-4: Output h of the category information integration unit o , input it into the category classifier to obtain the vectorized representation corresponding to the category with the largest category probability: FFN cls (h o )=max(0,h o W1+b1)W2+b2 E c =argmax(softmax(FFN cls (h o ))) The above W i ,b i (i=1,2) represents the category classifier FFN cls The weight and bias terms in ; 6-5: Based on the category representation obtained by the maximum judgment probability obtained in 6-4, it is concatenated with the output of the original information integration unit, input into the phrase classifier, and dot product is performed with the vectorized representation of each phrase in the candidate set G, and the probability value of each candidate input phrase is probabilistically calculated: FFN w (h c )=max(0,h v W1+b1)W2+b2 Among them, W i ,b i (i=1,2) represents the phrase classifier FFN w The weight term and offset term in , v represents a phrase in the candidate set G; In step 6, the function of accepting user input and formatting the output model prediction results is added. The specific implementation method is as follows: 7-1: The model converts the "*" in the input sentence into "[MASK]" and inputs it into the phrase prediction model; 7-2: The model calculates and processes the input sentence, predicts the category and probability of the corresponding position in turn, sorts the probability values of the part-of-speech categories of the vacant position, selects the top k categories for guided phrase prediction, predicts the phrases under the corresponding category distribution, and sorts them by the phrase prediction probability value, and takes the top o phrases as the recommended phrases under this category; 7-3: The model sorts the category probabilities in 7-2 and the corresponding phrases under the category according to the probability values, and outputs them to the user as the model's phrase recommendation results for the missing positions in the sentence input.
2. The Chinese writing phrase recommendation method based on the Bert language model according to claim 1, characterized in that: Step 1 describes the sentence segmentation of the crawled text, which specifically includes: 2-1: Use the sentence segmentation method based on punctuation marks to divide the paragraph-level text in step 1 into sentences; 2-2: For the sentences segmented by punctuation marks in 2-1, if their length exceeds the preset value len, they are segmented to ensure that the length of each sentence does not exceed len; 2-3: All sentences processed by 2-2 are archived to obtain neat sentence-level text data.
3. The Chinese writing phrase recommendation method based on the Bert language model according to claim 1, characterized in that: The filing according to the part of speech category described in step 2 specifically includes: 3-1: Divide phrases into ten categories according to their parts of speech, including pronouns, verbs, adverbs, prepositions, conjunctions, nouns, quantifiers, adjectives, question words and auxiliary words.
4. The Chinese writing phrase recommendation method based on the Bert language model according to claim 1, characterized in that: Step 3 constructs a dataset for the phrase masked text prediction task, specifically including: 4-1: For the sentence obtained after matching with the phrase set, mask the target phrase contained in the sentence and replace it with "[MASK]"; 4-2: Perform label conversion on the part-of-speech categories, map the categories to labels represented by numbers, and record the corresponding numbers for the categories corresponding to the target phrases contained in each sentence in 4-1 as the phrase part-of-speech category labels; 4-3: Perform label conversion on all phrases in the phrase set in step 2, map the phrases to labels represented by numbers, and form a mapping from phrases to numbers; 4-4: For each sentence in 4-1, randomly select m-1 phrase lists that do not contain the phrases originally contained in the sentence from the phrase set, and together with the phrases originally contained in the sentence, form a phrase candidate list of size m, and map the phrases to the phrase-to-digital labels described in 4-3; 4-5: Divide the dataset consisting of sentences and labels generated above into training set, validation set and test set in a ratio of 8:1:
1.
5. The Chinese writing phrase recommendation method based on the Bert language model according to claim 1, characterized in that: The step 4 is specifically as follows: 5-1: For the phrase set archived in step 2, build a vectorized dictionary, each phrase corresponds to a vectorized representation, the dimension is d K Dimension, and randomly initialize each representation; 5-2: For each category in the phrase set archived in step 2, build a vectorized dictionary, each category also corresponds to a vectorized representation, dimension d h dimensional, and each representation is randomly initialized.
