“[shujaatalishariati]” commited on
Commit
041e7ca
·
1 Parent(s): 8d6dec9

exernal gingerit library

Browse files
Files changed (2) hide show
  1. app.py +3 -1
  2. gingerit.py +59 -0
app.py CHANGED
@@ -6,6 +6,8 @@ import subprocess
6
  import nltk
7
  from nltk.corpus import wordnet
8
  from gensim import downloader as api
 
 
9
  from gingerit import GingerIt
10
 
11
  # Ensure necessary NLTK data is downloaded
@@ -132,4 +134,4 @@ with gr.Blocks() as interface:
132
  paraphrase_button.click(paraphrase_and_correct, inputs=text_input, outputs=output_text)
133
 
134
  # Launch the Gradio app
135
- interface.launch(debug=False)
 
6
  import nltk
7
  from nltk.corpus import wordnet
8
  from gensim import downloader as api
9
+
10
+ # Import the custom GingerIt module
11
  from gingerit import GingerIt
12
 
13
  # Ensure necessary NLTK data is downloaded
 
134
  paraphrase_button.click(paraphrase_and_correct, inputs=text_input, outputs=output_text)
135
 
136
  # Launch the Gradio app
137
+ interface.launch(debug=False)
gingerit.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ import requests
3
+ import cloudscraper
4
+
5
+
6
+ URL = "https://services.gingersoftware.com/Ginger/correct/jsonSecured/GingerTheTextFull" # noqa
7
+ API_KEY = "6ae0c3a0-afdc-4532-a810-82ded0054236"
8
+
9
+
10
+ class GingerIt(object):
11
+ def __init__(self):
12
+ self.url = URL
13
+ self.api_key = API_KEY
14
+ self.api_version = "2.0"
15
+ self.lang = "US"
16
+
17
+ def parse(self, text, verify=True):
18
+ session = cloudscraper.create_scraper()
19
+ request = session.get(
20
+ self.url,
21
+ params={
22
+ "lang": self.lang,
23
+ "apiKey": self.api_key,
24
+ "clientVersion": self.api_version,
25
+ "text": text,
26
+ },
27
+ verify=verify,
28
+ )
29
+ data = request.json()
30
+ return self._process_data(text, data)
31
+
32
+ @staticmethod
33
+ def _change_char(original_text, from_position, to_position, change_with):
34
+ return "{}{}{}".format(
35
+ original_text[:from_position], change_with, original_text[to_position + 1 :]
36
+ )
37
+
38
+ def _process_data(self, text, data):
39
+ result = text
40
+ corrections = []
41
+
42
+ for suggestion in reversed(data["Corrections"]):
43
+ start = suggestion["From"]
44
+ end = suggestion["To"]
45
+
46
+ if suggestion["Suggestions"]:
47
+ suggest = suggestion["Suggestions"][0]
48
+ result = self._change_char(result, start, end, suggest["Text"])
49
+
50
+ corrections.append(
51
+ {
52
+ "start": start,
53
+ "text": text[start : end + 1],
54
+ "correct": suggest.get("Text", None),
55
+ "definition": suggest.get("Definition", None),
56
+ }
57
+ )
58
+
59
+ return {"text": text, "result": result, "corrections": corrections}