Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

10 lines of code to realize the poetry writing robot

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

The aggregator includes not only big data calculation, but also poetry and distance. Recently, I saw a lot of news about poetry-writing robots, so I tried to simply implement one with an aggregator. This implementation is really simple, as simple as 10 lines of code, please see the implementation steps:

Download dictionaries and poems

Find a dictionary file for word segmentation on the Internet, which records the part of speech of each Chinese word. I found one from GitHub, processed it a little and saved it to the set file dict.btx.

Download the txt file of three hundred Song ci poems, remove spaces and line breaks, as well as other characters other than text, to get a long string.

2 training

Segment the long string obtained in the previous step, and save the result to the set file as a template. The implementation code is as follows:

A

B

C

one

= file ("dict.btx") .import@bi () .keys (WORD) .index ()

two

= create (WORD,LEN,T)

three

Song ci three hundred banquet mountain pavilion Zhao Kui north to see apricot flowers cut ice cuttings, light stack number …...

four

= len (A3)

> iSuppli 1

five

For (iword3=A2.find (mid (A3memijime 3)), word2=A2.find (mid (A3recordiMagne2)), word1=A2.find (mid (A3djimijin1))

six

If (Word3 percent null)

> A2.insert (0, word3. (WORD), 3 focus word 3. (t)), i=i+3

seven

Else if (Word2 invalid null)

> A2.insert (0, word2. (WORD), 2 focus word 2. (t)), i=i+2

eight

Else if (Word1 invalid null)

> A2.insert (0, word1. (WORD), 1 Magi word 1. (t)), i=i+1

nine

Else

> i=i+1

ten

= file ("Song ci model .txt") .export @ t (A2)

A1 reads the dictionary file and establishes an index

A2 defines the data structure of the template record. Three fields represent: word, length, and part of speech.

A5 to A9 is circular processing, and A3 is segmented. Here, the longest word is only three words.

A10 save the results

In this step, we get a template of the participle of all the words in the three hundred ci Poems of the Song Dynasty, as well as the part of speech of these participles.

3 creation

The principle of creation is to find a Song ci as the goal, get each word of it, and randomly find a word to replace it from the template in the previous step according to the length and part of speech of the word segmentation. The choice here is "when will there be a bright moon in Shuidiaogou?" let's take a look at the code:

A

B

C

one

= file ("Song ci model .txt") .import@t ()

two

= file ("dict.btx") .import@bi () .keys (WORD) .index ()

three

When will there be a bright moon? ask the blue sky about the wine. I don't know what year tonight is in the heavenly palace. I long to ride the wind and return. For fear that Qionglou Yuyu, the high place can not beat the cold, dancing to get a clear shadow, how like in the world.

four

= len (A3)

> str= "", iTunes 1, words=null

five

For (iword3=A2.find (mid (A3memijime 3)), word2=A2.find (mid (A3recordiMagne2)), word1=A2.find (mid (A3djimijin1))

six

If (Word3 percent null)

> words=A1.select (LEN==3&& T==word3. (t)), i=i+3, str=str+words (1+rand (words.len (). (WORD)

seven

Else if (Word2 invalid null)

> words=A1.select (LEN==2&& T==word2. (t)), i=i+2, str=str+words (1+rand (words.len (). (WORD)

eight

Else if (Word1 invalid null)

> words=A1.select (LEN==1&& T==word1. (t)), i=i+1, str=str+words (1+rand (words.len (). (WORD)

nine

Else

> str=str+mid (A3Magi iPol 1), i=i+1

ten

> output (str)

The results are as follows: although there are grammatical errors in these "works", they are still similar, which is much better than what I wrote myself.

In late autumn, I have no choice but to send, standing in the partridge sky. Has become the west under the yellow leaves, where the cold spring. I spent another farewell. Wine awakens Yang Guan rustling, so sad and joyful frost, see young Shadow for the first time, who has been in the setting sun.

What is the greeting of the spring cold? I don't understand the misty rain. Freshen up the westerly wind on the river, the bright moon is sorry for this place. Who first stirred up the sound of the water. Acacia Luoyang Xiao color, how can not become the soul, should not meet the competition, he is not at the top.

Now send it several times, ask each other about Qingtian. Urge the pots on the river, where to send them at dusk. It's a pity that you haven't ruled the street. Light blowing Jiangnan Nakan, first break leisure to leisure, meet restrained shadow, Ren and in the past.

There was nowhere to answer last night. I didn't know where to go. Ying brings the king and grandson upstairs, sometimes now. Any little fragrance. Coveted pond swings, looking around at the sky, wine waking up the shadow of grass, I especially put the end of the world.

Choose a song to post on moments and get a lot of likes.

The realization principle is the substitution of the same part of speech, according to which we can also realize the function of writing Tang poetry. If the template file is further adjusted and the part of speech is optimized according to prosody and tonal, the effect will be better. This is the artificial labor that belongs to artificial intelligence, which requires professional knowledge and hard work, and cannot be achieved at once.

Students who are interested can download the attachment.

15368090470002ffa.rar

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report