程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Can you generate love words, hidden poems and couplets in Python?

編輯:Python

hello , Hello, everyone ~

In the eyes of ordinary people, we programmers are all dull , They all wear a pair of glasses , Like to wear inch clothes ,T T-shirts are generally black, white and grey . Today I will teach you how to use python Generate Love words 、 Hide the head poem 、 Couplets .

Installation environment ( has mac For example )

One 、 Prepare the environment

1. The current environment supported by the propeller

  • macOS edition 10.11/10.12/10.13/10.14 (64 bit) ( I won't support it GPU edition )
  • Python edition 3.6/3.7/3.8/3.9 (64 bit)
  • pip or pip3 edition 20.2.2 Or later (64 bit)
  • PaddlePaddle I won't support it M1 chip (arm64 framework )

2.python edition

python --version

python Version must be in ,3.6.x - 3.9.x

3.pip edition

python -m pip --version

pip Version must >= 20.2.2

Two 、 Start installation

1. install

python -m pip install paddlepaddle==0.0.0 -f https://www.paddlepaddle.org.cn/whl/mac/cpu/develop.html

2. verification

import paddle
paddle.utils.run_check()

If appear PaddlePaddle is installed successfully!, Indicates that you have successfully installed .

Project presentation

One 、 Love word generation model

  • Code
import paddlehub as hub
module = hub.Module(name="ernie_gen_lover_words")
test_texts = [' Valentine's Day ', ' In my hometown ', ' I love you, you love me ']
results = module.generate(texts=test_texts, use_gpu=True, beam_width=5)
for result in results:
print(result)
  • effect
  • principle And Source code

Step by step official

Two 、 The generation model of Tibetan poems

  • Code
import paddlehub as hub
# When defining the model , Can be set by line=4 or 8 Specify the output quatrains or rhymes , Set up word=5 or 7 Specify to output five or seven words .
# Default line=4, word=7 That is, output seven character quatrains .
module = hub.Module(name="ernie_gen_acrostic_poetry", line=4, word=7)
test_texts = [' Nuggets are the best ']
results = module.generate(texts=test_texts, use_gpu=True, beam_width=5)
for result in results:
print(result)
  • effect
 Dig ditches to divert water and irrigate vegetable gardens ,
Golden Valley rootless seedling .
You'd better move your roots to the old room ,
A cow's head crows in the morning .
 Dig ditches to divert water and irrigate vegetable gardens ,
Golden Valley rootless seedling .
The most bitter year, the spring rain breaks ,
Countless cow doves are buried in the warehouse .
  • principle And Source code

Step by step official

3、 ... and 、 Poetry generation model

  • Code
import paddlehub as hub
module = hub.Module(name="ernie_gen_poetry")
test_texts = [' abed, I see a silver light , Li Bai is the king of volumes .']
results = module.generate(texts=test_texts, use_gpu=True, beam_width=5)
for result in results:
print(result)
  • effect
 The king's book is in bed , Month in bed .
Go to bed and play the piano , Play a song with the sound of the piano .
Play a song with the sound of the piano , The moonlight is bright .
At the end of the song, each is scattered , Dew drop banana green .
 The king's book is in bed , Month in bed .
Go to bed and play the piano , Play a song with the sound of the piano .
Play a song with the sound of the piano , The moonlight is bright .
After the song, the moon wants to fall , The wind is blowing in the cinnamon tree .
 The king's book is in bed , Month in bed .
Go to bed and play the piano , Play a song with the sound of the piano .
Play a song with the sound of the piano , The moonlight is bright .
After the song, the moon wants to fall , The laurel tree sleeps on its branches .
  • principle And Source code Step by step official

Four 、 Couplet generation model

  • Code
import paddlehub as hub
module = hub.Module(name="ernie_gen_couplet")
test_texts = [" The wind blows the clouds and the sky is full of tears "]
results = module.generate(texts=test_texts, use_gpu=True, beam_width=5)
for result in results:
print(result)
  • effect
 The rain beat the flowers and hurt the ground ,
The rain beat the flowers and frowned ,
The rain beat the flowers and the ground moved ,
The rain beat the frost and bullied the ground ,
The rain beat flowers and scattered sorrow on the ground .
Copy code 
  • principle And Source code

Step by step official


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved