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

Python Wireless couplet Generator

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces you to the python wireless couplet generator, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Introduction

On the Mid-Autumn Festival, when the ancients sang poems and talked about wine, and added a couplet to the festival, they complement each other.

The Mid-Autumn Festival is just around the corner to create a warm and harmonious festive atmosphere.

The editor expressed his best wishes for the festival in the form of a couplet.

What kind of couplets are there? Let's take a look at it.

Text

Environmental installation:

Python3.6, Pycharm, requests module, interface is based on PyQt5.

First of all, set the interface subtitle of the couplet generator.

Class CoupletGenerator (QWidget): def _ init__ (self, parent=None, * * kwargs): super (CoupletGenerator, self). _ init__ (parent) self.setWindowTitle ('couplet generator') self.setWindowIcon (QIcon ('data/icon.jpg')) self.setFixedSize (600,400)

As follows:

Then set up the corresponding button components and layout.

Self.label_title = QLabel ('couplet theme:') self.line_title = QLineEdit ('Mid-Autumn Festival') self.generate_btn = QPushButton ('generate') self.label_result = QLabel ('generate result:') self.text_couple = QTextEdit () grid = QGridLayout () grid.addWidget (self.label_title, 1,0) grid.addWidget (self.line_title, 1,1,1 39) grid.addWidget (self.generate_btn, 1,40) grid.addWidget (self.label_result, 2,0) grid.addWidget (self.text_couple, 2,1,1,40) self.setLayout (grid)

Finally, the couplets are generated as horizontal batch, upper couplet and lower couplet.

Def generate (self): if not self.line_title.text () .strip (): return url = f 'https://aip.baidubce.com/rpc/2.0/nlp/v1/couplets?access_token={self.access_token}' headers = {' Content-Type': 'application/json'} all_couplets = [] index_ranges = [(0,2), (3) 5), (6,8), (9,11), (12,14)] for idx in range (5): params = {'text': self.line_title.text (),' index': random.randint (* index_ ranges [IDX]),} response = requests.post (url, headers=headers) Json=params) response_json = response.json () center = response_json ['couplets'] [' center'] first = response_json ['couplets'] [' first'] second = response_json ['couplets'] [' second'] text = f 'horizontal batch: {center}\ nUpper: {first}\ nLower: {second}' All_couplets.append (text) self.text_couple.setText ('\ n\ n\ n'.join (all_couplets))

The effect is as follows:

Summary

All right! Do you know what to post on the couplet in front of the door?

On the python wireless couplet generator to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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