-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdictionary.py
159 lines (143 loc) · 5.48 KB
/
dictionary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import aiohttp
import discord
import contextlib
from bs4 import BeautifulSoup
import json
import logging
import re
from discord.ext import commands
from discord.ext.commands import Bot
from cogs.utils.test import pagify
def setup(bot):
bot.add_cog(Dictionary(bot))
class Dictionary(commands.Cog):
"""
Word, yo
Parts of this cog are adapted from the PyDictionary library.
"""
def __init__(self, bot):
self.bot = bot
self.session = aiohttp.ClientSession()
@commands.command()
async def define(self, ctx, *, word: str):
"""Displays definitions of a given word."""
search_msg = await ctx.send("Searching...")
search_term = word.split(" ", 1)[0]
result = await self._definition(ctx, search_term)
str_buffer = ""
if not result:
with contextlib.suppress(discord.NotFound):
await search_msg.delete()
await ctx.send("This word is not in the dictionary.")
return
for key in result:
str_buffer += f"\n**{key}**: \n"
counter = 1
j = False
for val in result[key]:
if val.startswith("("):
str_buffer += f"{str(counter)}. *{val})* "
counter += 1
j = True
else:
if j:
str_buffer += f"{val}\n"
j = False
else:
str_buffer += f"{str(counter)}. {val}\n"
counter += 1
with contextlib.suppress(discord.NotFound):
await search_msg.delete()
for page in pagify(str_buffer, delims=["\n"]):
await ctx.send(page)
async def _definition(self, ctx, word):
data = await self._get_soup_object(
f"http://wordnetweb.princeton.edu/perl/webwn?s={word}"
)
if not data:
return await ctx.send("Error fetching data.")
types = data.findAll("h3")
length = len(types)
lists = data.findAll("ul")
out = {}
if not lists:
return
for a in types:
reg = str(lists[types.index(a)])
meanings = []
for x in re.findall(r">\s\((.*?)\)\s<", reg):
if "often followed by" in x:
pass
elif len(x) > 5 or " " in str(x):
meanings.append(x)
name = a.text
out[name] = meanings
return out
@commands.command()
async def antonym(self, ctx, *, word: str):
"""Displays antonyms for a given word."""
search_term = word.split(" ", 1)[0]
result = await self._antonym_or_synonym(ctx, "antonyms", search_term)
if not result:
await ctx.send("This word is not in the dictionary or nothing was found.")
return
result_text = "*, *".join(result)
msg = f"Antonyms for **{search_term}**: *{result_text}*"
for page in pagify(msg, delims=["\n"]):
await ctx.send(page)
@commands.command()
async def synonym(self, ctx, *, word: str):
"""Displays synonyms for a given word."""
search_term = word.split(" ", 1)[0]
result = await self._antonym_or_synonym(ctx, "synonyms", search_term)
if not result:
await ctx.send("This word is not in the dictionary or nothing was found.")
return
result_text = "*, *".join(result)
msg = f"Synonyms for **{search_term}**: *{result_text}*"
for page in pagify(msg, delims=["\n"]):
await ctx.send(page)
async def _antonym_or_synonym(self, ctx, lookup_type, word):
if lookup_type not in ["antonyms", "synonyms"]:
return None
data = await self._get_soup_object(f"http://www.thesaurus.com/browse/{word}")
if not data:
await ctx.send("Error getting information from the website.")
return
website_data = None
script = data.find_all("script")
for item in script:
if item.string:
if "window.INITIAL_STATE" in item.string:
content = item.string
content = content.lstrip("window.INITIAL_STATE =").rstrip(";")
content = (
content.replace("undefined", '"None"')
.replace(": true", ': "True"')
.replace(": false", ': "False"')
)
try:
website_data = json.loads(content)
except json.decoder.JSONDecodeError:
return None
except Exception as e:
log.exception(e, exc_info=e)
await ctx.send(
"Something broke. Check your console for more information."
)
return None
final = []
if website_data:
tuna_api_data = website_data["searchData"]["tunaApiData"]
if not tuna_api_data:
return None
syn_list = tuna_api_data["posTabs"][0][lookup_type]
for syn in syn_list:
final.append(syn["term"])
return final
async def _get_soup_object(self, url):
try:
async with self.session.request("GET", url) as response:
return BeautifulSoup(await response.text(), "html.parser")
except Exception:
return None