sashtech commited on
Commit
4720d84
·
verified ·
1 Parent(s): 23c21b6

Update gingerit.py

Browse files
Files changed (1) hide show
  1. gingerit.py +34 -16
gingerit.py CHANGED
@@ -1,12 +1,10 @@
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
@@ -16,18 +14,34 @@ class GingerIt(object):
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):
@@ -39,6 +53,10 @@ class GingerIt(object):
39
  result = text
40
  corrections = []
41
 
 
 
 
 
42
  for suggestion in reversed(data["Corrections"]):
43
  start = suggestion["From"]
44
  end = suggestion["To"]
@@ -56,4 +74,4 @@ class GingerIt(object):
56
  }
57
  )
58
 
59
- return {"text": text, "result": result, "corrections": corrections}
 
1
+
2
  import requests
3
  import cloudscraper
4
 
 
5
  URL = "https://services.gingersoftware.com/Ginger/correct/jsonSecured/GingerTheTextFull" # noqa
6
  API_KEY = "6ae0c3a0-afdc-4532-a810-82ded0054236"
7
 
 
8
  class GingerIt(object):
9
  def __init__(self):
10
  self.url = URL
 
14
 
15
  def parse(self, text, verify=True):
16
  session = cloudscraper.create_scraper()
17
+ try:
18
+ request = session.get(
19
+ self.url,
20
+ params={
21
+ "lang": self.lang,
22
+ "apiKey": self.api_key,
23
+ "clientVersion": self.api_version,
24
+ "text": text,
25
+ },
26
+ verify=verify,
27
+ )
28
+
29
+ # Print the raw response text for debugging
30
+ print("Response Text:", request.text)
31
+
32
+ # Try parsing the response as JSON
33
+ try:
34
+ data = request.json()
35
+ except ValueError:
36
+ print("Failed to parse JSON response. Returning original text.")
37
+ return {"text": text, "result": text, "corrections": []}
38
+
39
+ # Process and return the corrected data
40
+ return self._process_data(text, data)
41
+
42
+ except requests.exceptions.RequestException as e:
43
+ print(f"An error occurred during the API request: {e}")
44
+ return {"text": text, "result": text, "corrections": []}
45
 
46
  @staticmethod
47
  def _change_char(original_text, from_position, to_position, change_with):
 
53
  result = text
54
  corrections = []
55
 
56
+ if "Corrections" not in data:
57
+ print("No corrections found in the API response.")
58
+ return {"text": text, "result": text, "corrections": corrections}
59
+
60
  for suggestion in reversed(data["Corrections"]):
61
  start = suggestion["From"]
62
  end = suggestion["To"]
 
74
  }
75
  )
76
 
77
+ return {"text": text, "result": result, "corrections": corrections}