简体   繁体   English

从txt文件中读取单词进行单词过滤

[英]Reading words from a txt file for word filtering

Here is my code I am trying to use in order to read words from an array in my words.txt file, but currently does not work and can't figure out how to do this.这是我尝试使用的代码,以便从我的 words.txt 文件中的数组中读取单词,但目前不起作用,也不知道如何执行此操作。

@client.listen('on_message')
async def msgfilter(message, member: discord.Member = None):

    wordfilter = open("filter.txt", "r")

    words = set(message.content.split())
    if not words.isdisjoint(wordfilter.read):
        await ctx.send("No")

Currently, I am just putting these words straight in my main.py file and have approximately 50,000 words within an array of words users cannot say, if they have the moderation module enabled.目前,我只是将这些词直接放在我的 main.py 文件中,并且在用户不能说的词数组中有大约 50,000 个词,如果他们启用了审核模块。

These are all within my main.py file but the combinations in order to avoid members from circumventing these restrictions make it even longer.这些都在我的 main.py 文件中,但为了避免成员规避这些限制而进行的组合使其变得更长。 But it currently makes the document slow to edit or save/close the file.但它目前使文档编辑或保存/关闭文件的速度变慢。

How would it be possible to read each word from the array within the file and check if the word in the users message was included in this document?如何从文件中的数组中读取每个单词并检查用户消息中的单词是否包含在该文档中?

This is an example of the words.txt document with the words within an array.这是 words.txt 文档的示例,其中包含数组中的单词。

wordfilter = ['badword', 'badword', 'badword', 'badword', 'badword', 'badword']
wordfilter = open("filter.txt", "r")

words = set(message.content.split())
filter_words = [w[1:-1] for w in wordfilter.read().strip().split(", ")]

if not words.isdisjoint(filter_words):
    await ctx.send("No")

wordfilter.close()

Assuming your filter.txt contains information in the format 'word1', 'word2', 'word3', ... .假设您的 filter.txt 包含格式为'word1', 'word2', 'word3', ...

It's normal your program is slow;你的程序很慢是正常的; 50k words is a lot, and you're re-opening the file everytime a message is sent. 50k 字很多,每次发送消息时您都需要重新打开文件。 You may want to see if it's possible to keep the file contents in memory instead.您可能想看看是否可以将文件内容保留在 memory 中。 I can't really see why it would crash, though.不过,我真的不明白为什么它会崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM