-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsavvy.py
139 lines (117 loc) · 4.58 KB
/
savvy.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
# Savvy Module
# Copyright 2023 Shadow Of Hassen
# Savvy is licensed under the the Lesser GPL License
import subprocess
import threading
import socket
import urllib
import urllib.parse
import urllib.request
import json
import http
DEFAULT_PORT = 8081
urlopen = urllib.request.build_opener(urllib.request.HTTPHandler).open
class Server:
def __init__(self, port=8081,):
self.host = socket.gethostbyname('localhost')
self.port = port
self.url = 'http://{}:{}'.format(self.host, self.port)+'/v2'
self.server = None
def terminate_server(self):
# Took this from language checker
error_message = ''
if self.server is not None:
try:
self.server.terminate()
except OSError:
pass
try:
error_message = self.server.communicate()[1].strip()
except (IOError, ValueError):
pass
try:
self.server.stdout.close()
except IOError:
pass
self.server = None
def _consume(self, stdout): # TODO: A les kututhu sounding name???
while stdout.readline():
pass
def start_server(self):
# I took this bit from the language_checker module
self.url = 'http://{}:{}'.format(self.host, self.port)+'/v2'
server_cmd =['java', '-cp', 'LanguageTool/languagetool-server.jar',
'org.languagetool.server.HTTPServer', '--port '+(str(self.port))+'allow-origin']
self.server = subprocess.Popen(server_cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
startupinfo=None
)
if self.server:
consumer_thread = threading.Thread(
target=lambda: self._consume(self.server.stdout))
consumer_thread.start()
self.active = True
class LanguageToolMistake:
def __init__(self, er): # er is the raw error. I'm sortening it because i'm going to use it a lot
self.message = er['message']
self.short_message = er['shortMessage']
self.replacements = []
for x in er['replacements']:
self.replacements.append(x['value'])
self.offset = er['offset']
self.length = er['length']
self.type =er['type']['typeName']
'''
LanguageToolChecker
This is the checker. it's different than the server.
The server above runs Language tool Java. This talks to the server.
It doesn't even need to use the server above set the url for a different place.
If you aren't using the server the must have the /v2 already in it
'''
class LanguageToolChecker:
def __init__(self, url=None, language='en-US'):
if url == None:
self.server = Server()
self.server.start_server()
self.url = self.server.url
else:
self.server = None
self.url = url
self.language= language
# TODO: Add disabled rules
def check(self, text):
params = urllib.parse.urlencode({'language':self.language, 'text':text}).encode() # Encode turns it to bytes type object
# Now we define a short name for something
data = urlopen(self.url+'/check',data=params)
# Now we parse the data.
for x in data:
pulled_data = x.decode() # No clue why it needs to go into a loop before I can get it.
# Now we must decode it.
# Language tool uses json so.
decoded_data = json.loads(pulled_data)
# Now we check for mistakes
mistakes = []
for piece in decoded_data['matches']: # We only need the errors
mistakes.append(LanguageToolMistake(piece))
return mistakes
def close_checker(self):
if self.server is not None:
self.server.terminate_server
def get_languages(self):
data = urlopen(self.url+ '/languages')
for x in data:
pulled_data = x.decode() # No clue why it needs to go into a loop before I can get it.
decoded_data = json.loads(pulled_data)
languages = []
for x in decoded_data:
languages.append((x['name'],x['longCode']))
return languages
if __name__ == '__main__':
l = LanguageToolChecker()
d = l.check('''Today's is the day you almost mispelled Captain Jack Spawwow!''')
for i in d:
print(i.replacements)
l.close_checker()