I am participating in the Mid-Autumn Festival Creative Submission contest, please see: Mid-Autumn Festival Creative Submission Contest for details

takeaway

Mid-Autumn moon, folk has been spreading a number of different legends and beautiful myths, of course, the most familiar is “Chang ‘e moon”. In multi-ethnic China, there are more than 20 ethnic groups are also celebrating the Mid-Autumn Festival, although different ways to celebrate, but its implication is also in the qi for peace and prosperity, food and clothing, round and safe.Why don’t we have a poem against this wonderful holiday season!

🐢 first

The characteristics of: According to the tang poetry data set to design a circular neural network model, to achieve you as long as you input any 4 words and the corresponding style, that is, you can achieve an ancient poem. Write a poem about your special someone.

Results show

Please input four word idiom: the golden autumn festival please input the type of poem (Tibetan poem or Tibetan word poem) : Tibetan poem poem golden Tiancheng will not be autumn qi jade king with jia Qi and mountain ring festival tomorrow to returnCopy the code

💖 of course this can also be used to express feelings

Please input the four word idiom: love female friend please input the type of poem (Tibetan poem or Tibetan word poem) : Tibetan poem poem poem love water line female Frost snow friends way lonely without words guest friends I in who knowsCopy the code

The source code

import re
import requests

word=input("Please enter the four-word idiom:")
style=input("Please enter type of poem (Tibetan poem or Tibetan poem) :")
base_url = "https://momodel.cn/pyapi/apps/run/"
app_id = "5bfd118f1afd942b66b36b30"
input_dic = {"Chinese_word": {"val": word, "type": "str"}, "style": {"val": style, "type": "str"}}
output_dic = {"Poetry": {"type": "str"}}
app_version = "0-0-12"
payload = {"app": {"input": input_dic, "output": output_dic}, "version": app_version}
response = requests.post(base_url + app_id, json=payload)
chinese_word=re.split('[,。]',response.json().get('response').get('Poetry'))
print("Poetry")
for i in chinese_word:
    print(i)

Copy the code

The second paragraph of 🐢

Talking about API without Baidu how can line ~. Although fatology has written a lot about invocation before. Today, we will introduce it again for the Mid-Autumn Festival.

  • Official link: Portal

If you sign up after that


Once created you can look at the official guide to get your own Key: Guide link

Call Baidu AI intelligent poem writing interface (for testing)
def get_poem(text, token_key, index=0) :
    """ Call Baidu AI intelligent poem writing interface and generate seven-character poem :param text: intelligent poem writing topic (official limit no more than 5 words) :param token_key: obtained token :param index: Different indexes will generate different seven-character poems """
    request_url = 'https://aip.baidubce.com/rpc/2.0/nlp/v1/poem'
    params_d = dict()
    params_d['text'] = text
    params_d['index'] = index
    params = json.dumps(params_d).encode('utf-8')
    access_token = token_key
    request_url = request_url + "? access_token=" + access_token
    request = Request(url=request_url, data=params)
    request.add_header('Content-Type'.'application/json')
    response = urlopen(request)
    content = response.read()
    if content:
        data = json.loads(content)
        return data
Copy the code

The data type in the returned data

{
  "poem": [{"title":      "Baidu"
  	  	"content": "China Baidu Qingyuan Gong Huaxia qianqiu singing great wind grand plan to open the flourishing red flag splendid sky."}}]Copy the code

Parse the resulting verse

def parse_poem(data) :
    """ Param data: valid data generated by invoking intelligent poem writing :return: title and content of poem """
    title = data['poem'] [0] ['title']
    poem = data['poem'] [0] ['content'].replace('\t'.'\n')
    print(title)
    print(poem)
    
Copy the code

The processing results

Mid-Autumn Festival "Mid-Autumn Festival" in the night said yuan Long, autumn sound degree of wind and rain. Good gas overflow double clear, the rhythm falls quite 踼.Copy the code