-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathspeller.py
executable file
·209 lines (171 loc) · 6.58 KB
/
speller.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# █ █ ▀ █▄▀ ▄▀█ █▀█ ▀
# █▀█ █ █ █ █▀█ █▀▄ █
# © Copyright 2022
# https://t.me/hikariatama
#
# 🔒 Licensed under the GNU AGPLv3
# 🌐 https://www.gnu.org/licenses/agpl-3.0.html
# scope: hikka_min 1.2.10
# meta pic: https://img.icons8.com/fluency/240/000000/spell-check.png
# meta banner: https://mods.hikariatama.ru/badges/speller.jpg
# meta developer: @hikarimods
# scope: hikka_only
# requires: requests cloudscraper requests_toolbelt aiohttp bs4 langid
import asyncio
import random
import re
import string
from typing import Union
import aiohttp
import cloudscraper
import langid
import requests
from bs4 import BeautifulSoup
from requests_toolbelt import MultipartEncoder
from telethon.tl.types import Message
from .. import loader, utils
URL = "https://services.gingersoftware.com/Ginger/correct/jsonSecured/GingerTheTextFull" # noqa
API_KEY = "6ae0c3a0-afdc-4532-a810-82ded0054236"
class GingerIt(object):
def __init__(self):
self.url = URL
self.api_key = API_KEY
self.api_version = "2.0"
self.lang = "US"
def parse(self, text, verify=True):
session = cloudscraper.create_scraper()
request = session.get(
self.url,
params={
"lang": self.lang,
"apiKey": self.api_key,
"clientVersion": self.api_version,
"text": text,
},
verify=verify,
)
data = request.json()
return self._process_data(text, data)
@staticmethod
def _change_char(original_text, from_position, to_position, change_with):
return "{}{}{}".format(
original_text[:from_position], change_with, original_text[to_position + 1 :]
)
def _process_data(self, text, data):
result = text
corrections = []
for suggestion in reversed(data["Corrections"]):
start = suggestion["From"]
end = suggestion["To"]
if suggestion["Suggestions"]:
suggest = suggestion["Suggestions"][0]
result = self._change_char(result, start, end, suggest["Text"])
corrections.append(
{
"start": start,
"text": text[start : end + 1],
"correct": suggest.get("Text", None),
"definition": suggest.get("Definition", None),
}
)
return {"text": text, "result": result, "corrections": corrections}
async def process(text: str) -> str:
fields = {"mytext": text, "autofix": "1", "lang_var": "Russian"}
boundary = "----WebKitFormBoundary" + "".join(
random.sample(string.ascii_letters + string.digits, 16)
)
m = MultipartEncoder(fields=fields, boundary=boundary)
a = requests.post(
"https://www.russiancorrector.com",
headers={
"Pragma": "no-cache",
"Cache-Control": "no-cache",
"Upgrade-Insecure-Requests": "1",
"Origin": "https://www.russiancorrector.com",
"Content-Type": m.content_type,
"User-Agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,"
" like Gecko) Chrome/92.0.4515.131 Safari/537.36"
),
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Sec-GPC": "1",
"Sec-Fetch-Site": "same-origin",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-User": "?1",
"Sec-Fetch-Dest": "document",
"Referer": "https://www.russiancorrector.com/",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.9,ru;q=0.8",
},
data=m,
).text
url = "https://www.russiancorrector.com" + re.search(
r"var url = \'(.+?)\'", a
).group(1)
res = "__wait__123"
while res == "__wait__123":
async with aiohttp.ClientSession() as session:
async with session.request("GET", url) as resp:
res = await resp.text()
if res != "__wait__123":
break
await asyncio.sleep(1)
return res
def parse(text: str) -> Union[bool, str]:
if "We could not find any errors in your text" in text:
return False
soup = BeautifulSoup(text, "html.parser")
for misspell in soup.find_all("div", class_="misspelling"):
try:
misspell.replace_with(
misspell.find("li", class_="replace-option").get_text() or ""
)
except Exception:
misspell.replace_with("")
return (
re.sub(r" {2,}", " ", soup.get_text().strip().replace("\n", " "))
.replace("Types and number of errors found: ", "")
.replace(
(
"Autocorrect: Check box to correct errors automatically, where"
" possible.A list of all corrected errors will be shown on the results"
" page. Submit"
),
"",
)
.strip()
)
@loader.tds
class SpellCheckMod(loader.Module):
"""Just a simple two-lang spell checker"""
strings = {
"name": "SpellCheck",
"processing": (
"👩🏫 <b>Let me take a look... Seems like this message is misspelled!</b>"
),
}
strings_ru = {
"processing": "👩🏫 <b>Дай глянуть. Похоже, тут есть ошибки!</b>",
"_cmd_doc_spell": "Проверить сообщение на грамотность",
"_cls_doc": "Проверяет правописание",
}
async def spellcmd(self, message: Message):
"""Perform spell check on reply"""
reply = await message.get_reply_message()
if not reply or not getattr(reply, "raw_text", False):
await message.delete()
message = await utils.answer(message, self.strings("processing"))
text = reply.text
tt = langid.classify(text)[0]
if tt == "en":
spell_checker = GingerIt()
result = spell_checker.parse(text)
corrected_text = result["result"]
elif tt == "ru":
corrected_text = parse(await process(text))
else:
await message.delete()
return
if corrected_text == text or not corrected_text:
await message.delete()
await utils.answer(message, f"✍️ {corrected_text}")