Example #1
0
class TestTextUtil(TestCase):
    def setUp(self):
        super().setUp()
        self.text_util = TextUtil()

    def test_text_normalization(self):
        in1 = "I'll eat an apple."
        expect1 = "I will eat an apple."
        out1 = self.text_util.text_normalization(in1)
        self.assertEqual(expect1, out1)

    def test_filter_stop_word(self):
        in1 = ['I', 'am', 'panda']
        expect1 = ['I', 'panda']
        out1 = self.text_util.filter_stop_word(in1)
        self.assertListEqual(out1, expect1)

    def test_filter_punctuation(self):
        in1 = ['Because', ',', 'I', 'am', 'panda', '.']
        expect1 = ['Because', 'I', 'am', 'panda']
        out1 = self.text_util.filter_punctuation(in1)
        self.assertListEqual(out1, expect1)
    async def notexist(self, ctx, dType: str):
        match = TextUtil.find_closest(dType, ["horse", "cat", "person", "artwork"])

        if match:
            img = requests.get(f"https://this{match}doesnotexist.com/{'image' if match == 'person' else ''}").content

            with open("files/fake/image.jpeg", "wb+") as f:
                f.write(img)
            
            await ctx.send(file=discord.File("files/fake/image.jpeg"))

        else:
            await ctx.send("Unknown option")
Example #3
0
        async def play(self, ctx, *args):
            channel = ctx.author.voice.channel
            song = ' '.join(args)

            # fuzzy match
            match = TextUtil.find_closest(song, MediaUtil.get_songs(True))
            if match:
                player = discord.FFmpegPCMAudio(f'files/music/{match}.mp3')
                await ctx.send(f"Playing :notes:  `{match}`")

                self._class.set("voice", await channel.connect())
                self._class.get("voice").play(player, after=None)
            else:
                await ctx.send(f"Song `{song}` does not exist.")
                return
    async def edit(self, ctx, *args):
        isRulesChannel = ctx.message.self.channel.id == 737096170043605032
        rules = JsonUtil.get("rules", "rules")
        ids = JsonUtil.get("rules", "ids")

        if len(args) > 2:
            rules[args[0]][args[1]] = ' '.join(args[2:])
        else:
            if not isRulesChannel:
                await ctx.send("Not enough arguments")
            return

        if args[0] != "ids" and (match := TextUtil.find_closest(
                args[0], rules, 50)):
            if args[0] != match:
                react = await TextUtil.wait_react(ctx, self.bot,
                                                  f"Did you mean: `{match}`?",
                                                  ["✅", "❌"], True)
                if react and react == "✅":
                    args[0] = match

            em = discord.Embed(title=f"{args[0].title()} Rules",
                               color=0x52c832)
            for num in rules[args[0]]:
                if num == args[1]:
                    rules[args[0]][args[1]] = ' '.join(args[2:])
                    em.add_field(name=f"Rule {num}",
                                 value=' '.join(args[2:]),
                                 inline=True)
                else:
                    em.add_field(name=f"Rule {num}",
                                 value=rules[args[0]][num],
                                 inline=True)

            msg = await self.channel.fetch_message(ids[args[0]])
            await msg.edit(embed=em)
Example #5
0
def static_text_cnn_word2vec_predict(sentence):
    global word2vec_util, text_cnn_model
    if word2vec_util is None:
        word2vec_util = WordEmbeddingUtil()
    text_util = TextUtil()
    row = text_util.text_normalization(sentence)
    words = text_util.lemmatize_sentence(row)
    words = text_util.filter_punctuation(words)
    words = text_util.filter_stop_word(words)
    words = text_util.get_words_with_len(words)
    words_matrix = np.zeros([Config.SENTENCE_MAX_LEN, Config.EMBEDDING_SIZE], dtype=np.float32)
    for idx, word in enumerate(words):
        words_matrix[idx] = word2vec_util.get_word2vec_vec(word)
    text_cnn_model.eval()
    words_matrix_tensor = torch.Tensor(words_matrix)
    words_matrix_tensor = torch.unsqueeze(words_matrix_tensor, 0)
    predict = text_cnn_model(words_matrix_tensor)
    result = predict.item()
    return result
Example #6
0
    async def help(self, ctx, category=None):
        embed = None

        for command in self.bot.commands:
            if command.name == category:
                embed=discord.Embed(title=f"{self.bot.command_prefix}{command.name}", color=0x52c832)
                embed.add_field(
                    name=f"{command.help}",
                    value=f"usage: {command.usage}"
                )
                embed.set_footer(text="* means its a required parameter")
                break

        if not embed:
            if not category:
                embed=discord.Embed(title="Commands List", color=0x52c832)
                for cat in self.categories:
                    embed.add_field(name=cat.title(), value=f"`{self.bot.command_prefix}help {cat}`", inline=True)

            else:
                match = TextUtil.find_closest(category, self.categories, 60)
                
                if not match:
                    await ctx.send(f"Unknown category: `{category}`")
                    return
                
                commands = [cmd for cmd in self.bot.commands if cmd.name in [os.path.basename(f).split('.')[0] for f in glob.glob(f"commands/{match}/*.py")]]

                embed=discord.Embed(title=f"{match.title()} Help", color=0x52c832)
                for com in commands:
                    embed.add_field(name=f"{self.bot.command_prefix}{match} {com.usage}", value=com.help, inline=True)
                embed.set_footer(text="* means its a required parameter")

        if not embed:
            await ctx.send(f"Unknown category or command: `{category}`")
            return

        await ctx.send(embed=embed)
Example #7
0
 def setUp(self):
     super().setUp()
     self.text_util = TextUtil()