Trang chủ Tin Học Lớp 7 Ờm...hôm trước có ai hỏi câu bot nên nay hỏi...

Ờm...hôm trước có ai hỏi câu bot nên nay hỏi lại :) $\\$ Mức độ: Rất rất dễ $\\$ Viết một Discord Bot. - Khi người dùng nhắn: suggest of keyword <keyword> t

Câu hỏi :

Ờm...hôm trước có ai hỏi câu bot nên nay hỏi lại :) $\\$ Mức độ: Rất rất dễ $\\$ Viết một Discord Bot. - Khi người dùng nhắn: suggest of keyword <keyword> thì bot nhắn gợi ý một keyword khác từ <keyword>, nếu không tìm thấy thì nhắn None. - Khi người dùng nhắn: search keyword <keyword> thì bot nhắn danh sách các keyword có liên quan đến <keyword>. - Khi người dùng nhắn: summary of keyword <keyword> thì bot nhắn tóm tắt <keyword>. $\\$ Vd: Ảnh $\\$ Ngôn ngữ lập trình: python

image

Lời giải 1 :

Code

import discord
import wikipedia
from difflib import SequenceMatcher
import wikipediaapi
def suggestKeyword(Keyword):
    result = wikipedia.search(Keyword, results = 10)
    if len(result) > 0:
        precent = [SequenceMatcher(None,Keyword.lower(),result[x].lower()).ratio() for x in range(len(result))]
        sgt = ""
        ratio = 0.0
        for x in range(len(precent)):
            if precent[x] > ratio and result[x].lower() != Keyword.lower():
                ratio = precent[x]
                sgt = result[x]
        return sgt
    else:
        return "None"
def listSuggest(Keyword):
    return wikipedia.search(Keyword, results = 10)
def summary(Keyword):
    result = wikipedia.search(Keyword, results = 10)
    precent = [SequenceMatcher(None,Keyword.lower(),result[x].lower()).ratio() for x in range(len(result))]
    wiki_wiki = wikipediaapi.Wikipedia('en')
    page = wiki_wiki.page(result[precent.index(max(precent))])
    string = page.summary
    n = 2000
    return [string[i:i+n] for i in range(0, len(string), n)]
client = discord.Client()
@client.event
async def on_ready():
    print("Successfully connected")
@client.event
async def on_message(message):
    if message.content.startswith("suggest of keyword"):
        k = message.content.split("suggest of keyword ")[1]
        await message.channel.send(suggestKeyword(k))
    elif message.content.startswith("search keyword"):
        k = message.content.split("search keyword ")[1]
        await message.channel.send(listSuggest(k))
    elif message.content.startswith("summary of keyword"):
        k = message.content.split("summary of keyword ")[1]
        for x in summary(k):
            await message.channel.send(x)
client.run("TOKEN")

Thảo luận

Bạn có biết?

Tin học, tiếng Anh: informatics, tiếng Pháp: informatique, là một ngành khoa học chuyên nghiên cứu quá trình tự động hóa việc tổ chức, lưu trữ, xử lý và truyền dẫn thông tin của một hệ thống máy tính cụ thể hoặc trừu tượng (ảo). Với cách hiểu hiện nay, tin học bao hàm tất cả các nghiên cứu và kỹ thuật có liên quan đến việc mô phỏng, biến đổi và tái tạo thông tin.

Nguồn : Wikipedia - Bách khoa toàn thư

Tâm sự 7

Lớp 7 - Năm thứ hai ở cấp trung học cơ sở, một cuồng quay mới lại đến vẫn bước tiếp trên đường đời học sinh. Học tập vẫn là nhiệm vụ chính!

Nguồn : ADMIN :))

Copyright © 2021 HOCTAP247