Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add grammar cog
- Loading branch information
Neuro Assassin
committed
Jun 12, 2019
1 parent
f432ff6
commit 259503c
Showing
3 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from .grammar import Grammar | ||
|
||
|
||
def setup(bot): | ||
bot.add_cog(Grammar(bot)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |