From 259503c18162678d2c7f830c7036fb008289f136 Mon Sep 17 00:00:00 2001 From: Neuro Assassin Date: Wed, 12 Jun 2019 14:04:35 -0400 Subject: [PATCH] Add grammar cog --- grammar/__init__.py | 5 ++++ grammar/converters.py | 70 +++++++++++++++++++++++++++++++++++++++++++ grammar/grammar.py | 44 +++++++++++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 grammar/__init__.py create mode 100644 grammar/converters.py create mode 100644 grammar/grammar.py diff --git a/grammar/__init__.py b/grammar/__init__.py new file mode 100644 index 00000000..a05f78cd --- /dev/null +++ b/grammar/__init__.py @@ -0,0 +1,5 @@ +from .grammar import Grammar + + +def setup(bot): + bot.add_cog(Grammar(bot)) diff --git a/grammar/converters.py b/grammar/converters.py new file mode 100644 index 00000000..3c78ff3e --- /dev/null +++ b/grammar/converters.py @@ -0,0 +1,70 @@ +import argparse + +from redbot.core.commands import BadArgument, Converter + + +class NoExitParser(argparse.ArgumentParser): + def error(self, message): + raise BadArgument() + + +class Gargs(Converter): + async def convert(self, ctx, argument): + argument = argument.replace("—", "--") + parser = NoExitParser(description="Grammar argument parser", add_help=False) + + parser.add_argument("--meaning-like", "--ml", nargs="*", dest="ml", default=[]) + parser.add_argument("--spelled-like", "--sp", nargs="?", dest="sp", default=[]) + parser.add_argument("--sounds-like", "--sl", nargs="?", dest="sl", default=[]) + parser.add_argument("--rhymes-with", "--rw", nargs="?", dest="rw", default=[]) + parser.add_argument("--adjectives-for", "--af", nargs="?", dest="af", default=[]) + parser.add_argument("--nouns-for", "--nf", nargs="?", dest="nf", default=[]) + parser.add_argument("--comes-before", "--cb", nargs="*", dest="ca", default=[]) + parser.add_argument("--comes-after", "--ca", nargs="*", dest="cb", default=[]) + parser.add_argument("--topics", "--t", nargs="*", dest="t", default=[]) + parser.add_argument("--synonyms-for", "--sf", nargs="*", dest="sf", default=[]) + parser.add_argument("--antonyms-for", "--anf", nargs="*", dest="anf", default=[]) + parser.add_argument("--kind-of", "--ko", nargs="?", dest="ko", default=[]) + parser.add_argument("--more-specific-than", "--mst", nargs="?", dest="mso", default=[]) + parser.add_argument("--homophones", "--h", nargs="?", dest="h", default=[]) + + try: + vals = vars(parser.parse_args(argument.split(" "))) + except Exception as error: + raise BadArgument() from error + + data = {} + if vals["ml"]: + data["ml"] = " ".join(vals["ml"]) + if vals["sp"]: + data["sp"] = vals["sp"] + if vals["sl"]: + data["sl"] = vals["sl"] + if vals["rw"]: + data["rel_rhy"] = vals["rw"] + if vals["af"]: + data["rel_jjb"] = vals["af"] + if vals["nf"]: + data["rel_jja"] = vals["nf"] + if vals["ca"]: + data["lc"] = " ".join(vals["ca"]) + if vals["cb"]: + data["rc"] = " ".join(vals["cb"]) + if vals["t"]: + if len(vals["t"]) > 5: + raise BadArgument("Topic can only be five words") + data["topics"] = " ".join(vals["t"]) + if vals["sf"]: + data["rel_syn"] = " ".join(vals["sf"]) + if vals["anf"]: + data["rel_ant"] = " ".join(vals["anf"]) + if vals["ko"]: + data["rel_spc"] = vals["ko"] + if vals["mso"]: + data["rel_gen"] = vals["mso"] + if vals["h"]: + data["rel_hom"] = vals["h"] + + data["max"] = 10 + + return data diff --git a/grammar/grammar.py b/grammar/grammar.py new file mode 100644 index 00000000..1c590bee --- /dev/null +++ b/grammar/grammar.py @@ -0,0 +1,44 @@ +import aiohttp +from redbot.core import commands + +from .converters import Gargs + +URL = "http://api.datamuse.com/words" + + +class Grammar(commands.Cog): + """Get words related to the specified arguments""" + + def __init__(self, bot): + self.bot = bot + self.session = aiohttp.ClientSession() + + @commands.command() + async def grammar(self, ctx, *, args: Gargs): + """Get words related to the passed arguments. + + Arguments must have `--` before them. +    `meaning-like`/`ml`: Get words that mean close to what the passed word means. +    `spelled-like`/`sp`: Get words that are spelled like the passed word. +    `sounds-like`/`sl`: Get words that sound like the passed word. +    `rhymes-with`/`rw`: Get words that rhyme with the passed word. +    `adjectives-for`/`af`: Get adjectives for the passed noun. +    `nouns-for`/`nf`: Get nouns for the passed adjective. +    `comes-before`/`cb`: Get words that usually come before the passed word. +    `comes-after`/`ca`: Get words that usually come after the passed word. +    `topics`: Get words that are related to the passed topic. Max 5 words. +    `synonyms-for`/`sf`: Get synonyms for the passed word. +    `antonyms-for`/`anf`: Get antonyms for the passed word. +    `kind-of`/`ko`: Get the kind of what the passed word is (Computer -> Machine). +    `more-specific-than`/`mst`: Get more specific nouns for the passed word (Ex: Machine -> Computer). +    `homophones`/`h`: Get homophones of the passed word.""" + data = args + async with self.session.get(URL, params=data) as r: + if r.status != 200: + return await ctx.send(f"Invalid status code: {r.status}") + text = await r.json() + sending = "Here are the top 10 words that came close to your filters:\n```\n" + for x in text: + sending += x["word"] + "\n" + sending += "```" + await ctx.send(sending)