Sentence acquisition method and device
By generating available metaphorical sentences for all common Chinese vocabulary, using triples and preset templates combined with vector cosine distance, the problem of insufficient number of metaphorical sentences in chat robots is solved, and the user experience and application scenarios are improved.
Patent Information
- Application Number
- CN202010287542.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-04-13
- Publication Date
- 2024-09-20
- Estimated Expiration
- 2040-04-13
AI Technical Summary
The corpus of metaphorical sentences in existing chatbots is limited, resulting in a small number of metaphorical sentences, many repeated uses, and a low user experience.
By obtaining the available metaphorical sentences of all commonly used Chinese words, using triples and preset metaphorical sentence templates to generate metaphorical sentences, determining the correlation based on the vector cosine distance of ontology words, metaphorical words and modifiers, and generating diversified metaphorical sentences .
It greatly increases the number of metaphorical sentences, avoids repeated use, improves the diversity and user experience of metaphorical sentences, and expands the scope of application scenarios.
Smart Images

Figure CN112307754B
Abstract
Description
Technical Field
[0001] The present application relates to the field of data processing technology, and in particular to a statement acquisition method and device. Background Art
[0002] With the continuous expansion of computer application fields, natural language processing has received great attention. Metaphor is a commonly used rhetorical device, which refers to using a metaphor that has similarities with the subject to describe or explain the subject. Using metaphors in writing and conversation can reflect a higher level of language proficiency, which has always been one of the difficulties in natural language processing. With the development of intelligent technology in recent years, chatbots or creative robots are evolving from "accuracy" to "openness" and "anthropomorphism". Generally speaking, using metaphors in conversations or texts can greatly improve users' favorability and encourage users to continue talking or reading.
[0003] In the existing chatbot, metaphors are first collected from a corpus, which includes chat logs or comment logs, and then the subject words and the object words in the collected metaphors are identified, and the subject words and the corresponding metaphors are stored in a database. When the subject word input by the user is received, the metaphor sentence corresponding to the subject word can be directly used.
[0004] However, the corpus for generating metaphors is limited, so the number of metaphors stored in the database is small, and there may be repeated use, resulting in a low user experience. Summary of the invention
[0005] The present application provides a sentence acquisition method and device, which can obtain available metaphor sentences of all commonly used Chinese words, thereby greatly increasing the number of metaphor sentences.
[0006] In a first aspect, the present application provides a statement acquisition method, comprising:
[0007] Get the ontology words;
[0008] According to the ontology word, at least one metaphor sentence corresponding to the ontology word is determined from a database, the database includes a plurality of ontology words and at least one metaphor sentence corresponding to each ontology word, the at least one metaphor sentence corresponding to each ontology word is generated according to at least one triple and a preset metaphor sentence template, the at least one triple is determined according to the correlation distance of an ontology word set, a metaphor word set, a modifier word set and the triple, the triple includes an ontology word, a metaphor word and a modifier word, the correlation distance is determined according to a first vector cosine distance between the ontology word and the modifier word, a second vector cosine distance between the metaphor word and the modifier word, and a difference between the first vector cosine distance and the second vector cosine distance, and the vector cosine distance is calculated according to the embedding vectors of the two words;
[0009] The at least one metaphor sentence is sent to a terminal device.
[0010] Optionally, the method further includes:
[0011] Perform word segmentation processing on the received corpus to obtain a word sample set, and determine a word embedding vector index corresponding to the word sample set according to a preset training model, wherein the word embedding vector index is used to store the embedding vector of each word;
[0012] Determine the body word embedding vector index according to the word embedding vector index and the body word set, and determine the modifier word embedding vector index according to the word embedding vector index and the modifier word set;
[0013] For each ontology word in the ontology word set, M triples are determined according to the embedding vector index of the metaphor word and the embedding vector index of the modifier word, where M is a preset positive integer;
[0014] At least one metaphor sentence is generated according to the correlation distance of each triple in the M triples and a preset metaphor sentence template.
[0015] Optionally, generating at least one metaphor sentence according to the correlation distance of each triple in the plurality of triples and a preset metaphor sentence template includes:
[0016] Calculate the correlation distance of each triple in the M triples respectively;
[0017] Determine P triplets with the smallest correlation distance from the M triplets, where P is a preset positive integer;
[0018] The at least one metaphor sentence is generated according to the P triples and a preset metaphor sentence template.
[0019] Optionally, determining M triples according to the metaphor word embedding vector index and the modifier word embedding vector index includes:
[0020] Determine N modifier words with the smallest vector cosine distance to the main word through the modifier word embedding vector index, where N is a preset positive integer;
[0021] For each of the N modifiers, determine Q body words whose vector cosine distances with the modifier are the smallest through the body word embedding vector index, and determine S body words whose vector cosine distances with the modifier are less than or equal to a first distance through the body word embedding vector index, where the first distance is the vector cosine distance between the main word and the modifier;
[0022] The M triplets are obtained according to the Q metaphorical words, the S metaphorical words and the N modifying words, where M=Q*N+S*N.
[0023] Optionally, the correlation distance is calculated by the following formula:
[0024] Dist α,β,γ =dist α,γ +dist β,γ +log(|dist α,γ -dis β,γ |+)
[0025] Among them, Dist α,β,γ is the correlation distance, dist α,γ is the cosine distance of the first vector, dist β,γ is the cosine distance of the second vector, |dist α,γ -dist β,γ | is the difference between the cosine distance of the first vector and the cosine distance of the second vector, and ξ is an integer.
[0026] Optionally, determining the metaphor word embedding vector index according to the word embedding vector index and the metaphor word set includes:
[0027] Finding the word embedding vectors of all the metaphor words in the metaphor word set from the word embedding vector index, and obtaining the metaphor word embedding vector index according to the word embedding vectors of all the metaphor words;
[0028] The determining the modifier word embedding vector index according to the word embedding vector index and the modifier word set includes:
[0029] The word embedding vectors of all modifiers in the modifier word set are found from the word embedding vector index, and the modifier word embedding vector index is obtained according to the word embedding vectors of all modifier words.
[0030] Optionally, the method further includes:
[0031] Obtaining a database of subject words, a database of metaphorical words and a database of modifier words;
[0032] The ontology word set, the metaphor word set and the modifier word set are extracted from the ontology word corpus, the metaphor word corpus and the modifier word corpus respectively.
[0033] In a second aspect, the present application provides a sentence acquisition device, comprising:
[0034] The acquisition module is used to obtain the ontology words;
[0035] a processing module, configured to determine, according to the ontology word, at least one metaphor sentence corresponding to the ontology word from a database, wherein the database includes a plurality of ontology words and at least one metaphor sentence corresponding to each ontology word, wherein the at least one metaphor sentence corresponding to each ontology word is generated according to at least one triple and a preset metaphor sentence template, wherein the at least one triple is determined according to a set of ontology words, a set of metaphorical words, a set of modifier words, and a correlation distance of the triple, wherein the triple includes an ontology word, a metaphorical word, and a modifier word, wherein the correlation distance is determined according to a first vector cosine distance between the ontology word and the modifier word, a second vector cosine distance between the metaphorical word and the modifier word, and a difference between the first vector cosine distance and the second vector cosine distance, wherein the vector cosine distance is calculated according to the embedding vectors of two words;
[0036] The sending module is used to send the at least one metaphor sentence to the terminal device.
[0037] Optionally, the device further comprises:
[0038] A word segmentation module is used to perform word segmentation processing on the received corpus to obtain a word sample set, and determine a word embedding vector index corresponding to the word sample set according to a preset training model, wherein the word embedding vector index is used to store the embedding vector of each word;
[0039] A first determination module is used to determine the body word embedding vector index according to the word embedding vector index and the body word set, and to determine the modifier word embedding vector index according to the word embedding vector index and the modifier word set;
[0040] A second determination module is used to determine, for each ontology word in the ontology word set, M triples according to the embedding vector index of the metaphor word and the embedding vector index of the modifier word, where M is a preset positive integer;
[0041] A generating module is used to generate at least one metaphor sentence according to the correlation distance of each triple in the M triples and a preset metaphor sentence template.
[0042] Optionally, the generating module is used to:
[0043] Calculate the correlation distance of each triple in the M triples respectively;
[0044] Determine P triplets with the smallest correlation distance from the M triplets, where P is a preset positive integer;
[0045] The at least one metaphor sentence is generated according to the P triples and a preset metaphor sentence template.
[0046] Optionally, the second determining module is used to:
[0047] Determine N modifier words with the smallest vector cosine distance to the main word through the modifier word embedding vector index, where N is a preset positive integer;
[0048] For each of the N modifiers, determine Q body words whose vector cosine distances with the modifier are the smallest through the body word embedding vector index, and determine S body words whose vector cosine distances with the modifier are less than or equal to a first distance through the body word embedding vector index, where the first distance is the vector cosine distance between the main word and the modifier;
[0049] The M triplets are obtained according to the Q metaphorical words, the S metaphorical words and the N modifying words, where M=Q*N+S*N.
[0050] Optionally, the correlation distance is calculated by the following formula:
[0051] Dist α,β,γ =dist α,γ +dist β,γ +log(|dist α,γ -dist β,γ |+)
[0052] Among them, Dist α,β,γ is the correlation distance, dist α,γ is the cosine distance of the first vector, dist β,γ is the cosine distance of the second vector, |dist α,γ -dist β,γ | is the difference between the cosine distance of the first vector and the cosine distance of the second vector, and ξ is an integer.
[0053] Optionally, the first determining module is used to:
[0054] Finding the word embedding vectors of all the metaphor words in the metaphor word set from the word embedding vector index, and obtaining the metaphor word embedding vector index according to the word embedding vectors of all the metaphor words;
[0055] The word embedding vectors of all modifiers in the modifier word set are found from the word embedding vector index, and the modifier word embedding vector index is obtained according to the word embedding vectors of all modifier words.
[0056] Optionally, the acquisition module is further used for:
[0057] Obtaining a database of subject words, a database of metaphorical words and a database of modifier words;
[0058] The processing module is also used to extract the entity word set, the metaphor word set and the modifier word set from the entity word corpus, the metaphor word corpus and the modifier word corpus respectively.
[0059] The sentence acquisition method and device provided in the present application, after acquiring the ontology word, determine at least one metaphor sentence corresponding to the ontology word from the database according to the ontology word, and then send the at least one metaphor sentence to the terminal device. Since the ontology words in the database in this embodiment are all commonly used words in Chinese, at least one metaphor sentence corresponding to each ontology word in the database is generated according to at least one triple and a preset metaphor sentence template, and at least one triple is determined according to the correlation distance between the ontology word set, the metaphor word set, the modifier word set and the triple. Therefore, the database stores the available metaphor sentences of all commonly used words in Chinese, and the number of metaphor sentences is greatly increased, which avoids repeated use, improves the diversity of metaphor sentences, expands the scope of application scenarios, and improves user experience. BRIEF DESCRIPTION OF THE DRAWINGS
[0060] In order to more clearly illustrate the technical solutions in the present application or the prior art, a brief introduction will be given below to the drawings required for use in the embodiments or the description of the prior art. 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 paying any creative labor.
[0061] Figure 1 A schematic diagram of an application scenario of this application;
[0062] Figure 2 A flowchart of an embodiment of a sentence acquisition method provided by the present application;
[0063] Figure 3 A flowchart of an embodiment of a sentence acquisition method provided by the present application;
[0064] Figure 4 This is a schematic diagram of the process of training the Word2vec model after receiving the corpus;
[0065] Figure 5 A schematic diagram of a word embedding vector index;
[0066] Figure 6 A schematic diagram of the process of obtaining the embedding vector index of the metaphor word and the embedding vector index of the modifier word according to the word embedding vector index;
[0067] Figure 7 This is a schematic diagram of the correlation between the subject word, the metaphorical word and the modifier;
[0068] Figure 8A schematic diagram of the process of determining M triples corresponding to each ontology word in an ontology word set provided by the present application;
[0069] Fig. 9 A schematic diagram of the structure of a sentence acquisition device provided by the present application;
[0070] Fig.10 A schematic diagram of the structure of a sentence acquisition device provided by the present application;
[0071] Fig.11 A schematic diagram of the hardware structure of the electronic device provided in this application. DETAILED DESCRIPTION
[0072] In order to make the purpose, technical solutions and advantages of this application clearer, the technical solutions in this application will be clearly and completely described below in conjunction with the drawings in this application. Obviously, the described embodiments are part of the embodiments of this application, not all of the embodiments. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0073] First, some terms in the embodiments of the present application are explained below to facilitate understanding by those skilled in the art.
[0074] 1. Word2vec is a group of related models used to generate word vectors. These models are shallow, two-layer neural networks that are trained to reconstruct linguistic word texts. The network is represented by words and needs to guess the input words in adjacent positions. Under the word bag model assumption in word2vec, the order of words is not important. After training, the word2vec model can be used to map each word to a vector, which can be used to represent the relationship between words. The vector is the hidden layer of the neural network.
[0075] 2. TensorFlow is a symbolic mathematics system based on dataflow programming, which is widely used in the programming implementation of various machine learning algorithms.
[0076] 3. Word embedding vector index. The word embedding vector index stores the embedding vector of each word. The Word2vec model is trained using Tensorflow according to the word sample set. After multiple trainings, the hidden layer parameter W of the neural network is the embedding vector of all words in the word sample set. In this application, the word embedding vector index can be implemented using any vector storage engine, such as Faiss or Milvus. After inputting the embedding vector v of any word, the word embedding vector index can find several words and their embedding vectors that are closest to v within a few milliseconds.
[0077] 4. Vector cosine distance refers to the vector cosine distance between two words. The word embedding vector index stores the embedding vector of each word. The vector cosine distance between two words is calculated based on the embedding vectors of the two words. The vector cosine distance calculation formula is: Where A and B are two word embedding vectors, and cosθ is the vector cosine distance, which is the dot product of A and B divided by the product of the lengths of A and B.
[0078] 5. A triplet refers to a combination of a main word, a metaphorical word and a modifier.
[0079] In the existing sentence acquisition method, the subject words and the metaphor words of the metaphor sentences in the collected corpus are identified, and the subject words and the corresponding metaphor sentences are stored in the database. The metaphor sentences in the database are obtained based on the collected existing metaphor sentences, and the existing metaphor sentences are limited. Therefore, the number of metaphor sentences stored in the database is small. During human-computer interaction, there will be repeated use, and the user experience is not high. To solve this problem, the present application provides a sentence acquisition method and device. The subject words in the database of the present application are all common words in Chinese. At least one metaphor sentence corresponding to each subject word in the database is generated according to at least one triple and a preset metaphor sentence template. At least one triple is determined according to the correlation distance of the subject word set, the metaphor word set, the modifier word set and the triple. Therefore, the database stores the available metaphor sentences of all common words in Chinese. The number of metaphor sentences is greatly increased, which avoids repeated use, improves the diversity of metaphor sentences, expands the scope of application scenarios, and improves user experience. The specific implementation process of the sentence acquisition method of the embodiment of the present application is described in detail below in conjunction with the accompanying drawings through specific embodiments.
[0080] The sentence acquisition method and device provided in this application can be applied to scenarios such as machine-generated text and chat robots (such as customer service robots). Taking customer service robots as an example, Figure 1 This is a schematic diagram of an application scenario of the present application, such as Figure 1As shown, the application scenario of the present application involves terminal devices and servers. The terminal devices are electronic devices such as mobile phones and personal computers. Figure 1 The interface of the terminal device shown in is a chat interface between the user and the customer service robot. For example, the user enters the sentence "What does the new moon look like" in the chat interface. The server can obtain the ontology word "new moon" based on the sentence entered by the user, and query whether there is the ontology word "new moon" from the database. If there is such an ontology word, at least one metaphor sentence corresponding to the ontology word is sent to the terminal device, for example, a metaphor sentence is sent: "The new moon is like a sickle / the new moon is like a fish hook". If there is no such ontology word, "invalid ontology" is sent to the terminal device. When the terminal device receives at least one metaphor sentence, the received metaphor sentence is displayed, so that the user can see the displayed content. The database in the present application stores the available metaphor sentences of all commonly used Chinese words. The number of metaphor sentences is greatly increased, which avoids repeated use, improves the diversity of metaphor sentences, expands the scope of application scenarios, and improves user experience.
[0081] Figure 2 This is a flowchart of an embodiment of a statement acquisition method provided by the present application. The execution subject of this embodiment may be Figure 1 The server shown, such as Figure 2 As shown, the method of this embodiment may include:
[0082] S101. Obtaining ontology words.
[0083] Specifically, the user inputs sentences or directly inputs ontological words through the terminal device. The ontological words are nouns. The ontological words are obtained by analyzing the sentences or words input by the user. For example, the user inputs "life", and life is the ontological word. For another example, the user inputs the sentence "What does the new moon look like", and the new moon is the ontological word.
[0084] S102. According to the ontology word, at least one metaphor sentence corresponding to the ontology word is determined from a database, the database includes multiple ontology words and at least one metaphor sentence corresponding to each ontology word, the at least one metaphor sentence corresponding to each ontology word is generated according to at least one triple and a preset metaphor sentence template, at least one triple is determined according to the correlation distance of the ontology word set, the metaphor word set, the modifier word set and the triple, the triple includes the ontology word, the metaphor word and the modifier word, and the correlation distance is determined according to the first vector cosine distance between the ontology word and the modifier word, the second vector cosine distance between the metaphor word and the modifier word, and the difference between the first vector cosine distance and the second vector cosine distance.
[0085] Specifically, the database includes multiple ontology words, each of which corresponds to at least one metaphor sentence. The server searches the database for the ontology word obtained in S101 to see if the ontology word exists. If the ontology word exists, at least one metaphor sentence corresponding to the ontology word is sent to the terminal device, for example, a metaphor sentence is sent: "The new moon is like a sickle / The new moon is like a fishhook". If the ontology word does not exist, "invalid ontology" is sent to the terminal device, and the terminal device displays the received content, thereby realizing interaction with the user. In this embodiment, the at least one metaphor sentence corresponding to each ontology word can be generated by the server or the metaphor sentence generation device according to the correlation distance of the ontology word set, the metaphor word set, the modifier word set and the triples, and then based on the multiple triples and the preset metaphor sentence template. Among them, the number of multiple triples can be preset according to the computing power of the server and actual needs. For each triple, the triple includes a main word, a metaphor word and a modifier. The correlation distance of the triple is determined according to the first vector cosine distance between the main word and the modifier, the second vector cosine distance between the metaphor word and the modifier, and the difference between the first vector cosine distance and the second vector cosine distance. The correlation distance of the triple is used to characterize the possibility that the triple can be used as a metaphor. The smaller the correlation distance of the triple, the greater the possibility that the triple can be used as a metaphor.
[0086] It should be noted that the at least one metaphor sentence corresponding to each ontological word can be generated by the server or by other metaphor sentence generating devices. If it is generated by the server, the server will store the multiple ontological words and at least one metaphor sentence corresponding to each ontological word in the database after generation; if it is generated by other metaphor sentence generating devices, the server will store the multiple ontological words generated by other metaphor sentence generating devices and at least one metaphor sentence corresponding to each ontological word in the database.
[0087] Among them, the main word set, the metaphor word set and the modifier word set are three different word sets. The main words and the metaphor words are nouns, and the modifier words are verbs and adjectives. In an implementable manner, the main word set, the metaphor word set and the modifier word set can be pre-stored. The main words of the metaphor sentence are generally abstract things that are difficult to understand, such as "life", "love", etc., while the metaphor words are generally common figurative things, such as "honey" and "mirage". Therefore, in this embodiment, the main words in the main word set can be words in modern poetry collections, modern prose collections and literary magazine collections, the metaphor words in the metaphor word set can be words in chat logs or comment logs, and the modifier words in the modifier word set can be words in adjective dictionaries, verb dictionaries or noun dictionaries. In another practicable manner, the ontology word set, the metaphor word set and the modifier word set may be obtained online. In this case, the method of this embodiment may further include, before S101: obtaining an ontology word database, a metaphor word database and a modifier word database, and extracting the ontology word set, the metaphor word set and the modifier word set from the ontology word database, the metaphor word database and the modifier word database respectively. For example, the ontology word database may be a collection of modern poems, modern prose and literary magazines, and all the corpora in the ontology word database are segmented by a word segmentation system to obtain the ontology word set; the metaphor word database may be a chat log or a comment log, and all the corpora in the metaphor word database are segmented by a word segmentation system to obtain the metaphor word set; the modifier word database may be an adjective dictionary, a verb dictionary or a noun dictionary, and the words in the dictionary may be directly used as modifiers to obtain the modifier word set.
[0088] Among them, the vector cosine distance between two words is calculated based on the embedding vectors of the two words. In this embodiment, the embedding vector of each word can be trained using Tensorflow based on the word sample set. The Word2vec model, after multiple trainings, the hidden layer parameter W of the neural network is the embedding vector of all words in the word sample set. In this application, the word embedding vector index can be implemented using any vector storage engine, such as Faiss or Milvus. After inputting the embedding vector v of any word, the word embedding vector index can find several words and their embedding vectors that are closest to v within a few milliseconds.
[0089] S103: Send at least one metaphor sentence to a terminal device.
[0090] Specifically, after receiving at least one metaphor sentence, the terminal device displays it according to the received content, thereby realizing interaction with the user.
[0091] The sentence acquisition method provided in the present embodiment determines at least one metaphor sentence corresponding to the ontology word from a database according to the ontology word after acquiring the ontology word, and then sends the at least one metaphor sentence to a terminal device. Since the ontology words in the database in the present embodiment are all commonly used words in Chinese, at least one metaphor sentence corresponding to each ontology word in the database is generated according to at least one triple and a preset metaphor sentence template, and at least one triple is determined according to the correlation distance between the ontology word set, the metaphor word set, the modifier word set and the triple. Therefore, the database stores available metaphor sentences of all commonly used words in Chinese, and the number of metaphor sentences is greatly increased, which avoids repeated use, improves the diversity of metaphor sentences, expands the scope of application scenarios, and improves user experience.
[0092] Figure 1 In the embodiment shown, the plurality of ontology words and the at least one metaphor sentence corresponding to each ontology word stored in the database may be pre-generated and stored, and may be updated later according to the above generation method, or may be generated online and directly stored in the database after generation. Before S101, the generation process of at least one metaphor sentence corresponding to each ontology word may also be included. Figure 2 The process of generating at least one metaphor sentence corresponding to each ontological word is described in detail.
[0093] Figure 3 This is a flowchart of an embodiment of a statement acquisition method provided by the present application. The execution subject of this embodiment may be Figure 1 The server shown, such as Figure 3 As shown, the method of this embodiment may include:
[0094] S201, performing word segmentation processing on the received corpus to obtain a word sample set, and determining a word embedding vector index corresponding to the word sample set according to a preset training model, where the word embedding vector index is used to store the embedding vector of each word.
[0095] The received corpus may be product reviews, chat logs, and user-created documents. It is understandable that the received corpus includes multiple sentences. Specifically, each sentence can be segmented through a word segmentation system to obtain a word sample set. The word sample set includes multiple words obtained after word segmentation. For example, a sentence is "Today is sunny", and after word segmentation, "today", "weather" and "sunny" are obtained. The words in the word sample set are stored in the natural order of the sentence. Next, the word embedding vector index corresponding to the word sample set is determined according to the preset training model, wherein the preset training model may be a Word2vec model. When it is a Word2vec model, the Word2vec model is specifically trained using Tensorflow based on the word sample set. Figure 4This is a schematic diagram of the process of training the Word2vec model after receiving the corpus. Figure 4 As shown, the Word2vec model is trained on the word sample set. After multiple trainings, the input layer, hidden layer and output layer of the neural network are obtained. The hidden layer parameter W of the neural network (size is D×300, D is the total number of words in the word sample set) is the embedding vector of all words in the word sample set. In this embodiment, for example, the hidden layer width can be set to 300 (also the length of the word embedding vector obtained by training), the word selection window width can be set to 8, for example, and D is the total number of different words in the sample set, which can generally be between tens of thousands and hundreds of thousands. After obtaining the embedding vectors of all words in the word sample set, the embedding vectors of all words are stored in a vector storage engine to obtain a word embedding vector index. In this application, the word embedding vector index can be implemented using any vector storage engine, such as Faiss or Milvus, wherein Faiss is a framework that provides efficient similarity search and clustering for dense vectors, and Milvus is an open source similarity search engine for massive feature vectors. Figure 5 A schematic diagram of a word embedding vector index, such as Figure 5 As shown, the word embedding vector index stores each word and its embedding vector. For example, the embedding vector corresponding to the word life is "0.23576 0.81324 0.33255 -0.27385...".
[0096] S202, determining a body word embedding vector index according to the word embedding vector index and the body word set, and determining a modifier word embedding vector index according to the word embedding vector index and the modifier word set.
[0097] Specifically, as an implementable method, Figure 6 The present invention is a schematic diagram of the process of obtaining a body word embedding vector index and a modifier word embedding vector index according to a word embedding vector index. After obtaining the word embedding vector index according to S201, the body word set can be intersected with the word embedding vector index to obtain the body word embedding vector index; the modifier word set can be intersected with the word embedding vector index to obtain the modifier word embedding vector index.
[0098] S203. For each ontology word in the ontology word set, M triples are determined according to the embedding vector index of the metaphor word and the embedding vector index of the modifier word, where M is a preset positive integer.
[0099] Specifically, M triplets are determined based on the embedding vector index of the metaphorical word and the embedding vector index of the modifier word, that is, M triplets that can constitute a metaphorical sentence are determined. Generally speaking, the correlation between the subject word and the metaphorical word is not strong (such as "life" and "honey"), but they must have commonalities ("sweetness"), which can be expressed by modifiers. Figure 7 This is a schematic diagram of the correlation between the subject word, the metaphor word and the modifier word, such as Figure 7 As shown, the ontology word is "life", and the four metaphorical words with the smallest vector cosine distance from the ontology word "life" can be found through the metaphorical word embedding vector index, such as "red", "sweet", "brave", and "painful". The number between each two words is the vector cosine distance (such as the vector cosine distance between life and red is 0.816). Then, the modifier word with the smallest vector cosine distance from each determined metaphorical word can be determined through the modifier word embedding vector index, for example, three modifier words "dense", "blood" and "hell" are determined. Multiple triples can be obtained by combining the ontology word with these metaphorical words and modifier words.
[0100] As an implementable manner, S203 may specifically be:
[0101] S2031. Determine N modifier words with the smallest vector cosine distance to the main word through the modifier word embedding vector index, where N is a preset positive integer.
[0102] S2032. For each of the N modifiers, determine the Q metaphorical words whose vector cosine distances with the modifier are the smallest through the metaphorical word embedding vector index, and determine the S metaphorical words whose vector cosine distances with the modifier are less than or equal to a first distance through the metaphorical word embedding vector index, where the first distance is the vector cosine distance between the main word and the modifier.
[0103] S2033. Obtain M triplets according to the Q metaphorical words, S metaphorical words and N modifying words, M=Q*N+S*N.
[0104] S204 , generating at least one metaphor sentence according to the correlation distance of each triple in the M triples and a preset metaphor sentence template.
[0105] Specifically, S204 may include:
[0106] S2041. Calculate the correlation distance of each triple in the M triples respectively.
[0107] Among them, taking the subject word α, the metaphor word β and the modifier word γ as an example, the relevance distance can be calculated by the following formula:
[0108] Dist α,β,γ =dist α,γ +dist β,γ +log(|dist α,γ -distβ,γ|+)
[0109] Among them, Dist α,β,γ is the correlation distance, dist α,γis the cosine distance of the first vector, dist β,γ is the cosine distance of the second vector, |dist α,γ -dist β,γ | is the difference between the cosine distance of the first vector and the cosine distance of the second vector, and ξ is an integer, for example, 1. Dist α,β,γ The smaller it is, the more likely it is that the triple (α, β, γ) can be used as a metaphor.
[0110] S2042. Then determine P triplets with the smallest correlation distance from the M triplets, where P is a preset positive integer.
[0111] S2043: Generate at least one metaphor sentence according to the P triples and a preset metaphor sentence template.
[0112] Specifically, the preset metaphor template may be one or more. The following table 1 shows examples of three metaphor templates and corresponding metaphor sentences:
[0113]
[0114]
[0115] Specifically, according to P triples, a complete metaphor sentence can be generated by replacing the corresponding wildcard [] in the template with the subject word, metaphor word, and modifier of each triple. For example, the triple ("life", "honey", "sweet") is substituted into the template "[subject] is like [metaphor] [adjective modifier]" to generate the metaphor sentence "Life is as sweet as honey".
[0116] The sentence acquisition method provided in the present embodiment obtains a word sample set by performing word segmentation processing on the received corpus, and determines the word embedding vector index corresponding to the word sample set according to a preset training model, and then determines the metaphor word embedding vector index and the modifier word embedding vector index respectively according to the word embedding vector index and the metaphor word set and the modifier word set, and then for each ontology word in the ontology word set, M triples are determined according to the metaphor word embedding vector index and the modifier word embedding vector index, and finally at least one metaphor sentence is generated according to the correlation distance of each triple in the M triples and a preset metaphor sentence template, so that available metaphor sentences of all commonly used Chinese words can be generated according to the received corpus, the potential metaphor relationship of any word can be intelligently discovered, and many metaphor sentences that humans cannot imagine can be discovered, thereby expanding the boundaries of text visualization, and the generalization ability is strong. The fluency of the generated metaphor sentence can also be similar to that of human handwritten metaphor sentences, which is suitable for use in machine-generated text, chat robots and other scenarios, and can improve user experience.
[0117] Figure 8A schematic diagram of the process of determining the M triples corresponding to each ontology word in the ontology word set provided in the present application. In this embodiment, N is 100, Q is 10, and S is 10 as an example. Then, M=Q*N+S*N=10*100+10*100=2000 accordingly. Figure 8 As shown, the process of determining M triples may include:
[0118] S301, determining whether there are any unselected words in the ontology word set.
[0119] If yes, execute S302, if no, terminate.
[0120] S302. Select an ontology word from the ontology word set and set it as W.
[0121] S303. Determine 100 modifier words with the smallest vector cosine distance to the main word through the modifier word embedding vector index.
[0122] S304 , for each of the 100 modifiers, determine 10 metaphorical words with the smallest vector cosine distance to the modifier through the metaphorical word embedding vector index.
[0123] S305. Determine 10 metaphor words whose vector cosine distances with the modifier words are less than or equal to a first distance through the metaphor word embedding vector index. The first distance is the vector cosine distance between the body word W and the modifier word.
[0124] S306. Obtain 2000 triples based on the entity word W, 10 metaphorical words, 10 metaphorical words and 100 modifier words.
[0125] Fig. 9 A schematic diagram of the structure of a sentence acquisition device provided by this application, such as Fig. 9 As shown, the device of this embodiment may include: an acquisition module 11, a processing module 12 and a sending module 13, wherein:
[0126] The acquisition module 11 is used to acquire ontology words;
[0127] The processing module 12 is used to determine at least one metaphor sentence corresponding to the ontology word from a database according to the ontology word, the database includes multiple ontology words and at least one metaphor sentence corresponding to each ontology word, the at least one metaphor sentence corresponding to each ontology word is generated according to at least one triple and a preset metaphor sentence template, at least one triple is determined according to the correlation distance of the ontology word set, the metaphor word set, the modifier word set and the triple, the triple includes the ontology word, the metaphor word and the modifier word, the correlation distance is determined according to the first vector cosine distance between the ontology word and the modifier word, the second vector cosine distance between the metaphor word and the modifier word, and the difference between the first vector cosine distance and the second vector cosine distance, and the vector cosine distance is calculated according to the embedding vectors of the two words;
[0128] The sending module 13 is used to send at least one metaphor sentence to a terminal device.
[0129] The device provided in the embodiment of the present application can execute the above Figure 2 The specific implementation principle and technical effects of the method embodiment shown can be found in the above method embodiment, which will not be repeated here in this embodiment.
[0130] Fig.10 A schematic diagram of the structure of a sentence acquisition device provided by this application, such as Fig.10 As shown, the device of this embodiment is Fig. 9 On the basis of the device shown in the figure, it can further include: a word segmentation module 14, a first determination module 15, a second determination module 16 and a generation module 17, wherein:
[0131] The word segmentation module 14 is used to perform word segmentation processing on the received corpus to obtain a word sample set, and determine the word embedding vector index corresponding to the word sample set according to a preset training model, and the word embedding vector index is used to store the embedding vector of each word;
[0132] The first determination module 15 is used to determine the body word embedding vector index according to the word embedding vector index and the body word set, and determine the modifier word embedding vector index according to the word embedding vector index and the modifier word set;
[0133] The second determination module 16 is used to determine M triples for each ontology word in the ontology word set according to the embedding vector index of the metaphor word and the embedding vector index of the modifier word, where M is a preset positive integer;
[0134] The generating module 17 is used to generate at least one metaphor sentence according to the correlation distance of each triple in the M triples and a preset metaphor sentence template.
[0135] Furthermore, the generating module 17 is used for:
[0136] Calculate the correlation distance of each triple in the M triples respectively;
[0137] Determine P triplets with the smallest correlation distance from the M triplets, where P is a preset positive integer;
[0138] At least one metaphor sentence is generated according to the P triples and a preset metaphor sentence template.
[0139] Further, the second determining module 16 is used for:
[0140] The N modifiers with the smallest vector cosine distance to the main word are determined through the modifier embedding vector index, where N is a preset positive integer. For each of the N modifiers, the Q body words with the smallest vector cosine distance to the modifier are determined through the body word embedding vector index, and the S body words with the vector cosine distance to the modifier less than or equal to the first distance are determined through the body word embedding vector index. The first distance is the vector cosine distance between the main word and the modifier. M triplets are obtained based on the Q body words, the S body words and the N modifiers, where M=Q*N+S*N.
[0141] Optionally, the correlation distance is calculated using the following formula:
[0142] Dist α,β,γ =dist α,γ +dist β,γ +log(|dist α,γ -dist β,γ |+)
[0143] Among them, Dist α,β,γ is the correlation distance, dist α,γ is the cosine distance of the first vector, dist β,γ is the cosine distance of the second vector, |dist α,γ -dist β,γ | is the difference between the cosine distance of the first vector and the cosine distance of the second vector, and ξ is an integer.
[0144] Furthermore, the first determining module 15 is used to:
[0145] Find the word embedding vectors of all the metaphor words in the metaphor word set from the word embedding vector index, and obtain the metaphor word embedding vector index based on the word embedding vectors of all the metaphor words;
[0146] Find the word embedding vectors of all modifiers in the modifier set from the word embedding vector index, and obtain the modifier embedding vector index based on the word embedding vectors of all modifiers.
[0147] Furthermore, the acquisition module 11 is also used for:
[0148] Obtaining a database of subject words, a database of metaphorical words and a database of modifier words;
[0149] The processing module is also used to extract the entity word set, the metaphor word set and the modifier word set from the entity word corpus, the metaphor word corpus and the modifier word corpus respectively.
[0150] The device provided in the embodiment of the present application can execute the above Figure 3 The specific implementation principle and technical effects of the method embodiment shown can be found in the above method embodiment, which will not be repeated here in this embodiment.
[0151] Fig.11 This is a schematic diagram of the hardware structure of the electronic device provided in this application. Fig.11 As shown, the electronic device 20 of this embodiment may include: a memory 21 and a processor 22;
[0152] A memory 21, used for storing computer programs;
[0153] The processor 22 is used to execute the computer program stored in the memory to implement the printing method in the above embodiment. For details, please refer to the relevant description in the above method embodiment.
[0154] Optionally, the memory 21 may be independent or integrated with the processor 22 .
[0155] When the memory 21 is a device independent of the processor 22, the electronic device 20 may further include:
[0156] The bus 23 is used to connect the memory 21 and the processor 22 .
[0157] Optionally, this embodiment further includes: a communication interface 24, which can be connected to the processor 22 through the bus 23. The processor 22 can control the communication interface 23 to implement the above-mentioned receiving and sending functions of the electronic device 20.
[0158] The electronic device provided in this embodiment can be used to execute the above method, and its implementation method and technical effect are similar, which will not be described in detail in this embodiment.
[0159] In the several embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic, for example, the division of modules is only a logical function division, and there may be other division methods in actual implementation, such as multiple modules can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces, devices or modules, which can be electrical, mechanical or other forms.
[0160] The modules described as separate components may or may not be physically separated, and the components shown as modules may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0161] In addition, each functional module in each embodiment of the present application can be integrated into one processing unit, or each module can exist physically separately, or two or more modules can be integrated into one unit. The above-mentioned module-composed unit can be implemented in the form of hardware or in the form of hardware plus software functional units.
[0162] The above-mentioned integrated module implemented in the form of a software function module can be stored in a computer-readable storage medium. The above-mentioned software function module is stored in a storage medium, including a number of instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) or a processor (English: processor) to perform some steps of the methods of various embodiments of the present application.
[0163] 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), etc. A general-purpose processor may be a microprocessor or any conventional processor. The steps of the method disclosed in the invention may be directly implemented by a hardware processor or implemented by a combination of hardware and software modules in the processor.
[0164] The memory may include a high-speed RAM memory, and may also include a non-volatile storage NVM, such as at least one disk memory, and may also be a USB flash drive, a mobile hard disk, a read-only memory, a magnetic disk or an optical disk, etc.
[0165] The bus may be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, or an Extended Industry Standard Architecture (EISA) bus, etc. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, the bus in the drawings of the present application is not limited to only one bus or one type of bus.
[0166] The computer-readable storage medium may be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk or optical disk. The storage medium may be any available medium that can be accessed by a general or special purpose computer.
[0167] Those skilled in the art can understand that all or part of the steps of implementing the above-mentioned method embodiments can be completed by hardware related to program instructions. The aforementioned program can be stored in a computer-readable storage medium. When the program is executed, the steps of the above-mentioned method embodiments are executed; and the aforementioned storage medium includes: ROM, RAM, disk or optical disk and other media that can store program codes.
[0168] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit it. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or replace some or all of the technical features therein with equivalents. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present application.
Claims
1. A sentence acquisition method, characterized in that: include: Get the ontology words; According to the ontology word, at least one metaphor sentence corresponding to the ontology word is determined from a database, the database includes a plurality of ontology words and at least one metaphor sentence corresponding to each ontology word, the at least one metaphor sentence corresponding to each ontology word is generated according to at least one triple and a preset metaphor sentence template, the at least one triple is determined according to the correlation distance of an ontology word set, a metaphor word set, a modifier word set and the triple, the triple includes an ontology word, a metaphor word and a modifier word, the correlation distance is determined according to a first vector cosine distance between the ontology word and the modifier word, a second vector cosine distance between the metaphor word and the modifier word, and a difference between the first vector cosine distance and the second vector cosine distance, and the vector cosine distance is calculated according to the embedding vectors of the two words; The at least one metaphor sentence is sent to a terminal device.
2. The method according to claim 1, characterized in that The method further comprises: Perform word segmentation processing on the received corpus to obtain a word sample set, and determine a word embedding vector index corresponding to the word sample set according to a preset training model, wherein the word embedding vector index is used to store the embedding vector of each word; Determine the body word embedding vector index according to the word embedding vector index and the body word set, and determine the modifier word embedding vector index according to the word embedding vector index and the modifier word set; For each ontology word in the ontology word set, M triples are determined according to the embedding vector index of the metaphor word and the embedding vector index of the modifier word, where M is a preset positive integer; At least one metaphor sentence is generated according to the correlation distance of each triple in the M triples and a preset metaphor sentence template.
3. The method according to claim 2, characterized in that The step of generating at least one metaphor sentence according to the correlation distance of each triple in the plurality of triples and a preset metaphor sentence template comprises: Calculate the correlation distance of each triple in the M triples respectively; Determine P triplets with the smallest correlation distance from the M triplets, where P is a preset positive integer; The at least one metaphor sentence is generated according to the P triples and a preset metaphor sentence template.
4. The method according to claim 2, characterized in that: The determining of M triples according to the embedding vector index of the metaphor word and the embedding vector index of the modifier word includes: Determine N modifier words with the smallest vector cosine distance to the main word through the modifier word embedding vector index, where N is a preset positive integer; For each of the N modifiers, determine Q body words whose vector cosine distances with the modifier are the smallest through the body word embedding vector index, and determine S body words whose vector cosine distances with the modifier are less than or equal to a first distance through the body word embedding vector index, where the first distance is the vector cosine distance between the main word and the modifier; The M triplets are obtained according to the Q metaphorical words, the S metaphorical words and the N modifying words, where M=Q*N+S*N.
5. The method according to any one of claims 1 to 4, characterized in that: The correlation distance is calculated by the following formula: Dist α,β,γ =dist α,γ +dist β,γ +log(|dist α,γ -dist β,γ |+ξ) Among them, Dist α,β,γ is the correlation distance, dist α,γ is the cosine distance of the first vector, dist β,γ is the cosine distance of the second vector, |dist α,γ -dist β,γ | is the difference between the cosine distance of the first vector and the cosine distance of the second vector, and ξ is an integer.
6. The method according to any one of claims 2 to 4, characterized in that: The determining the metaphor word embedding vector index according to the word embedding vector index and the metaphor word set includes: Finding the word embedding vectors of all the metaphor words in the metaphor word set from the word embedding vector index, and obtaining the metaphor word embedding vector index according to the word embedding vectors of all the metaphor words; The determining the modifier word embedding vector index according to the word embedding vector index and the modifier word set includes: The word embedding vectors of all modifiers in the modifier word set are found from the word embedding vector index, and the modifier word embedding vector index is obtained according to the word embedding vectors of all modifier words.
7. The method according to any one of claims 1 to 4, characterized in that: The method further comprises: Obtaining a database of subject words, a database of metaphorical words and a database of modifier words; The ontology word set, the metaphor word set and the modifier word set are extracted from the ontology word corpus, the metaphor word corpus and the modifier word corpus respectively.
8. A sentence acquisition device, characterized in that: include: The acquisition module is used to obtain the ontology words; a processing module, configured to determine, according to the ontology word, at least one metaphor sentence corresponding to the ontology word from a database, wherein the database includes a plurality of ontology words and at least one metaphor sentence corresponding to each ontology word, wherein the at least one metaphor sentence corresponding to each ontology word is generated according to at least one triple and a preset metaphor sentence template, wherein the at least one triple is determined according to a set of ontology words, a set of metaphorical words, a set of modifier words, and a correlation distance of the triple, wherein the triple includes an ontology word, a metaphorical word, and a modifier word, wherein the correlation distance is determined according to a first vector cosine distance between the ontology word and the modifier word, a second vector cosine distance between the metaphorical word and the modifier word, and a difference between the first vector cosine distance and the second vector cosine distance, wherein the vector cosine distance is calculated according to the embedding vectors of two words; The sending module is used to send the at least one metaphor sentence to the terminal device.
9. The device according to claim 8, characterized in that The device also includes: A word segmentation module is used to perform word segmentation processing on the received corpus to obtain a word sample set, and determine a word embedding vector index corresponding to the word sample set according to a preset training model, wherein the word embedding vector index is used to store the embedding vector of each word; A first determination module is used to determine the body word embedding vector index according to the word embedding vector index and the body word set, and to determine the modifier word embedding vector index according to the word embedding vector index and the modifier word set; A second determination module is used to determine M triples for each ontology word in the ontology word set according to the embedding vector index of the metaphor word and the embedding vector index of the modifier word, where M is a preset positive integer; A generating module is used to generate at least one metaphor sentence according to the correlation distance of each triple in the M triples and a preset metaphor sentence template.
10. The device according to claim 9, characterized in that The generation module is used for: Calculate the correlation distance of each triple in the M triples respectively; Determine P triplets with the smallest correlation distance from the M triplets, where P is a preset positive integer; The at least one metaphor sentence is generated according to the P triples and a preset metaphor sentence template.
11. The device according to claim 9, characterized in that The second determining module is used for: Determine N modifier words with the smallest vector cosine distance to the main word through the modifier word embedding vector index, where N is a preset positive integer; For each of the N modifiers, determine Q body words whose vector cosine distances with the modifier are the smallest through the body word embedding vector index, and determine S body words whose vector cosine distances with the modifier are less than or equal to a first distance through the body word embedding vector index, where the first distance is the vector cosine distance between the main word and the modifier; The M triplets are obtained according to the Q metaphorical words, the S metaphorical words and the N modifying words, where M=Q*N+S*N.
12. The device according to any one of claims 8 to 11, characterized in that: The correlation distance is calculated by the following formula: Dist α,β,γ =dist α,γ +dist β,γ +log(|dist α,γ -dist β,γ |+ξ) Among them, Dist α,β,γ is the correlation distance, dist α,γ is the cosine distance of the first vector, dist β,γ is the cosine distance of the second vector, |dist α,γ -dist β,γ | is the difference between the cosine distance of the first vector and the cosine distance of the second vector, and ξ is an integer.
13. The device according to any one of claims 9 to 11, characterized in that: The first determining module is used for: Finding the word embedding vectors of all the metaphor words in the metaphor word set from the word embedding vector index, and obtaining the metaphor word embedding vector index according to the word embedding vectors of all the metaphor words; The word embedding vectors of all modifiers in the modifier word set are found from the word embedding vector index, and the modifier word embedding vector index is obtained according to the word embedding vectors of all modifier words.
14. The device according to any one of claims 8 to 11, characterized in that: The acquisition module is also used for: Obtaining a database of subject words, a database of metaphorical words and a database of modifier words; The processing module is also used to extract the entity word set, the metaphor word set and the modifier word set from the entity word corpus, the metaphor word corpus and the modifier word corpus respectively.
15. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the statement acquisition method according to any one of claims 1 to 7 is implemented.
16. An electronic device, characterized in that: include: processor; as well as A memory, configured to store executable instructions of the processor; The processor is configured to execute the statement acquisition method according to any one of claims 1 to 7 by executing the executable instructions.
