【Python教程】制作无线对联生成器

零 Python教程评论99字数 2257阅读7分31秒阅读模式

所需工具:

Python

聪明的大脑文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/7574.html

勤劳的双手文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/7574.html

 文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/7574.html

注意:本站只提供教程,不提供任何成品+工具+软件链接,仅限用于学习和研究。文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/7574.html

 文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/7574.html

导语

时逢中秋佳节,古人吟诗对唱、把酒言欢的日子, 佳节加上对联,那就是相得益彰。文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/7574.html

普天同庆、花好月圆庆中秋,中秋将至,为营造温馨、和谐的节日氛围文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/7574.html

以对对联的形式,表达了对节日的祝福文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/7574.html

都有哪些对联呢?让我们一睹为快吧~文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/7574.html

 文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/7574.html

正文

环境安装:

Python3.6、Pycharm、requests模块、界面是基于PyQt5。​

首先设置对联生成器的界面小标题等。

Python客栈送红包、纸质书

class CoupletGenerator(QWidget):
def __init__(self, parent=None, **kwargs):
super(CoupletGenerator, self).__init__(parent)
self.setWindowTitle('对联生成器')
self.setWindowIcon(QIcon('data/icon.jpg'))
self.setFixedSize(600, 400)

 

如下:

 

​然后设置好相应的按钮组件、布局。

self.label_title = QLabel('对联主题:')
self.line_title = QLineEdit('中秋佳节')
self.generate_btn = QPushButton('生成')
self.label_result = QLabel('生成结果:')
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)

 

最后生成对联为横批、上联、下联。

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'横批: {center}\n上联: {first}\n下联: {second}'
all_couplets.append(text)
self.text_couple.setText('\n\n\n'.join(all_couplets))
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'横批: {center}\n上联: {first}\n下联: {second}'
all_couplets.append(text)
self.text_couple.setText('\n\n\n'.join(all_couplets))

效果如下:​

零
  • 转载请务必保留本文链接:https://www.0s52.com/bcjc/pythonjc/7574.html
    本社区资源仅供用于学习和交流,请勿用于商业用途
    未经允许不得进行转载/复制/分享

发表评论