Rhythm-controlled poetry generation method, device, equipment and storage medium
By obtaining the embedding vectors of the characters, sentences and position information of the source character sequence, and combining them with the rhythmic embedding vectors, the encoder and decoder models are used to generate poems, which solves the shortcomings of the neural network model in rhythmic control and achieves higher rhythmic accuracy.
Patent Information
- Application Number
- CN201911039063.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2019-10-29
- Publication Date
- 2025-09-19
- Estimated Expiration
- 2039-10-29
AI Technical Summary
Existing neural network models have difficulty accurately controlling rhythm when generating poetry, resulting in large deviations between the generated verses and the rhythmic rules of traditional poetry.
By obtaining the character information, sentence information, position information and rhythmic embedding vector of the source character sequence and superimposing them, a comprehensive embedding vector is generated, which is then processed using the encoder and decoder models, and combined with the rhythmic control information to generate verses and improve rhythmic accuracy.
The generated verses are more accurate in terms of level and tone rhyme, conform to the rhythmic rules of traditional poetry, and improve the rhythmic control effect of the verses.
Smart Images

Figure CN111046648B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of natural language processing technology, and in particular to a method, device, equipment and storage medium for generating poetry with rhythmic control. Background Art
[0002] Ancient Chinese poets emphasized the importance of rhyme and rhythm in their poetry, leveraging the balanced alternation of Chinese tones to create a rhythmic flow that is catchy and memorable. Currently, research on poetry generation, both domestically and internationally, has evolved from template-based generation methods to statistical machine translation and finally to neural network-based approaches.
[0003] With the rise of neural networks, the encoder-decoder based framework has gradually been applied to poetry generation. This framework uses the powerful fitting ability of neural networks to generate poems with a certain degree of fluency, but its rhythmic expression deviates greatly from the traditional poetry rhythm rules. Summary of the Invention
[0004] The main purpose of this application is to provide a rhythm-controlled poetry generation method, device, equipment and storage medium, aiming to improve the accuracy of the level and rhyme of the generated poems.
[0005] In a first aspect, the present application provides a method for generating poetry with rhythmic control, the method comprising the following steps:
[0006] Obtaining a source character sequence, and superimposing the embedding vectors of character information, sentence information, and position information of the source character sequence with the prosody embedding vector of the source character sequence to obtain a comprehensive embedding vector;
[0007] Encoding the comprehensive embedding vector to obtain an input text representation vector;
[0008] Obtaining an initial embedding vector and prosody control information, and superimposing the initial embedding vector and the embedding vector of the prosody control information to obtain an initialized output text representation vector;
[0009] The output text representation vector and the input text representation vector are decoded to obtain a generated sentence corresponding to the source character sequence.
[0010] In a second aspect, the present application further provides a method for training a poetry generation model, the method comprising:
[0011] Obtaining a training sample, the training sample including a sample character sequence and a subsequent line of poetry following the sample character sequence, wherein the rhythm of the subsequent line of poetry matches that of the sample character sequence;
[0012] Superimposing the embedding vectors of the character information, sentence information, and position information of the sample character sequence and the prosody embedding vector of the sample character sequence to obtain a comprehensive embedding vector;
[0013] Encoding the comprehensive embedding vector to obtain an input text representation vector;
[0014] Embedding the rhythm information of the second sentence of the poem to obtain a rhythm expectation vector;
[0015] Obtaining an initial embedding vector, and superimposing the initial embedding vector and the prosody expectation vector to obtain an initialized output text representation vector;
[0016] Decoding the output text representation vector and the input text representation vector to obtain a generated sentence corresponding to the sample character sequence;
[0017] A loss value is calculated based on the rhythmic information of the generated sentence and the rhythmic information of the subsequent sentence of poetry, and the parameters of the poetry generation model are adjusted based on the loss value.
[0018] In a third aspect, the present application further provides a rhythm-controlled poetry generation device, the rhythm generation device comprising:
[0019] The vector superposition module is used to obtain a source character sequence, and superimpose the embedding vectors of the character information, sentence information, and position information of the source character sequence and the rhythm embedding vector of the source character sequence to obtain a comprehensive embedding vector.
[0020] The encoding module is used to encode the comprehensive embedding vector to obtain an input text representation vector.
[0021] The vector superposition module is used to obtain an initial embedding vector and prosody control information, and superimpose the initial embedding vector and the embedding vector of the prosody control information to obtain an initialized output text representation vector.
[0022] A decoding module is used to decode the output text representation vector and the input text representation vector to obtain a generated sentence corresponding to the source character sequence.
[0023] In a fourth aspect, the present application also provides a computer device comprising a processor, a memory, and a computer program stored in the memory and executable by the processor, wherein when the computer program is executed by the processor, the steps of the above-mentioned rhythmically controlled poetry generation method are implemented.
[0024] In a fifth aspect, the present application also provides a computer-readable storage medium, on which a computer program is stored, wherein when the computer program is executed by a processor, the steps of the above-mentioned rhythm-controlled poetry generation method are implemented.
[0025] The present application provides a rhythmically controlled poetry generation method, apparatus, device and computer-readable storage medium. The present application obtains a comprehensive embedding vector by superimposing the embedding vectors of the character information, sentence information and position information of a source character sequence and the rhythmic embedding vector of the source character sequence, and encodes the comprehensive embedding vector to obtain an input text representation vector; then obtains an initial embedding vector and rhythmic control information, superimposes the initial embedding vector and the embedding vector of the rhythmic control information to obtain an initialized output text representation vector; then decodes the output text representation vector containing the rhythmic control information and the input text representation vector containing the rhythmic information of the source character sequence to obtain a generated sentence corresponding to the source character sequence, and the rhythmic accuracy of the generated sentence is higher. BRIEF DESCRIPTION OF THE DRAWINGS
[0026] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the following is a brief introduction to the drawings required for use in the description of the embodiments. Obviously, the drawings described below are some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.
[0027] Figure 1 A flowchart of a method for generating poetry with rhythmic control provided in an embodiment of the present application;
[0028] Figure 2 A schematic diagram of a source character sequence embedding process for a prosody-controlled poetry generation method provided in an embodiment of the present application;
[0029] Figure 3 A schematic diagram of a source character sequence rhythm embedding process for a rhythm-controlled poetry generation method provided in an embodiment of the present application;
[0030] Figure 4 A schematic block diagram of a method for training a poetry generation model provided in an embodiment of the present application;
[0031] Figure 5 A schematic block diagram of a rhythm-controlled poetry generation device provided in an embodiment of the present application;
[0032] Figure 6 A schematic block diagram of a training device for a poetry generation model provided in an embodiment of the present application;
[0033] Figure 7This is a schematic block diagram of the structure of a computer device involved in one embodiment of the present application.
[0034] The realization of the objectives, functional features and advantages of this application will be further explained in conjunction with embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION
[0035] The following will be combined with the drawings in the embodiments of this application to clearly and completely describe the technical solutions in the embodiments of this application. Obviously, the embodiments described are part of the embodiments of this application, not all of them. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of this application.
[0036] The flowcharts shown in the accompanying drawings are for illustrative purposes only and do not necessarily include all contents and operations / steps, nor must they be executed in the order described. For example, some operations / steps may be decomposed, combined, or partially merged, so the actual execution order may vary depending on the actual situation.
[0037] The present invention provides a method, apparatus, computer device, and computer-readable storage medium for generating poetry with rhythmic control. The method can be applied to a terminal device, such as a mobile phone, tablet computer, laptop computer, desktop computer, personal digital assistant, or wearable device.
[0038] The following describes some embodiments of the present application in detail with reference to the accompanying drawings. In the absence of conflict, the following embodiments and features therein may be combined with each other.
[0039] Please refer to Figure 1 , Figure 1 A flowchart of a rhythm-controlled poetry generation method provided in an embodiment of the present application.
[0040] like Figure 1 As shown, the rhythm-controlled poetry generation method includes steps S101 to S104.
[0041] Step S101: Obtain a source character sequence, and superimpose the embedding vectors of the character information, sentence information, and position information of the source character sequence and the prosody embedding vector of the source character sequence to obtain a comprehensive embedding vector.
[0042] Exemplarily, the source character sequence may include keywords used for writing poetry, such as spring and peach blossom; or the source character sequence may include the first sentence of a poem, or the first and second sentences of a poem.
[0043] For example, Figure 2 As shown, the source character sequence includes "sunlight incense burner produces purple smoke". Figure 2 As shown, the character information of the source character sequence is embedded to obtain the embedding vector of the character information of the source character sequence, that is, the character embedding vector Token Embeddings; specifically, each word in the source character sequence is converted into a vector of fixed dimension, for example, each word is converted into a 768-dimensional vector representation.
[0044] like Figure 2 As shown, the sentence information of the source character sequence is embedded to obtain an embedding vector for the sentence information of the source character sequence, namely a sentence embedding vector Segment Embeddings. Specifically, the embedding vector of the sentence information is related to the number of sentences in the source character sequence. For example, if the source character sequence includes a sentence of poetry, the elements corresponding to each character in the source character sequence in the sentence embedding vector are the same; if the source character sequence includes different sentences, the elements corresponding to each character in the different sentences in the sentence embedding vector are different.
[0045] like Figure 2 As shown, the position information of the source character sequence is embedded to obtain the embedding vector of the position information of the source character sequence, that is, the position embedding vector Position Embeddings.
[0046] The position embedding vector is learned.
[0047] In one embodiment, a poetry generation model using a prosody-controlled poetry generation method includes an encoder and a decoder. For example, a 12-layer BERT is used as the encoder.
[0048] The BERT model can process input sequences of up to 512 tokens. The BERT model learns a vector representation at each position to encode information about the sequence order. In fact, the Position Embeddings layer is a lookup table of size (512,768). The first row of the table represents the first position of the first sequence, the second row represents the second position of the sequence, and so on.
[0049] For example, the Token Embeddings of character information is the vector representation of each character in the source character sequence, the Segment Embeddings of sentence information can assist the BERT model in distinguishing the vector representations of different sentences, and the Position Embeddings of position information can enable the BERT model to learn the sequential properties of the input.
[0050] Exemplarily, the embedding vectors of character information, sentence information, and position information are all (1, n, 768) vectors, where n is determined according to the number of characters in the source character sequence.
[0051] In some implementations, a start symbol [CLS] is added at the beginning of the source character sequence, a separator [SEP] is added between different sentences of the source character sequence, and a separator [SEP] is added at the end of the source character sequence. Figure 2 As shown, the prosodic information of the source character sequence is embedded to obtain the prosodic embedding vector of the source character sequence.
[0052] In some implementations, the prosodic embedding vector of the source character sequence includes a level-tone embedding vector and / or a rhyme embedding vector.
[0053] For example, Figure 3 As shown in Figure 1, the tone information of the source character sequence is embedded to obtain a tone embedding vector. For example, in the tone embedding vector, "1" represents a tone, "0" represents a tone, and "-1" represents a character without tone information, such as the start and end characters.
[0054] For example, Figure 3 As shown, the rhyme information of the source character sequence is embedded to obtain a rhyme embedding vector. For example, in the rhyme embedding vector, the last character of the poem is represented by 1-13, representing the thirteen rhyme information of the poem, and the other characters are not considered for rhyme and are replaced by "0".
[0055] Exemplarily, superimposing the embedding vectors of the character information, sentence information, and position information of the source character sequence and the rhythm embedding vector of the source character sequence includes: superimposing the embedding vectors of the character information, sentence information, and position information of the source character sequence and the level and tone embedding vector and / or rhyme embedding vector.
[0056] For example, the pingze embedding vector and the rhyme embedding vector are also vectors of (1, n, 768), where n is determined according to the number of characters in the source character sequence. Then the comprehensive embedding vector is also a vector of (1, n, 768).
[0057] Step S102: Encode the comprehensive embedding vector to obtain an input text representation vector.
[0058] Since the comprehensive embedding vector is integrated with the prosodic embedding vector, the input text representation vector obtained by encoding also contains the prosodic information of the source character sequence, such as level and rhyme information.
[0059] In some implementations, the comprehensive embedding vector is encoded based on an encoder to obtain an input text representation vector.
[0060] Exemplarily, the encoder includes a BERT (Bidirectional Encoder Representations from Transformers) model, that is, a bidirectional transformer encoder.
[0061] The encoder pre-trains deep bidirectional representations by jointly conditioning on context in all layers. The Transformer is a method that relies entirely on self-attention to compute input and output representations.
[0062] Exemplarily, by calling the pre-trained BERT model, a corresponding feature vector, that is, an input text representation vector, is extracted according to the input comprehensive embedding vector. The input text representation vector can represent the semantic features of the input text.
[0063] Since the comprehensive embedding vector input to the encoder incorporates a prosodic embedding vector, for example, a prosodic embedding vector containing prosodic information and / or a rhyme embedding vector containing rhyme information, the input text representation vector output by the encoder also contains prosodic information, such as prosodic information and rhyme information.
[0064] For example, when the comprehensive embedding vector is a vector of (1, n, 768), the input text representation vector output by the encoder is also a vector of (1, n, 768).
[0065] Step S103: Acquire an initial embedding vector and prosody control information, and superimpose the initial embedding vector and the embedding vector of the prosody control information to obtain an initialized output text representation vector.
[0066] Exemplarily, the initial embedding vector may be preset, for example, each element in the initial embedding vector is 0.
[0067] For example, the initial embedding vector can be determined based on an initialization character sequence, for example, by performing embedding processing on the initialization character sequence to obtain the initial embedding vector. The initialization character sequence specifically includes character information of the next sentence to be generated. In some embodiments, all characters in the initialization character sequence can also be initialized to 0.
[0068] Exemplarily, the prosody control information is specifically the prosody information of the sentence to be generated. For example, the prosody control information includes tone control information and / or rhyme control information. The tone control information is specifically the tone information of the sentence to be generated, and the rhyme control information is specifically the rhyme information of the sentence to be generated. Exemplarily, the tone control information and the rhyme control information can be input by the user and are used to control the tone and rhyme of the generated sentence. The tone control information and / or the rhyme control information are embedded to obtain respective embedding vectors of the tone control information and / or the rhyme control information.
[0069] Exemplarily, the obtaining of tone control information and / or rhyme control information includes: obtaining a target sentence input by a user, determining the tone control information and / or rhyme control information based on the target sentence, and the target sentence and the generated sentence expected by the user have the same rhythmic rules.
[0070] For example, the tone control information and rhyme control information may be formulated based on the rhythm rules in the rhythm template.
[0071] Exemplarily, the tone control information and rhyme control information input by the user are used to control the tone and rhyme of the generated sentence.
[0072] The embedding vector of the initialization character sequence and the embedding vectors of the tone control information and / or rhyme control information are superimposed to obtain an initialized output text representation vector. Since the output text representation vector incorporates the rhythm control information, it can be used to control the rhythm of the generated poem, such as tone and rhyme.
[0073] Specifically, before decoding, the level and rhyme information of the poem to be generated is input to control the level and rhyme of the generated poem, thereby generating a poem with level and rhyme that conforms to the rules of poetry.
[0074] Step S104: Decode the output text representation vector and the input text representation vector to obtain a generated sentence corresponding to the source character sequence.
[0075] Since the output text representation vector contains rhythmic control information, such as level and tone control information and / or rhyme control information, and the input text representation vector also contains the rhythmic information of the source character sequence, such as level and tone information and rhyme information, the generated sentence obtained by the decoding process can reflect both the rhythmic information of the source character sequence and the rhythmic control information, thereby improving the rhythmic accuracy of the generated poem.
[0076] In some embodiments, based on the Transformer decoder, the input text representation vector is integrated through an attention mechanism to update the initialized output text representation vector word by word to obtain a generated sentence corresponding to the source character sequence.
[0077] For example, the decoder is the decoder of the Transformer model. The decoder of the Transformer model is a decoder model that is completely based on the attention mechanism. It completely abandons the loop and convolution structures that other models still retain after introducing the attention mechanism. It relies entirely on the attention mechanism to describe the global dependency between input and output, and has greatly improved task performance, parallel capability and ease of training.
[0078] Specifically, the initialized output text representation vector is input into the masked multi-head attention layer of the decoder, and the input text representation vector is input into the multi-head attention layer of the decoder. The softmax layer of the decoder outputs the output probability, selects the word with the highest probability, such as "I", and then updates it to the output text representation vector.
[0079] After the decoder based on the Transformer model updates the output text representation vector, it outputs the generated sentence corresponding to the source character sequence through the softmax layer, such as the next line of poetry after the source character sequence.
[0080] Based on the attention mechanism of the Transformer decoder, the output text representation vector containing prosodic control information is fused, such as the input text representation vector containing the prosodic information of the source character sequence, so that the prosodic accuracy of the generated sentence corresponding to the source character sequence is higher.
[0081] For example, when constructing the initialized output text representation vector, the tone information and / or rhyme information that you want to control to be generated is added to the output representation vector of the initialized output text. Therefore, the output text representation vector contains the tone control information and / or rhyme control information, thereby realizing the control of the tone and rhyme of the generated verses, thereby generating generated sentences with tone and rhyme that conform to the rules of poetry.
[0082] The above-mentioned embodiments of the present specification provide a method for generating poetry with rhythmic control, which obtains a comprehensive embedding vector by superimposing the embedding vectors of the character information, sentence information, and position information of the source character sequence and the rhythmic embedding vector of the source character sequence, and encodes the comprehensive embedding vector to obtain an input text representation vector; then, an initial embedding vector and rhythmic control information are obtained, and the initial embedding vector and the embedding vector of the rhythmic control information are superimposed to obtain an initialized output text representation vector; thereafter, the output text representation vector containing the rhythmic control information and the input text representation vector containing the rhythmic information of the source character sequence are decoded to obtain a generated sentence corresponding to the source character sequence, and the rhythmic accuracy of the generated sentence is higher.
[0083] Please refer to the above examples Figure 4 , Figure 4 A method for training a poetry generation model is provided in an embodiment of the present application.
[0084] Exemplarily, the poetry generation model includes an encoder and a decoder. Specifically, the open-source Chinese BERT model from Google is used as the initial model. Further model training is performed using poetry to obtain a BERT pre-trained model that better reflects the characteristics of poetry. The encoder's model parameters are then initialized based on the model parameters of the BERT pre-trained model. Because the BERT model has the same structure as the Transformer decoder, the decoder is also initialized based on the model parameters of the BERT pre-trained model.
[0085] By pre-training the Bert model based on the poetry corpus, the parameters of the encoder and decoder are initialized to accelerate the convergence of the model.
[0086] like Figure 4 As shown, the training method of the poetry generation model includes steps S201 to 207.
[0087] Step S201: Acquire a training sample, wherein the training sample includes a sample character sequence and a subsequent line of poetry following the sample character sequence, wherein the rhythm of the subsequent line of poetry matches that of the sample character sequence.
[0088] The rhythm of the latter sentence of poetry and the sample character sequence matches, specifically, the latter sentence of poetry and the sample character sequence conform to the rhythmic rules of poetry.
[0089] For example, the sample character sequence in the training sample is a poem extracted from a quatrain poem corpus, such as "The sun shines on the incense burner and produces purple smoke." The second line of the sample character sequence can be, for example, "I look at the waterfall hanging in the distance."
[0090] Step S202: Superimpose the embedding vectors of the character information, sentence information, position information of the sample character sequence and the prosody embedding vector of the sample character sequence to obtain a comprehensive embedding vector.
[0091] Specifically, the embedding result of character information, Token Embeddings, is represented by the vectors of each character in the sample character sequence. The embedding result of sentence information, Segment Embeddings, can be represented by the vectors that assist the BERT model to distinguish different sentences. The embedding result of position information, Position Embeddings, can enable the BERT model to learn the sequential attributes of the input.
[0092] Furthermore, the prosody embedding vector includes the level and oblique tones embedding vector and the rhyming embedding vector. Thus, the comprehensive embedding vector is superimposed by five vectors.
[0093] Step S203: Perform encoding processing on the comprehensive embedding vector to obtain an input text representation vector.
[0094] Specifically, the prosody embedding vector is integrated into the comprehensive embedding vector of the input encoder, so the encoder can fully learn the level and oblique tones and rhyming information of the poem.
[0095] Step S204: Perform embedding processing on the prosody information of the latter line of the poem to obtain a prosody expectation vector.
[0096] Specifically, according to the latter line of the poem of the sample character sequence in the corpus, such as "Looking afar, the cataract hangs in front of the river", obtain the level and oblique tones information and / or rhyming information of the latter line of the poem of the sample character sequence.
[0097] Perform embedding processing on the level and oblique tones information of the latter line of the poem to obtain a level and oblique tones expectation vector representing the level and oblique tones information of the expected generated sentence; perform embedding processing on the rhyming information of the latter line of the poem to obtain a rhyming expectation vector representing the rhyming information of the expected generated sentence.
[0098] Before decoding, input the level and oblique tones information and rhyme information of the poem to be generated to control the level and oblique tones and rhyming of the generated poem, so that the trained model can generate sentences that conform to the level and oblique tones control information and rhyming control information.
[0099] Step S205: Obtain an initial embedding vector, and superimpose the initial embedding vector and the prosody expectation vector to obtain an initialized output text representation vector.
[0100] Obtain an initialized character sequence, and perform embedding processing on the initialized character sequence to obtain an initialized embedding vector. Superimpose the initialized embedding vector and the prosody expectation vector to obtain an initialized output text representation vector.
[0101] Step S206: Decode the output text representation vector and the input text representation vector to obtain a generated sentence corresponding to the sample character sequence.
[0102] Specifically, the decoder based on the Transformer model integrates the representation vector of the input text to update the initialized output text representation vector. The decoder can then output the generated sentence corresponding to the sample character sequence, such as the next line of poetry after the source character sequence. At the same time, the softmax layer can obtain the predicted probability corresponding to each word in the generated sentence.
[0103] Step S207: Calculate a loss value based on the rhythmic information of the generated sentence and the rhythmic information of the subsequent sentence of poetry, and adjust the parameters of the poetry generation model based on the loss value.
[0104] According to the level and tone of each word in the generated sentence, the predicted probability of each word is determined to obtain the level and tone type and probability of each character in the generated sentence; according to the type of rhyme of the generated sentence and the predicted probability of the rhyme, the type and probability of the rhyme of the generated sentence are obtained; thus, the level and tone or rhyme probability distribution predicted by the poetry generation model is obtained.
[0105] The tone information and / or rhyme information of the poem following the sample character sequence is the real tone information and / or rhyme information, and the loss value can be calculated according to the preset cross entropy loss function.
[0106] Specifically, the loss of the level or rhyme of each word is calculated according to the following formula:
[0107]
[0108] Among them, v is the length of the dictionary, y is the actual tone information or rhyme information, and s is the predicted tone or rhyme probability distribution; the tone, rhyme and word losses of each word are added and integrated to obtain the loss value predicted by the model; then, by performing gradient descent on the total loss value, the tone and rhyme control of the poem can be made more precise without sacrificing the fluency and expression of the poem.
[0109] By integrating the loss functions of level and rhyme, the accuracy of level and rhyme of generated poems is further improved without sacrificing the generation quality of poems.
[0110] The training method of the poetry generation model provided in the above embodiment is as follows: the training sample includes a sample character sequence and a poem following the sample character sequence, the rhythm of the poem following the sample character sequence is matched, the embedding vector of the character information, sentence information, and position information of the sample character sequence is superimposed with the rhythm embedding vector of the sample character sequence to obtain a comprehensive embedding vector; the comprehensive embedding vector is encoded to obtain an input text representation vector, the rhythm information of the poem following the poem is embedded to obtain a rhythm expectation vector; then an initial embedding vector is obtained, the initial embedding vector is superimposed with the rhythm expectation vector to obtain an initialized output text representation vector; then the output text representation vector and the input text representation vector are decoded to obtain a generated sentence corresponding to the sample character sequence; finally, a loss value is calculated based on the rhythm information of the generated sentence and the rhythm information of the poem following the poem, and the parameters of the poetry generation model are adjusted based on the loss value. This makes the level and rhyme control of the poem more precise without sacrificing the fluency and meaning of the poem.
[0111] Please refer to Figure 5 , Figure 5 A schematic block diagram of a rhythmically controlled poetry generation device provided in an embodiment of the present application.
[0112] like Figure 5 As shown, the rhythm-controlled poetry generation device 300 includes: a vector superposition module 301, an encoding module 302, a vector superposition module 303 and a decoding module 304.
[0113] The vector superposition module 301 is used to obtain a source character sequence, and superimpose the embedding vectors of the character information, sentence information, and position information of the source character sequence and the prosody embedding vector of the source character sequence to obtain a comprehensive embedding vector.
[0114] The encoding module 302 is configured to encode the comprehensive embedding vector to obtain an input text representation vector.
[0115] The vector superposition module 303 is configured to obtain an initial embedding vector and prosody control information, and superimpose the initial embedding vector and the embedding vector of the prosody control information to obtain an initialized output text representation vector.
[0116] The decoding module 304 is configured to decode the output text representation vector and the input text representation vector to obtain a generated sentence corresponding to the source character sequence.
[0117] In one embodiment, the vector superposition module 303 is further used to obtain an initialization character sequence, perform embedding processing on the initialization character sequence, and obtain an embedding vector of the initialization character sequence; obtain tone control information and / or rhyme control information, perform embedding processing on the tone control information and / or rhyme control information, and obtain respective embedding vectors of the tone control information and / or rhyme control information; and superimpose the embedding vector of the initialization character sequence and the respective embedding vectors of the tone control information and / or rhyme control information.
[0118] Please refer to Figure 6 , Figure 6 A schematic block diagram of a training device 400 for a poetry generation model provided in an embodiment of the present application.
[0119] like Figure 6 As shown, the training device 400 of the poetry generation model includes: an acquisition module 401, a vector superposition module 402, an encoding module 403, an embedding processing module 404, a vector superposition module 405, a decoding module 406 and an adjustment module 407.
[0120] The acquisition module 401 is used to acquire a training sample, wherein the training sample includes a sample character sequence and a subsequent sentence of poetry following the sample character sequence, wherein the rhythm of the subsequent sentence of poetry matches that of the sample character sequence.
[0121] The vector superposition module 402 is configured to superimpose the embedding vectors of the character information, sentence information, and position information of the sample character sequence and the prosody embedding vector of the sample character sequence to obtain a comprehensive embedding vector.
[0122] The encoding module 403 is used to encode the comprehensive embedding vector to obtain an input text representation vector.
[0123] Embedding processing module 404 is used to embed the rhythm information of the latter sentence of poetry to obtain the rhythm expectation vector
[0124] The vector superposition module 405 is used to obtain an initial embedding vector, and superimpose the initial embedding vector and the prosody expectation vector to obtain an initialized output text representation vector.
[0125] The decoding module 406 is configured to decode the output text representation vector and the input text representation vector to obtain a generated sentence corresponding to the sample character sequence.
[0126] The adjustment module 407 is used to calculate the loss value based on the rhythm information of the generated sentence and the rhythm information of the subsequent sentence of the poem, and adjust the parameters of the poem generation model according to the loss value.
[0127] It should be noted that those skilled in the art will clearly understand that, for the convenience and brevity of description, the specific working processes of the above-described device and each module and unit can refer to the corresponding processes in the aforementioned fraud identification method embodiment, and will not be repeated here.
[0128] The apparatus provided in the above embodiment can be implemented in the form of a computer program. The computer program can be used in Figure 7 Runs on the computer equipment shown.
[0129] See also Figure 7 , Figure 7 This is a schematic block diagram of the structure of a computer device provided in an embodiment of the present application. The computer device may be a terminal device or a server.
[0130] like Figure 7 As shown, the computer device includes a processor, a memory, and a network interface connected via a system bus, wherein the memory may include a non-volatile storage medium and an internal memory.
[0131] The non-volatile storage medium can store an operating system and a computer program. The computer program includes program instructions, which, when executed, can cause the processor to execute any one of the prosody-controlled poetry generation methods or poetry generation model training methods.
[0132] The processor is used to provide computing and control capabilities and support the operation of the entire computer equipment.
[0133] The internal memory provides an environment for the operation of the computer program in the non-volatile storage medium. When the computer program is executed by the processor, the processor can execute any rhythm-controlled poetry generation method or poetry generation model training method.
[0134] The network interface is used for network communication, such as sending assigned tasks, etc. Those skilled in the art will understand that Figure 7 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than shown in the figure, or combine certain components, or have a different component arrangement.
[0135] It should be understood that the processor may be a central processing unit (CPU), other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or any conventional processor, etc.
[0136] In one embodiment, the processor is configured to execute a computer program stored in the memory to implement the following steps:
[0137] Obtaining a source character sequence, and superimposing the embedding vectors of character information, sentence information, and position information of the source character sequence with the prosody embedding vector of the source character sequence to obtain a comprehensive embedding vector;
[0138] Encoding the comprehensive embedding vector to obtain an input text representation vector;
[0139] Obtaining an initial embedding vector and prosody control information, and superimposing the initial embedding vector and the embedding vector of the prosody control information to obtain an initialized output text representation vector;
[0140] The output text representation vector and the input text representation vector are decoded to obtain a generated sentence corresponding to the source character sequence.
[0141] In one embodiment, when the processor is implemented, the prosody embedding vector includes a level-tone embedding vector and / or a rhyme embedding vector, for implementing:
[0142] Obtaining an initialization character sequence, performing embedding processing on the initialization character sequence, and obtaining an embedding vector of the initialization character sequence;
[0143] Acquiring tone control information and / or rhyme control information, and performing embedding processing on the tone control information and / or rhyme control information to obtain respective embedding vectors of the tone control information and / or rhyme control information;
[0144] The embedding vector of the initialization character sequence and the embedding vectors of the tone control information and / or rhyme control information are superimposed.
[0145] In one embodiment, the processor, when implementing the acquiring of the tone control information and / or the rhyme control information, is used to implement:
[0146] A target sentence input by a user is obtained, and tone control information and / or rhyme control information are determined according to the target sentence, wherein the target sentence and the generated sentence expected by the user have the same prosody rules.
[0147] In one embodiment, the processor encodes the comprehensive embedding vector to obtain an input text representation vector for implementing:
[0148] Based on an encoder including a BERT model, the comprehensive embedding vector is encoded to obtain an input text representation vector.
[0149] In one embodiment, the processor decodes the output text representation vector and the input text representation vector during implementation to obtain a generated statement corresponding to the source character sequence, for implementing:
[0150] Based on the Transformer decoder, the input text representation vector is integrated through the attention mechanism to update the initialized output text representation vector word by word to obtain the generated sentence corresponding to the source character sequence.
[0151] In another embodiment, the processor is configured to execute a computer program stored in the memory to implement the following steps:
[0152] Obtaining a training sample, the training sample including a sample character sequence and a subsequent line of poetry following the sample character sequence, wherein the rhythm of the subsequent line of poetry matches that of the sample character sequence;
[0153] Superimposing the embedding vectors of the character information, sentence information, and position information of the sample character sequence and the prosody embedding vector of the sample character sequence to obtain a comprehensive embedding vector;
[0154] Encoding the comprehensive embedding vector to obtain an input text representation vector;
[0155] Embedding the rhythm information of the second sentence of the poem to obtain a rhythm expectation vector;
[0156] Obtaining an initial embedding vector, and superimposing the initial embedding vector and the prosody expectation vector to obtain an initialized output text representation vector;
[0157] Decoding the output text representation vector and the input text representation vector to obtain a generated sentence corresponding to the sample character sequence;
[0158] A loss value is calculated based on the rhythmic information of the generated sentence and the rhythmic information of the subsequent sentence of poetry, and the parameters of the poetry generation model are adjusted based on the loss value.
[0159] An embodiment of the present application also provides a computer-readable storage medium, on which a computer program is stored. The computer program includes program instructions. The method implemented when the program instructions are executed can refer to the various embodiments of the rhythm-controlled poetry generation method or the poetry generation model training method of the present application.
[0160] The computer-readable storage medium may be an internal storage unit of the computer device described in the aforementioned embodiment, such as a hard disk or memory of the computer device. The computer-readable storage medium may also be an external storage device of the computer device, such as a plug-in hard disk, a SmartMedia Card (SMC), a Secure Digital (SD) card, a flash memory card, etc., equipped on the computer device.
[0161] It should be understood that the terms used in this specification are only for the purpose of describing specific embodiments and are not intended to limit the present application. As used in this specification and the appended claims, the singular forms "a", "an", and "the" are intended to include the plural forms unless the context clearly indicates otherwise.
[0162] It should also be understood that the term "and / or" used in this specification and the appended claims refers to any combination of one or more of the associated listed items and all possible combinations, including these combinations. It should be noted that, in this article, the terms "include", "comprise" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or system that includes a series of elements includes not only those elements, but also other elements that are not explicitly listed, or also includes elements that are inherent to such process, method, article or system. In the absence of further restrictions, an element defined by the sentence "including a..." does not exclude the presence of other identical elements in the process, method, article or system that includes the element.
[0163] The serial numbers of the embodiments of the present application are for description only and do not represent the advantages or disadvantages of the embodiments. The above description is only a specific embodiment of the present application, but the scope of protection of the present application is not limited thereto. Any technician familiar with the technical field can easily think of various equivalent modifications or replacements within the technical scope disclosed in this application, and these modifications or replacements should be included in the scope of protection of this application. Therefore, the scope of protection of this application should be based on the scope of protection of the claims.
Claims
1. A method for generating poetry with rhythmic control, characterized in that: include: Obtaining a source character sequence, and superimposing the embedding vectors of character information, sentence information, and position information of the source character sequence with the prosody embedding vector of the source character sequence to obtain a comprehensive embedding vector; Encoding the comprehensive embedding vector to obtain an input text representation vector; Obtaining an initial embedding vector and prosody control information, and superimposing the initial embedding vector and the embedding vector of the prosody control information to obtain an initialized output text representation vector; The output text representation vector and the input text representation vector are decoded to obtain a generated sentence corresponding to the source character sequence.
2. The method for generating poetry with rhythmic control as claimed in claim 1, wherein: The prosody embedding vector includes a level-tone embedding vector and / or a rhyme embedding vector; The poetry generation method also includes: Embedding the level and tone information of the source character sequence to obtain a level and tone embedding vector; and / or Embedding the rhyme information of the source character sequence to obtain a rhyme embedding vector; The step of superimposing the embedding vectors of the character information, sentence information, and position information of the source character sequence and the prosody embedding vector of the source character sequence includes: The embedding vectors of the character information, sentence information, position information of the source character sequence and the tone embedding vector and / or rhyme embedding vector are superimposed.
3. The method for generating poetry with rhythmic control as claimed in claim 2, wherein: The acquiring of the initial embedding vector and the prosody control information, and superimposing the initial embedding vector and the embedding vector of the prosody control information comprises: Obtaining an initialization character sequence, performing embedding processing on the initialization character sequence, and obtaining an embedding vector of the initialization character sequence; Acquiring tone control information and / or rhyme control information, and performing embedding processing on the tone control information and / or rhyme control information to obtain respective embedding vectors of the tone control information and / or rhyme control information; The embedding vector of the initialization character sequence and the embedding vectors of the tone control information and / or rhyme control information are superimposed.
4. The method for generating poetry with rhythmic control as claimed in claim 3, wherein: The obtaining of tone control information and / or rhyme control information includes: A target sentence input by a user is obtained, and tone control information and / or rhyme control information are determined according to the target sentence, wherein the target sentence and the generated sentence expected by the user have the same prosody rules.
5. The method for generating poetry with rhythmic control as claimed in claim 1, wherein: The encoding process of the comprehensive embedding vector to obtain the input text representation vector includes: Based on an encoder including a BERT model, the comprehensive embedding vector is encoded to obtain an input text representation vector.
6. The method for generating poetry with rhythmic control as claimed in claim 5, wherein: The decoding process of the output text representation vector and the input text representation vector to obtain a generated sentence corresponding to the source character sequence includes: Based on the Transformer decoder, the input text representation vector is integrated through the attention mechanism to update the initialized output text representation vector word by word to obtain the generated sentence corresponding to the source character sequence.
7. A method for training a poetry generation model, characterized in that: include: Obtaining a training sample, the training sample including a sample character sequence and a subsequent line of poetry following the sample character sequence, wherein the rhythm of the subsequent line of poetry matches that of the sample character sequence; Superimposing the embedding vectors of the character information, sentence information, and position information of the sample character sequence and the prosody embedding vector of the sample character sequence to obtain a comprehensive embedding vector; Encoding the comprehensive embedding vector to obtain an input text representation vector; Embedding the rhythm information of the second sentence of the poem to obtain a rhythm expectation vector; Obtaining an initial embedding vector, and superimposing the initial embedding vector and the prosody expectation vector to obtain an initialized output text representation vector; Decoding the output text representation vector and the input text representation vector to obtain a generated sentence corresponding to the sample character sequence; A loss value is calculated based on the rhythmic information of the generated sentence and the rhythmic information of the subsequent sentence of poetry, and the parameters of the poetry generation model are adjusted based on the loss value.
8. A rhythm-controlled poetry generation device, characterized in that: The rhythm-controlled poetry generation device includes: a vector superposition module, configured to obtain a source character sequence, and superimpose the embedding vectors of the character information, sentence information, and position information of the source character sequence and the prosody embedding vector of the source character sequence to obtain a comprehensive embedding vector; An encoding module, configured to encode the comprehensive embedding vector to obtain an input text representation vector; a vector superposition module, configured to obtain an initial embedding vector and prosody control information, and superimpose the initial embedding vector and the embedding vector of the prosody control information to obtain an initialized output text representation vector; A decoding module is used to decode the output text representation vector and the input text representation vector to obtain a generated sentence corresponding to the source character sequence.
9. A computer device, characterized in that: The computer device includes a processor, a memory, and a computer program stored in the memory and executable by the processor, wherein when the computer program is executed by the processor, the steps of the prosody-controlled poetry generation method as described in any one of claims 1 to 7 are implemented.
10. A computer-readable storage medium, characterized in that The computer-readable storage medium stores a computer program, wherein when the computer program is executed by a processor, the steps of the prosody-controlled poetry generation method according to any one of claims 1 to 7 are implemented.
Citation Information
Patent Citations
Method for automatically generating Chinese poetry based on corpus and metrical rule
CN106569995A
Ancient poetry generation method of based on image content
CN107480132A