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

How to Design an automatic Generator of Spring Festival couplets by QT

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how QT designs an automatic generator of Spring Festival couplets, I believe most people do not know much about it, so share this article for your reference. I hope you will gain a lot after reading this article. Let's learn about it together.

1. Preface

The Spring Festival is the most grand traditional festival in China. When it comes to the Spring Festival, every household must paste Spring Festival couplets. On the eve of the Spring Festival, blessing words will be written in bright red paper and strong ink, which will be pasted on both sides of the door frame on the day of the Spring Festival, implying good luck for a year. It will also paste the word Fu upside down on the door, meaning "blessing to Linmen". In order to find suitable Spring Festival couplets sentences, I use ready-made natural language processing interface to achieve a software to automatically generate Spring Festival couplets, input prompts can complete the generation of Spring Festival couplets, convenient to write Spring Festival couplets for reference.

Here is the effect of the implementation:

When I was a child, the day I was most looking forward to was the Spring Festival. I could wear new clothes, eat delicious food that I could not eat normally, watch the Spring Festival Gala together after dinner, and set off firecrackers; now the living standard is gradually improving, and I usually wear not bad food, and I don't look forward to it very much. now after the Spring Festival meal, everyone is staring at the mobile phone to play, feeling that there is no feeling like when I was a child.

two。 Realization principle

The software is designed using Qt framework and calls the intelligent Spring Festival couplets interface of Baidu brain to complete the generation of Spring Festival couplets.

Official website address: https://ai.baidu.com/tech/nlp_apply

HTTP request method: POST

Request address: https://aip.baidubce.com/rpc/2.0/creation/v1/couplets

Parameter of URL: access_token obtained by access_token through API Key and Secret Key

Request header Header:Content-Type application/json

Request parameter

Prompt characters for the theme of text Spring Festival couplets

The index of index Spring Festival couplets. The default is 0, the same prompt, when fetched multiple times, this index + 1 can access the following Spring Festival couplets.

Return parameter interpretation

First Spring Festival couplets: a couplet with 7-9 words

Second Spring Festival couplets: generate Spring Festival couplets with 7-9 words

Center Spring Festival couplets horizontal batch: generate 4-word Spring Festival couplets horizontal batch

Example of request:

{"text": "fresh water", "index": 0}

Example of the returned data:

{"couplets": {"first": "Spring Water Qianshan Green", "second": "Spring breeze", "center": "Welcome the Spring Festival"} 3. Sample code

The main core code is listed below, which can be posted directly into your own project and can be used.

3.1 get Spring Festival couplets API / / get Spring Festival couplets void Widget::get_CoupletText (QString terms,int index) {function_select=1; QString requestUrl; QNetworkRequest request; / / store pictures BASE64 code QString imgData; / / package request parameter assignment QJsonObject post_data; QJsonDocument document; / / set request address QUrl url; QByteArray post_param / / request address requestUrl = "https://aip.baidubce.com/rpc/2.0/creation/v1/couplets"; / / set data submission format request.setHeader (QNetworkRequest::ContentTypeHeader, QVariant (" application/json ")); / / set request parameters: post_data.insert (" text ", terms); / / string (limited to 5 characters) is the theme post_data.insert (" index ", index) of Spring Festival couplets The / / int integer defaults to 0, that is, the first Spring Festival couplet. For each change, add 1 to the value, and a certain number will return the results of the previous Spring Festival couplets. / / construct request url.setUrl (requestUrl + "? access_token=" + Token); request.setUrl (url); document.setObject (post_data); post_param = document.toJson (QJsonDocument::Compact); / / send request manager- > post (request, post_param);} 3.2 data parsing code / / parsing feedback result void Widget::replyFinished (QNetworkReply * reply) {int statusCode = reply- > attribute (QNetworkRequest::HttpStatusCodeAttribute). ToInt () / / read all data QByteArray replyData = reply- > readAll (); qDebug ()

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

Development

Wechat

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

12
Report