File size: 2,718 Bytes
4720d84
041e7ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4720d84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
041e7ca
 
 
 
 
 
 
 
 
 
 
4720d84
 
 
 
041e7ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4720d84
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

import requests
import cloudscraper

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()
        try:
            request = session.get(
                self.url,
                params={
                    "lang": self.lang,
                    "apiKey": self.api_key,
                    "clientVersion": self.api_version,
                    "text": text,
                },
                verify=verify,
            )
            
            # Print the raw response text for debugging
            print("Response Text:", request.text)
            
            # Try parsing the response as JSON
            try:
                data = request.json()
            except ValueError:
                print("Failed to parse JSON response. Returning original text.")
                return {"text": text, "result": text, "corrections": []}
            
            # Process and return the corrected data
            return self._process_data(text, data)
        
        except requests.exceptions.RequestException as e:
            print(f"An error occurred during the API request: {e}")
            return {"text": text, "result": text, "corrections": []}

    @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 = []

        if "Corrections" not in data:
            print("No corrections found in the API response.")
            return {"text": text, "result": text, "corrections": 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}